34 min total

Deep Learning

Transformer training & inference

Transformer training & inference

Why shift the target sequence?

Shift the decoder input one token to the right and apply a causal mask. Each position predicts its target token using only earlier target tokens, matching the information available during generation.

Transformer training & inference — diagram

Encoder input: the source sequence of tokens.

Encoder output: contextual token representations produced by self-attention and feed-forward layers.

Decoder input: the target sequence shifted right, beginning with a start-of-sequence token.

Decoder output: a probability distribution for the next token at each target position. The end-of-sequence token marks completion.

Training pass

  1. Sequence of words at input is converted into embeddings
    transformed into positional embeddings
    fed to encoder
  2. Stack of Encoders produce encoded representations
  3. Words which have been generated so far are inputted to decoder as a sequence
  4. Stack of decoders inputs encoded representations and positional embeddings
    produces encoded representations for output
  5. Decoder outputs probabilities of the next word in sequence.
    Total number of neurons at output layer is total number of unique words in corpus
  6. At output loss function is used to compare predicted words with actual output

Inference

For greedy decoding, start with the beginning-of-sequence token and repeatedly append the highest-probability next token.

Generation steps

  1. Input English sentence to encoder and start of sequence to decoder
  2. Add first output word in seq of decoder
  3. Iterate by appending decoder sequence and predicting next word in decoder seq until <EOS> input token reached