Chapter Transformers
Chapter Transformers
3900
Shaunticlair Ruiz
Fall 2024
Contents
8 Transformers 3
8.0.1 CNNs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
8.0.2 The problem with locality . . . . . . . . . . . . . . . . . . . . . . . . . 4
8.0.3 RNNs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
8.0.4 Transformers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
8.1 Vector embeddings and tokens . . . . . . . . . . . . . . . . . . . . . . . . . . 8
8.1.1 One-hot encoding isn’t enough . . . . . . . . . . . . . . . . . . . . . . 8
8.1.2 Word Embeddings: Similarity between words . . . . . . . . . . . . . 8
8.1.3 Vector Similarity: Dot Products . . . . . . . . . . . . . . . . . . . . . . 9
8.1.4 Word2vec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
8.1.5 Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
8.1.6 "Adding" words together . . . . . . . . . . . . . . . . . . . . . . . . . 14
8.1.7 Tokenization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
8.2 Attention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
8.2.1 The Attention Mechanism: queries, keys . . . . . . . . . . . . . . . . 17
8.2.2 The Attention Mechanism: attention weights . . . . . . . . . . . . . . 18
8.2.3 Scaling factor for softmax . . . . . . . . . . . . . . . . . . . . . . . . . 21
8.2.4 The Attention Mechanism: values, attention . . . . . . . . . . . . . . 22
8.2.5 Why we need context . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8.2.6 Why we need attentive context . . . . . . . . . . . . . . . . . . . . . . 27
8.2.7 Self-attention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
8.2.8 Self-attention in matrix form . . . . . . . . . . . . . . . . . . . . . . . 29
8.2.9 Positional Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
8.2.10 Masking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
8.2.11 Attention Heads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
8.3 Transformers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1
MIT 6.390 Fall 2024 2
Transformers
Definition 1
Natural Language Processing (NLP) is a field of machine learning all about process-
ing, understanding, and using human language.
We’ll start by considering a few candidate models for NLP, before moving to the state-of-
the-art: transformers.
8.0.1 CNNs
In the previous chapter, we introduced the notion of a CNN:
• Convolutional Neural Networks (CNNs) view small regions of data, searching for
patterns across the image.
3
MIT 6.390 Fall 2024 4
This kind of structure is useful for image processing: nearby pixels tend to be related to
each other. They might form a sin-
gle line, or a corner, for
example.
• By prioritizing "nearby" information, we can create models that easily find those lo-
calized patterns.
Concept 2
CNNs are designed to represent locality:
• Rather than thinking about every possible connection between data, we only
connect "nearby" data. Thus, we need fewer parameters.
• If we focus on information that is nearby, we’re missing out on information that’s far
away.
• We need a way to encode "distance" of information, that doesn’t ignore the "distant"
info.
Concept 3
If information is spread over long distances, our CNN model won’t capture it.
• If a pattern is too big for our CNN filter, we’ll have more trouble finding it.
• The sweater that I found in the back of my old closet, which I hadn’t opened since
we moved into the house several years ago, still fits me perfectly.
Note that the beginning and the end of this sentence are linked as a single idea: "The
sweater still fits me perfectly".
• But there’s a huge gap between these phrases: it might be difficult to connect infor-
mation over such a wide gap, while ignoring what’s in-between.
• This also comes up in longer passages: in a paragraph, the first sentence might create
context for the last sentence.
Concept 4
In language, words can be far apart, while still providing important context for the
meaning of the text.
• Thus, language processing is difficult for models which focus too much on local-
ity.
8.0.3 RNNs
One useful observation might be that language tends to be sequential: words come in a very
particular order.
Concept 5
In image processing, we see many pixels at the same time: the whole image is pro-
cessed in parallel.
Recurrent Neural Networks (RNNs) are, thus, a sequential model, designed for processing
language.
Each xt is one word in our sentence: we process the text, one word at a time. After every
word, we update our memory ("state" st ). yt is our output at time t.
By storing information about previous words (using a state), our model can "read" each
word in order, while still remembering earlier parts of the text.
• While a CNN can only observe k consecutive pixels/words in a row, our RNN might
be able to contain some information about words that are much further back in time.
How well does this work? RNNs have seen success in the past, but it struggles with for-
getting: our RNN can only store so much information about words it’s seen before.
• As a passage gets longer, our RNN is only paying attention to words it’s seen re-
cently.
Moreover, our RNN doesn’t have any way to choose which words to prioritize: each new
word will have to replace some information about older words.
• So, our RNN naturally prioritizes the most recent words. The more recent words
haven’t been replaced
• But the most recent word isn’t always the most important one, as we saw above (in yet.
the sweater example)!
Concept 6
RNNs (Recurrent Neural Networks) tend to struggle with longer bodies of text:
• The longer we run our RNN, the less it usually remembers about the distant
past.
Moreover, it prioritizes recent words, even when more distant words may be more
important.
In the end, RNNs have, in most language applications, been replaced by transformers: a
different model for language processing. However, some trans-
former models have be-
gun using the concepts
of LSTMs, an RNN vari-
ant. We won’t cover
this topic here.
8.0.4 Transformers
One clever way to think about this problem is to recognize that our goal is to decide which
words are related to each other, whether they’re nearby or far apart.
• In other words, which words should we pay attention to, in order to understand the
text we’re reading?
This is exactly the problem that transformer models solve, using the appropriately named
attention mechanism.
Clarification 7
In this chapter, we’ll use transformers to process language, using the mechanism of
attention.
• But the same tools can be applied to many other problems: image and audio
processing, robotics, etc.
• First (11.1), we’ll convert words into vectors. One-hot encoding is too simple, so we’ll
use a different approach: vector embeddings.
• Next (11.2), we’ll figure out which words in a passage are relevant(or connected) to
each other, using a clever system called attention.
• Finally (11.3), we’ll put together these ideas to create a complete model, known as a
transformer.
table
bed
couch
(8.1)
chair
0 1 0 0
0 0 0 1
vchair =
0
vtable =
0
vcouch =
1
vbed =
0
(8.2)
1 0 0 0
Concept 8
One-hot encoding loses a lot of information about the objects it’s representing.
• It’s hard to say which words are "similar" to each other, for example.
Example: You probably associate the word "sugar" with "sweet", and "salt" with "savory".
• But, if you use one-hot encoding, all of these words are "equally different". You could shuffle the
rows of one-hot vectors,
and represent the same
information.
0 1 0 0
So, we can’t use the
0 0 0 1
vsalt =
0
vsavory =
0
vsugar =
1
vsweet =
0
(8.3) order of 1’s and 0’s to
determine "closeness":
1 0 0 0 the order can be freely
changed.
In order to incorporate this information, we’ll need a better way to represent words as
vectors.
w −→ vw v w ∈ Rd (8.4)
How do we want to convert words into vectors? Above, we mentioned that one-hot doesn’t
tell us how similar two words are.
Clarification 9
There are many ways for words to be similar: similar word length, similar choice of
letters, etc.
But in our case, we’re interested in semantics: the meanings of the words. We want to
know which words have similar meanings.
• Example: We don’t consider "sugar" and "sweet" to be similar because they both start
with "s".
– They’re similar because of meaning: sugar tastes sweet. Sweet strawberries con-
tain sugar.
Concept 10
We often want our word embeddings vw to tell us which words are semantically
similar to each other: which words have similar meanings.
Our goal is to make this statement true. But we have a problem: these are concepts, rather
than computable numbers.
• We’ve come across this problem multiple times, and we’ll solve it the same way as
always: using the dot product.
Concept 11
Review from the Classification chapter
You can use the dot product between vectors u and v, normalized by their magni-
tudes, to measure their "cosine similarity".
u·v
SC (u, v) =
|u| · |v|
If two vectors are more similar, they have a larger normalized dot product.
Clarification 12
You can use SC (u, v) to measure the similarity between two vectors, ignoring magni-
tude.
But for simplicity, we’ll skip the normalizing step, and just take the dot product:
SD (u, v) = u · v = u⊤ v
Similar vectors
z }| {
(va · vb ) is large ⇐⇒ a and b are semantically similar words (8.5)
8.1.4 Word2vec
Next, we should get into the math of how to determine which words are likely to be simi-
lar.
• But this is a bit cumbersome, and isn’t really necessary for understanding transform-
ers. The short version: we
expect words which
frequently appear in
So, we relegate this mathematical labor to Appendix D, where we’ll get into the details of
the same contexts, to be
skipgram and word2vec. similar.
Definition 13
We can think of word2vec as a system for word embeddings where words which have
similar meanings, have similar vector embeddings.
Instead, we’ll skip a couple steps, and look at things from a high level.
8.1.5 Probability
Our goal is to be able to numerically talk about the "similarity" or "relatedness" of words.
Above, we represent this with a dot product: this gives us a real number u · v ∈ R.
• This number isn’t very meaningful, though. For example, what does a "similarity of
37" even mean? Is that high? Is that low?
Generally, our best bet for understanding a number like this is to compare it to other num-
bers. You know that someone
who is 6’5" is really tall,
So, let’s think about the relative similarity of words: if we have two words, w1 and w2 , because you know how
tall other people tend to
which one is v more related to? be.
We’ll focus on one simple tool for comparison: probability.
• One way to think about it is, "how likely is wi to be the most relevant word to v, in
any given context?" In skipgram, our prob-
ability comes from ask-
• Alternatively, "how confident are we that these words are actually closely related, ing, "how likely is wi
to show up in the same
compared to others?" context?"
The higher the probability of word w1 , the lower the probability of word w2 , and vice
versa.
Concept 14
One way to describe the relatedness of different words wi is with a probability
P(wi | v).
e zk
Softmax(zk ) = P z (8.6)
ie
i
Definition 15
Suppose that we have n possible words (n "classes"), and we want to figure out which
one is correct.
To keep it positive, zk is converted to ezk : each ezi competes to see which class is more
likely.
• To create a probability, we compare the score of class k to all of our other classes,
using softmax.
All classes
Class k
z}|{ z }| {
X ezk
ezk vs e zi =⇒ Softmax(zk ) = P z
ie
i
i
We repeat this process for every possible word i, to get all of our predictions.
• The higher (va · vb ) is, the more similar/related we expect a and b to be.
• The same is true for zk : if zk is larger, then our probability goes up.
zb = v a · v b (8.7)
Key Equation 16
The more similar (bigger dot product) a and b are, the more likely we predict to find
them together.
eva ·vb
P b a = P v ·v
ie
a i
exp va · vb
P b a =
P
i exp va · vi
This kind of interpretation makes our word embeddings a bit more useful.
• Later, we’ll find that it’s the most important part of making transformers work!
One example is the idea of "adding" words together. Normally, it’s hard to say how to "add
words" together, but we do know how to add vectors.
ruler
z }| { female ruler
z }| {
vking − vman + vwoman ≈ vqueen (8.9)
We can repeat this process for other words: Paris is the capital of
France, and Rome is the
capital of Italy.
vparis − vfrance + vitaly ≈ vrome (8.10)
Concept 17
Transforming a word into a vector allows you to use vector operations, like addition
and subtraction.
This approach doesn’t always work, but the fact that it works sometimes suggests that
our vectors might capture real information about the "meanings" of words.
Concept 18
Reducing a word to a single vector can cause problems, because the same word might
change its meaning, based on context.
• Example: For example, the word "bank" has a very different meaning when you
compare "bank account" to "river bank".
8.1.7 Tokenization
One clarification, before we move on: so far, we’ve talked about predicting whole words,
because it’s easy to work with.
• But often, for language analysis, we break up words into parts, called tokens.
Definition 19
Rather than using/predicting entire words, we use small parts of words, called to-
kens.
• Example: You might break up the word "eating" into "eat" and "ing": both are mean-
ingful, by themselves.
While "tokens" are used more often than "words", words often make for better examples,
so we’ll keep using them through the rest of this chapter.
Clarification 20
We’ll continue using words (instead of tokens) for examples, when it’s convenient.
8.2 Attention
Our word embedding technique has given us a basic way to talk about which words are
"related".
• We can even use this to learn some about the "meanings" of words.
Concept 21
Our word embedding technique has two major problems, for representing the mean-
ings of words:
• There’s a lot of information we’re missing: similarity to other words isn’t enough.
We’ll need a vector to represent that information.
• The meaning of a word is contextual: the sentence you put a word in, will affect
its meaning.
It may not look like it, but our word embedding technique has already given us the basic
tools we need to solve these problems.
Concept 22
We’ll create a system that solves both of these problems, using 3 word embeddings: v,
k, and q.
• When finding the meaning of a word, we’ll calculate context from nearby words.
– We’ll use word similarity to figure out which parts of the context are most
important.
– For this purpose, word will need two embeddings: a key vector k, and a
query vector q.
This description is over-simplified, which is why we’ll need to go into detail below.
Suppose we want a general idea of what "mexican" food is like. We’ll need to consider lots
of foods, and take an average of those we consider to be "mexican". Admittedly, we’re turn-
ing "mexican-ness" into
a number, which can be
a bit strange.
This problem comes in three parts: let’s consider the first two, "query" and "key".
It may help to think
this way: "if someone
• Query q: we’re searching for "mexican" food. The word "mexican" is represented by is talking about mexi-
a query vector q. can food, how often are
they talking about this
food?"
– This is like our previous word2vec embedding: if two vectors are similar, then
we expect them to have similar/related meanings.
– So, we’ll compare q to each food, to see which foods are ’close’ to mexican.
Definition 23
The query vector q represents a word, that we’re comparing to several other
words ("keys").
• Key k: Each food (apple, burrito, sushi...) has a key vector k, representing it.
Definition 24
The key vector k represents a word, that we want to compare to the query q.
– It answers the question, "what kinds of searches does this word match"?
Because it’s a word embedding, which encodes meaning, we expect that, if k and
q are similar, then our key word is more relevant to our query.
Each embedding has a role: a query is used to search for relevant words, and a key is
responding to that search, on behalf of one word. Reminder that when
we say "word", we’re
simplifying: we could
Last Updated: 12/25/24 08:36:30 talk about any kind of
token.
MIT 6.390 Fall 2024 18
Concept 25
Another way we could view keys vs. queries:
Notice that we’ve made a perspective shift, in how we view word embeddings:
Concept 26
When we were developing word2vec, we wanted similar vectors to represent seman-
tically similar words.
We look for keys that are the most relevant to our query.
These two ideas don’t necessarily conflict, but they have somewhat different goals.
Key Equation 27
We can get a score for how relevant the word b is to word a, by taking the dot product
between b’s key, and a’s query.
q a · kb
q · k = qT k
This gives us a "score": the higher k · q is, the more similar they are.
We convert w1 and wq into a key and query, respectively, before taking the dot product.
Notation 28
Note that k and q have to have the same length: they’re both (dk × 1) column vectors.
If their lengths don’t
mach, we can’t take the
But we’re not just considering one key word: we’re considering all of of them. dot product.
• In our "mexican food" example, we need to check every food, to see which ones best
fit the category.
Notation 29
We have N distinct keys.
In the official notes, we
use n instead of N. This
How do we compare each of these keys? doesn’t affect any of our
math.
• Once again, we’ll reuse a tool from word2vec: softmax.
Key Equation 30
We can compute the relative relevance of a key kj , by:
eq·kj
P kj q = P q·k
ie
i
Finally, we’ve converted each word into their "probability" of being relevant.
• But, one benefit of matrix multiplication, is that we can combine multiple operations
into one.
First, we’ll combine all of our key vectors into a matrix K: This matrix has shape
(N × dk ): the transpose
of what you might ex-
⊤ pect.
With that, we can compute all of our dot products at the same time: This product has shape
(1 × N).
⊤
q · k1
q · k2
q ⊤ KT =
..
(8.12)
.
q · kN
Key Equation 31
By combining all of our keys into a matrix K, we can compute all of our attention
weights at the same time.
⊤
P k1 q
P k q
2
softmax q⊤ K⊤
P K q =
..
=
.
P kN q
Now, our diagram is visually simpler, though it reflects the same information. "MatMul"
means "Matrix Multiplication".
Notation 32
Reminder that keys and queries are both vectors of length (dk × 1).
We have one problem: the larger dk is, the more terms in our dot product: our dot product
can grow unreasonably large.
Key Equation 33
When computing attention weights, we normalize our dot product qT k by a factor
√
dk .
• This compensates for the fact that longer vectors will create larger dot products.
q ⊤ K⊤
a(q, K) = softmax √
dk
• Now, we want to make them useful. Our original goal was to get an average sense of
what "mexican" food is like.
To make this concrete, we’ll introduce our third embedding: the value vector.
• Value v: Each food has a value vector, directly storing information about a word.
– Unlike the key/query vectors, this embedding isn’t based on similarity to other
words.
– Instead, it usually contains more direct information about our word: in this ex-
ample, maybe it contains the price, calories, ingredients, etc. Note that, in a real
model, value vectors
are often "learned" dur-
ing training. So, they
Definition 34
won’t always contain
The value vector v represents a word, and stores useful information that it can such simple, easily ex-
plained data.
contribute to the query.
– It answers the question, "what useful data could this word contribute to the
query?"
By adding together the value vectors from each word relevant to the query, we
can get an overall "averaged value" for q.
For our example, let’s suppose that the value vector contains price, calories, and salt.
pricei
vi = cali (8.13)
salti
• Some foods are common for mexican food, and some are more rare.
• So, to get an average, we’ll need to emphasize more "common" mexican food.
How do we do that? Using our attention weights: the larger the attention weight, the more
"relevant" a food is to our mexican food calculation.
If we use q to represent mexican food, and ki is the key for the ith food, we get:
Weighted average
z
X }| {
calq = P(ki |q) cali (8.14)
i
Rather than repeating this process for each row of v, we can just do a weighted average of
the whole vector, at the same time:
X
vq = P(ki |q) vi (8.15)
i
Key Equation 35
Each word i has a value vector vi , which represents all of the useful information it can
provide to the query.
• We can use a weighted average to combine all of these value vectors together:
this provides the "overall context" for the query.
• Each value is weighted based on its attention weight P(ki |q): how likely it is to
be relevant.
X
vq = P(ki |q) vi
i
Just like we did for the ki · q operation, we can re-write this in terms of matrix multiplica-
tion.
⊤
P k1 q
P k q
2
P K q =
..
= softmax(qT KT )
.
P kN q
⊤
Notation 36
We’ll assume that we have N value vectors of length dk .
Now, we can compute with every value vector at once: If you study the classic
"Attention is all you
need" paper, you’ll find
Key Equation 37 that their version of k
and q are transposed
We can compute attention using matrix multiplication: compared to ours.
q ⊤ K⊤
Attention q, K, V = softmax √ V
dk
⊤ ⊤
q√ K
Where softmax dk
computes our attention weights.
Definition 38
Attention(q, K, V) is the weighted average of all of our value vectors (transposed).
Concept 39
Attention is a mechanism that allows you to combine information from multiple to-
kens, weighting each token by how relevant it is.
Each token has a value vector (information from that token), and a key vector (used to
compare this token to the query).
Clarification 40
There are multiple ways we can implement attention.
So far, we’ve mostly focused on the mathy details of how attention works: an abstract idea
of "relevance" between words, "combining" the value ("meaning") of different words, etc.
• Here, we’ll try something different: we’ll focus more on why we use attention, and
how it applies to a real, concrete situation.
Consider the task of language translation: we have a sentence in one language, and we
want to convert it into another language, while preserving the meaning.
• But most humans would say that, in this situation, the word "warm" means ’friendly’
or ’kind’.
We know this because of the context: a "warm smile" usually means a "kind smile". The
word ’smile’ has changed the meaning of the word ’warm’.
Concept 41
The meaning of a word can change based on the other words which are nearby.
If our machine blindly translated "warm", without context, we could’ve ended up with the
wrong meaning in another language.
In the above sentence, the word "smile" changed the meaning of "warm".
• How do we know that "smile" is the important context word? "her" is equally far
from "warm".
• Attention handles this for us: we "pay more attention" to the word ’smile’ than the
word ’her’, when we’re trying to understand "warm".
Concept 42
Attention allows us to determine which parts of the context are most important to a
particular word.
8.2.7 Self-attention
Attention has given us a tool for comparing one word to every other word in a sentence.
"dog" is represented by a query qdog , compared to the key for every other word in the
sentence. This gives us our attention weights.
Next, we combine these weights with the value vector for each word. This gives us our
attention: the "contextual meaning" of the word dog.
• But we need to get the meaning of every word in the sentence, based on the context
from other words.
This time, we use the query qbites for the word "bites". However, the key and value vectors
are still the same for each word.
We need to repeat this attention process once for each word.
This is interesting: we’re seeing how much each word affects each other word in the sen-
tence. We’re seeing how the sentence provides context for itself.
Definition 43
Self-attention is the process of using attention on every word in a passage.
• For the ith word, we compare it to every other word in the passage.
This allows us to interpret each word, based on the context provided by the rest of the
sentence.
Technically, we also
compare each word to
itself.
8.2.8 Self-attention in matrix form
How do we handle this, mathematically?
• When we are getting the attention for word wi , we use its query qi to compare it to
other words in the sentence.
We’ve gone from having a single query q to having many qi : one for each word in the
sentence.
T
Notation 44
A few useful dimensions: in an attention problem, we have...
• nk keys of length dk .
• nq queries of length dq .
• nv values of length dv .
Each of these queries will create a separate set of attention weights, softmax(qTi K).
Key Equation 45
We define the self-attention weight matrix A, to represent all attention weights:
√
softmax qT1 KT / dk
softmax qT KT /√d
T
= softmax QK
2 k
A= .. √
dk
.
√
softmax qTN KT / dk
This is an (N × N) matrix.
• Element αij (row i, col j) tells us, "how important is word j (key) as context for
word i (query)"?
QKT
Attention(Q, K, V) = AV = softmax √ V
dk
It is a (N × dk ) matrix.
Row i gives the averaged value vector y(i) for the ith word, based on all of the surrounding
context. We could view this as
the "output" for the ith
word.
• We can write this in element-wise form:
X
N
(i)
y = αij vj
j=1
One theme we’ll run into, many times in this chapter, is that attention-based models benefit
from being able to parallelize:
Concept 47
Transformer Parallelization I
• Each qTj ki term is independent of the others: we can compute all of the key-
query dot products at the same time, rather than waiting for one to finish before
starting the others.
• Currently, our key ki is determined by asking the identity of the word at index i.
• This key doesn’t encode information about the position of this word in the sentence.
• Example: "The cat lies on the green table" and "the green cat lies on the table" are not
the same: moving the word "green" to a different index changes its meaning.
Definition 48
We apply positional encoding to each word embedding: each embedding includes
information about the position of a word in the text.
• This allows our attention mechanism to use this information when deciding the
relevance of different words.
8.2.10 Masking
One common use for transformer models is text prediction: learning what word should
come next, based on what it has seen so far.
Typically, we would give our model the text, and give it a chance to try to predict each
index, before it can see it.
• We don’t want our model to be able to see the words it’s supposed to be predicting.
So, we’ll hide those words, so our model can’t see them. In this case, we want our model
to predict the next word: "dog".
Definition 49
Masking is a technique where we hide some information from our model, so it can’t
use that information.
• For example, if our model is being used to predict text, we hide the text that it’s
trying to predict.
However, the word "masking" can apply to any situation where we want to hide tokens
from the model.
But there’s something we haven’t considered: the "importance" of different words, depends
on what you’re interested in. Let’s consider a couple examples:
Example: "The boy kicks the red ball": our focus is on the word "ball".
Example: "I miss her warm smile": our focus is on the word "warm".
– The word "smile" changes the meaning of warm from ’high temperature’ to
’kind’.
Example: "John said that he isn’t hungry": our focus is on the word "John".
– "he" refers to the same object as "John": if we apply something to the word "he",
it also applies to "John".
Concept 50
What is "important" in a sentence can change, based on what you’re trying to study.
• And generally, these ideas of "important" won’t agree with each other.
• Rather than having our attention mechanism try to handle all of these kinds of im-
portance, we could create a separate mechanism for each one of them.
We’ll do just that: each "perspective" will be represented by a different mechanism. We call
each of these, attention heads.
Definition 51
A transformer model may use multiple attention mechanisms at the same time:
If we have 3 different attention heads, each one may encode the word "silly" differently.
We could have three different keys for this one word: k1 , k2 , and k3 .
• Each head will require a distinct word encoding: K(h) , Q(h) , and V (h) .
Concept 52
Transformer Parallelization II
Each attention head uses calculations which are independent from the others: we can
compute each attention head at the same time!
8.3 Transformers
Now that we’ve built up attention, we’ll use it to build a transformer. We’ll assume our
transformer uses self-attention, though the math works out similarly even if it doesn’t.
Definition 53
A transformer block is a collection of attention heads running in parallel, applied to
the same text.
• We aren’t actually given them: we’re given a sequence of tokens: each token is a
vector x representing a word. So, our whole body of text is a matrix X. Each vector is length d:
this is different from the
length of the embed-
We’ll compute each embeddings by using a linear transformation:
ding, dk .
Key Equation 54
We use projection matrices Wk , Wq , and Wv to transform each token x(i) into embed-
dings k,q, and v.
ki = Wk⊤ x(i)
qi = Wq⊤ x(i)
vi = Wv⊤ x(i)
Key Equation 55
We can compute K, Q, and V:
K = XWk
Q = XWq
V = XWv
Concept 56
One benefit of computing keys, values, and queries based on weight matrices is that
we can train these matrices:
• Rather than manually designing the embeddings, we can allow our model to
learn whichever embedding is most useful.
Notation 57
If we have H attention heads in a transformer block we’ll indicate the hth head with:
K(h) = XWh,k
Q(h) = XWh,q
V (h) = XWh,v
Here’s an example with H = 3 attention heads. Each uses a distinct set of keys, values, and
queries.
• Transform each token back into the original dimensions: going from length-dk to
length-d.
• Combine the results from each attention head: we’ll do a weighted average.
Key Equation 58
After computing attention for each head, we take a weighted average of our heads,
combining them together:
• For each head, we use matrix Wh,c to scale the weight of each head, and convert
them back to their original shape.
X
H
Attention Q(h) , K(h) , V (h) Wh,c
u=
h=1
u, the final output of our multi-headed attention, has shape (N × d), where the jth
column represents the jth token.
⊤
In fact, we can compute this multi-headed attention, one u(i) at a time. Reminder that αij is an
attention weight from
A, and v(j) is a value
Key Equation 59 vector of V.
We can combine our multi-attention heads as
Heads Attention
z }| { z }| {
XH XN
(h) (h)
⊤
u(i) = Wh,c αij vj
h=1 j=1
This
P is a nested sum:
h,j (·),
Definition 60
(Review from Neural Networks 2)
Vanishing gradient occurs when a deep neural network ends up with very small gra-
dients in the earlier layers.
This happens because a deeper neural network has a longer chain rule: if all of the
terms are less than one, they’ll multiply into a very small value, "vanishing".
This means that our gradient descent will have almost no effect on these earlier weights,
slowing down our algorithm considerably.
In short: the "further away" from our input layer, the messier our gradients get.
One simple solution is to include our original, unmodified input, deeper in the neural
network: we just add it, so that our second layer gets to see the input data, too.
Our output contains direct information about the input. This hopefully improves training.
Definition 61
In a residual block, the input x is added to the output F(x) of the block (in our case,
multi-headed attention) .
output = F(x) + x
– The long chain rule is what causes vanishing gradient: we’ve created a
shorter chain rule.
If you ever hear some-
one refer to a "ResNet"
or "Residual Network",
this is a CNN that uses
the same technique!
Last Updated: 12/25/24 08:36:30
MIT 6.390 Fall 2024 41
Definition 62
(Review from Neural Networks 2)
• Standardize the pre-activation for each layer across data points in the batch us-
ing mean µi and standard deviation σi (for the ith dimension).
Zij − µi
Zij =
σi
• Choose the new mean and standard deviation for the pre-activation using (n×1)
vectors G and B
bik = Gi ∗ Zij + Bi
Z
In short: we set the
(mean, sd) to (0,1) and
We would get the same kinds of benefits from normalization in transformers as we did then scale it back up to
(Gi , Bi ).
before in NNs.
Stabilizing our training
But rather than normalizing across multiple data points (batch), we’ll normalize across the process, mostly.
features (layer) of a single token.
Key Equation 63
h iT
Suppose we have a (d × 1) data point z = z1 z2 · · · zd .
Layer normalization computes the mean µz and standard deviation σz across our fea-
tures zi
v
1X u1 X
u d
µz = zi σz = t (zi − µz )2
d d
i i=1
z − µz
znorm =
σz
z − µ
z
LayerNorm(z; γ, β) = γ +β
σz
Layer normalization
can be used on a single
Now that we understand this process, we can apply this to our transformer model: data point, while batch
normalization requires
many.
Last Updated: 12/25/24 08:36:30
MIT 6.390 Fall 2024 42
• After we get u + x (creating the residual block), we use layernorm on each token sep-
arately:
Concept 64
At the end of our residual block, we apply LayerNorm to each of our tokens separately
u(i)
norm = LayerNorm(u
(i)
+ X(i) , γ1 , β1 )
We append a LayerNorm layer. We’ll follow the convention from the "Attention is all you
need" paper and combine these into a single unit: "Add+Norm".
• We’ll follow the same sort of pattern here: the main difference being that we apply
feed-forward after only one layer of multi-headed attention.
Key Equation 65
After we apply Add & Norm to our Multi-headed attention, we run the output through
a feed-forward layer, processing the data it receives.
z = W2T ReLU W1T unorm
We can think of this as apply a hidden FC layer to our network, followed by another linear
transform.
Linear, ReLU, linear. Once again, following "Attention is all you need", we simply call this
the "Feed forward" Layer.
Concept 66
After our feed-forward layer, we apply Add & Norm again.
z(i)
norm = LayerNorm(z
(i)
+ u(i)
norm , γ2 , β2 )
Definition 67
A transformer block is made up of several functions composed together:
• Multi-headed attention
– Each head encodes the input text X as keys K(h) , a value Qh, and vectors
V (h) : one for each token.
• Feed-forward
Both "Add & Norm" layers accomplish the same thing: they create a residual connec-
tion.
Concept 68
Each layer of our transformer block serves an important function:
• The multi-headed attention layer explores connections between tokens, and pro-
vides information about the internal structure of our data.
• The add & norm layers create residual connections between the input/output of
the preceding layer, improving our gradient-training process.
From here, we can design a transformer model by combining many of these transformer
units in series.
Our transformer will start by predicting the first word in the sentence: presumably "to-
davía".
• But not necessarily: if our model isn’t well-trained yet, it might predict some random
word, like "espacio". It’s also possible for us
to have multiple valid
translations, but we’ll
Now, we want to predict the second word in our output. But we just brought up an impor-
ignore that for now.
tant problem:
• The best "second word" in our translation is dependent on the first word. We should
factor that into our model, when predicting the second word.
• If our first word was wrong, then we’re more likely to use an incorrect second word!
The solution? Instead of using the first word we predicted, we use the correct first word.
• Only one condition we need to remember: we need to mask the rest of the "correct"
output sentence, so our model can’t use it to cheat.
Concept 69
When training our model to complete a language task, our model predicts each word
(token) one-by-one, based on two pieces of data:
• The desired output sequence for every token before the one we want to predict.
Example: Suppose we’re predicting the third word in our above sentence. We’ll use the
first two "correct" words as part of our model: In this case, "tengo" is
the word we want to
" # predict.
I’m not hungry yet
=⇒ tengo
Todavía no
Something to take note of: we predict the ith token based on the input, and the first i − 1
desired inputs.
• That means that, when predicting token gi , we don’t care what we predicted for the
previous tokens!
Concept 70
Transformer Parallelization III
• That means we can predict every token in our sentence at the same time!
This is a huge advantage in training transformers: it can essentially think about the entire
sentence at the same time, massively speeding up training.
Clarification 71
We can’t parallelize token generation when we’re using our model after training:
• We can parallelize during training because we’re using the desired output for
the previous i − 1 tokens.
• When using our model for unseen data, we don’t have "desired output": we have
to use our actual output for the previous tokens.
We have to wait for our model to predict the first i − 1 tokens, before it predicts the ith
token.
Concept 72
There are three tasks we want our model to complete:
• We’ll process our prompt using a complete transformer block. This unit is our en-
coder: we encode our prompt in a form that is more meaningful to our computer.
• However, for our desired output, we’ll only use attention, learning about the internal
structure of the output. Why not add the feed-
forward layer? We’ll
add it later: there’s an-
– We’ll also use this unit to mask our output (so our transformer can’t "look
other component we
ahead" at future tokens). want to add first (see
below).
We’ll add the second feed-forward unit later. First, we want to combine information from
our input, with the earlier tokens of our output.
We accomplish this with another attention unit: this time, we’ll use cross-attention.
Definition 73
In cross-attention, our queries come from one sequence of text, while our keys/values
come from a different sequence of text.
Our goal is to use the earlier part of our output sentence to determine which parts of our
input we should pay attention to in our input sentence, when choosing the next token. For example: if our out-
put sentence already in-
cludes a word, it might
• Our keys/values represent the words we might want to pay attention to.
be less likely we’ll need
to use that word again.
• Our queries help us decide what to pay attention to.
We can use "attend" as
Thus, we’ll use keys/values from our encoded input, and queries from our previous out- a verb meaning "pay
attention to": this is
put tokens. common when talking
about transformers.
Now that we’ve integrated information from both our input and output, we finally include
our feed-forward unit: we’ll process our integrated information.
Concept 75
We break our transformer into an "encoder" and "decoder" unit:
• The encoder transforms our input into a representation that contains more useful
information: connections between tokens in the prompt, etc.
• The decoder transforms that encoding into a output/response: this decoder takes
the information we’ve gathered, and applies it to our problem.
• The encoder stores our English text in a form that hopefully represents the meaning.
• The decoder "decodes" that representation into a form we can read, but in a different
language: Spanish, in our example.
In this analogy, we’ve created a special "code" that we write in English, and read in Spanish.
• We linearly transform our data: each token gets a "score", based on how likely we
think it is to be the correct one.
• We apply softmax, to turn these scores into probabilities. We get a probability for
every possible token.
This is the (now-famous) diagram from the "Attention is all you need" paper! We’ve excluded the ini-
tial embedding (turning
Only one detail still missing: words into vectors) and
positional embedding
(adding information
• Our decoder/encoder typically has several copies of the same unit in a row: for about the position of
example, we might have 3 transformer blocks in a row for our encoder. each word in the sen-
tence).
Notably, this is only one kind of transformer model: which architecture we use depends We could include those
on the problem, cost constraints, etc. for completeness, but
that would just take up
more space.
Definition 76
In pre-training, we expose our model to a very large dataset of human language, so it
can learn patterns in that language.
Definition 77
In fine-tuning, we take our pre-trained model, and train it for a specific task.
8.3.11 Variations
We could make variations on this network:
• Use a different style of attention (rather than the dot product, we use some other
similarity metric).
8.4 Terms
• Natural Language Processing (NLP)
• Locality
• Co-occurrence
• Context window
• Skipgram
• Word2vec
• Token
• Key Vector
• Query Vector
• Value Vector
• Attention Weights
• dk
• Attention
• Self-attention
• Positional Encoding
• Masking
• Attention Head
• Projection Matrix
• Multi-headed attention
• Residual Block
• Residual Connection
• Layer Normalization
• Transformer Block
• Cross-attention
• Encoder (Transformers)
• Decoder (Transformers)
• Encoder-Decoder Layer
• Pre-training
• Fine-tuning