released
Browse files- .gitattributes +3 -34
- README.md +232 -0
- config.json +93 -0
- model.safetensors +3 -0
- statickd_inference.py +87 -0
- tokenizer.json +3 -0
.gitattributes
CHANGED
|
@@ -1,35 +1,4 @@
|
|
| 1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
|
| 27 |
-
*.
|
| 28 |
-
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
|
@@ -1,3 +1,235 @@
|
|
| 1 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
license: cc-by-sa-4.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
language:
|
| 3 |
+
- af
|
| 4 |
+
- am
|
| 5 |
+
- ar
|
| 6 |
+
- az
|
| 7 |
+
- be
|
| 8 |
+
- bg
|
| 9 |
+
- bs
|
| 10 |
+
- ca
|
| 11 |
+
- cs
|
| 12 |
+
- cy
|
| 13 |
+
- da
|
| 14 |
+
- de
|
| 15 |
+
- el
|
| 16 |
+
- en
|
| 17 |
+
- es
|
| 18 |
+
- et
|
| 19 |
+
- eu
|
| 20 |
+
- fa
|
| 21 |
+
- fi
|
| 22 |
+
- fr
|
| 23 |
+
- gl
|
| 24 |
+
- gu
|
| 25 |
+
- he
|
| 26 |
+
- hi
|
| 27 |
+
- hr
|
| 28 |
+
- hu
|
| 29 |
+
- hy
|
| 30 |
+
- id
|
| 31 |
+
- is
|
| 32 |
+
- it
|
| 33 |
+
- ja
|
| 34 |
+
- ka
|
| 35 |
+
- kk
|
| 36 |
+
- kn
|
| 37 |
+
- ko
|
| 38 |
+
- ky
|
| 39 |
+
- lt
|
| 40 |
+
- lv
|
| 41 |
+
- mk
|
| 42 |
+
- ml
|
| 43 |
+
- mn
|
| 44 |
+
- mr
|
| 45 |
+
- ms
|
| 46 |
+
- my
|
| 47 |
+
- ne
|
| 48 |
+
- nl
|
| 49 |
+
- 'no'
|
| 50 |
+
- or
|
| 51 |
+
- pl
|
| 52 |
+
- ps
|
| 53 |
+
- pt
|
| 54 |
+
- ro
|
| 55 |
+
- ru
|
| 56 |
+
- sk
|
| 57 |
+
- sl
|
| 58 |
+
- so
|
| 59 |
+
- sq
|
| 60 |
+
- sr
|
| 61 |
+
- sv
|
| 62 |
+
- sw
|
| 63 |
+
- ta
|
| 64 |
+
- te
|
| 65 |
+
- th
|
| 66 |
+
- tl
|
| 67 |
+
- tr
|
| 68 |
+
- uk
|
| 69 |
+
- ur
|
| 70 |
+
- vi
|
| 71 |
+
- zh
|
| 72 |
license: cc-by-sa-4.0
|
| 73 |
+
pipeline_tag: text-classification
|
| 74 |
+
tags:
|
| 75 |
+
- iptc
|
| 76 |
+
- news
|
| 77 |
+
- topic-classification
|
| 78 |
+
- knowledge-distillation
|
| 79 |
+
- static-embeddings
|
| 80 |
+
- multilingual
|
| 81 |
+
base_model: minishlab/potion-multilingual-128M
|
| 82 |
---
|
| 83 |
+
|
| 84 |
+
# StaticKD-compact: Multilingual IPTC News-Topic Classifier With a Pruned Vocabulary
|
| 85 |
+
|
| 86 |
+
StaticKD-compact is a smaller copy of the StaticKD classifier. It assigns a news article to one of the 17 top-level topics of the IPTC Media Topic taxonomy. The model is a table that holds 17 topic scores (logits) for each token of its vocabulary. A text is classified by tokenizing it, averaging the table rows of its tokens, and adding a bias vector. Inference needs only `numpy`, `tokenizers`, and `safetensors`.
|
| 87 |
+
|
| 88 |
+
**Use the full StaticKD model unless RAM is the main constraint and all target languages are well covered by the training data.** The compact variant needs about half the RAM of StaticKD, but its accuracy falls almost to zero on languages whose rare tokens were removed from the vocabulary (see [How it differs from StaticKD](#how-it-differs-from-statickd)).
|
| 89 |
+
|
| 90 |
+
## Resources
|
| 91 |
+
|
| 92 |
+
- Code and reproduction: <https://github.com/obikastanya/statickd-iptc>
|
| 93 |
+
- Data, annotations, and predictions of every model in the paper: <https://doi.org/10.5281/zenodo.24646310>
|
| 94 |
+
- StaticKD: <https://huggingface.co/obikastanya/statickd-iptc>
|
| 95 |
+
|
| 96 |
+
## Model description
|
| 97 |
+
|
| 98 |
+
StaticKD is a distilled copy of the multilingual IPTC classifier [`classla/multilingual-IPTC-news-topic-classifier`](https://huggingface.co/classla/multilingual-IPTC-news-topic-classifier), an XLM-RoBERTa-large model with 559.9 million parameters (the teacher). StaticKD was built in three steps.
|
| 99 |
+
|
| 100 |
+
1. **Distillation.** The teacher labeled 285,999 news articles in 69 languages once and stored its 17 logits for every article. A student was then trained to imitate the teacher distribution with a Kullback–Leibler loss at temperature 1. The student averages the static token embeddings of an article and applies one linear layer. Its embeddings start from [`minishlab/potion-multilingual-128M`](https://huggingface.co/minishlab/potion-multilingual-128M), a Model2Vec model that places the tokens of 101 languages in one shared 256-dimensional space. The embeddings and the linear layer were trained together.
|
| 101 |
+
2. **Folding.** The classification layer is linear, so it can be multiplied into the embeddings after training. The product is a table `T = E Wᵀ` with one row of 17 logits per token.
|
| 102 |
+
3. **Ensembling.** Three students were trained with seeds 0, 1, and 2, and their tables and biases were averaged into one model.
|
| 103 |
+
|
| 104 |
+
StaticKD-compact then prunes the vocabulary of StaticKD without further training. Every token piece that occurs fewer than 10 times in the training data is removed from the tokenizer and from the table. Single characters are always kept, so every text can still be tokenized. The remaining table rows and the bias are identical to those of StaticKD.
|
| 105 |
+
|
| 106 |
+
| Component | StaticKD-compact | StaticKD |
|
| 107 |
+
|---|---|---|
|
| 108 |
+
| Token table | 335,441 tokens × 17 labels, float16 | 500,353 tokens × 17 labels, float16 |
|
| 109 |
+
| Bias | 17 values, float32 (identical) | 17 values, float32 |
|
| 110 |
+
| Tokenizer | pruned Unigram tokenizer of potion-multilingual-128M (case-sensitive, no lowercasing) | full Unigram tokenizer |
|
| 111 |
+
| Maximum input | first 1,024 tokens of a text | first 1,024 tokens of a text |
|
| 112 |
+
| File size | 23 MiB | 34 MiB |
|
| 113 |
+
|
| 114 |
+
**Computation.** The tokenizer encodes the text without special tokens. The first 1,024 token ids are kept and unknown tokens (`[UNK]`) are removed. A text without any known token is represented by token id 0. The table rows of the remaining ids are averaged in float32, the bias is added, and softmax gives the label probabilities. `statickd_inference.py` implements exactly this computation.
|
| 115 |
+
|
| 116 |
+
## How it differs from StaticKD
|
| 117 |
+
|
| 118 |
+
The pruned tokenizer splits words that contained a removed token into shorter pieces, so the model reads different tokens than StaticKD on the same text. This costs little in well-covered languages and fails in languages whose text consists mostly of rare tokens.
|
| 119 |
+
|
| 120 |
+
- **Smaller and faster.** Model RAM falls from 501 to 277 MiB, the file from 34 to 23 MiB, and single-core throughput rises from 539 to 601 articles per second.
|
| 121 |
+
- **Lower test accuracy.** Test macro-F1 falls from 0.545 to 0.484 and test accuracy from 0.641 to 0.544. The paired difference of 0.060 [0.003–0.100] is not significant after Holm correction in the main bootstrap of the paper (p = 0.090), but it is significant in the bootstrap stratified by test-set source (p = 0.018).
|
| 122 |
+
- **Failure on languages with rare tokens.** In Bangla, Malayalam, Odia, and Punjabi, at least 67% of the token occurrences come from tokens seen fewer than 10 times in the training data. StaticKD-compact reaches an accuracy of 0.013 on these four languages against 0.578 on the other languages. The full StaticKD reaches 0.624 on the same four languages and the teacher 0.738.
|
| 123 |
+
- **Development set.** On the four languages of the development set, which are well covered by the training data, the two models score almost the same (macro-F1 0.754 against 0.761).
|
| 124 |
+
|
| 125 |
+
## Intended use
|
| 126 |
+
|
| 127 |
+
- Topic classification of news articles (title and body) into the 17 top-level IPTC Media Topics when RAM is tight and every target language has substantial training data.
|
| 128 |
+
- High-volume multilingual pipelines on CPU, for example filtering or routing a news stream.
|
| 129 |
+
|
| 130 |
+
Before deployment, check accuracy on labeled articles in every target language. The paper used the title and body truncated to the first 512 words as input. Training articles had at least 75 words (200 characters for Chinese, Japanese, Thai, and Burmese).
|
| 131 |
+
|
| 132 |
+
## Out-of-scope use
|
| 133 |
+
|
| 134 |
+
- Languages with little or no training data, and any language in which most tokens are rare in the training data (for example Bangla, Malayalam, Odia, and Punjabi). Use the full StaticKD model instead.
|
| 135 |
+
- Multi-label classification or IPTC levels below the top level. The model gives one distribution over the 17 top-level topics.
|
| 136 |
+
- Short texts such as headlines or social media posts, and text that is not news.
|
| 137 |
+
- Decisions about individuals or other high-stakes uses without human review.
|
| 138 |
+
|
| 139 |
+
## Labels
|
| 140 |
+
|
| 141 |
+
The 17 top-level IPTC Media Topics, in the order of the output vector:
|
| 142 |
+
|
| 143 |
+
| id | label | id | label |
|
| 144 |
+
|---|---|---|---|
|
| 145 |
+
| 0 | arts, culture, entertainment and media | 9 | labour |
|
| 146 |
+
| 1 | conflict, war and peace | 10 | lifestyle and leisure |
|
| 147 |
+
| 2 | crime, law and justice | 11 | politics |
|
| 148 |
+
| 3 | disaster, accident and emergency incident | 12 | religion |
|
| 149 |
+
| 4 | economy, business and finance | 13 | science and technology |
|
| 150 |
+
| 5 | education | 14 | society |
|
| 151 |
+
| 6 | environment | 15 | sport |
|
| 152 |
+
| 7 | health | 16 | weather |
|
| 153 |
+
| 8 | human interest | | |
|
| 154 |
+
|
| 155 |
+
## How to use
|
| 156 |
+
|
| 157 |
+
Install the three dependencies and download all files of this repository into one folder.
|
| 158 |
+
|
| 159 |
+
```bash
|
| 160 |
+
pip install numpy tokenizers safetensors
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
```python
|
| 164 |
+
from statickd_inference import StaticKDClassifier
|
| 165 |
+
|
| 166 |
+
model = StaticKDClassifier(".") # folder with config.json, model.safetensors, tokenizer.json
|
| 167 |
+
|
| 168 |
+
texts = [
|
| 169 |
+
"The central bank raised its key interest rate by half a percentage point on Thursday "
|
| 170 |
+
"to curb inflation, and markets fell sharply after the announcement.",
|
| 171 |
+
"El delantero marcó dos goles en la segunda parte y el equipo se clasificó para la final "
|
| 172 |
+
"de la Copa del Rey.",
|
| 173 |
+
]
|
| 174 |
+
print(model.predict(texts, top_k=2))
|
| 175 |
+
# [[('economy, business and finance', 1.0), ('arts, culture, entertainment and media', 0.0)],
|
| 176 |
+
# [('sport', 1.0), ('arts, culture, entertainment and media', 0.0)]]
|
| 177 |
+
|
| 178 |
+
probs = model.predict_proba(texts) # numpy array of shape (2, 17), label order as above
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
From the command line:
|
| 182 |
+
|
| 183 |
+
```bash
|
| 184 |
+
python statickd_inference.py "Heavy rain and strong winds are expected across the region this weekend."
|
| 185 |
+
```
|
| 186 |
+
|
| 187 |
+
On the 12,744 test articles and the 1,000 development articles of the paper, `statickd_inference.py` reproduces the logits of the reference implementation bit for bit.
|
| 188 |
+
|
| 189 |
+
## Evaluation
|
| 190 |
+
|
| 191 |
+
**Test set.** The test set has 12,744 news articles in 47 languages. Its labels do not come from the teacher. They come from human annotation (32.1% of the articles), publisher categories (20.4%), and news-site sections (47.5%), each mapped to the top-level IPTC topics. The articles come from CC-NEWS 2024–2026 and from five public datasets (MasakhaNEWS, L3Cube-IndicNews, MN-DS, a Bangla news dataset, and IndoNews). The CC-NEWS articles come from news sites that are absent from the training data, and a contamination filter removed articles that share a large part of their text with training documents.
|
| 192 |
+
|
| 193 |
+
| Model | Test macro-F1 [95% CI] | Test accuracy | Share of teacher macro-F1 | Development macro-F1 |
|
| 194 |
+
|---|---|---|---|---|
|
| 195 |
+
| Teacher (XLM-R-large) | 0.598 [0.546–0.643] | 0.700 | 100.0% | 0.822 |
|
| 196 |
+
| StaticKD | 0.545 [0.490–0.605] | 0.641 | 91.1% | 0.761 |
|
| 197 |
+
| **StaticKD-compact** | **0.484 [0.421–0.597]** | **0.544** | **81.0%** | **0.754** |
|
| 198 |
+
|
| 199 |
+
The confidence intervals come from a bootstrap that resamples whole news sites. The development set is the development split of EMMediaTopic (1,000 articles in Catalan, Greek, Croatian, and Slovenian with GPT-4o labels). The teacher's hyperparameters were tuned on this split, so the development scores are not comparable with the test scores.
|
| 200 |
+
|
| 201 |
+
**Label noise.** About two thirds of the test labels are editorial labels, which record newsroom decisions and not annotation under a guideline. Checks on samples of these articles found that the editorial label names the main topic for 76.5% to 79.3% of the articles (80.4% after a post-hoc recheck). Absolute scores therefore understate agreement with the main topic of an article.
|
| 202 |
+
|
| 203 |
+
## Efficiency
|
| 204 |
+
|
| 205 |
+
| Model | Latency p50 / p95, 1 core (ms) | Throughput, 1 core (articles/s) | Throughput, 4 cores (articles/s) | Model RAM (MiB) | File size (MiB) |
|
| 206 |
+
|---|---|---|---|---|---|
|
| 207 |
+
| Teacher, PyTorch fp32 | 2,816 / 2,894 | 0.343 | 0.969 | 2,950 | 2,136 |
|
| 208 |
+
| Teacher, ONNX int8 | 1,190 / 1,229 | 0.790 | 2.53 | 1,375 | 537 |
|
| 209 |
+
| StaticKD | 2.01 / 3.31 | 539 | 1,827 | 501 | 34 |
|
| 210 |
+
| **StaticKD-compact** | **1.78 / 3.17** | **601** | **2,005** | **277** | **23** |
|
| 211 |
+
|
| 212 |
+
All models ran on an Intel Core Ultra 7 155H laptop with each process pinned to one or four physical performance cores, and each value is the median of three sessions. Throughput used batches of 64 on 3,000 test articles for the StaticKD models and batches of 8 on 96 articles for the teacher. Latency was measured with one article per call. Model RAM is the increase in resident memory after loading. Most of the model RAM is taken by the Unigram tokenizer, so pruning the vocabulary lowers RAM much more than file size.
|
| 213 |
+
|
| 214 |
+
## Training data
|
| 215 |
+
|
| 216 |
+
- **EMMediaTopic 1.0**: the 20,000-article training split in Catalan, Greek, Croatian, and Slovenian. The teacher was trained on the same split.
|
| 217 |
+
- **CC-News 2019–2021**: 265,999 news articles in 69 languages. The language of every article was checked with fastText language identification. Articles were kept with at least 75 words and truncated to 512 words, and the number of articles per site and language was capped.
|
| 218 |
+
|
| 219 |
+
The combined set has 285,999 articles in 69 languages from 2,960 news sites. The training targets are the teacher's logits for these articles, not human labels. The token counts that decided which tokens StaticKD-compact keeps were taken from the same 285,999 articles.
|
| 220 |
+
|
| 221 |
+
Training settings of StaticKD: 6 epochs, batch size 64, AdamW (learning rate 0.003) for the linear layer, SparseAdam (learning rate 0.001) for the embeddings, dropout 0.1 on the averaged embedding, and token dropout 0.2. StaticKD-compact received no further training.
|
| 222 |
+
|
| 223 |
+
## Limitations
|
| 224 |
+
|
| 225 |
+
- **Pruned languages.** Accuracy collapses on languages whose text consists mostly of tokens that are rare in the training data (see above). Accuracy must be checked per language before deployment.
|
| 226 |
+
- **Weak labels.** For the full StaticKD, test F1 is lowest for *society* (0.16), *disaster* (0.29), *human interest* (0.32), and *conflict* (0.37). These labels also rank among the hardest for the teacher.
|
| 227 |
+
- **Weak languages.** The full StaticKD is weakest on African languages that are absent from its training data and from the pre-training data of XLM-R (Igbo, Luganda, Lingala, Nigerian Pidgin, Kirundi, Shona, Tigrinya, and Yoruba), with a mean accuracy of 0.407 against 0.643 for the teacher. Per-language results for the compact variant on these languages are not reported in the paper.
|
| 228 |
+
- **Test scores are not deployment estimates.** The test set has at most 50 articles per language–label pair, so its label distribution is artificial. In 27 of its 47 languages the articles come from fewer than 3 effective news sites, and the whole test set has an effective number of only 13.8 site clusters.
|
| 229 |
+
- **Single-label, top level only.** Multi-topic articles and deeper IPTC levels were not evaluated.
|
| 230 |
+
- **Inherited errors.** The model learned only from the teacher's outputs and passes on the teacher's errors and biases.
|
| 231 |
+
- **Hardware.** Efficiency was measured on one laptop. Absolute speeds will differ on other hardware.
|
| 232 |
+
|
| 233 |
+
## Citation
|
| 234 |
+
|
| 235 |
+
Citation will be added when the paper is published.
|
config.json
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_type": "statickd",
|
| 3 |
+
"variant": "compact",
|
| 4 |
+
"task": "text-classification",
|
| 5 |
+
"problem_type": "single_label_classification",
|
| 6 |
+
"num_labels": 17,
|
| 7 |
+
"id2label": {
|
| 8 |
+
"0": "arts, culture, entertainment and media",
|
| 9 |
+
"1": "conflict, war and peace",
|
| 10 |
+
"2": "crime, law and justice",
|
| 11 |
+
"3": "disaster, accident and emergency incident",
|
| 12 |
+
"4": "economy, business and finance",
|
| 13 |
+
"5": "education",
|
| 14 |
+
"6": "environment",
|
| 15 |
+
"7": "health",
|
| 16 |
+
"8": "human interest",
|
| 17 |
+
"9": "labour",
|
| 18 |
+
"10": "lifestyle and leisure",
|
| 19 |
+
"11": "politics",
|
| 20 |
+
"12": "religion",
|
| 21 |
+
"13": "science and technology",
|
| 22 |
+
"14": "society",
|
| 23 |
+
"15": "sport",
|
| 24 |
+
"16": "weather"
|
| 25 |
+
},
|
| 26 |
+
"label2id": {
|
| 27 |
+
"arts, culture, entertainment and media": 0,
|
| 28 |
+
"conflict, war and peace": 1,
|
| 29 |
+
"crime, law and justice": 2,
|
| 30 |
+
"disaster, accident and emergency incident": 3,
|
| 31 |
+
"economy, business and finance": 4,
|
| 32 |
+
"education": 5,
|
| 33 |
+
"environment": 6,
|
| 34 |
+
"health": 7,
|
| 35 |
+
"human interest": 8,
|
| 36 |
+
"labour": 9,
|
| 37 |
+
"lifestyle and leisure": 10,
|
| 38 |
+
"politics": 11,
|
| 39 |
+
"religion": 12,
|
| 40 |
+
"science and technology": 13,
|
| 41 |
+
"society": 14,
|
| 42 |
+
"sport": 15,
|
| 43 |
+
"weather": 16
|
| 44 |
+
},
|
| 45 |
+
"vocab_size": 335441,
|
| 46 |
+
"weights": {
|
| 47 |
+
"file": "model.safetensors",
|
| 48 |
+
"token_table": {
|
| 49 |
+
"name": "token_table",
|
| 50 |
+
"shape": [
|
| 51 |
+
335441,
|
| 52 |
+
17
|
| 53 |
+
],
|
| 54 |
+
"dtype": "float16"
|
| 55 |
+
},
|
| 56 |
+
"bias": {
|
| 57 |
+
"name": "bias",
|
| 58 |
+
"shape": [
|
| 59 |
+
17
|
| 60 |
+
],
|
| 61 |
+
"dtype": "float32"
|
| 62 |
+
}
|
| 63 |
+
},
|
| 64 |
+
"tokenizer": {
|
| 65 |
+
"file": "tokenizer.json",
|
| 66 |
+
"type": "Unigram",
|
| 67 |
+
"add_special_tokens": false,
|
| 68 |
+
"lowercase": false,
|
| 69 |
+
"unk_token": "[UNK]",
|
| 70 |
+
"drop_unk_tokens": true,
|
| 71 |
+
"empty_input_token_id": 0
|
| 72 |
+
},
|
| 73 |
+
"max_tokens": 1024,
|
| 74 |
+
"pooling": "mean",
|
| 75 |
+
"compute_dtype": "float32",
|
| 76 |
+
"output": "softmax",
|
| 77 |
+
"inference": [
|
| 78 |
+
"encode the text with tokenizer.json without special tokens (normalization is defined in tokenizer.json; no lowercasing)",
|
| 79 |
+
"keep the first max_tokens token ids, then remove the [UNK] id",
|
| 80 |
+
"if no id is left, use the single id empty_input_token_id",
|
| 81 |
+
"gather the rows of token_table for these ids and average them in float32",
|
| 82 |
+
"add bias to obtain 17 logits",
|
| 83 |
+
"apply softmax to obtain label probabilities"
|
| 84 |
+
],
|
| 85 |
+
"ensemble_seeds": [
|
| 86 |
+
0,
|
| 87 |
+
1,
|
| 88 |
+
2
|
| 89 |
+
],
|
| 90 |
+
"base_embeddings": "minishlab/potion-multilingual-128M",
|
| 91 |
+
"teacher": "classla/multilingual-IPTC-news-topic-classifier",
|
| 92 |
+
"compact_min_token_count": 10
|
| 93 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9317a6a341aeb702ac60d46d65eac92d2ff832860a593f55dd5a90715d0cb923
|
| 3 |
+
size 11405246
|
statickd_inference.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Standalone inference for StaticKD IPTC news-topic classifiers.
|
| 2 |
+
|
| 3 |
+
Requirements: numpy, tokenizers, safetensors.
|
| 4 |
+
|
| 5 |
+
The model is a table with 17 topic logits per token. A text is classified by
|
| 6 |
+
tokenizing it, averaging the table rows of its tokens, adding a bias vector,
|
| 7 |
+
and applying softmax.
|
| 8 |
+
|
| 9 |
+
Usage from Python:
|
| 10 |
+
from statickd_inference import StaticKDClassifier
|
| 11 |
+
model = StaticKDClassifier("path/to/model/folder")
|
| 12 |
+
model.predict(["Title and body of a news article ..."])
|
| 13 |
+
|
| 14 |
+
Usage from the command line:
|
| 15 |
+
python statickd_inference.py "Title and body of a news article ..."
|
| 16 |
+
"""
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
import json
|
| 20 |
+
import sys
|
| 21 |
+
from pathlib import Path
|
| 22 |
+
|
| 23 |
+
import numpy as np
|
| 24 |
+
from safetensors.numpy import load_file
|
| 25 |
+
from tokenizers import Tokenizer
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def softmax(logits: np.ndarray) -> np.ndarray:
|
| 29 |
+
z = np.asarray(logits, dtype=np.float64)
|
| 30 |
+
z = z - z.max(axis=-1, keepdims=True)
|
| 31 |
+
p = np.exp(z)
|
| 32 |
+
return p / p.sum(axis=-1, keepdims=True)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class StaticKDClassifier:
|
| 36 |
+
"""Loads config.json, model.safetensors and tokenizer.json from one folder."""
|
| 37 |
+
|
| 38 |
+
def __init__(self, path: str | Path = "."):
|
| 39 |
+
path = Path(path)
|
| 40 |
+
self.config = json.loads((path / "config.json").read_text(encoding="utf-8"))
|
| 41 |
+
tensors = load_file(str(path / self.config["weights"]["file"]))
|
| 42 |
+
self.table = tensors["token_table"] # (vocab_size, 17), float16
|
| 43 |
+
self.bias = tensors["bias"] # (17,), float32
|
| 44 |
+
tok_cfg = self.config["tokenizer"]
|
| 45 |
+
self.tokenizer = Tokenizer.from_file(str(path / tok_cfg["file"]))
|
| 46 |
+
self.unk_id = self.tokenizer.token_to_id(tok_cfg["unk_token"])
|
| 47 |
+
self.empty_id = int(tok_cfg["empty_input_token_id"])
|
| 48 |
+
self.max_tokens = int(self.config["max_tokens"])
|
| 49 |
+
self.labels = [self.config["id2label"][str(i)] for i in range(self.config["num_labels"])]
|
| 50 |
+
|
| 51 |
+
def token_ids(self, texts: list[str]) -> list[np.ndarray]:
|
| 52 |
+
"""Token ids per text: no special tokens, first max_tokens ids, unknown tokens removed."""
|
| 53 |
+
out = []
|
| 54 |
+
for enc in self.tokenizer.encode_batch(list(texts), add_special_tokens=False):
|
| 55 |
+
ids = np.asarray(enc.ids[: self.max_tokens], dtype=np.int64)
|
| 56 |
+
if self.unk_id is not None:
|
| 57 |
+
ids = ids[ids != self.unk_id]
|
| 58 |
+
out.append(ids if len(ids) else np.full(1, self.empty_id, dtype=np.int64))
|
| 59 |
+
return out
|
| 60 |
+
|
| 61 |
+
def logits(self, texts: list[str]) -> np.ndarray:
|
| 62 |
+
"""Array of shape (len(texts), 17): mean of the token rows plus the bias."""
|
| 63 |
+
if isinstance(texts, str):
|
| 64 |
+
texts = [texts]
|
| 65 |
+
ids = self.token_ids(texts)
|
| 66 |
+
x = np.stack([self.table[i].mean(axis=0, dtype=np.float32) for i in ids])
|
| 67 |
+
return x + self.bias
|
| 68 |
+
|
| 69 |
+
def predict_proba(self, texts: list[str]) -> np.ndarray:
|
| 70 |
+
"""Array of shape (len(texts), 17) with label probabilities in the order of self.labels."""
|
| 71 |
+
return softmax(self.logits(texts))
|
| 72 |
+
|
| 73 |
+
def predict(self, texts: list[str], top_k: int = 1) -> list[list[tuple[str, float]]]:
|
| 74 |
+
"""For every text, the top_k (label, probability) pairs, highest first."""
|
| 75 |
+
probs = self.predict_proba(texts)
|
| 76 |
+
order = np.argsort(-probs, axis=1, kind="stable")[:, :top_k]
|
| 77 |
+
return [[(self.labels[j], float(p[j])) for j in row] for p, row in zip(probs, order)]
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
if len(sys.argv) < 2:
|
| 82 |
+
sys.exit('usage: python statickd_inference.py "text" ["text" ...]')
|
| 83 |
+
model = StaticKDClassifier(Path(__file__).resolve().parent)
|
| 84 |
+
for text, top in zip(sys.argv[1:], model.predict(sys.argv[1:], top_k=3)):
|
| 85 |
+
print(text[:60].replace("\n", " "))
|
| 86 |
+
for label, prob in top:
|
| 87 |
+
print(f" {prob:.3f} {label}")
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8b9ac969bed7ace2f27b8f676fc11a49a5cbe4a8b2168d6ffd1e6cdad4587834
|
| 3 |
+
size 12901067
|