poltextlab commited on
Commit
3067ef1
1 Parent(s): 7f61413

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +128 -0
README.md ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ---
3
+ ---
4
+ license: mit
5
+ language:
6
+ - da
7
+ tags:
8
+ - zero-shot-classification
9
+ - text-classification
10
+ - pytorch
11
+ metrics:
12
+ - accuracy
13
+ - f1-score
14
+ ---
15
+ # poltextlab/xlm-roberta-large-danish-parlspeech-cap-v3
16
+ ## Model description
17
+ An `xlm-roberta-large` model finetuned on danish training data containing texts of the `parlspeech` domain labelled with [major topic codes](https://www.comparativeagendas.net/pages/master-codebook) from the [Comparative Agendas Project](https://www.comparativeagendas.net/).
18
+
19
+ ## How to use the model
20
+ #### Loading and tokenizing input data
21
+ ```python
22
+ import pandas as pd
23
+ import numpy as np
24
+ from datasets import Dataset
25
+ from transformers import (AutoModelForSequenceClassification, AutoTokenizer,
26
+ Trainer, TrainingArguments)
27
+
28
+ CAP_NUM_DICT = {0: '1', 1: '2', 2: '3', 3: '4', 4: '5', 5: '6',
29
+ 6: '7', 7: '8', 8: '9', 9: '10', 10: '12', 11: '13', 12: '14',
30
+ 13: '15', 14: '16', 15: '17', 16: '18', 17: '19', 18: '20', 19:
31
+ '21', 20: '23', 21: '999'}
32
+
33
+ tokenizer = AutoTokenizer.from_pretrained('xlm-roberta-large')
34
+ num_labels = len(CAP_NUM_DICT)
35
+
36
+ def tokenize_dataset(data : pd.DataFrame):
37
+ tokenized = tokenizer(data["text"],
38
+ max_length=MAXLEN,
39
+ truncation=True,
40
+ padding="max_length")
41
+ return tokenized
42
+
43
+ hg_data = Dataset.from_pandas(data)
44
+ dataset = hg_data.map(tokenize_dataset, batched=True, remove_columns=hg_data.column_names)
45
+ ```
46
+
47
+ #### Inference using the Trainer class
48
+ ```python
49
+ model = AutoModelForSequenceClassification.from_pretrained('poltextlab/poltextlab/xlm-roberta-large-danish-parlspeech-cap-v3',
50
+ num_labels=21,
51
+ problem_type="multi_label_classification") )
52
+
53
+ training_args = TrainingArguments(
54
+ output_dir='.',
55
+ per_device_train_batch_size=8,
56
+ per_device_eval_batch_size=8
57
+ )
58
+
59
+ trainer = Trainer(
60
+ model=model,
61
+ args=training_args
62
+ )
63
+
64
+ probs = trainer.predict(test_dataset=dataset).predictions
65
+ predicted = pd.DataFrame(np.argmax(probs, axis=1)).replace({0: CAP_NUM_DICT}).rename(
66
+ columns={0: 'predicted'}).reset_index(drop=True)
67
+
68
+ ```
69
+
70
+ ### Fine-tuning procedure
71
+ `poltextlab/xlm-roberta-large-danish-parlspeech-cap-v3` was fine-tuned using the Hugging Face Trainer class with the following hyperparameters:
72
+ ```python
73
+ training_args = TrainingArguments(
74
+ output_dir=f"../model/{model_dir}/tmp/",
75
+ logging_dir=f"../logs/{model_dir}/",
76
+ logging_strategy='epoch',
77
+ num_train_epochs=10,
78
+ per_device_train_batch_size=8,
79
+ per_device_eval_batch_size=8,
80
+ learning_rate=5e-06,
81
+ seed=42,
82
+ save_strategy='epoch',
83
+ evaluation_strategy='epoch',
84
+ save_total_limit=1,
85
+ load_best_model_at_end=True
86
+ )
87
+ ```
88
+ We also incorporated an EarlyStoppingCallback in the process with a patience of 2 epochs.
89
+
90
+ ## Model performance
91
+ The model was evaluated on a test set of 44159 examples (10% of the available data).<br>
92
+ Model accuracy is **0.94**.
93
+ | label | precision | recall | f1-score | support |
94
+ |:-------------|------------:|---------:|-----------:|----------:|
95
+ | 0 | 0.91 | 0.92 | 0.92 | 2310 |
96
+ | 1 | 0.9 | 0.9 | 0.9 | 1285 |
97
+ | 2 | 0.98 | 0.96 | 0.97 | 3400 |
98
+ | 3 | 0.95 | 0.95 | 0.95 | 1972 |
99
+ | 4 | 0.92 | 0.93 | 0.93 | 2679 |
100
+ | 5 | 0.96 | 0.96 | 0.96 | 2778 |
101
+ | 6 | 0.94 | 0.94 | 0.94 | 2458 |
102
+ | 7 | 0.96 | 0.94 | 0.95 | 1173 |
103
+ | 8 | 0.95 | 0.96 | 0.96 | 1948 |
104
+ | 9 | 0.95 | 0.97 | 0.96 | 3276 |
105
+ | 10 | 0.94 | 0.95 | 0.94 | 3224 |
106
+ | 11 | 0.92 | 0.93 | 0.93 | 2270 |
107
+ | 12 | 0.94 | 0.93 | 0.93 | 1510 |
108
+ | 13 | 0.89 | 0.89 | 0.89 | 1759 |
109
+ | 14 | 0.96 | 0.95 | 0.95 | 1941 |
110
+ | 15 | 0.95 | 0.93 | 0.94 | 1343 |
111
+ | 16 | 0.89 | 0.9 | 0.9 | 402 |
112
+ | 17 | 0.95 | 0.94 | 0.95 | 3337 |
113
+ | 18 | 0.92 | 0.92 | 0.92 | 3484 |
114
+ | 19 | 0.95 | 0.95 | 0.95 | 834 |
115
+ | 20 | 0.93 | 0.91 | 0.92 | 776 |
116
+ | macro avg | 0.94 | 0.93 | 0.94 | 44159 |
117
+ | weighted avg | 0.94 | 0.94 | 0.94 | 44159 |
118
+
119
+ ## Inference platform
120
+ This model is used by the [CAP Babel Machine](https://babel.poltextlab.com), an open-source and free natural language processing tool, designed to simplify and speed up projects for comparative research.
121
+
122
+ ## Cooperation
123
+ Model performance can be significantly improved by extending our training sets. We appreciate every submission of CAP-coded corpora (of any domain and language) at poltextlab{at}poltextlab{dot}com or by using the [CAP Babel Machine](https://babel.poltextlab.com).
124
+
125
+ ## Debugging and issues
126
+ This architecture uses the `sentencepiece` tokenizer. In order to run the model before `transformers==4.27` you need to install it manually.
127
+
128
+ If you encounter a `RuntimeError` when loading the model using the `from_pretrained()` method, adding `ignore_mismatched_sizes=True` should solve the issue.