34 min total

Deep Learning

Encoders & decoders

Encoders & decoders
Encoders & decoders — diagram

Encoder blocks

  1. Positional Encoding

Add context of positions in the sentence
inputs word embeddings
outputs positional embeddings

  1. Multi-head attention
    generates attention vectors
  2. Add and Norm
    adds matrices
    normalizes the layer
  3. Feed Forward

The position-wise feed-forward network transforms each token representation between attention layers.

Residual paths and parallel computation

Encoders & decoders — diagram

These 4 features allows transformers to

  1. Encode the words into numbers
  2. Encode position of words
  3. Encode the relationships of words
  4. Train quickly in parallel
Encoders & decoders — diagram

Within a layer, embeddings, positional encodings, projections, attention outputs, and residual operations can be computed across token positions in parallel during training. Autoregressive generation still produces new tokens sequentially.

Residual connections

Purpose:

  1. Knowledge preservation, forward propagation modifies input considerably by time it reaches last layer. Results in loss of information
  2. To solve Vanishing gradient problem

Decoder blocks

  1. Masked Multi-head attention
    attention block on decoder side, generates attention vector for every word in output language.

This attention vector represents how much each word in output sentence is related to every word in the same sentence

Decoders multi-head attention is masked to ensure each position only attends to previous positions

  1. Encoder decoder attention block
    takes input from encoder and input from decoder's Masked attention block

Determines attention vector from input to output

Outputs attention vectors

  1. Linear layer
    expands dimensions for output language
  2. Softmax layer
    transforms output into probablility distribution
    word with highest probability is selected

Encoder decoder model is executed several time steps until we reach the end of sentence

Causal masking

To ensure that the prediction for a given position only depends on the positions before it, not on the future positions.

Encoders & decoders — diagram
Encoders & decoders — diagram

Cross-attention: connecting encoder and decoder

Encoders & decoders — diagram

Make query for word 'x' in decoder

Create keys for each word in encoder

Calculate similarities between word 'x' and keys by calculating Dot products

We run similarities through SoftMax function

The softmax weights determine how strongly the decoder attends to each encoder position.

Encoders & decoders — diagram
Encoders & decoders — diagram

Ek Tareeqy se Encoder Decoder ki attention values ka bhi ek cell bnjata

Encoders & decoders — diagram

We then pass the encoder decoder attention values to a fully connected layer, we get the output translation for let's go = vamos

Encoders & decoders — diagram
Encoders & decoders — diagram

Types of transformers

  1. Encoder only
    BERT
    code Bert
    Med Bert
  2. Decoder only
    ChatGPT
  3. Encoder Decoder
    Text-to-Text transformer

Architecture reference: Attention Is All You Need