Feed-Forward Network (FFN) Sub-Layer (Transformer): Difference between revisions
Replaced content with "Category:Activation function A gated linear unit (GLU) is a trainable activation function. $$ \operatorname{GLU}(\mathbf{X}) =\left(\mathbf{X}\mathbf{W}_1+\mathbf{1}\vec b_1^{\top}\right) \odot f\left(\mathbf{X}\mathbf{W}_2+\mathbf{1}\vec b_2^{\top}\right). $$ The '''gating function''' $f$ is commonly any of * Gaussian error linear unit (GeLU) * sigmoid function * (leaky) rectified/exponential linear units (ReLU, LeakyReLU, ELU) GLUs are fou..." Tags: Replaced Reverted |
No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
[[Category: | [[Category:Transformer]] | ||
The '''feed-forward network (FFN) sub-layer''' is the 2nd sub-layer of a typical [[Transformer]] layer.<ref>{{#cite:Q3130}}</ref> | |||
== 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{ | \operatorname{FFN}(\mathbf{X}) | ||
=\left(\mathbf{X}\mathbf{W}_1+\mathbf{1}\vec b_1^{\top}\right) | =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]]. | |||
<ref>{{#cite:Q3130}}</ref> | |||
== 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 [[o(x) and o(x)|big O notation]]. | |||
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.
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.