Complete Mixer Block with Residuals
A complete MLP-Mixer block combines token mixing and channel mixing with residual connections:
Where:
- TokenMix: (mix across N tokens)
- ChannelMix: , a two-layer MLP with no biases applied per-token (use the tanh-approximate GELU defined in the starter code)
The residual connections ensure stable training and allow the network to learn identity mappings when beneficial.
Task: Implement a complete Mixer block with both mixing stages and residual connections.
Example
X (2×2), W_token=zeros, W1=I, W2=I
X + 0 + GELU(X) = X + GELU(X)
- Inputs: is a matrix of 2 tokens (rows) by 2 channels (columns), here . is the zero matrix, and both channel weights are the identity, so the hidden width is .
- Token mixing: each new token row is a weighted blend of all token rows, . With every weight zero the blend is the zero matrix, so .
- Channel mixing: each token row is multiplied by , passed through GELU entry by entry, then multiplied by . With identity weights, ChannelMix is just GELU of every entry: , , , .
- Output: , a matrix like the input:
[[1.8412, 3.9546], [5.9964, 7.9999]](rounded to 4 decimal places). - Notice: for large positive , , so entries approach . The residuals keep even when a stage contributes nothing.
Constraints
- Input : Shape
- : Shape
- : Shape for channel MLP
- : Shape for channel MLP