Instructions to use ai4kazakh/mmbert-base-kaznerd with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ai4kazakh/mmbert-base-kaznerd with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="ai4kazakh/mmbert-base-kaznerd")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForTokenClassification tokenizer = AutoTokenizer.from_pretrained("ai4kazakh/mmbert-base-kaznerd") model = AutoModelForTokenClassification.from_pretrained("ai4kazakh/mmbert-base-kaznerd", device_map="auto") - Notebooks
- Google Colab
- Kaggle
mmbert-base-kaznerd-weighted
mmBERT-base (307M parameters, ModernBERT architecture, 1,833-language pre-training) fine-tuned for Kazakh Named Entity Recognition on the KazNERD dataset (110,675 sentences, 1,359,908 tokens, 132,409 entities, 25 entity types in BIO format, CC BY 4.0), using a class-weighted cross-entropy loss to emphasize rare entity types.
Results
Entity-level seqeval scores on the KazNERD test split (11,068 sentences, 13,072 entities):
| Model | Params | Precision | Recall | Micro F1 | Macro F1 |
|---|---|---|---|---|---|
| This model (weighted loss) | 307M | 94.11 | 95.79 | 94.95 | 84.25 |
| ai4kazakh/xlm-roberta-large-kaznerd (XLM-R-large fine-tune) | 560M | 96.05 | 97.03 | 96.54 | 87.25 |
The comparison model is our XLM-R-large fine-tune on the same dataset and evaluation protocol (see its model card). Note that it is 1.8× larger (560M vs 307M parameters).
Validation micro F1: 94.88.
Per-tag F1 (test split)
| Tag | P | R | F1 | Support |
|---|---|---|---|---|
| ADAGE | 43.75 | 41.18 | 42.42 | 17 |
| ART | 85.96 | 91.30 | 88.55 | 161 |
| CARDINAL | 97.13 | 98.28 | 97.70 | 2789 |
| CONTACT | 79.17 | 95.00 | 86.36 | 20 |
| DATE | 96.12 | 96.90 | 96.51 | 2584 |
| DISEASE | 89.84 | 96.64 | 93.12 | 119 |
| EVENT | 70.31 | 87.66 | 78.03 | 154 |
| FACILITY | 65.20 | 75.13 | 69.81 | 197 |
| GPE | 97.11 | 97.28 | 97.19 | 1691 |
| LANGUAGE | 90.91 | 97.56 | 94.12 | 41 |
| LAW | 66.13 | 74.55 | 70.09 | 55 |
| LOCATION | 88.00 | 84.62 | 86.27 | 208 |
| MISCELLANEOUS | 77.42 | 92.31 | 84.21 | 26 |
| MONEY | 98.14 | 98.83 | 98.48 | 427 |
| NON_HUMAN | 0.00 | 0.00 | 0.00 | 1 |
| NORP | 96.27 | 98.10 | 97.17 | 368 |
| ORDINAL | 95.83 | 96.34 | 96.08 | 382 |
| ORGANISATION | 86.70 | 89.00 | 87.84 | 718 |
| PERCENTAGE | 98.49 | 98.70 | 98.59 | 462 |
| PERSON | 97.22 | 97.13 | 97.18 | 1151 |
| POSITION | 94.89 | 96.48 | 95.68 | 597 |
| PRODUCT | 72.94 | 82.67 | 77.50 | 75 |
| PROJECT | 79.82 | 88.35 | 83.87 | 206 |
| QUANTITY | 95.59 | 96.77 | 96.18 | 403 |
| TIME | 92.79 | 93.64 | 93.21 | 220 |
| micro avg | 94.11 | 95.79 | 94.95 | 13072 |
| macro avg | 82.23 | 86.58 | 84.25 | 13072 |
Intended uses
Named entity recognition on Kazakh news-style text: PERSON, ORGANISATION, GPE, DATE, LAW, MONEY, PROJECT, and 19 further types (full taxonomy in the dataset card). Input is raw Kazakh text; the model was trained on whitespace-separated tokens, so no punctuation splitting or morphological pre-processing is needed.
Training
- Base:
jhu-clsp/mmBERT-base(307M parameters; Gemma 2 tokenizer, 8k context) - 8 epochs, batch 32, LR 5e-5, linear decay, 10% warmup, bf16, seed 42
- Max length 256 (covers 100% of sentences; p99 sentence length = 23 words)
- All-subtoken labeling: every subtoken of a word receives the word's tag during training; only the first subtoken of each word is scored at evaluation time
- Class-weighted loss: inverse-sqrt-frequency weights, damped and mean-normalized. The largest
weights:
I-NON_HUMAN11.4×,B-NON_HUMAN7.3×,I-NORP3.8×,I-MISCELLANEOUS2.1×,I-LANGUAGE1.7×,B-ADAGE1.6×,B-CONTACT1.5× - Best checkpoint selected by validation entity-level F1
- Single RTX 4090 (24 GB), ~20 minutes
Usage
import torch
from transformers import AutoTokenizer, AutoModelForTokenClassification
name = "ai4kazakh/mmbert-base-kaznerd-weighted"
tokenizer = AutoTokenizer.from_pretrained(name)
model = AutoModelForTokenClassification.from_pretrained(name).eval()
def tag(text):
words = text.split()
enc = tokenizer(words, is_split_into_words=True, return_tensors="pt", truncation=True, max_length=256)
enc = {k: v.to(model.device) for k, v in enc.items()}
with torch.no_grad():
preds = model(**enc).logits[0].argmax(-1).tolist()
word_ids = tokenizer(words, is_split_into_words=True, truncation=True, max_length=256).word_ids(0)
id2label = model.config.id2label
word_tags = {}
for i, wid in enumerate(word_ids):
if wid is not None and wid not in word_tags:
word_tags[wid] = id2label[preds[i]]
# group consecutive words into entities
entities, cur, start = [], None, None
for wid in sorted(word_tags):
t = word_tags[wid]
if t == "O":
if cur: entities.append((start, wid, cur)); cur = None
continue
prefix, etype = t.split("-", 1)
if prefix == "B" or cur != etype:
if cur: entities.append((start, wid, cur))
cur, start = etype, wid
if cur: entities.append((start, len(words), cur))
return [" ".join(words[s:e]) + f" [{t}]" for s, e, t in entities]
print(tag("Аслан Мухамедияров 2019 жылы Алматы қаласында жаңа жобаны ұсынды ."))
# ['Аслан Мухамедияров [PERSON]', '2019 жылы [DATE]', 'Алматы [GPE]']
Limitations
- NON_HUMAN is always 0.00 despite an 11.4× loss weight — the test split contains a single instance; no amount of reweighting can learn a class from one example.
- Rare classes with <100 test entities (ADAGE, CONTACT, LANGUAGE, LAW, MISCELLANEOUS, PRODUCT) remain the weakest tags (42–94 F1).
- mmBERT's Gemma 2 tokenizer fragments Kazakh words more than XLM-R's, which contributes to the remaining gap to larger XLM-R models on this task.
- Evaluation uses first-subtoken decoding on whitespace-tokenized text; scores on differently pre-processed input are not directly comparable.
Citation
If you use this model, cite the KazNERD dataset:
@inproceedings{yeshpanov2022kaznerd,
title = {KazNERD: Kazakh Named Entity Recognition Dataset},
author = {Yeshpanov, Rustem and Varis, Nafis and S Winston, Aizhan and Haji-{\'{S}}ulaiman, Nadiya and Bojar, Ond{\v{r}}ej},
booktitle = {Proceedings of the Thirteenth Language Resources and Evaluation Conference (LREC 2022)},
year = {2022},
pages = {2044--2052}
}
- Downloads last month
- -
Model tree for ai4kazakh/mmbert-base-kaznerd
Base model
jhu-clsp/mmBERT-base