A compact RoBERTa-style Masked Language Model (MLM) for Persian (Farsi). We trained a Persian BPE tokenizer on a mixed corpus combining formal text with social-media and chat data. The model is pre-trained with this tokenizer, optimized for Persian script and evaluated on two downstream tasks:
Model size and training hyperparameters were kept identical to the baselines to ensure fair comparisons.
The repository name on the Hub should be:
selfms/persian_roberta_opt_tokenizer.
Backbone (example config):
Adjust numbers above to your final
config.jsonif they differ. All baselines used the same parameter budget.
Pretraining objective: Masked Language Modeling
Fine-tuning hyperparameters (shared across all compared models):
epochs = 3
batch_size = 8
learning_rate = 3e-5
weight_decay = 0.01
max_tokens = 128
optimizer = AdamW
scheduler = linear with warmup (recommended 10% warmup)
seed = 42
| Model | Precision | Recall | F1-Score |
|---|---|---|---|
| Proposed (this model) | 93.4 | 94.8 | 94.08 |
| TooKaBERT-base | 94.9 | 96.2 | 95.5 |
| FABERT | 94.1 | 95.3 | 94.7 |
| Model | F1-score (%) |
|---|---|
| Proposed (this model) | 90 |
| TooKaBERT-base | 91 |
| FABERT | 88 |
All three models used identical hyperparameters, token length, and parameter budgets to isolate architecture/tokenizer effects.
from transformers import AutoTokenizer, AutoModelForMaskedLM, pipeline
path = "selfms/persian_roberta_opt_tokenizer"
tokenizer = AutoTokenizer.from_pretrained(path)
model = AutoModelForMaskedLM.from_pretrained(path)
model.eval()
fill = pipeline("fill-mask", model=model, tokenizer=tokenizer, top_k=10)
print(fill(" سلام کسی تحلیل دقیقی ازاین <mask> داره کی میخواد حرکت کنه"))
import torch
from transformers import AutoTokenizer, AutoModel
path = "selfms/persian_roberta_opt_tokenizer"
tok = AutoTokenizer.from_pretrained(path)
mdl = AutoModel.from_pretrained(path).eval()
def embed(text):
with torch.no_grad():
x = tok(text, return_tensors="pt", truncation=True, max_length=256)
h = mdl(**x).last_hidden_state
a = x["attention_mask"].unsqueeze(-1)
v = (h * a).sum(1) / a.sum(1).clamp(min=1)
return (v / v.norm(dim=1, keepdim=True)).squeeze(0) # 1D vector
text = "متن فارسی به بردار 768 بعدی تبدیل میشه"
vec = embed(text)
print(len(vec))
from transformers import AutoTokenizer
path = "selfms/persian_roberta_opt_tokenizer"
tok = AutoTokenizer.from_pretrained(path)
text = "برای tokenizer از پیش پردازش معنایی روی دیتاست ها مختلف خبری و شبکه های اجتماعی استفاده شده"
enc = tok(text, return_tensors="pt")
tokens = tok.convert_ids_to_tokens(enc["input_ids"][0])
print("Tokens:", tokens)
print("IDs :", enc["input_ids"][0].tolist())
Under identical parameter budgets and training settings:
These results suggest the tokenizer/backbone choices here are strong for RE and competitive for NER, especially considering the compact backbone.
max_tokens=128. Longer contexts may require re-tuning and more memory. from transformers import AutoModelForMaskedLM, AutoTokenizer
tok = AutoTokenizer.from_pretrained("selfms/persian_roberta_opt_tokenizer")
mdl = AutoModelForMaskedLM.from_pretrained("selfms/persian_roberta_opt_tokenizer")
epochs=3, batch_size=8, lr=3e-5, weight_decay=0.01, max_tokens=128
config.json model.safetensors or pytorch_model.bin tokenizer_config.json, special_tokens_map.json, tokenizer.json vocab.json, merges.txt (BPE) README.md, LICENSE, .gitattributesEnsure
mask_tokenis set to<mask>andpipeline_tag: fill-maskis present so the Hub widget works out-of-the-box.
If you use this model, please cite:
@misc{persian_roberta_opt_tokenizer_2025,
title = {persian\_roberta\_opt\_tokenizer: A compact RoBERTa-style Persian Masked LM},
author = {selfms},
year = {2025},
howpublished = {\url{https://huggingface.co/selfms/persian_roberta_opt_tokenizer}},
note = {Pretrained on Persian text; evaluated on ARMAN+PEYMA (NER) and PERLEX (RE).}
}
Apache-2.0 (recommended). Please verify dataset licenses (ARMAN, PEYMA, PERLEX) before redistribution.
<s>=0, <pad>=1, </s>=2, <mask> as mask token.