Info Session — Mentor-Led Data Science & AI Program

Register
Academy

Transformers From Scratch: Attention Explained With Small Numbers

Magnimind Academy · · 8 min read

Transformers From Scratch: Attention Explained With Small Numbers — Magnimind Academy article illustration

This technical guide deconstructs the transformer attention mechanism using simple arithmetic. We move past the jargon to explain how Queries, Keys, and Values interact through dot-product operations, providing a step-by-step walkthrough of the math that powers modern large language models without relying on abstract black-box explanations.

Most technical literature on large language models treats the attention mechanism as a magical black box where words suddenly become aware of their neighbors. While the conceptual leap is impressive, the underlying mathematics is surprisingly pedestrian. At its core, transformer attention is a series of weighted averages determined by how well one vector aligns with another. If you can multiply and add, you can understand the architecture that currently dominates the field of artificial intelligence.

The primary barrier to entry for most practitioners isn't the calculus, but the high-dimensional geometry. We often talk about 512-dimensional embeddings or 8-billion parameter models, numbers so large they lose all meaning. To truly grasp how a model decides that the word bank refers to a river edge rather than a financial institution, we must look at the mechanism through the lens of small, manageable integers. This article strips away the complexity to show how simple dot products create context.

The anatomy of the Query, Key, and Value

In a standard feed-forward neural network, an input passes through a weight matrix and an activation function. In a transformer, the input is split into three distinct functional roles: the Query (Q), the Key (K), and the Value (V). Think of this like a retrieval system. The Query represents what you are looking for, the Key represents the label or index of the information available, and the Value is the actual information you want to extract.

Imagine we have a simple two-word sequence: [3, 1] and [1, 2]. These are our initial embeddings. To generate the Q, K, and V vectors, we multiply these inputs by weight matrices that the model learns during training. For our manual walk-through, let's assume the weight matrices are simple identity-like structures. The goal is to transform each input vector into three separate representations that serve different purposes during the self-attention phase.

The Query vector is the 'active' part of the token; it reaches out to other tokens to see if they are relevant. The Key vector is the 'passive' part; it sits there waiting to be matched by a Query. When a Query and a Key align closely, they produce a high score. This score then determines how much of the corresponding Value vector is passed through to the next layer of the model. This three-way split is what allows the model to differentiate between the position of a word and its semantic meaning.

Python data analysis code in an editor — Calculating the dot-product similarity
Python data analysis code in an editor — Calculating the dot-product similarity

Calculating the dot-product similarity

The first major calculation in transformer attention is the dot product between the Query of the current word and the Keys of all other words in the sequence. If we have a Query vector q1 = [2, 0] and a Key vector k2 = [1, 2], their dot product is (2 * 1) + (0 * 2) = 2. If we have another Key k3 = [2, 1], the dot product is (2 * 2) + (0 * 1) = 4. A higher number indicates a stronger relationship or 'attention' between the words.

This operation is performed for every token in the sequence against every other token. In a sequence of length n, this creates an n x n matrix of scores. This is why transformers have a quadratic computational cost relative to sequence length; as you double the number of words, you quadruple the number of dot-product operations required to calculate the attention scores. This is the primary bottleneck in scaling long-context models.

It is important to note that these raw scores can become very large, which leads to gradients vanishing or exploding during backpropagation. To counter this, we apply a scaling factor. We divide the dot product by the square root of the dimension of the Key vectors. If our vectors have a dimension of 64, we divide by 8. This ensures that the variance of the scores remains stable, allowing the softmax function to operate in a range where it is most sensitive to changes in input.

Softmax and the creation of weights

Once we have our scaled scores, we need to turn them into probabilities that sum to 1. This is achieved via the softmax function. For a set of scores like [2, 4, 0], the softmax function exponentiates each number and divides by the sum of all exponentiated numbers. This creates a probability distribution where the highest score gets the most 'attention' while lower scores are suppressed but not entirely eliminated.

Using our small numbers, if a word has a score of 0.9 for itself and 0.1 for its neighbor, the output will be a weighted mixture. It will take 90% of its own 'Value' vector and 10% of its neighbor's 'Value' vector. This is how context is 'mixed' into the representation of a single token. The word bank suddenly gains 'river-like' properties because it attended strongly to the word water earlier in the sentence.

This step is essentially a differentiable lookup table. Unlike a hard lookup in a standard database, where you either find a match or you don't, softmax allows the model to look at multiple places at once with varying degrees of intensity. This 'soft' selection is what makes gradient-based learning possible, as the model can slightly adjust its attention weights to minimize the loss function during training.

Numerical example of attention distribution

TokenRaw ScoreExp(x)Softmax Weight
Word A4.054.60.84
Word B2.07.40.11
Word C0.51.60.05
Large language model tooling on a developer screen — Multiplying by the Value matrix
Large language model tooling on a developer screen — Multiplying by the Value matrix

Multiplying by the Value matrix

The final step in the attention head is multiplying the softmax weights by the Value vectors (V). If the weights are [0.8, 0.2] and the Value vectors are v1 = [10, 10] and v2 = [0, 0], the resulting vector is (0.8 * [10, 10]) + (0.2 * [0, 0]) = [8, 8]. The output is a new representation of the token that has literally 'absorbed' information from the other tokens in the sequence.

