Sub topic 8
Summarising Text with an Encoder-Decoder Model
This chapter assumes that you know basic Python, understand tensors and loss functions, and have a high-level idea of Transformer self-attention. You should also be familiar with tokenisation and the Hugging Face Transformers library.
What summarisation asks the model to do
A summarisation model receives a source document and generates a shorter target document. The target usually keeps the main ideas while removing details.
There are two broad styles:
- Extractive summarisation selects sentences or phrases from the source.
- Abstractive summarisation generates new wording based on the source.
Encoder-decoder Transformers usually perform abstractive summarisation. The encoder reads the source text and builds contextual representations. The decoder then generates the summary one token at a time.
At each decoding step, the decoder uses:
- The tokens it has already generated.
- Cross-attention over the encoder's representation of the source.
- A probability distribution over the next token.
The process stops when the decoder emits an end-of-sequence token or reaches a length limit.
Running a pretrained summariser
A pretrained model has already learned language patterns and summarisation behaviour from a large training corpus. facebook/bart-large-cnn is a BART model fine-tuned on news summarisation data.
The pipeline interface handles tokenisation, model execution, and decoding:
from transformers import pipeline
summarizer = pipeline(
"summarization",
model="facebook/bart-large-cnn",
)
article = (
"The city council voted on Tuesday to expand the bike lane network "
"by 40 kilometres over the next three years. Supporters said the plan "
"would reduce traffic and emissions, while some shop owners worried "
"about losing parking spaces near their stores. The first new lanes "
"are expected to open next spring."
)
result = summarizer(
article,
max_length=60,
min_length=15,
do_sample=False,
)
print(result[0]["summary_text"])do_sample=False makes decoding deterministic. Given the same model and input, the output should normally be the same. min_length and max_length constrain the generated summary, but they count tokens rather than words.
A summary is not guaranteed to be factually correct. The model may combine facts, omit an important qualification, or introduce a detail that does not appear in the source. Treat generated text as a draft when accuracy matters.
Choosing generation settings
Generation settings control the balance between predictable output, coverage, and repetition.
| Setting | Effect | Typical use |
|---|---|---|
| `max_length` | Sets a maximum token length | Legacy or simple length control |
| `min_length` | Prevents very short output | Avoiding incomplete summaries |
| `max_new_tokens` | Limits only generated tokens | Clearer control when input lengths vary |
| `num_beams` | Explores several candidate sequences | More fluent, focused summaries |
| `no_repeat_ngram_size` | Blocks repeated token phrases | Reducing repetition |
Beam search keeps several candidate sequences instead of selecting only the locally most likely token. It can improve fluency, but a larger beam does not guarantee factual accuracy. Excessive length limits can produce truncated sentences, while very small limits can remove essential context.
For newer code, prefer max_new_tokens when possible because it refers specifically to the generated portion. Always test settings on representative documents rather than judging from one example.
Handling input length
Every model has a maximum context window. If a document is longer than that limit, the tokenizer may truncate it or the model may reject it. Silent truncation can remove the conclusion, which is often where the main result appears.
A practical long-document strategy is:
- Split the document into overlapping chunks.
- Summarise each chunk.
- Join the chunk summaries.
- Summarise the joined text again.
This hierarchical approach is useful, but it can lose relationships between facts that occur in different chunks. Chunk boundaries should follow paragraphs or sections where possible.
Checking the result
Compare the summary with the source. Check factual claims manually, especially numbers and named entities. Automatic metrics such as ROUGE measure overlap with reference summaries, but overlap does not prove factual correctness or usefulness.
Exercise
Why might increasing num_beams fail to fix a summary that invents a date?
Show answer
Beam search chooses among likely sequences according to the model's learned probabilities. It does not verify claims against an external source, so it cannot guarantee factual accuracy.