Sub topic 2
Zero-shot cross-lingual transfer in practice
This chapter assumes you know basic Python, have used the Hugging Face transformers library, have fine-tuned a pretrained encoder for classification, and can read accuracy and F1 scores.
Training on one language, testing on many
Zero-shot cross-lingual transfer means fine-tuning a multilingual model on labeled data in one language, then evaluating it on other languages that had no task-specific labels. The model carries what it learned across languages through its shared representations.
The example below fine-tunes XLM-R on English XNLI, a natural language inference task that labels a premise and hypothesis as entailment, neutral, or contradiction. It then scores the same model on English, German, and Swahili test sets.
from datasets import load_dataset
from transformers import (
AutoModelForSequenceClassification,
AutoTokenizer,
Trainer,
TrainingArguments,
)
name = "xlm-roberta-base"
tok = AutoTokenizer.from_pretrained(name)
model = AutoModelForSequenceClassification.from_pretrained(name, num_labels=3)
def encode(batch):
return tok(batch["premise"], batch["hypothesis"], truncation=True, max_length=128)
train = load_dataset("facebook/xnli", "en", split="train").map(encode, batched=True)
tests = {
lang: load_dataset("facebook/xnli", lang, split="test").map(encode, batched=True)
for lang in ["en", "de", "sw"]
}
def accuracy(pred):
return {"accuracy": float((pred.predictions.argmax(-1) == pred.label_ids).mean())}
args = TrainingArguments(
output_dir="xlmr-xnli-en",
per_device_train_batch_size=32,
learning_rate=2e-5,
num_train_epochs=2,
)
trainer = Trainer(
model=model,
args=args,
train_dataset=train,
processing_class=tok,
compute_metrics=accuracy,
)
trainer.train()
for lang, ds in tests.items():
print(lang, trainer.evaluate(ds)["eval_accuracy"])What makes transfer work or fail
Transfer usually works better when:
- the target language is close to the source in vocabulary and grammar, such as German and English
- the target uses the same script, so the shared subword vocabulary covers it well
- the pretraining corpus holds a large amount of text in the target language
Transfer tends to fail for low-resource languages, for scripts the tokenizer splits into many pieces, and for tasks that depend on culture-specific knowledge.
Measuring transfer: XNLI, XTREME and per-language reporting
XNLI provides parallel test sets in 15 languages, so the same examples appear in every language. XTREME bundles 9 tasks across 40 languages, which lets you test transfer beyond classification.
Report one score per language, not a single average. An average of 78 can hide a language that scores 55. State the gap between the source language and the weakest target language.
Exercise
The model scores 81 on English and 62 on Swahili. Why is an average score of 71.5 a poor report?
Show answer
The average hides the 19-point gap. Report each language separately and state the gap between the source language and the weakest target language.