Understand how attention works through queries, keys, values, similarity scores, and weighted outputs. A beginner-friendly explanation of the core concepts behind the groundbreaking “Attention Is All You Need” paper.

Attention network is the core of Transformers, Vision Transformers, and Large Language Models (LLMs). Understanding the Attention mechanism and the neural architecture of the attention network helps to understand the transformers architecture and the LLMs much better.
In this tutorial, I focused on explaining the concepts of attention networks through the attention mechanism and the neural architecture of the attention network.

Attention Mechanism
Attention Mechanism is a neural architecture which resembles the retrieval process of a key-value pair, for example a dictionary, based on some condition (query). For example

A Key-Value pair
However in attention mechanism this type of retrieval is probabilistic and based on the Attention equation:

The attention mechanism is the generalization the retrieval process in a dictionary. To retrieve a value based on some key and a query in a dictionary, the similarity of the query and only one of the keys is 1 and the similarity of the query and the other keys is 0. Therefore for a query and a given key, we find only one value. But in the attention network we retrieve the weighted combination of the values, to be able to do back-propagation.
2- Neural Architecture of the attention network:
The Attention values is obtained by computing the similarity of query and the keys and computing the activation values. and then applying the attention function.
The first layer in the attention network is some keys and the computed similarity of a query and each key.

The first layer in Attention Network
where Sᵢ = f(q, kᵢ) is the similarity function. The most used similarity function are:
Dot Product

2. Scaled Dot Product:

where d is the dimensionality of each key. The advantages of scaled dot product over simple dot product is that it keeps the dot product in a certain scale.
3. General Dot Product

where W is the weight matrix. General dot products projects the query into the key space. It helps to keep the query and the keys in a same space.
4. Additive Similarity

Where q is the query, and W is the weight matrix.
The next layer is the computation of activation function in the form of hidden nodes. We use sᵢ and the summation of all the similarity values for For computing aᵢ. Therefore, a fully connected networks comes along.

Activation layer
where aᵢ is

The attention values computed by adding up the multiplication of aᵢ and vᵢ (a₁ v₁ + a₂ v₂ + …)

Attention Values
What comes next?
Understanding the concept of attention network is the basic step in large language model (LLM) roadmap. In my next article, I explained Transformer Architecture for LLM, which explains Multi-Head Attention and Masked Multi-Head Attention. Then, I gave an explanation for transformer Architecture.