Skip to content

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

XLM-R

Provides multilingual contextual encoder representations using large-scale masked-language pretraining. Unlike a translation generator, it is principally an encoder adapted to classification, tagging, retrieval, or…

Partly runs in CodeSplash Complete calculation 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
Complete calculation
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

Provides multilingual contextual encoder representations using large-scale masked-language pretraining. Unlike a translation generator, it is principally an encoder adapted to classification, tagging, retrieval, or other representation-based tasks.

Question / objective

Can multilingual masked pretraining support strong cross-language encoder transfer?

Architecture / computational structure

A shared bidirectional encoder produces contextual representations for task heads. Learned weights and downstream heads are parameters; language sampling, tokenizer, pooling, and task loss choices are hyperparameters.

Assumptions

More languages can improve sharing but also compete for finite capacity.

Formula / computational definition

L=−Eℓ,x,M∑i∈Mlog⁡pθ(xi∣x∖M,ℓ).\mathcal L=-\mathbb E_{\ell,x,M}\sum_{i\in M}\log p_\theta(x_i\mid x_{\setminus M},\ell).

Minimize E_l,x,M[-sum_i_in_M log p_theta(x_i|x_not_M,l)] across sampled languages and mask sets M. A downstream classification head may optimize cross-entropy on a pooled representation.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Language balancing, tokenizer, mask policy, maximum length, encoder capacity, pooling/head choice, and per-language adaptation data.

Fitting, fine-tuning, and adaptation

Fine-tune a classification or token-label head, optionally freezing lower layers or using compatible LoRA/adapters. Domain masked-language adaptation may help but requires independent validation. Evaluate each language, calibration, and retention; language-weighted aggregate loss can hide degradation in small groups.

How to use and calibrate

Report per-language and macro performance, script/domain stress tests, calibration, and sensitivity to translation artifacts in benchmarks. 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

Recognize entities across several scripts using one shared encoder.

Interpretation / pitfalls

Watch out

Strong aggregate multilingual scores may coexist with severe low-resource or domain failures; tokenizer fragmentation affects effective context length. 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: Use documented XLM-R encoder or task-head classes in Transformers; R bridges through reticulate. Native R examples can evaluate outputs, but importing an English-only downstream head does not create a valid multilingual classifier.

Example scope: XLM-R evaluation aggregation illustrating macro versus population-weighted language accuracy.

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

Complete calculation — XLM-R evaluation aggregation illustrating macro versus population-weighted language accuracy.

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)
accuracies, counts = [0.9,0.6,0.4], [1000,100,50]
macro = sum(accuracies)/len(accuracies)
weighted = dot(accuracies,counts)/sum(counts)
print(macro, weighted)
R

Complete calculation — XLM-R evaluation aggregation illustrating macro versus population-weighted language accuracy.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
accuracies <- c(0.9,0.6,0.4); counts <- c(1000,100,50)
print(c(macro=mean(accuracies), weighted=sum(accuracies*counts)/sum(counts)))

Sources

From the CodeSplash reference, entry LNG030.