absltnull commited on
Commit
23a265d
·
verified ·
1 Parent(s): eb80f5c

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* 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
 
 
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
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,3 +1,150 @@
1
  ---
 
 
 
 
2
  license: mit
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ language:
3
+ - bs
4
+ - hr
5
+ - sr
6
  license: mit
7
+ library_name: transformers
8
+ tags:
9
+ - text-classification
10
+ - language-identification
11
+ - xlm-roberta
12
+ - bosnian
13
+ - croatian
14
+ - serbian
15
+ - south-slavic
16
+ base_model: absltnull/RoBERTo
17
+ pipeline_tag: text-classification
18
  ---
19
+ # RoBERTo-v2 :: bs / hr / sr Language Identifier
20
+
21
+ *A new and improved version of `absltnull/RoBERTo`. Memory footprint halved, accuracy shot up by 20 points, and a major shortcut/problem with the previous model solved.*
22
+
23
+ ## Architecture
24
+
25
+ | Name | Value |
26
+ | --- | --- |
27
+ | Architecture | `xlm-roberta-base` |
28
+ | Base Model | `absltnull/RoBERTo` |
29
+ | Max Seq. Len. | **512 tokens** |
30
+ | Classes | `bs`, `hr`, `sr` |
31
+ | Type | **Transformer-based Language Classifier** |
32
+
33
+ *Note: Recommended to use with Latin text. Cyrillic is still a battle ground.*
34
+
35
+ ## Problem
36
+ **RoBERTo**, while impressively capable in differentiating Bosnian, Croatian and Serbian in Wikipedia text, **has a massive flaw in its training data.**
37
+
38
+ While testing the model on non-Cyrillic Serbian and Cyrillic Bosnian examples, I've noticed a fatal bias: **Cyrillic, whether it be Bosnian or Serbian, is always predicted as Serbian with ~99.97% `(0.9997)` or even 100% `(1.00)` confidence.**
39
+
40
+ Looking at the training data for the original model, **almost all of the Serbian Wikipedia pages were written entirely in Cyrillic**, while none of the Bosnian pages got such treatment, presenting the model with a **massive bias: "Cyrillic is always Serbian."**
41
+
42
+ Further testing the model, it has revealed **even more Serbian-related bias.** Since almost every Serbian example it has seen was in Cyrillic, the model has learned to associate Latin with Bosnian and Croatian, completely ruling Serbian out and entirely tying it to Cyrillic script. It has learned a **clear, consistent, yet entirely wrong signal: "Latin is always NOT Serbian."**
43
+
44
+ **That cannot be left as-is. Not as long as I'm alive.**
45
+
46
+ ## Solution
47
+ After 4 unsuccessful attempts of trying to teach the model the correct path, "Cyrillc is not Serbian, it can also be Bosnian. Serbian can also be in Latin", while preserving its original intelligence, I decided to think outside of the box and look at the problem in another way:
48
+
49
+ **"The model likes Cyrillic? Snatch it away entirely and force-feed it Latin through its nose."**
50
+
51
+ I've made an **entirely new, Latin-only dataset** by converting the entirety of the previous dataset to Latin and salting it with new labeled data from Twitter and SETimes. The dataset taught the model some informal language (Twitter) and gave it new formal sources (SETimes) while still keeping its original dominant field (Wikipedia).
52
+
53
+ The result? **Performance shot up by 20 percentage points of accuracy on Latin-only evaluation.**
54
+
55
+ *(While I was at it, I also halved the memory it uses to load and run, and it only takes up ~500 MB on disk. You're welcome.)*
56
+
57
+ ## Evaluation
58
+ Now comes the Latin-only evaluation I was talking about. I've compared **RoBERTo vs RoBERTo-v2 vs FastText vs CLD2.**
59
+
60
+ Take a look:
61
+
62
+ ![Evaluation Accuracy Chart](./model_comparison.png)
63
+
64
+ As seen in the chart:
65
+ - **RoBERTo (acc 0.663, f1 0.615):** As already mentioned, when you strip away its "Serbian is Cyrillic and Cyrillic only" defense, the model entirely collapses on actual LID benchmarks, sitting on **only 66% accuracy**, which basically brings it down by 25 points from its original ~91% accuracy it had on the biased evaluation data.
66
+ - **RoBERTO-v2 / roberto-finetuned (acc 0.867, f1 0.867):** The new, Latin-only replacement shows **actually promising results with ~87% accuracy**, meaning it isn't guessing the language, but failing on ambiguous labeling cases, as explained in the next section.
67
+ - **FastText (acc 0.551, f1 0.451):** Unreliable for real-world HBS labeling usecases, as it's worse on Latin-only than it was on the biased Cyrillic data. Only 5 points more accurate than a coinflip.
68
+ - **CLD2 (acc 0.442, f1 0.456):** Genuinely the worst option you could pick. Statistically, a coinflip would be more reliable at labeling the languages than this model. Don't use this.
69
+
70
+ ## The Confusions
71
+ Just like in the previous release, this model also fails at differentiating short, ambiguous, or unrelated text (languages outside of HBS, emoji/symbol spam, etc).
72
+
73
+ Looking at the matrix...
74
+
75
+ ![Confusion Matrix](./confusion_matrix.png)
76
+
77
+ ...we can see:
78
+
79
+ - Out of 2,950 Bosnian examples, it was mislabeled as Croatian 292 times, and mislabeled as Serbian 139 times.
80
+ - Out of 3060 Croatian examples, it was mislabeled as Bosnian 321 times, and mislabeled as Serbian 58 times.
81
+ - Out of 2987 Serbian examples, it was mislabeled as Bosnian 322 times, and mislabeled as Croatian 68 times.
82
+
83
+ So we can conclude:
84
+ - The model is **very good at differentiating Croatian from Serbian** (negligable confusion amounts).
85
+ - It is **more likely to confuse Serbian with Bosnian than vice versa** (322 > 139).
86
+ - It is **more likely to confuse Croatian with Bosnian than vice versa** (321 > 292).
87
+
88
+ And looking at this report...
89
+ ```text
90
+ ==================================================
91
+ RoBERTo
92
+ ==================================================
93
+ precision recall f1-score support
94
+
95
+ bs 0.54 0.89 0.67 2950
96
+ hr 0.77 0.88 0.82 3060
97
+ sr 0.98 0.21 0.35 2987
98
+
99
+
100
+ ==================================================
101
+ roberto-finetuned
102
+ ==================================================
103
+ precision recall f1-score support
104
+
105
+ bs 0.80 0.85 0.82 2950
106
+ hr 0.88 0.88 0.88 3060
107
+ sr 0.93 0.87 0.90 2987
108
+ ```
109
+ ...when the model says that something is Bosnian, **it actually is Bosnian 80% of the time,** compared to the original's ~54%, and it catches ~85% of such cases.
110
+ When it says that something is Croatian, **it actually is Croatian 88% of the time,** compared to the original's 77%, and it catches 88% of such cases.
111
+ And when it says that something is Serbian, **it actually is Serbian 93% of the time,** compared to the original's misleading 98%, and it catches 87% of such cases, whereas the original misses 71% of actual Serbian cases.
112
+
113
+ ## How to use
114
+ Again, this is still based on XLM-RoBERTa-Base, so the inference is as easy as it was in the last release, only this time the model takes up half as much memory on disk and in RAM because of the new BF16 weights.
115
+
116
+ ```python
117
+ # RoBERTo — sr/bs/hr language identification
118
+
119
+ import torch
120
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
121
+
122
+ MODEL_NAME = "absltnull/RoBERTo-v2"
123
+
124
+ # labels (sr/bs/hr) are already baked into the model's config
125
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
126
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
127
+
128
+ classifier = pipeline(
129
+ "text-classification",
130
+ model=model,
131
+ tokenizer=tokenizer,
132
+ device=0 if torch.cuda.is_available() else -1,
133
+ )
134
+
135
+ # Only use Latin. For the love of God.
136
+ texts = [
137
+ "Ove sedmice moram da kupim bijeli hljeb.", # bs
138
+ "Ove nedelje moram da kupim beli hleb.", # sr
139
+ "Ovog tjedna moram da kupim bijeli kruh.", # hr
140
+ ]
141
+
142
+ # simple usage: top predicted label
143
+ predictions = classifier(texts)
144
+ for text, pred in zip(texts, predictions):
145
+ print(f"[{pred['label']}] ({pred['score']:.2%}) {text}")
146
+
147
+ ```
148
+
149
+ ## TL;DR
150
+ Also, if I haven't made it obvious yet... don't use this with Cyrillic. The model was trained to be good at Latin. That's about it. Cya.
config.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_cross_attention": false,
3
+ "architectures": [
4
+ "XLMRobertaForSequenceClassification"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "bos_token_id": 0,
8
+ "classifier_dropout": null,
9
+ "dtype": "bfloat16",
10
+ "eos_token_id": 2,
11
+ "hidden_act": "gelu",
12
+ "hidden_dropout_prob": 0.1,
13
+ "hidden_size": 768,
14
+ "id2label": {
15
+ "0": "sr",
16
+ "1": "bs",
17
+ "2": "hr"
18
+ },
19
+ "initializer_range": 0.02,
20
+ "intermediate_size": 3072,
21
+ "is_decoder": false,
22
+ "label2id": {
23
+ "bs": 1,
24
+ "hr": 2,
25
+ "sr": 0
26
+ },
27
+ "layer_norm_eps": 1e-05,
28
+ "max_position_embeddings": 514,
29
+ "model_type": "xlm-roberta",
30
+ "num_attention_heads": 12,
31
+ "num_hidden_layers": 12,
32
+ "output_past": true,
33
+ "pad_token_id": 1,
34
+ "position_embedding_type": "absolute",
35
+ "problem_type": "single_label_classification",
36
+ "tie_word_embeddings": true,
37
+ "transformers_version": "5.14.1",
38
+ "type_vocab_size": 1,
39
+ "use_cache": false,
40
+ "vocab_size": 250002
41
+ }
confusion_matrix.png ADDED
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:845b2375dca890e25b9b3dce6cd68e93a11aaf2b0101df319c32f139c56d87cd
3
+ size 556116254
model_comparison.png ADDED
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:acbd420e2269cdc1ef45332d3d5c418be4aef6b8cb5a0b7ccae0893485307153
3
+ size 17098086
tokenizer_config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": true,
3
+ "backend": "tokenizers",
4
+ "bos_token": "<s>",
5
+ "cls_token": "<s>",
6
+ "eos_token": "</s>",
7
+ "is_local": false,
8
+ "local_files_only": false,
9
+ "mask_token": "<mask>",
10
+ "model_max_length": 512,
11
+ "pad_token": "<pad>",
12
+ "sep_token": "</s>",
13
+ "tokenizer_class": "XLMRobertaTokenizer",
14
+ "unk_token": "<unk>"
15
+ }
training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4a122d27f1f59b9126e9fc35e39012fcfdb8290578d107d6c0335ce9c4c1540e
3
+ size 5201