Skip to content

Context changes meaning: ELMo and ULMFiT

A static word vector gives "bank" one meaning. ELMo mixes the layers of a bidirectional language model into vectors that depend on the sentence; ULMFiT showed how to fine-tune a pretrained language model without wrecking it.

Applied ~13 min8 steps

What runs in CodeSplash

You will be able to

  • Explain the difference between static and contextual word vectors
  • Describe ELMo's learned mixture of language-model layers
  • Name ULMFiT's three schedule ideas and what each protects against
  • Set up a ULMFiT-style fine-tuning schedule in CodeSplash and label it correctly

Read first

01

One vector per word is not enough

Read

Word2vec gives "bank" one vector, averaged over river banks and savings banks. Downstream models then have to undo that blur from the surrounding words.

A contextual embeddingContextual embeddingA vector computed for each occurrence of a word from its whole sentence, so the same word gets different vectors in different contexts. is computed for each occurrence, from the whole sentence. "bank" in "we fished from the bank" and in "the bank raised rates" gets two different vectors. The idea needs a model that reads the sentence first: a language model.

02

ELMo: mix the layers of a language model

Read

ELMo trains two LSTM language models on a large corpus, one reading left to right and one right to left. For each token it collects the representations from every layer, then lets each downstream task learn how to mix them:

ELMok=γ∑j=0Lsj hk,j,s=softmax(a)\mathrm{ELMo}_k = \gamma \sum_{j=0}^{L} s_j\, h_{k,j}, \qquad s = \mathrm{softmax}(a)

Lower layers tend to carry syntax, higher ones meaning in context. In the reference program, three layer scores become mixing weights 0.288, 0.475 and 0.236, and the scale γ stretches the mixed vector.

03

ULMFiT: fine-tune gently

Read

ULMFiT (2018) made transfer the normal way to train text classifiers. Pretrain a language model on general text, fine-tune it as a language model on your domain's text, then fine-tune it as a classifier. Three schedule ideas keep the last stage from erasing what pretraining learned:

  • Discriminative learning rates. Lower layers hold general features, so they get smaller steps. The reference program divides the rate by 2.6 per layer going down: 0.010 at the top, 0.0038 below it and 0.0015 below that.
  • Gradual unfreezing. Train the top layer first, then unfreeze one more layer each epoch.
  • Slanted triangular rates. A short rise, then a long linear fall.
04

What happens without them?

Predict

Predict first

You fine-tune every layer of a pretrained encoder on 200 labeled texts with one large learning rate from the first step. What is the risk?

It learns faster and keeps everythingIt can overwrite general features with noise from the small labeled setNothing, because pretrained weights can't change
Reveal

Large early updates driven by a few labeled examples can overwrite the general features in the lower layers, a form of catastrophic forgettingCatastrophic forgettingA network trained further on new data losing what it learned from its old data.. The schedules move lower layers less and later.

05

The schedules in CodeSplash

Read

In Text encoder, set Training to Fine-tune the top layers and turn on the ULMFiT-style options: Layer learning-rate decay, Gradual unfreezing and Slanted triangular learning rate. One test case fine-tunes the top two layers for 3 epochs, updating 3,549,316 weights, and reaches cross-validated ROC AUC 0.869 on a small review set.

Call this a ULMFiT-style schedule, not ULMFiT. The encoder is a pretrained Transformer (MiniLM or another of the learner's encoders), not an LSTM language model, and there is no language-model fine-tuning stage. ELMo itself is explained here only.

06

Where it stands in CodeSplash

Read
07

Check: contextual vectors and gentle fine-tuning

Checkpoint

Check yourself

How does ELMo give "bank" different vectors in "river bank" and "bank loan"?

Show the answer

Answer: b

The vector comes from the language models' layers at that position, so it depends on everything around the word.

Check yourself

What does a downstream task learn in ELMo's layer mixture?

Show the answer

Answer: a

The language model stays fixed; the task learns how much of each layer to use and a scale γ. Syntax-heavy tasks can lean on lower layers, meaning-heavy tasks on higher ones.

Check yourself

With discriminative learning rates, which layers take the smallest steps?

Show the answer

Answer: b

Each layer down divides the learning rate by a factor (2.6 in the paper), so general features change least.

Check yourself

You fine-tune MiniLM in CodeSplash with all three ULMFiT-style options on. How should the report describe it?

Show the answer

Answer: b

ULMFiT names a whole recipe: an LSTM language model, domain language-model fine-tuning, then classifier fine-tuning. Borrowing its schedules doesn't make a run ULMFiT.

08

Recap

Recap
  • Contextual embeddings are computed per occurrence from the whole sentence.
  • ELMo mixes the layers of two LSTM language models with learned weights and a scale.
  • ULMFiT's discriminative rates, gradual unfreezing and slanted triangular rates protect pretrained features.
  • Text encoder offers ULMFiT-style schedules for fine-tuning; label them as such.

Concepts in this lesson