Sub topic 5
Zero-Shot Cross-Lingual Transfer in Practice
Prerequisites and the Core Idea
This chapter assumes that you know basic Python, the Hugging Face transformers library, transformer encoders, masked language modeling, fine-tuning, and accuracy or F1.
Zero-shot cross-lingual transfer means fine-tuning a multilingual model with labeled data in one language, then evaluating it on another language without using labeled examples from that target language.
For example, you can fine-tune XLM-R on English natural language inference (NLI). NLI is the task of classifying whether a hypothesis is entailed by, contradicts, or unrelated to a premise. You then evaluate the same model on German or Swahili NLI examples. The model receives no German or Swahili labels during fine-tuning.
This works because multilingual pretraining gives the model a partly shared representation space. Similar meanings in different languages can produce similar internal features.
Fine-Tune on One Language
The following example trains on English XNLI and evaluates on English, German, and Swahili. The training set is labeled English data. The test sets are used only for evaluation.
from datasets import load_dataset
from transformers import (
AutoModelForSequenceClassification,
AutoTokenizer,
Trainer,
TrainingArguments,
)
model_name = "xlm-roberta-base"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(
model_name,
num_labels=3,
)
def encode(batch):
return tokenizer(
batch["premise"],
batch["hypothesis"],
truncation=True,
max_length=128,
)
train_data = load_dataset(
"facebook/xnli",
"en",
split="train",
).map(encode, batched=True)
test_data = {
language: load_dataset(
"facebook/xnli",
language,
split="test",
).map(encode, batched=True)
for language in ["en", "de", "sw"]
}
def accuracy(predictions):
predicted = predictions.predictions.argmax(-1)
return {
"accuracy": float(
(predicted == predictions.label_ids).mean()
)
}
arguments = TrainingArguments(
output_dir="xlmr-xnli-en",
per_device_train_batch_size=32,
learning_rate=2e-5,
num_train_epochs=2,
report_to="none",
)
trainer = Trainer(
model=model,
args=arguments,
train_dataset=train_data,
processing_class=tokenizer,
compute_metrics=accuracy,
)
trainer.train()
for language, dataset in test_data.items():
result = trainer.evaluate(dataset)
print(language, result["eval_accuracy"])The English score measures ordinary same-language transfer. The German and Swahili scores measure zero-shot transfer. A gap between English and another language shows that the model learned the task better in its training language than in that target language.
A zero-shot result is meaningful only when the training and evaluation data are kept separate. Do not translate target test examples into English before evaluation. That would measure a translation pipeline, not direct cross-lingual transfer.
Interpreting the Results
Expect target-language scores to vary. A high-resource language with a script and grammar similar to English may transfer more easily than a low-resource language with a different script. Tokenization also matters: if a sentence becomes many inefficient subword pieces, the model receives less useful context within the same token limit.
Report results per language rather than only one average. A macro-average gives each language equal weight, while a data-weighted average favors languages with larger test sets. The macro-average is usually better for showing whether the model serves languages fairly.
> callout note > Zero-shot does not mean the model never saw the target language. XLM-R may have encountered target-language text during pretraining. It means the model received no labeled target-language examples for this downstream task.
Exercise
Exercise
You fine-tune XLM-R on English sentiment data and evaluate it on Japanese sentiment data without Japanese labels. What kind of transfer is this, and what should you compare with the Japanese score?
Show answer
It is zero-shot cross-lingual transfer. Compare it with the English test score and report both scores separately so the transfer gap is visible.