Skip to content

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

mT5

Scales the T5 text-to-text approach to multilingual pretraining. It supports many language tasks through one architecture while highlighting that language coverage, sampling, and accidental translation need explicit…

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 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

Scales the T5 text-to-text approach to multilingual pretraining. It supports many language tasks through one architecture while highlighting that language coverage, sampling, and accidental translation need explicit evaluation.

Question / objective

Can text-to-text pretraining transfer across many languages without separate models?

Architecture / computational structure

A multilingual T5-style encoder–decoder shares vocabulary and weights across language corpora. Learned weights are parameters; corpus sampling exponent, span corruption, and adaptation language mixtures are hyperparameters.

Assumptions

Shared parameters can yield positive or negative transfer.

Formula / computational definition

p(ℓ)=nℓα∑jnjα,L=Eℓ∼p(ℓ)Lspan,ℓ.p(\ell)=\frac{n_\ell^\alpha}{\sum_jn_j^\alpha},\qquad \mathcal L=\mathbb E_{\ell\sim p(\ell)}\mathcal L_{\mathrm{span},\ell}.

Apply span-corruption loss across languages with sampling probabilities proportional to corpus size raised to exponent alpha. For data sizes n_l, p(l)=n_l^alpha/sum_j n_j^alpha; alpha below one relatively increases exposure to lower-resource languages.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Sampling exponent, language mixture, tokenizer vocabulary, corruption schedule, task language, model size, and adaptation dataset.

Fitting, fine-tuning, and adaptation

Train task-specific text-to-text pairs while retaining multilingual examples when cross-language retention matters. Freeze selected modules or use compatible LoRA/adapters if full fine-tuning overfits. Validate per-language metrics and unintended translation, not only the dominant language's loss.

How to use and calibrate

Separate per-language accuracy, generation-language fidelity, low-resource performance, and transfer direction; compare macro averages with data-weighted averages. 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

Build multilingual question answering with supervision concentrated in a few languages.

Interpretation / pitfalls

Watch out

Fine-tuning on one language may reduce multilingual performance or cause answers to appear in the wrong language; script coverage is not evidence of cultural or domain competence. 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: Load documented mT5 classes and corresponding tokenizers in Transformers; R can call them through reticulate. A native mixture calculation demonstrates the sampling mechanism without claiming a full multilingual pretraining run.

Example scope: mT5 language sampling with a sublinear corpus-size exponent.

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 — mT5 language sampling with a sublinear corpus-size exponent.

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)
sizes, alpha = [1000,100,10], 0.3
raw = [n**alpha for n in sizes]
print([x/sum(raw) for x in raw])
R

Component demo, not a trained model — mT5 language sampling with a sublinear corpus-size exponent.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
sizes <- c(1000,100,10); alpha <- 0.3
raw <- sizes^alpha
print(raw/sum(raw))

Sources

From the CodeSplash reference, entry LNG028.