Skip to content

Language models, retrieval, prompting, and language adaptation · Contextual and sentence encoders · LNG009 · Training method

ULMFiT

Adapts a pretrained language model to a target corpus and then a text classifier. Its discriminative learning rates, gradual unfreezing, and learning-rate schedule demonstrate how transfer learning can preserve general…

Partly runs in CodeSplash Component demo, not a trained model CodeSplash's own run

In CodeSplash

A CodeSplash method covers part of this entry; some options or variants aren't built yet.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Partly runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Adapts a pretrained language model to a target corpus and then a text classifier. Its discriminative learning rates, gradual unfreezing, and learning-rate schedule demonstrate how transfer learning can preserve general knowledge with relatively little labeled data.

Question / objective

How can a pretrained recurrent language model adapt efficiently to text classification?

Architecture / computational structure

The original recipe uses an AWD-LSTM language model plus a classifier head. Learned parameters are recurrent/embedding/head weights; gradual unfreezing, discriminative learning rates, and schedules control adaptation.

Assumptions

Unlabeled adaptation data should match deployment rather than leak test labels or duplicate evaluation examples.

Formula / computational definition

ηℓ−1=ηℓ/κ,Ltask=−∑ilog⁡pθ(yi∣xi).\eta_{\ell-1}=\eta_\ell/\kappa,\qquad \mathcal L_{\mathrm{task}}=-\sum_i\log p_\theta(y_i\mid x_i).

First minimize causal language-model loss on target text, then classification cross-entropy. Layer-specific rates eta_l allow different update sizes across depth; a schedule changes eta_l over training while layers are progressively unfrozen.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Target-domain adaptation duration, layer learning-rate ratios, dropout, schedule shape, unfreezing order, and pooling strategy.

Fitting, fine-tuning, and adaptation

Perform general language-model pretraining, target-domain language-model adaptation, then classifier fine-tuning. Use gradual unfreezing and layer-specific learning rates rather than treating them as new architectures. Select checkpoints on labeled validation performance and monitor language-model retention and overfitting.

How to use and calibrate

Compare no adaptation, full fine-tuning, and gradual unfreezing under fixed labeled-data budgets and repeated splits. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.

Example application

Train a domain sentiment classifier from relatively few labeled reviews.

Interpretation / pitfalls

Watch out

Aggressive fine-tuning can forget general features; recurrent backbones are not directly equivalent to modern transformer recipes. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.

Python and R code examples

Full implementation route: fastai-based historical implementations are a Python route. R requires a custom training schedule or reticulate; document the recurrent architecture and schedule rather than merely labeling any language-model fine-tuning ULMFiT.

Example scope: Discriminative layer learning rates in ULMFiT; a schedule component, not recurrent training.

Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.

Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Discriminative layer learning rates in ULMFiT; a schedule component, not recurrent training.

Shared setup for this chapter
import math

def softmax(xs):
    m = max(xs)
    es = [math.exp(x-m) for x in xs]
    return [e/sum(es) for e in es]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def matvec(matrix, vector):
    return [dot(row, vector) for row in matrix]

def meanvec(rows):
    return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]

def sigmoid(x):
    return 1/(1+math.exp(-x))

def nll(probabilities):
    return -sum(math.log(p) for p in probabilities)
base_lr, factor = 0.01, 2.6
rates_bottom_to_top = [base_lr / factor ** depth for depth in [2, 1, 0]]
print(rates_bottom_to_top)
R

Component demo, not a trained model — Discriminative layer learning rates in ULMFiT; a schedule component, not recurrent training.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
base_lr <- 0.01; factor <- 2.6
rates_bottom_to_top <- base_lr / factor^c(2,1,0)
print(rates_bottom_to_top)

Sources

From the CodeSplash reference, entry LNG009.