Transformers
Dr. Dinesh Kumar Vishwakarma
PROFESSOR, DEPARTMENT OF INFORMATION TECHNOLOGY
DELHI TECHNOLOGICAL UNIVERSITY, DELHI.
Webpage: http://www.dtu.ac.in/Web/Departments/InformationTechnology/faculty/dkvishwakarma.php
Email: dinesh@dtu.ac.in
Introduction
2
11/11/2026
Encoder–decoder architecture
An encoder reads the input and a decoder writes the output. Introduced in 2017 by Vaswani et al.
Fixes RNN and LSTM limits
Built to overcome the slow, word-by-word processing of RNNs and LSTMs in tasks like translation.
Self-attention at the core
Every word looks at every other word to work out what it means in this sentence.
Whole sequence at once
Reads all words in parallel, not one by one, so it trains much faster than an RNN.
In plain terms: a Transformer is like a translator who reads the whole sentence first, checking how each word relates to the others, before writing anything.
Overview of Transformer Model
3
11/11/2026
How to read this figure
1 Embedding
Each word becomes a list of numbers, like a point on a map.
2 Attention
Each word looks at the other words and shifts toward the meaning they suggest.
3 Stack and predict
Attention + feedforward repeat a few times; Softmax picks the output word.
In plain terms: a Transformer turns words into numbers, lets every word look at every other word, and repeats this a few times before producing the output.
Embedding
4
11/11/2026
An embedding turns each word into numbers: similar meanings get similar numbers.
In plain terms: embedding is the bridge between language and maths. Once words are numbers, the model can measure how close two meanings are.
Embedding…
5
11/11/2026
In plain terms: each word is placed by its coordinates. Fruits sit together and vehicles sit together, so apple belongs at (5, 5), next to banana and strawberry.
Embedding…
6
11/11/2026
Where do we put the apple?
Problem
In plain terms: apple has two meanings. One fixed point cannot sit next to both banana and Android, so a single embedding is not enough. Attention fixes this.
Attention
7
11/11/2026
In plain terms: Attention captures the context. Next to orange, apple moves toward fruit (8, 8.5); next to phone, it moves toward technology (4, 5).
What about the other words?
8
11/11/2026
Attention
In plain terms: every word can pull on apple, but how hard it pulls depends on how related it is. Orange pulls hard; please, buy, am, and sit far away and barely pull.
Multi-Head Attention
9
11/11/2026
In plain terms: multi-head attention looks at the words through several embeddings at once, each one a cheap transformation of the original.
Linear Transformation
10
11/11/2026
Okay
The original embedding
Bad
Skewed: related words pulled apart
Good
Apple moves closer to orange
In plain terms: a linear transformation is a matrix multiplication. It reshapes the whole map at once, and some new maps place related words better than others.
Score
11
11/11/2026
In plain terms: the score tells the model which embedding to trust. The Good map (score 4) brings apple and orange together, so it counts 40 times more than the Bad one (0.1).
Multi-head Attention
12
11/11/2026
In plain terms: each head reads the sentence in its own way (one may track meaning, another grammar). Concat joins all views into one embedding.
Input: Q (query), K (key), V (value) for every word
Head 1
Linear
Scaled dot-product attention
Head 2
Linear
Scaled dot-product attention
Head h
Linear
Scaled dot-product attention
…
Concat: place all head outputs side by side
Linear: mix them back to one embedding size
Output: one embedding per word, now with context
How it works
1 Q, K, V: each word asks (Query), advertises (Key) and carries content (Value)
2 Linear: each head projects Q, K, V its own way, giving one new view of the words
3 Attention: each head scores every Query against every Key and mixes the Values: softmax(QKᵀ/√dₖ)·V
4 Concat + Linear: join the h views and blend them into one result
Why we need?
13
11/11/2026
RNN
Reads one word at a time
Problems
LSTM
Better memory, same order
Problems
Transformer
Reads the whole sentence at once
How it fixes this
In plain terms: RNNs and LSTMs read a sentence the way we read aloud, one word after another. A Transformer sees every word at the same time.
Applications
14
11/11/2026
In plain terms: if the data is a sequence where order and context matter (words, DNA, code), a Transformer can learn from it.
Recap: the RNN encoder–decoder
15
11/11/2026
Recap: RNN with attention
16
11/11/2026
Transformer
17
11/11/2026
Vaswani, Ashish, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, and Illia Polosukhin. "Attention is all you need." Advances in neural information processing systems 30 (2017).
The Transformer architecture
18
11/11/2026
Vaswani, Ashish, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, and Illia Polosukhin. "Attention is all you need." Advances in neural information processing systems 30 (2017).
From input text to output word
19
11/11/2026
A High Level Look
20
11/11/2026
A High Level Look…
21
11/11/2026
A High Level Look…
22
11/11/2026
Conclusion
23
11/11/2026
Self Attention Mechanism
24
11/11/2026
Step involved in Self Attention
25
11/11/2026
Consider 3 inputs
Step involved in Self Attention…
Step1: Consider 3 inputs with dimension 4
26
11/11/2026
Input 1: [1, 0, 1, 0] �Input 2: [0, 2, 0, 2]�Input 3: [1, 1, 1, 1]
Step involved in Self Attention…
27
11/11/2026
Step involved in Self Attention…
28
11/11/2026
[[0, 0, 1],�[1, 1, 0],�[0, 1, 0],�[1, 1, 0]]
Weights for key:
Weights for query:
[[1, 0, 1],�[1, 0, 0],�[0, 0, 1],�[0, 1, 1]]
Weights for value:
[[0, 2, 0],�[0, 3, 0],�[1, 0, 3],�[1, 1, 0]]
Step involved in Self Attention…
29
11/11/2026
[0, 0, 1]�[1, 0, 1, 0] x [1, 1, 0] = [0, 1, 1]� [0, 1, 0]� [1, 1, 0]
[0, 0, 1]�[0, 2, 0, 2] x [1, 1, 0] = [4, 4, 0]� [0, 1, 0]� [1, 1, 0]
Input-1
Input-2
Input-3
[0, 0, 1]�[1, 1, 1, 1] x [1, 1, 0] = [2, 3, 1]� [0, 1, 0]� [1, 1, 0]
[0, 0, 1]�[1, 0, 1, 0] [1, 1, 0] [0, 1, 1]�[0, 2, 0, 2] x [0, 1, 0] = [4, 4, 0]�[1, 1, 1, 1] [1, 1, 0] [2, 3, 1]
A faster way is to vectorise the above operations:
Step involved in Self Attention…
30
11/11/2026
Derive key representations from each input
Step involved in Self Attention…
31
11/11/2026
[0, 2, 0]�[1, 0, 1, 0] [0, 3, 0] [1, 2, 3] �[0, 2, 0, 2] x [1, 0, 3] = [2, 8, 0]�[1, 1, 1, 1] [1, 1, 0] [2, 6, 3]
Derive value representations from each input
Step involved in Self Attention…
32
11/11/2026
[1, 0, 1]�[1, 0, 1, 0] [1, 0, 0] [1, 0, 2]�[0, 2, 0, 2] x [0, 0, 1] = [2, 2, 2]�[1, 1, 1, 1] [0, 1, 1] [2, 1, 3]
Derive query representations from each input
Step involved in Self Attention…
33
11/11/2026
To obtain attention scores, we take dot product between Input 1’s query (red) with all keys (orange), including itself.
[0, 4, 2]�[1, 0, 2] x [1, 4, 3] = [2, 4, 4]�[1, 0, 1]
Step involved in Self Attention…
34
11/11/2026
[0, 4, 2]�[2, 2, 2] x [1, 4, 3] = [4, 8, 12]�[1, 0, 1]
[0, 4, 2]� [2, 1, 3] x [1, 4, 3] = [4, 12, 10]�[1, 0, 1]
Step involved in Self Attention…
35
11/11/2026
softmax([2, 4, 4]) = [0.0, 0.5, 0.5]
Step involved in Self Attention…
36
11/11/2026
The softmaxed attention scores for each input (blue) is multiplied by its corresponding value (purple). This results in 3 alignment vectors (yellow). In this tutorial, we’ll refer to them as weighted values.
1: 0.0 * [1, 2, 3] = [0.0, 0.0, 0.0]�2: 0.5 * [2, 8, 0] = [1.0, 4.0, 0.0]�3: 0.5 * [2, 6, 3] = [1.0, 3.0, 1.5]
Step involved in Self Attention…
37
11/11/2026
The resulting vector [2.0, 7.0, 1.5] (dark green) is Output 1, which is based on the query representation from Input 1 interacting with all other keys, including itself.
Step involved in Self Attention…
38
11/11/2026
We repeat Steps 4 to 7 for Output 2 and Output 3
Example
39
11/11/2026
Word | Embedding (vector) |
I | [1, 0] |
am | [0, 1] |
going | [1, 1] |
to | [0, 2] |
university | [2, 1] |
Example…
40
11/11/2026
Example…
41
11/11/2026
Word | Q (same as K, V) |
I | [1, 0] |
am | [0, 1] |
going | [1, 1] |
to | [0, 2] |
university | [2, 1] |
Word | Kᵢ | Dot product (Qgoing · Kᵢ) |
I | [1, 0] | (1.1 + 1.0 = 1) |
am | [0, 1] | (1.0 + 1.1 = 1) |
going | [1, 1] | (1.1 + 1.1 = 2) |
to | [0, 2] | (1.0 + 1.2 = 2) |
university | [2, 1] | (1.2 + 1.1 = 3) |
S=[1,1,2,2,3]
Example …
42
11/11/2026
Word | | |
I | 0.707 | 2.028 |
am | 0.707 | 2.028 |
going | 1.414 | 4.113 |
to | 1.414 | 4.113 |
university | 2.121 | 8.340 |
Word | |
I | 0.0984 |
am | 0.0984 |
going | 0.1995 |
to | 0.1995 |
university | 0.4042 |
Example …
43
11/11/2026
Example…
44
11/11/2026
Word | Output (Ox, Oy) | Interpretation |
I | (1.21, 0.90) | Learns a mix of itself and “university” — subject linked to goal. |
am | (0.63, 1.28) | Emphasizes grammatical information (high y-axis). |
going | (1.11, 1.10) | Balanced between action and context. |
to | (0.44, 1.53) | Grammatical connector (high y weight). |
university | (1.53, 1.00) | Semantically rich — conceptually dominant. |
Summary
45
11/11/2026
Reference
46
11/11/2026
Thank You�Contact: dinesh@dtu.ac.in �Mobile: +91-9971339840
47
11/11/2026