Skip to content

Encoder–decoder models: from seq2seq to BART, T5, PEGASUS and MASS

An encoder reads the source, a decoder writes the target. Follow the line from LSTM seq2seq and attention to the denoising objectives of BART, T5, PEGASUS and MASS, and see what each pretraining task teaches.

Applied ~15 min8 steps

What runs in CodeSplash

You will be able to

  • Explain teacher forcing and why free-running decoding behaves differently
  • Describe how attention lets the decoder look back at every source position
  • Tell apart the corruption-and-reconstruction tasks of BART, T5, PEGASUS and MASS
  • Say what runs in CodeSplash for generating and scoring summaries

Read first

01

Read, then write

Read

An encoder–decoderEncoder–decoder modelAn encoder reads the source sequence and a decoder writes the target token by token, attending back to the encoder. model maps one sequence to another: a sentence to its translation, an article to its summary. The first sequence-to-sequence models used two LSTMs. The encoder read the source into a final state; the decoder started from that state and produced the target one token at a time.

A target's probability is the product of its token probabilities. With three correct tokens at probabilities 0.7, 0.6 and 0.8, the reference program gets sequence probability 0.336 and mean token loss 0.364. Longer targets have smaller probabilities, which matters again when decoding.

02

Teacher forcing

Predict

Predict first

During training the decoder gets the correct previous token as input at every step, even when it predicted something else. What weakness does this teacher forcingTeacher forcingTraining a decoder on the correct previous tokens instead of its own predictions; at use time it must feed on its own outputs. create?

It never sees its own mistakes during trainingIt learns more slowly than free runningIt can't produce long outputs
Reveal

It never sees its own mistakes during training. At use time it feeds on its own outputs, so one early error can lead it into inputs it never trained on (exposure bias). Teacher forcing is still standard, because it makes training parallel and stable.

03

Attention: look back at every source word

Read

Squeezing a whole sentence into one final state loses detail, especially in long sentences. Bahdanau attention lets the decoder, at each step, score every encoder state, turn the scores into weights with a softmax, and read their weighted average as a context vector.

In the reference program, three encoder states get weights 0.408, 0.255 and 0.337. The weights make a tempting alignment heatmap, but they show where the model looked, not why it decided.

04

Pretraining by corruption

Read

Transformer encoder–decoders are pretrained by breaking text and asking the decoder to repair it. They differ in what they break and what the decoder must write:

  • BART replaces spans with one mask each (and shuffles, deletes) and rebuilds the whole original text.
  • T5 replaces spans with numbered sentinels and writes only the missing spans: in the reference program the target is <extra_id_0> red boat <extra_id_1>. Every task is cast as text in, text out.
  • MASS masks one contiguous fragment in the encoder; the decoder writes only that fragment.
  • PEGASUS removes the most important sentences and generates them, a task close to summarization.
05

Fluent is not faithful

Read

PEGASUS's gap sentences show the risk. In the reference program the removed sentence is "Sales rose 12%.", chosen because it scored highest for salience. A model trained to regenerate such sentences learns to write confident, specific claims. In a summary, a number that isn't in the source is a hallucinationHallucinationFluent model output that is not supported by any source, including invented numbers, citations and column names., however fluent.

Overlap metrics like ROUGE reward shared words, not correct facts. Check summaries for claims missing from the source, with a judge or a person.

06

Where it stands in CodeSplash

Read
EntryIn CodeSplash
Seq2seq LSTM, Bahdanau attention (LNG021, LNG022)Explained here
BART (LNG023)Explained here; summaries you bring are scored by Generated-text metrics
T5 (LNG024)Explained here; a T5 encoder–decoder runs inside Chronos forecasting
PEGASUS, MASS (LNG025, LNG026)Explained here

Generating text goes through the AI layer, with a provider you configure. Keep generated columns labeled as model output.

Reference · LNG025 · Explained herePEGASUSPretrains summarization-oriented encoder–decoders by removing important sentences and asking the model to generate them from the remaining document.
07

Check: encoder–decoder models

Checkpoint

Check yourself

Why did plain LSTM seq2seq models struggle with long sentences?

Show the answer

Answer: a

One fixed-size state is a bottleneck. Attention removed it by letting the decoder read every encoder state.

Check yourself

What is exposure bias?

Show the answer

Answer: b

Teacher forcing hides the model's own errors during training, so at use time an early mistake can push it into unfamiliar inputs.

Check yourself

In Bahdanau attention, what is the context vector at a decoder step?

Show the answer

Answer: b

The decoder re-weights the source at every step, so each output token can draw on different source positions.

Check yourself

An alignment heatmap shows the decoder put most weight on one source word when writing "bank". What can you claim?

Show the answer

Answer: b

Attention weights describe mixing, not causal importance. Explanations need tests such as removing or changing inputs.

Check yourself

BART's encoder reads "the arrived". What must its decoder write?

Show the answer

Answer: a

BART is a denoising autoencoder: it reconstructs the full text, which suits generation tasks like summarization.

Check yourself

A dataset gets a new column of summaries written by a model. What should travel with that column?

Show the answer

Answer: b

Generated text must stay marked as generated. Without provenance, later readers may treat it as source data.

Check yourself

How does T5's span corruption differ from BART's text infilling?

Show the answer

Answer: b

Writing only the spans makes T5's targets short and cheap; BART's full reconstruction practices generating whole texts.

Check yourself

What does T5's "text-to-text" framing mean?

Show the answer

Answer: b

A task prefix such as "summarize:" tells the model which job to do; even labels are generated as words.

Check yourself

How does PEGASUS choose what to hide during pretraining?

Show the answer

Answer: b

Gap-sentence generation looks like summarization, which is why PEGASUS transfers well to it.

Check yourself

A PEGASUS-style summary says "revenue rose 15%"; the source never gives a figure. Which check catches this?

Show the answer

Answer: b

Overlap metrics reward shared words and fluency; a factual check catches claims the source doesn't support.

Check yourself

In MASS, the encoder reads "a text". What does the decoder predict?

Show the answer

Answer: b

MASS trains encoder and decoder together on one contiguous fragment: the encoder must understand the visible parts, the decoder must write the hidden part.

Check yourself

What do MASS and T5 have in common?

Show the answer

Answer: a

MASS hides one fragment, T5 several spans with sentinels; in both the decoder writes what was hidden.

08

Recap

Recap
  • Encoder–decoder models read a source and write a target; teacher forcing trains them, exposure bias follows.
  • Attention reads all encoder states; its weights aren't explanations.
  • BART rebuilds the whole text, T5 and MASS write only hidden spans, PEGASUS generates key sentences.
  • In CodeSplash, Generated-text metrics scores outputs, generation goes through the AI layer, and Chronos runs a T5 for forecasting.

Concepts in this lesson