The MLP Mixes Features at Each Position

The Transformer MLP expands and contracts each token record with shared weights but no cross-position communication. Trace two records, derive projection shapes, test independence, and count parameters.

After attention exchanges information across positions, the block applies the same multilayer perceptron to every token record independently. A basic two-projection MLP is

h=ϕ(rWup+bup),m=hWdown+bdown.h=\phi(rW_{up}+b_{up}),\qquad m=hW_{down}+b_{down}.

For model width dmodeld_{model} and hidden width dffd_{ff},

Wup:(dmodel,dff),Wdown:(dff,dmodel).W_{up}:(d_{model},d_{ff}),\qquad W_{down}:(d_{ff},d_{model}).

The MLP expands the feature axis and contracts it again. It does not change sequence length.

Trace Two Positions Independently

Let dmodel=2d_{model}=2, dff=3d_{ff}=3, use ReLU, omit biases, and choose

Wup=[101011],Wdown=[100111].W_{up}=\begin{bmatrix}1&0&1\\0&1&-1\end{bmatrix},\qquad W_{down}=\begin{bmatrix}1&0\\0&1\\1&1\end{bmatrix}.

For r1=[1,2]r_1=[1,2], the expanded values are [1,2,1][1,2,-1]. ReLU gives [1,2,0][1,2,0], so the output is [1,2][1,2].

For r2=[1,1]r_2=[-1,1], the expanded values are [1,1,2][-1,1,-2]. ReLU gives [0,1,0][0,1,0], so the output is [0,1][0,1].

The weights are shared, but each position follows its own calculation.

Position-Wise Does Not Mean Feature-Wise

Changing one token record must not alter another token's MLP output. Within one record, however, each hidden unit can combine every input feature, and each output coordinate can combine every hidden unit. The MLP is independent across positions but dense across features.

This gives a useful implementation test: copy an input sequence, alter one position, run only the MLP, and confirm that all other output positions remain unchanged.

Count the Learned Entries

With two biases, the parameter count is

dmodeldff+dff+dffdmodel+dmodel.d_{model}d_{ff}+d_{ff}+d_{ff}d_{model}+d_{model}.

For dmodel=2d_{model}=2 and dff=3d_{ff}=3, this is 6+3+6+2=176+3+6+2=17. A count that omits biases must say so.

Q1. Count a two-projection MLP

How many learned entries does a biased MLP have when dmodel=4d_{model}=4 and dff=8d_{ff}=8?

Compute it first, then check your number.

Hint
Count both matrices and both bias vectors.
Solution
4(8)+8+8(4)+4=764(8)+8+8(4)+4=76 learned entries.
Not attempted
Review

Not marked done.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.