Jump to content

Feed-Forward Network (FFN) Sub-Layer (Transformer): Difference between revisions

From Wikibase
No edit summary
No edit summary
 
Line 1: Line 1:
[[Category:Transformer]]
[[Category:Transformer]]
The '''feed-forward network (FFN) sub-layer''' is the 2nd of the 2 sub-layers of a [[Transformer]] block: a 2-layer [[feedforward algorithm|feedforward network]] applied to each position of the sequence separately, after [[self-attention]].<ref>{{#cite:Q3130}}</ref>
The '''feed-forward network (FFN) sub-layer''' is the 2nd sub-layer of a typical [[Transformer]] layer.<ref>{{#cite:Q3130}}</ref>


== Definition ==
== Definition ==

Latest revision as of 13:29, 17 November 2026

The feed-forward network (FFN) sub-layer is the 2nd sub-layer of a typical Transformer layer.[1]

Definition

Let $\mathbf{X}\in\mathbb{R}^{T\times d}$, and let $\mathbf{W}_1\in\mathbb{R}^{d\times d_{ff}}$, $\vec b_1\in\mathbb{R}^{d_{ff}}$, $\mathbf{W}_2\in\mathbb{R}^{d_{ff}\times d}$, $\vec b_2\in\mathbb{R}^{d}$. The sub-layer is

$$ \operatorname{FFN}(\mathbf{X}) =f\left(\mathbf{X}\mathbf{W}_1+\mathbf{1}\vec b_1^{\top}\right)\mathbf{W}_2+\mathbf{1}\vec b_2^{\top}, $$

where $\mathbf{1}\in\mathbb{R}^{T}$ is a column vector of $1$s, so $\mathbf{1}\vec b_1^{\top}$ forms a $T \times d_{ff}$ matrix with the bias vector repeating over every row, and $f$ is an activation function.

[1]

Properties

Row-wise operation

Unlike self-attention, $\operatorname{FFN}$ is row-wise: row $t$ of the output depends on row $t$ of the input alone.

Compute

The compute for each row of $\mathbf{X}$ is dominated by $d\,d_{ff}$ multiplications with $\mathbf{W}_1$ and $d_{ff}\,d$ multiplications with $\mathbf{W}_2$ (since multiplications dominate over addition), hence

$$ \operatorname{FFN} \in O\left(T\,d\,d_{ff}\right) $$

where $O(x)$ is the big O notation.