Sub topic 1
Summarisation and Translation as Text-to-Text Problems
Prerequisites
This chapter assumes you know basic Python, can read a neural network training loop, and have a rough picture of the Transformer and self-attention. You should have the Hugging Face transformers library installed (pip install transformers sentencepiece).
One Framing for Many Tasks
An encoder-decoder model maps an input sequence to an output sequence. Summarisation maps a long text to a short one. Translation maps English text to German text. Both fit the same shape, so one architecture can serve both.
Text-to-text models such as T5 make this explicit. Every task is written as text in and text out, and a short task prefix tells the model what to do, for example summarize: or translate English to German:.
How an Encoder-Decoder Model Works
The model has two halves:
- Encoder: reads the whole source sequence and produces a vector representation for each input token.
- Decoder: generates the target one token at a time. Each new token depends on the tokens already generated.
flowchart LR
A[Source text tokens] --> B[Encoder]
B --> C[Encoder states]
C --> D[Decoder cross-attention]
E[Tokens generated so far] --> D
D --> F[Next token]Cross-Attention: How the Decoder Reads the Source
Cross-attention is the link between the halves. At each decoding step, the decoder builds a query from its own state and compares it with the encoder states. The result is a weighted mix of source information. When the decoder writes a German word, cross-attention can focus on the English word it corresponds to.
Tokenisation, Special Tokens and Target Text
Models do not read raw characters. A tokenizer splits text into subword pieces and maps each piece to an integer ID. Special tokens mark boundaries, such as the end-of-sequence token that tells the decoder to stop.
For training, the target text must be tokenised too. Passing it through text_target produces the labels the model needs.
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("t5-small")
source = "summarize: The council approved 40 km of new bike lanes."
target = "The council approved 40 km of new bike lanes."
batch = tokenizer(source, text_target=target, return_tensors="pt")
print(batch["input_ids"])
print(tokenizer.convert_ids_to_tokens(batch["labels"][0]))The final token in the labels is the end-of-sequence token. Without it, the model would not learn when to stop.
> Keep the task prefix in the source text. A model trained with one prefix may behave poorly when the prefix changes.
Replace the blockquote above with a callout in your final output. Use this instead:
Exercise
Exercise
In the T5 example, why must the target text be passed through text_target rather than as a plain string argument?
Show answer
Passing it through text_target tokenises the target as the decoder's output and returns it under the labels key. The model uses these labels to compute the training loss. A plain string would be tokenised as encoder input.