Week 06 · Sequence-to-sequence models

Compare a decoder trained on the right prefix with its own generated prefix.

← Week 5 · Subject contents · Week 7 →

Compare a decoder trained on the right prefix with its own generated prefix.

Work through the problem

An autoregressive decoder factors P(y|x)=ΠₜP(yₜ|y₁…yₜ₋₁,x), where the prefix contains earlier output tokens and x is the source. Teacher forcing evaluates or trains on the true previous token. During generation the decoder must condition on what it produced.

A simple encoder below counts source length and identifies the source’s first token. A toy decoder should output start, then a source-dependent word, then stop. Force a wrong first token and compare the free-running output. Even when later steps work on the correct prefix, one early error can take the decoder into a different part of its state space.

Run the Python lab

def encode(source):
    return len(source),source[0]
def decode(context,previous):
    length,head = context
    if previous=="<BOS>":
        return "start"
    if previous=="start":
        return head if length>1 else "single"
    return "<EOS>"
context = encode(["cache","miss"])
truth = ["start","cache","<EOS>"]
teacher = [decode(context,p) for p in ["<BOS>"]+truth[:-1]]
free = ["wrong"]
while free[-1]!="<EOS>" and len(free)<4:
    free.append(decode(context,free[-1]))
print("Teacher-forced:",teacher)
print("After an early error:",free)
assert teacher==truth and free==["wrong","<EOS>"]

Output from the lab

Teacher-forced: ['start', 'cache', '<EOS>']
After an early error: ['wrong', '<EOS>']

Practice

  1. Why evaluate complete outputs?

Check your reasoning

  1. Per-token scores on true prefixes can hide failures caused by the model conditioning on its own mistakes.

← Week 5 · Subject contents · Week 7 →

Further reading · Jurafsky and Martin · Speech and Language Processing