Instructions to use mimibn/swedish-grammar-kbbert with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mimibn/swedish-grammar-kbbert with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="mimibn/swedish-grammar-kbbert")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("mimibn/swedish-grammar-kbbert") model = AutoModelForSequenceClassification.from_pretrained("mimibn/swedish-grammar-kbbert", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Swedish grammaticality scorer (KB-BERT)
Scores a Swedish text from 0 (ungrammatical) to 1 (well-formed). KB-BERT (125M parameters) fine-tuned as a regressor in two stages: on real learner errors (DaLAJ-GED) plus rule-based synthetic errors, then briefly on the selection task's 478 toy examples. Built for the AI Sweden master's thesis selection task. Code, experiments and evaluation: https://github.com/meryambn/swedish_grammar
Usage
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
name = "mimibn/swedish-grammar-kbbert"
tok = AutoTokenizer.from_pretrained(name)
model = AutoModelForSequenceClassification.from_pretrained(name, dtype=torch.float32).eval()
texts = ["Vi måste vänta på bussen.", "Vi måste vänta för bussen."]
with torch.inference_mode():
enc = tok(texts, padding=True, truncation=True, max_length=256, return_tensors="pt")
scores = model(**enc).logits.squeeze(-1).clamp(0, 1)
print(scores.tolist()) # ~[0.96, 0.39]
Or with the repository's interface: python predict.py --input test.jsonl --output scores.jsonl. It applies
the same spacing normalisation as training and runs the model in plain PyTorch (bert_scorer.py, same
scores as above) to avoid the start-up cost of importing transformers.
Training
- Stage 1 — data (mimibn/swedish-grammaticality-data,
split
train): DaLAJ-GED train (35,125 texts: learner sentences with one error, their corrected versions, coursebook sentences) plus 15,000 synthetic errors made by 10 word-pattern rules (verb form, agreement, word order, definiteness, pronoun case, gender, adverb placement, governed preposition, split compound, missing/extra word), length-matched to the correct texts. Target: the toy data's mean score for 0 / 1 / 2 / 3 errors (0.953 / 0.393 / 0.224 / 0.094). One regression output, MSE loss, 2 epochs, batch 32, learning rate 3e-5, warm-up 6%, max 256 tokens; best checkpoint by Spearman on DaLAJ dev (training_config_stage1.yaml, ~6 min on a laptop GPU). - Stage 2 — the 478 toy training examples with their own human scores (split
train_stage2), 5 epochs, batch 16, learning rate 2e-5, warm-up 10%, final weights kept (training_config_stage2.yaml, ~10 s). Stage 2 was adopted because 5-fold cross-validation on the toy training set (scoring only texts each model never saw) improved Spearman by +0.031 (95% CI +0.017 to +0.045, 3 seeds). - Three seeds (13, 14, 15) were trained; this is seed 14, the best on DaLAJ dev. Weights are stored in float16 (scores unchanged to three decimals).
Results
Spearman (ROC-AUC). The toy test was never trained on or used to choose a model, setting or seed, but its results were seen during development, so it is not an untouched holdout.
| toy test (120) | toy train (478, texts unseen in training*) | DaLAJ dev | DaLAJ test | DaLAJ test, learner sentences | |
|---|---|---|---|---|---|
| this model (seed 14) | 0.854 (0.957) | 0.839 (0.985) | 0.744 (0.930) | 0.755 (0.936) | 0.533 (0.872) |
| same recipe, mean of 3 seeds | 0.855 (0.961) | 0.836 (0.982) | 0.743 (0.929) | 0.751 (0.934) | 0.530 (0.870) |
| stage 1 only, mean of 3 seeds | 0.825 (0.954) | 0.806 (0.972) | 0.761 (0.939) | 0.771 (0.944) | 0.528 (0.869) |
| ELECTRA-small (22M), same two stages, mean of 3 | 0.831 (0.957) | 0.808 (0.968) | 0.711 (0.910) | 0.720 (0.916) | 0.508 (0.855) |
| starter baseline | 0.528 (0.783) | — | 0.523 (0.802) | 0.520 (0.800) | 0.350 (0.744) |
* For models with stage 2: 5-fold cross-validation (each text scored by a model trained without it).
Stage 2 trades about 0.02 on DaLAJ overall for +0.03 on toy-style text; on DaLAJ's learner sentences it is neutral, so the loss is mainly on coursebook sentences (all correct in DaLAJ, which a model can partly recognise by style). With 120 texts, toy-test differences below ~0.03 are not meaningful.
Speed with predict.py, including Python start-up and model loading (Intel Core Ultra 9 185H laptop CPU):
~2,300 texts/min for 120 texts and ~5,200 texts/min for 4,335 texts.
Limitations
Errors the training rules do not produce are missed or only weakly penalised (sentences not in the training data; score of the correct version → incorrect version):
- comparative with mer: Han är mer ung än jag 0.94 (correct yngre 0.97);
- infinitive instead of imperative: Stänga dörren när du går 0.98 (Stäng 0.98);
- tense against a time adverb: I går äter vi pizza 0.81 (åt 0.97);
- word choice: Han tog ett misstag 0.84 (gjorde 0.96).
Long multi-sentence texts get lower scores than short ones (the training data are mostly single sentences). Scores rank texts; they are not calibrated probabilities. Inputs are cut at 256 tokens.
License and credits
CC BY 4.0. Base model: KBLab/bert-base-swedish-cased (National Library of Sweden). Training data derived from DaLAJ-GED (Språkbanken Text, SuperLim 2.0, CC BY 4.0) and the selection task's toy examples (https://github.com/nicher92/swedish_correctness_classifier).
- Downloads last month
- -
Model tree for mimibn/swedish-grammar-kbbert
Base model
KBLab/bert-base-swedish-cased