Multi-Head Attention Tensor Shapes

DataMListic · Beginner ·📐 ML Fundamentals ·1w ago

Key Takeaways

Explains multi-head attention tensor shapes in transformer models

Original Description

A sequence of tokens goes into multi-head attention as a [T, d_model] block and comes back out exactly the same shape, and almost everyone who gets stuck on attention is really stuck on what happens to the dimensions in between. This video is a tour of those shapes. Each token is projected into a query, a key, and a value, and the matmul rule [a,b] @ [b,c] = [a,c] tells you exactly what comes out: X[T, d_model] @ W_Q[d_model, d_head] = Q[T, d_head]. The scores Q@Kᵀ are [T, T], and the d_head axis you measured similarity in is consumed by the dot product, so it never appears in the score matrix. That vanishing dimension is the single most confused point in attention. Then we add heads. d_model = n_heads × d_head (512 = 8 × 64) just partitions the width: one fat W_Q[512, 512] projects every head at once, a reshape splits the 512 into (n_heads, d_head), a transpose slides the head axis next to the batch axis, and from there matmul treats heads as nothing more than another batch dimension, so there is no head loop in the code. The payoff is the part people get wrong in both directions: that fat W_Q is one shared tensor, but it holds eight distinct learned blocks side by side, so the heads run through one matrix yet still learn different projections. Not eight separate matrices, and not one matrix making identical heads, but one tensor, eight different blocks. *Related Videos* ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ Self-Attention - Explained: https://youtu.be/chSVRI_tQXk Multi-Head Attention (MHA), Multi-Query Attention (MQA), Grouped-Query Attention (GQA): https://youtu.be/efg-qgQ5t88 Multi-Head Latent Attention (MLA) - Explained: https://youtu.be/DWBKSbj8CqA Attention Mechanism Variations: https://youtu.be/LremmIyKr0w Sliding Window Attention: https://youtu.be/_8Vcui3DSbQ Softmax function - Explained: https://youtu.be/oJU6-qW6xZU Word2Vec — How Words Became Vectors: https://youtu.be/9Rd6sh-PmlY *Contents* ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 00:00 - Same shape in, same shape out 00:34 - Q, K
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

📰
Research First. Prompt Later—Day 30
Learn to prioritize research over prompting in machine learning to improve model performance and efficiency
Medium · Machine Learning
📰
How TypeScript Makes Your JavaScript Run Faster
Learn how TypeScript can indirectly improve JavaScript runtime performance
Dev.to · Doogal Simpson
📰
Mastering One Hot Encoding in Machine Learning: A Complete Guide to Encoding Nominal Categorical…
Learn to effectively use one hot encoding for nominal categorical variables in machine learning to improve model performance and avoid common pitfalls
Medium · AI
📰
Mastering One Hot Encoding in Machine Learning: A Complete Guide to Encoding Nominal Categorical…
Master one hot encoding in machine learning to effectively handle nominal categorical data and improve model performance
Medium · Machine Learning
Up next
How to Train AI to Play Games ? How AI Learns to Play ? Several Methods EXPLAINED
MaxonShire
Watch →