Gated Linear Units › Visualizing GLU Representations
Language AI HandbookAll chapters Gated Linear Units › Visualizing GLU RepresentationsOne section · Sections in this chapter9 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To understand how GLU transforms representations differently from standard activations, let's visualize the hidden activations for the same inputs processed through different FFN variants:
# Create comparable FFNs
d_model_vis = 64
d_ff_vis = 256
# Standard FFN weights
W1_std = np.random.randn(d_model_vis, d_ff_vis) * np.sqrt(
2.0 / (d_model_vis + d_ff_vis)
)
W2_std = np.random.randn(d_ff_vis, d_model_vis) * np.sqrt(
2.0 / (d_ff_vis + d_model_vis)
)
# GLU weights (same initialization for comparison)
W_glu = np.random.randn(d_model_vis, d_ff_vis) * np.sqrt(
2.0 / (d_model_vis + d_ff_vis)
)
V_glu = np.random.randn(d_model_vis, d_ff_vis) * np.sqrt(
2.0 / (d_model_vis + d_ff_vis)
)
W2_glu = np.random.randn(d_ff_vis, d_model_vis) * np.sqrt(
2.0 / (d_ff_vis + d_model_vis)
)
def get_hidden_activations(x, variant="standard"):
"""Get hidden layer activations for visualization."""
if variant == "standard":
return relu(x @ W1_std)
elif variant == "swiglu":
return swish(x @ W_glu) * (x @ V_glu)
elif variant == "geglu":
return gelu(x @ W_glu) * (x @ V_glu)
# Generate a batch of inputs
batch_size = 50
X_batch = np.random.randn(batch_size, d_model_vis)
# Get hidden activations for each variant
hidden_std = np.array([get_hidden_activations(x, "standard") for x in X_batch])
hidden_swi = np.array([get_hidden_activations(x, "swiglu") for x in X_batch])

Let's also compare the distribution of activation values:

The key observation is that SwiGLU produces more continuous, less sparse activations. While ReLU creates hard sparsity (many exactly-zero values), SwiGLU allows for small negative values and smoother transitions. This can improve gradient flow during training and allows the network to represent graded feature combinations.
Let's quantify this sparsity difference more precisely by looking at how activation values distribute across magnitude ranges:
