C.W.K.
Stream
Lesson 04 of 07 · published

Attention and Normalization

~10 min · layers

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

MultiHeadAttention: the Transformer core

MultiHeadAttention is the layer the entire Transformer family is built around. It lets every position in a sequence look at every other position and decide what's relevant — "attention" — and it runs that lookup in several independent heads at once so the model can attend to different relationships in parallel. Two args carry most of the meaning: num_heads is how many parallel attention patterns to learn, and key_dim is the width of each head's query/key space.

Call it with the same tensor for query, key, and value and you get self-attention (a sequence relating to itself); pass a different source for the key/value and you get cross-attention (the encoder-decoder link).

Normalization: what keeps deep nets trainable

Normalization layers rescale activations to a stable distribution so gradients don't explode or vanish as depth grows. The one you pick depends on what axis you normalize over:

LayerNormalizesBest For
BatchNormalizationPer batch (across samples)CNNs, general training
LayerNormalizationPer sample (across features)Transformers, RNNs
GroupNormalizationPer group of channelsSmall batch sizes

The split matters because BatchNormalization depends on batch statistics — fragile when the batch is tiny or the sequence length varies — while LayerNormalization normalizes each sample on its own, which is why Transformers and RNNs reach for it.

Code

MultiHeadAttention with Flash Attention enabled·python
# Multi-head attention (Transformer key component)
layers.MultiHeadAttention(
    num_heads=8,          # Number of attention heads
    key_dim=64,           # Dimension of each head
    flash_attention=True,  # Enable Flash Attention (3.7+)
)

External links

Exercise

Build a Transformer encoder block using MultiHeadAttention + LayerNormalization in pre-norm style: x → LayerNorm → MHA → +residual → LayerNorm → FFN (Dense→Dense) → +residual. Run a tiny sanity fit on random data and confirm the loss actually decreases. Then delete one LayerNorm and watch training degrade — feel why the principle callout is non-negotiable.
Hint
Pre-norm means LayerNorm goes BEFORE the sublayer, then you add the original input back as the residual. Keep key_dim × num_heads equal to your model width for the residual add to line up.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.