A transformer is a Natural Language Processing (NLP) model. Older versions are RNNs which rely on recursion to remember and add context, but transformers found a better way to predict the next token/add context. Through self-attention.

Self-attention has one goal: relate each token with every other token in a sentence. There are many versions of attention, but the one that powers transformers is the scale dot product attention.

Transformer Architecture (ex. BERT & GPT & T5)

2 parts:

(1) Encoder = take prompt → tokenize → embedding → send to Encoder 2 → embed again → send to Encoder N → output: matrix of vectors (sent to decoder)

Goal: Encoder stack uses multiple encoders

encoder

Multi-head attention lets a model learn the grammatical or syntax of a sentence.

I am buying a present for my friend. She likes books. My present will be put in a book bouquet.

The model will relate she and friend, and I and my.

(2) Decoder = take in matrix of vectors

Goal: be able to predict next token

Decoder stack has multiple decoders

decoder

Different types of attention and the encoder/decoder architecture allow a model to understand context of a sentence. The weights computed during the decoder part allow a model to be predict the next token or fill in the blank/answer the prompt.


Self-attention

3 vectors: 

Matrix = number of tokens * token dimension

matrix

Matrix = 2 tokens * weight matrix of 4 = vector spaces (query space, key space, value space)

Goal: compress matrix into a smaller vector space that still holds the semantic meaning


Scale dot product attention

atteq

K, Q, V = matrices represent attention scores (more context gained, higher scores…lower context gained, smaller scores)

(1) Q and the transpose of K are multiplied together

(2) divided by root of num of dimensions in key matrix = the dimension of the output matrix, in this case 3 (in reality, the dimensions are in the hundreds)

(3) Multiple V so that the the value vector space becomes a context full representation of each token

(4) Take a softmax of the entire matrix → every row is a probability distribution

attdir

Another way to look at it is if they are 2 directions. The angle between the two arrows is measured. The smaller the angle, the more closely related the words are to each other giving a higher attention score.

attmath

By multiplying the attention score and the embedded vector form of that token, the value matrix is being adjusted to look at each token. V = value space now represents a relevance embedding dimension. Each token now has 3 dimensions of how it is relevant to the sequence. Now the vectors have become context-full.


Multi-headed attention

A transformer has multiple encoders. Each encoder then has multiple encoders with smaller embedding dimensions concatenated again. This helps a transformer focus on multiple relationships simultaneously. It takes in multiple grammatical rules.

ex. I can dance. The dance floor loves me.

I and me are connected. Loves and me has an object connection. Can dance has a connection, and so on.

With multiple heads learning at the same time, the response has much more context.

Process: Take input sentence → tokenize → embed each token → split into heads by multiplying embedding * weight matrix → use outputs: Q, K, V to calculate attention → multiple attention score with original embedding → add all output matrices Z0, Z1, Z2… to form one matrix Z→ multiply weight matrix and Z = output of the layer

Masked multi-head attention helps a model understand how to think and connect the dots.


Cross attention

Cross attention can take attention from the encoder and attention from the decoder, then puts them together. This helps the model relate each word and know which one is input or output. Using cross attention and the decoder, a weight or probability will be decided. The token with the highest weight is the next predicted token.

T5 transformer uses this cross attention.

Has a query = “what’s the point of this sentence?” - Queries generated from multi-head attention

Combines query matrix from decoder and keys/values from encoder → scale dot to get attention score

Why is this important? Relates the output to the original input given by the encoder + Allows it to encode grammatical rules in an encoder stack + speak freestyle

Significance? Helps a model understand and perform more complicated tasks


MLOps - Deploying transformer models

MLOps = Machine Learning Operations

Builds off of DevOps automation and development principles → automate pipelines to train models

Rolling back = returning to a previous model

Drift - change in the data, environment, or model

(1) How to detect drift?

(2) How to measure drift?

Take sample from past → run a K-S test against a known distribution from training time → if significant test results retrain model\

A new feature comes out → update model with feature

Online learning: models updated with real-time stream of data

Meant for models that rely on weights, coefficients, gradients, neural networks