This process happens in parallel for every word. The output of the attention mechanism is a matrix of the same shape as the input. Each row in this matrix is the new, context-aware embedding for a word. It's no longer just a static vector from a dictionary; it is a vector that has been modified by every other relevant word in its local environment. This is why transformers perform so much better than older RNNs, which had to compress all previous context into a single hidden state.

In multi-head attention, this process is simply repeated multiple times with different weight matrices. One 'head' might focus on grammatical relationships (subject-verb agreement), while another head might focus on semantic relationships (synonyms). The outputs of all these heads are concatenated and projected back to the original dimension, creating a rich, multi-faceted representation of the text.

Attention is not about focusing on one thing; it is about the mathematical blending of all relevant inputs into a single, contextualized vector.

The cost of global attention

While powerful, this mechanism is computationally expensive. Because every token attends to every other token, the number of calculations grows at a rate of O(n^2). In a sequence of 1,000 tokens, you have 1,000,000 attention pairs. In a sequence of 100,000 tokens, that number jumps to 10 billion. This quadratic scaling is the reason why early transformers were limited to short context windows like 512 or 1024 tokens.

Modern optimizations like FlashAttention reduce the memory overhead by avoiding the materialization of the large n x n matrix in slow GPU memory (VRAM). Instead, they compute the attention in blocks that fit into the fast SRAM. However, the fundamental arithmetic operations remain the same. Even with hardware acceleration, the quadratic nature of dot-product attention dictates the latency and cost of running large models.

Data scientists must balance the 'receptive field' of the model against these costs. Using a 128k context window is technically possible today, but the 'Time to First Token' (TTFT) increases significantly because the model must compute this massive attention matrix before it can generate a single word of output. Understanding the small-number math helps you realize why 'context' is the most expensive part of the inference pipeline.

Machine learning model training results on screen — Common mistakes in implementing attention
Machine learning model training results on screen — Common mistakes in implementing attention

Common mistakes in implementing attention

One of the most frequent errors practitioners make when building custom attention layers is forgetting the causal mask. In a decoder-only model (like GPT), a word should not be able to 'see' the words that come after it. Without a mask, the model will cheat during training by looking at the answer, leading to 0% loss during training but total failure during real-world inference. The mask is simply a matrix of negative infinity added to the raw scores before softmax, which zeros out the attention to future tokens.

Another common pitfall is ignoring the scaling factor 1/sqrt(d_k). As the dimensionality of the model grows, the dot products tend to grow in magnitude. Without scaling, the softmax function enters regions where the gradient is extremely small. This 'kills' the learning process, as the weights stop updating. If you notice your model's loss plateaus early or your attention weights become one-hot vectors too quickly, check your scaling factor.

  • Forgetting to apply the mask in decoder-only architectures.
  • Omitting the scaling factor in the dot-product calculation.
  • Incorrectly reshaping the multi-head attention tensors, leading to 'mixing' across the wrong dimension.
  • Using a context window larger than the positional embeddings can support.
  • Assuming attention is the same as correlation; it is a directional relationship.

What to practise this week

To transition from theoretical understanding to practical mastery, you should move away from high-level libraries like Hugging Face for a few days and try to rebuild the core components yourself. The goal is to see the numbers move through the system. Follow these steps to solidify your knowledge of transformer attention.

  1. Implement a single-head self-attention function using only NumPy. Use a sequence length of 3 and a dimension of 4.
  2. Manually calculate the softmax of a 3x3 matrix and verify that the rows sum to 1.
  3. Create a look-ahead mask (triangular matrix) and apply it to your attention scores to see how it blocks future information.
  4. Write a script to visualize the attention map of a small pre-trained model like GPT-2 using a library like BertViz.
  5. Calculate the memory requirements (in bytes) for the attention matrix of a 4096-token sequence using float32 precision.

By focusing on these low-level operations, you develop an intuition for why models behave the way they do. When a model hallucinates or fails to follow instructions in a long prompt, you will be able to visualize the attention weights failing to converge on the relevant 'Key' tokens. This technical depth is what separates a library-user from an AI engineer.

Keep reading

Related posts

Picked by shared topics and what other readers are reading this month.

Deep Learning

Shares: Deep learning, Large language models

Computer Vision in 2026: Practical Detection and Segmentation Workflows

A technical deep dive into the 2026 computer vision landscape, focusing on the convergence of foundational vision-language models and real-time edge deployment. We analyze modern detection and segmentation workflows, discussing the trade-offs between zero-shot inference, parameter-efficient fine-tuning, and the shift toward unified architectural paradigms for production environments.

· 9 min read

Read article →
Deep Learning

Shares: Deep learning, AI in business

Reinforcement Learning for Practitioners: Where It Works Outside Games

Reinforcement learning has moved beyond the controlled environments of Atari and Chess into production environments where decision-making is sequential and rewards are delayed. This technical guide explores practical implementation strategies in supply chain logistics, personalized recommendation systems, and energy management, focusing on the infrastructure and safety constraints necessary for real-world deployment.

· 9 min read

Read article →
Deep Learning

Shares: Deep learning, AI in business

Machine Learning Vs. Deep Learning: What Is The Difference?

Two of the most talked-about subfields of artificial intelligence (AI) are machine learning and deep learning. They are not the same thing, even though they are frequently used interchangeably. Businesses and organizations looking to implement AI-based solutions need to know the difference…

· 2 min read

Read article →
Browse all 218 articles →

Not sure which program fits? Book a free info session.

Talk to a mentor about your background, your target role, and which cohort makes sense.