34 min total

Deep Learning

Transformers & self-attention

Transformers & self-attention

Sequence-to-sequence models map an input sequence to an output sequence. In an encoder–decoder model, the encoder builds representations of the input and the decoder uses them to generate the output.

Why replace recurrence?

Recurrent processing makes positions depend on earlier computation. Long-range dependencies and limited parallelism motivate attention-based alternatives.

From recurrent models to Transformers

Non-recurrence model

Consists of encoder unit and decoder unit

Token embeddings

For NNs the most commonly used methods to convert words into numbers is word embeddings

Transformers & self-attention — diagram
Transformers & self-attention — diagram

Input to the embedding NN is one hot vector for the word for which we wanna create the embedding vector

The output is a learned embedding vector; the two coordinates shown here are a small illustrative example.

Positional encoding

There are several ways to represent token positions. These notes use sinusoidal positional encoding.

L: length of sequence

k: position of object in input sequence

d: dimension of output embedding space

P(k, j): outputs positional embedding. k, j are indexes in positional matrix

n: user defined scalar

i: used to map column indices

Transformers & self-attention — diagram

Compute in Radians

Attention overview

Transformers & self-attention — diagram

Self-attention uses context to change a token’s representation. In the example, relationships among “pizza”, “oven”, and “it” help the model represent the pronoun.

A worked self-attention example

Transformers & self-attention — diagram

To calculate self-attention for 'go' you don’t need to calculate key and values again, they remain same

You just need to create the query that represents the word 'go'

Calculate similarity scores between new query and keys

Run the similarity score through the softmax

Scale the values

Then add them

Then we get the self attention values for 'go'

Each query, key, and value projection has its own learned weights. The same projection matrices are applied at every token position.

We don’t have to calculate query key and value for first word first, before moving on to the second word

We can calculate queries, keys and values for each word at the same time

Transformers can take advantage of parallel computing and run fast

Multiple attention heads can learn different relationships among tokens.

Scaled dot-product attention

Query (Q):
Search term you enter into the search engine. This is what you are looking for.

Keys (K):
The indexed terms or metadata associated with web pages, such as titles, descriptions, and keywords. These help the search engine understand what each page is about.

Values (V):
The actual web pages or the content that the search engine returns as results. These pages contain the information you want.

Transformers & self-attention — diagram

Scaling by √dₖ keeps dot-product magnitudes from growing with the key dimension and pushing softmax toward saturation.

The outputs of scaled-dot Attention are attention vectors

Multi-head attention

Multi-head attention runs several heads in parallel, then concatenates and projects their outputs. Eight heads is one possible configuration.

Each attention head computes a weighted sum of value vectors.

We use multi-head Attention to run attention mechanism in parallel

Transformers & self-attention — diagram

Learned projections in multi-head attention

  1. Mapping Input to Output
  2. Reduce dimensions

ith head in Attention

Concatenation in Multi-head

Transformers & self-attention — diagram