Skip to content

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

XLM cross-lingual language model

Learns multilingual representations using language-modeling objectives, including masked prediction and translation language modeling with parallel sentences.

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

Learns multilingual representations using language-modeling objectives, including masked prediction and translation language modeling with parallel sentences. It connects monolingual pretraining with explicit cross-lingual alignment signals.

Question / objective

Can monolingual and parallel-text prediction objectives align representations across languages?

Architecture / computational structure

A shared multilingual encoder uses causal/masked/translation-language objectives according to configuration. Learned encoder and embedding weights are parameters; objective mix, language codes, and parallel-pair sampling are hyperparameters.

Assumptions

Parallel corpora can be noisy or domain-specific, and a shared vocabulary alone does not establish alignment.

Formula / computational definition

LTLM=−∑i∈Mlog⁡pθ(si∣s∖M),s=[x;y] for a parallel pair.\mathcal L_{\mathrm{TLM}}=-\sum_{i\in M}\log p_\theta(s_i\mid s_{\setminus M}),\qquad s=[x;y]\ \text{for a parallel pair}.

Masked-language modeling predicts hidden tokens within one sentence; translation language modeling predicts masked tokens from a concatenated parallel pair (x,y). The loss sums negative log probabilities over masked positions, permitting attention to evidence from either language.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Objective mixture, parallel-data availability, vocabulary sharing, language sampling, masked fraction, and language identifiers.

Fitting, fine-tuning, and adaptation

Continue the appropriate language-model objective or fit a cross-lingual task head. Keep parallel pairs separated across data splits, use cautious encoder updates/adapters, and evaluate zero-shot transfer separately from supervised target-language performance. Check whether adaptation destroys alignment for other languages.

How to use and calibrate

Measure cross-lingual retrieval, transfer classification, and translation by language pair, with monolingual-versus-parallel objective ablations. 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

Transfer a topic classifier from a labeled source language to an unlabeled target language.

Interpretation / pitfalls

Watch out

Benchmark translation overlap can exaggerate apparent cross-language transfer. 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 offers XLM implementations, while exact pretraining may require the original training code and data recipe. R uses reticulate for model inference; the example illustrates masked-position loss over paired sentences, not supervised translation labels.

Example scope: XLM translation-language masked loss with one target position from each language.

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 — XLM translation-language masked loss with one target position from each language.

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)
masked_pair_target_probabilities = [0.6,0.8]
print(nll(masked_pair_target_probabilities)/2)
R

Component demo, not a trained model — XLM translation-language masked loss with one target position from each language.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
masked_pair_target_probabilities <- c(0.6,0.8)
print(-mean(log(masked_pair_target_probabilities)))

Sources

From the CodeSplash reference, entry LNG029.