v9.1.1
Menu

Channel Mixing with GELU

mediumPROMLP-MixerNeural Networks, Activation FunctionsAdvanced Architectures

Channel Mixing in MLP-Mixer applies an MLP independently to each token's features:

Y=W2⋅GELU(W1⋅XT)TY = W_2 \cdot \text{GELU}(W_1 \cdot X^T)^T

Or equivalently per-token: yi=W2⋅GELU(W1⋅xi)y_i = W_2 \cdot \text{GELU}(W_1 \cdot x_i)

Where:

  • X∈RN×DX \in \mathbb{R}^{N \times D}: Input tokens
  • W1∈RD×HW_1 \in \mathbb{R}^{D \times H}: Expand to hidden dim H
  • W2∈RH×DW_2 \in \mathbb{R}^{H \times D}: Project back to D
  • GELU: Gaussian Error Linear Unit activation

GELU approximation: GELU(x)≈0.5x(1+tanh⁡(2/π(x+0.044715x3)))\text{GELU}(x) \approx 0.5x(1 + \tanh(\sqrt{2/\pi}(x + 0.044715x^3)))

Task: Implement channel mixing with GELU activation.

Example

Input
X (2×2), W1 = I (2×4), W2 = I (4×2)
Output
GELU applied element-wise to X
Reasoning

With identity-like projections, the MLP reduces to applying GELU to each element. GELU(1) ≈ 0.8412, GELU(0) = 0.

Constraints

  • NN: Number of tokens
  • DD: Input/output dimension
  • HH: Hidden dimension (typically 4×D)

More from Advanced Architectures

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.