Run this model with HF Jobs
Run mimibn/swedish-grammar-kbbert on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/mimibn/swedish-grammar-kbbert. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/mimibn/swedish-grammar-kbbert on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
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 synthetic errors from word-pattern rules, 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.38]
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), 15,000 synthetic errors from 10 word-pattern rules, and 1,448 texts from 6 further rules (tense against a time phrase, comparative with mer, plural form, reflexive, plural agreement, prepositions). 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, 5–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). Adopted because it improved 5-fold cross-validated toy-train Spearman for the 10-rule KB-BERT by +0.031 (95% CI +0.017 to +0.045, 3 seeds). - The 6 further rules. A first version (E10, revision
6a07269) used word lists that contained exact toy-train mistakes, replaced part of the older synthetic data, and lowered the mean DaLAJ learner-dev Spearman by 0.003 although the adoption rule said it must not drop. This version (E11) removes those words and swaps found to leave correct Swedish, and adds to the older data instead of replacing it. Its adoption rule was written before training: against the 10-rule version, cross-validated toy-train Spearman +0.012 (95% CI 0.000 to +0.026) and ROC-AUC +0.007 (0.000 to +0.014), DaLAJ learner dev +0.008 (+0.003 to +0.013). The toy-train gains are still optimistic, because the error types were chosen from toy-train mistakes. - Three seeds (13, 14, 15) were trained; this is seed 15, the best on DaLAJ dev. Weights are stored in float16.
Results
Spearman (ROC-AUC). The toy test was never trained on. It did influence development: work on the 6 further rules started to raise its ROC-AUC. It was not opened for E11 until the model and seed had been chosen, but 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 (E11, seed 15) | 0.864 (0.980) | 0.835 (0.981) | 0.754 (0.935) | 0.761 (0.939) | 0.537 (0.875) |
| same recipe, mean of 3 seeds | 0.872 (0.979) | 0.849 (0.989) | 0.748 (0.932) | 0.755 (0.936) | 0.531 (0.871) |
| stage 1 only, mean of 3 seeds | 0.841 (0.976) | 0.809 (0.978) | 0.761 (0.940) | 0.771 (0.945) | 0.529 (0.870) |
| first version of the further rules (E10), mean of 3 | 0.872 (0.976) | 0.842 (0.988) | 0.744 (0.930) | 0.752 (0.934) | 0.528 (0.869) |
| 10 rules (E9), same two stages, mean of 3 | 0.855 (0.961) | 0.836 (0.982) | 0.743 (0.929) | 0.751 (0.934) | 0.530 (0.870) |
| ELECTRA-small (22M), 10 rules, 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).
With 120 texts, toy-test differences below ~0.03 are not meaningful. DaLAJ as a whole rewards recognising coursebook style (its coursebook sentences are all correct), so the learner sentences are reported separately.
Speed with predict.py, including Python start-up and model loading, measured with E10 (same architecture and
size) on an Intel Core Ultra 9 185H laptop: 2.9 s for 120 texts on CPU and 2.2 s on GPU (RTX 2000 Ada). For 4,335
texts, 38–45 s on CPU (116 s in one session in which the laptop was slowed down) and 3.6 s on GPU.
Limitations
Some errors are missed (sentences not in the training data; score of the incorrect version, the correct one scores 0.94–0.98):
- infinitive instead of imperative: Stänga dörren när du går 0.98;
- word choice: Han tog ett misstag 0.92;
- past tense with a future phrase: Nästa vecka åkte vi till Göteborg 0.94.
Caught: Han är mer ung än jag 0.42 (a known miss that a rule was written for) and Om det regnar vi stannar hemma 0.37. For the first ten rules, a native speaker on Reddit (r/svenska) judged 37 of 38 sampled outputs ungrammatical; the six further rules were not checked by a native speaker, and a few of their outputs may be acceptable Swedish. The training data are mostly single sentences, so long texts are less tested. 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
- 66
Model tree for mimibn/swedish-grammar-kbbert
Base model
KBLab/bert-base-swedish-cased