File size: 4,248 Bytes
e81d91a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
---
pipeline_tag: fill-mask
license: mit
base_model: FacebookAI/xlm-roberta-base
library_name: kerasformers
tags:
- keras
- kerasformers
- xlm-roberta
- fill-mask
- multilingual
- text-encoder
- arxiv:1911.02116
- pytorch
- jax
- tf
---

## ***See [our collection](https://huggingface.co/collections/kerasformers/xlm-roberta-6a6e8fd0a258b1a8991cf608) for all versions of XLM-RoBERTa.***

# Run XLM-RoBERTa with Keras 3: JAX, PyTorch, or TensorFlow

[![GitHub](https://img.shields.io/badge/GitHub-KerasFormers-black?logo=github)](https://github.com/IMvision12/KerasFormers) [![Docs](https://img.shields.io/badge/Docs-XLM--RoBERTa-blue)](https://imvision12.github.io/KerasFormers/xlm_roberta/) [![Collection](https://img.shields.io/badge/HF-XLM--RoBERTa%20collection-yellow)](https://huggingface.co/collections/kerasformers/xlm-roberta-6a6e8fd0a258b1a8991cf608)

# kerasformers/xlm_roberta_base

Paper: [Unsupervised Cross-lingual Representation Learning at Scale (arXiv:1911.02116)](https://arxiv.org/abs/1911.02116) · [HF Papers](https://huggingface.co/papers/1911.02116)

XLM-RoBERTa is the **multilingual** RoBERTa: same encoder architecture, pretrained on 2.5TB CommonCrawl across **100 languages**, with a 250k SentencePiece vocabulary (mask token `<mask>`).

For more details on the model, please go to the upstream [model card](https://huggingface.co/FacebookAI/xlm-roberta-base).

Pure-**Keras 3** conversion of [`FacebookAI/xlm-roberta-base`](https://huggingface.co/FacebookAI/xlm-roberta-base) for [kerasformers](https://github.com/IMvision12/KerasFormers). One implementation runs unmodified on **TensorFlow / Torch / JAX**.

This is a **fill-mask / encoder** checkpoint (`XLMRobertaMaskedLM`, base). Task heads load via `hf:` fine-tunes.

## ✨ Quick start (multilingual fill-mask)

```python
import os
os.environ["KERAS_BACKEND"] = "torch"  # or "jax" / "tensorflow"

from kerasformers.models.xlm_roberta import (
    XLMRobertaMaskedLM,
    XLMRobertaTokenizer,
)

mlm = XLMRobertaMaskedLM.from_weights("kerasformers/xlm_roberta_base")
tokenizer = XLMRobertaTokenizer.from_weights("kerasformers/xlm_roberta_base")

# Multilingual: same <mask> API as RoBERTa, 100-language SentencePiece vocab.
inputs = tokenizer("La capitale de la France est <mask>.")
logits = mlm(inputs)  # (1, L, vocab_size)
mask = int((inputs["input_ids"][0] == tokenizer.mask_token_id).argmax())
print(tokenizer.decode([int(logits[0, mask].argmax())]))
```

Load any XLM-RoBERTa variant the same way with `from_weights("kerasformers/<variant>")`:

| Variant | Hub |
|---|---|
| `xlm_roberta_base` | [`kerasformers/xlm_roberta_base`](https://huggingface.co/kerasformers/xlm_roberta_base) |
| `xlm_roberta_large` | [`kerasformers/xlm_roberta_large`](https://huggingface.co/kerasformers/xlm_roberta_large) |

## Available classes

Load any of these from this repo with `from_weights("kerasformers/xlm_roberta_base")` (or on the fly via the `hf:` prefix). The pretrained backbone is shared; task heads not stored in this checkpoint start randomly initialized, ready for fine-tuning (or load a `hf:` fine-tune).

| Class | Task |
|---|---|
| `XLMRobertaModel` | Encoder backbone |
| `XLMRobertaMaskedLM` | Masked language modeling (fill-mask) |
| `XLMRobertaSequenceClassify` | Sequence classification |
| `XLMRobertaTokenClassify` | Token classification (NER / POS) |
| `XLMRobertaQnA` | Extractive question answering |
| `XLMRobertaMultipleChoice` | Multiple choice |

```python
from kerasformers.models.xlm_roberta import XLMRobertaSequenceClassify
model = XLMRobertaSequenceClassify.from_weights("kerasformers/xlm_roberta_base")
```

## Tips

- Set `KERAS_BACKEND` **before** importing Keras / kerasformers.
- Prefer `XLMRobertaTokenizer.from_weights(...)` so the SentencePiece vocab matches.
- Use `<mask>` (not `[MASK]`).
- See [XLM-RoBERTa docs](https://imvision12.github.io/KerasFormers/xlm_roberta/) and [Loading Weights](https://imvision12.github.io/KerasFormers/loading_weights/).
- Community / upstream safetensors still work via the `hf:` prefix, e.g. `XLMRobertaMaskedLM.from_weights("hf:FacebookAI/xlm-roberta-base")`.

## Special Thanks

A huge thank you to the Facebook AI XLM-RoBERTa authors for creating and releasing these models.

License: MIT.