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.

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
- Sequence of words at input is converted into embeddings
transformed into positional embeddings
fed to encoder - Stack of Encoders produce encoded representations
- Words which have been generated so far are inputted to decoder as a sequence
- Stack of decoders inputs encoded representations and positional embeddings
produces encoded representations for output - Decoder outputs probabilities of the next word in sequence.
Total number of neurons at output layer is total number of unique words in corpus - 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
- Input English sentence to encoder and start of sequence to decoder
- Add first output word in seq of decoder
- Iterate by appending decoder sequence and predicting next word in decoder seq until <EOS> input token reached