I Finally Understood Self-Attention. Here’s How Transformers Actually Work.

Understanding the mechanism behind ChatGPT, LLMs, and modern AI — one step at a time.
I recently watched a video explaining Self-Attention, and something finally clicked.
I had heard terms like Transformers, embeddings, Query, Key, Value, and attention scores many times.
I knew these concepts were important to Large Language Models (LLMs). But knowing the terminology and understanding what actually happens inside a Transformer are two different things.
The video helped me connect those pieces.
Here is my understanding, in simple terms.

Self Attention in LLM - infographic
1. The Real Problem: Words Don't Have Meaning in Isolation
Consider this sentence:
“The animal didn't cross the street because it was too tired.”
What does it refer to?
The animal or the street?
For us, the answer seems obvious. We naturally connect “tired” with “animal.”
But how does a machine learn that relationship?
A word's meaning often depends on the other words around it.
Earlier neural network architectures, such as RNNs, processed sequences step by step. This made it harder to preserve information across long distances and limited how much computation could happen in parallel.
Transformers approached the problem differently.
Instead of relying on sequential information transfer, Self-Attention allows each token to calculate how much information it should take from other relevant tokens.
That is the central idea.
2. Everything Starts With Embeddings
Computers don't understand words directly. They operate on numbers.
The input text is first broken into tokens, and each token is represented by a numerical vector called an embedding.
For example:
- Cat → a vector
- Sat → another vector
- Mat → another vector
These vectors provide the numerical representation that the model can work with.
But having embeddings alone isn't enough.
The model also needs to work out how these tokens relate to each other in the current context.
That is where Self-Attention comes in.
3. The Three Most Important Concepts: Query, Key, and Value
This was the part that made the whole mechanism much easier for me to understand.
For every token, the attention layer creates three different vectors using learned transformations.
Query (Q): What information am I looking for?
Key (K): What information do I have that another token might find relevant?
Value (V): What information can I contribute?
Think of it like searching for information.
Your Query describes what you need.
The Keys help identify which pieces of information are relevant.
The Values contain the information that gets combined to form the result.
The model learns how to perform these matches during training.
Nothing here requires manually defining rules such as “this noun must always connect to that verb.”
4. How Does the Model Decide What Deserves Attention?
Once we have Q, K, and V, the mathematics begins.
The model compares Queries with Keys using a dot product.
In matrix notation:
Scores = Q × Kᵀ
This produces an attention score for each allowed token pair.
Higher scores indicate stronger matches in that particular attention calculation.
But there is a small problem.
As the vector dimensions increase, dot-product scores can become very large.
So the scores are divided by the square root of the Key dimension:
Scaled Scores = (Q × Kᵀ) / √dₖ
Why?
Because very large scores can make the next operation, softmax, produce extremely concentrated weights, which can make training more difficult.
This scaling keeps the calculation better behaved.
It seems like a small mathematical adjustment, but it matters.
5. Softmax: Turning Scores Into Attention Weights
Now the model has matching scores.
But how much information should it take from each token?
Softmax converts those scores into non-negative weights that add up to 1.
Imagine that a token receives attention weights like these:
- Token A: 0.60
- Token B: 0.25
- Token C: 0.15
These are illustrative values, not fixed rules.
They mean that, for this attention calculation, Token A contributes more heavily to the resulting representation.
The model then uses these weights to combine the Value vectors.
Output = Attention Weights × V
The result is a new representation of the token, informed by the surrounding context.
That's the key insight.
The token is no longer represented only by its starting vector. It now incorporates information from the other tokens it attended to.
6. The Entire Self-Attention Mechanism in One Equation
After understanding each part, the famous equation suddenly looks much less intimidating:
Attention(Q, K, V) = softmax(QKᵀ / √dₖ) × V
Let's read it in plain English.
- Compare Queries with Keys to calculate relevance scores.
- Scale those scores to help maintain stable learning.
- Apply softmax to get attention weights.
- Use those weights to combine the Value vectors.
- Produce new, context-aware representations.
That's the core Self-Attention operation.
The equation didn't change.
My understanding of what each component was doing changed.
7. Why Do Transformers Use Multiple Attention Heads?
There is another interesting idea.
Consider the word “bank.”
In one sentence, it might refer to a financial institution.
In another, it might refer to the side of a river.
A single attention calculation offers one learned way of relating tokens.
Multi-Head Attention creates several attention calculations using different learned projections.
This gives the model the ability to capture different types of relationships and information patterns in parallel.
The results are then combined.
Not every head necessarily learns a neat human-readable linguistic role, but the overall mechanism gives the model more flexibility.
One important detail: in GPT-style autoregressive models, attention is also causally masked. When predicting the next token, a position cannot look at future tokens.
8. Why This Matters for Modern AI
This mechanism helps explain why Transformers became the foundation for so many AI systems.
Self-Attention provides a way to build context-aware representations through operations that can be computed efficiently in parallel during training.
Combined with multiple layers, learned parameters, positional information, feed-forward networks, and large-scale training, it supports capabilities such as:
- Understanding relationships within text
- Generating coherent responses
- Working with code
- Processing information across documents
- Connecting information across different types of inputs
Of course, attention alone does not guarantee accurate reasoning or factual answers.
It is one important mechanism within a much larger architecture.
My Biggest Takeaway
Before watching this explanation, Self-Attention felt like another complicated mathematical concept behind AI.
Now I see the basic process much more clearly:
Find relevant information. Decide how much it matters. Combine it to create a better contextual representation.
What I particularly liked about the video was that it connected the mathematics to the visual movement of information between tokens.
Seeing the attention matrix, its weights, and the final output vectors helped me understand what the equation was actually computing.
And that reminded me of something important about learning AI:
Sometimes the difficulty isn't the mathematics. It's not yet having a clear picture of what the mathematics is doing.
If you are learning Transformers or trying to understand what happens behind LLMs, I recommend watching this visual explanation.
🎥 Video: Self-Attention Explained: How Transformers Actually Work — Visual AI
📄 Foundational paper: Attention Is All You Need (Vaswani et al., 2017)
https://arxiv.org/abs/1706.03762
I'm continuing to share what I learn about AI, one concept at a time.
What's one AI concept that made much more sense to you after seeing it explained visually?
#SelfAttention #Transformers #GenerativeAI #LLM #ArtificialIntelligence #DeepLearning
Originally published on LinkedIn.