Skip to content

Language models, retrieval, prompting, and language adaptation · Encoder–decoder and multilingual models · LNG024 · Model

T5

Casts diverse language tasks into a text-to-text interface using an encoder–decoder model. A common output format makes translation, question answering, classification, and summarization comparable within one training…

Explained here Component demo, not a trained model CodeSplash's own run

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran here in one language.
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

Casts diverse language tasks into a text-to-text interface using an encoder–decoder model. A common output format makes translation, question answering, classification, and summarization comparable within one training and adaptation framework.

Question / objective

Can heterogeneous NLP tasks share a single input-text/output-text training interface?

Architecture / computational structure

An encoder–decoder Transformer processes text task prefixes and generates text targets. Learned weights include embeddings, attention, feed-forward layers, and output projection; span corruption and task mixtures are hyperparameters.

Assumptions

Textual labels still require parsing and calibration; generated label probabilities depend on tokenization and prompt form.

Formula / computational definition

Lspan=−∑tlog⁡pθ(zt∣z<t,x~),z=sentinelTargets⁡(x,M).\mathcal L_{\mathrm{span}}=-\sum_t\log p_\theta(z_t\mid z_{<t},\widetilde x),\qquad z=\operatorname{sentinelTargets}(x,M).

For corrupted input x_tilde, sentinel-delimited removed spans form target z; minimize -sum_t log p_theta(z_t|z_<t,x_tilde). During supervised adaptation, task instructions and inputs become text and the desired answer becomes target text.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Span corruption density, average span length, sentinel vocabulary, task mixtures, input/output budgets, model size, and task prefixes.

Fitting, fine-tuning, and adaptation

Adapt with task-formatted source/target pairs and decoder-target masking. Compare head-free full generation with parameter-efficient adapters or LoRA supported by the exact model. Choose task mixtures and checkpoints on validation tasks, track label parsing and calibration, and test prior capabilities after updates.

How to use and calibrate

Use task-appropriate accuracy or generation quality, exact output-format compliance, loss, and robustness to paraphrased task specifications. 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

Use one model to classify a document, extract fields, and generate a summary through task prefixes.

Interpretation / pitfalls

Watch out

Denoising success alone does not ensure generalization to unseen tasks. 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: Transformers provides T5 tokenizer/model implementations. R uses reticulate for matching checkpoints or an explicit R torch reproduction. The example illustrates supervised token likelihood; it does not silently equate T5 with later instruction-tuned variants.

Example scope: T5 sentinel-delimited span-corruption target construction.

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 — T5 sentinel-delimited span-corruption target construction.

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)
tokens = ["the","red","boat","arrived"]
encoder = [tokens[0],"<extra_id_0>",tokens[3]]
target = ["<extra_id_0>"] + tokens[1:3] + ["<extra_id_1>"]
print(encoder, target)
R

Component demo, not a trained model — T5 sentinel-delimited span-corruption target construction.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
tokens <- c("the","red","boat","arrived")
encoder <- c(tokens[1],"<extra_id_0>",tokens[4])
target <- c("<extra_id_0>",tokens[2:3],"<extra_id_1>")
print(list(encoder=encoder, target=target))

Sources

From the CodeSplash reference, entry LNG024.