Commit
•
b2c41ee
1
Parent(s):
4fc4f9a
Upload README.md with huggingface_hub
Browse files
README.md
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
---
|
3 |
+
---
|
4 |
+
license: mit
|
5 |
+
language:
|
6 |
+
- en
|
7 |
+
tags:
|
8 |
+
- zero-shot-classification
|
9 |
+
- text-classification
|
10 |
+
- pytorch
|
11 |
+
metrics:
|
12 |
+
- accuracy
|
13 |
+
- f1-score
|
14 |
+
---
|
15 |
+
# poltextlab/xlm-roberta-large-english-media-cap-v3
|
16 |
+
## Model description
|
17 |
+
An `xlm-roberta-large` model finetuned on english training data containing texts of the `media` 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-english-media-cap-v3',
|
50 |
+
num_labels=23,
|
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-english-media-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 37345 examples (10% of the available data).<br>
|
92 |
+
Model accuracy is **0.78**.
|
93 |
+
| label | precision | recall | f1-score | support |
|
94 |
+
|:-------------|------------:|---------:|-----------:|----------:|
|
95 |
+
| 0 | 0.81 | 0.73 | 0.77 | 1705 |
|
96 |
+
| 1 | 0.76 | 0.58 | 0.66 | 1075 |
|
97 |
+
| 2 | 0.88 | 0.81 | 0.84 | 2167 |
|
98 |
+
| 3 | 0.8 | 0.73 | 0.77 | 473 |
|
99 |
+
| 4 | 0.67 | 0.64 | 0.66 | 801 |
|
100 |
+
| 5 | 0.83 | 0.89 | 0.86 | 2001 |
|
101 |
+
| 6 | 0.77 | 0.85 | 0.81 | 1274 |
|
102 |
+
| 7 | 0.84 | 0.83 | 0.84 | 1064 |
|
103 |
+
| 8 | 0.68 | 0.63 | 0.65 | 203 |
|
104 |
+
| 9 | 0.8 | 0.87 | 0.83 | 1796 |
|
105 |
+
| 10 | 0.79 | 0.75 | 0.77 | 3377 |
|
106 |
+
| 11 | 0.81 | 0.66 | 0.73 | 548 |
|
107 |
+
| 12 | 0.72 | 0.69 | 0.7 | 1007 |
|
108 |
+
| 13 | 0.81 | 0.72 | 0.76 | 2796 |
|
109 |
+
| 14 | 0.75 | 0.77 | 0.76 | 3138 |
|
110 |
+
| 15 | 0.78 | 0.67 | 0.72 | 759 |
|
111 |
+
| 16 | 0.79 | 0.67 | 0.72 | 515 |
|
112 |
+
| 17 | 0.75 | 0.84 | 0.8 | 4618 |
|
113 |
+
| 18 | 0.79 | 0.86 | 0.82 | 6475 |
|
114 |
+
| 19 | 0.7 | 0.55 | 0.61 | 559 |
|
115 |
+
| 20 | 0.67 | 0.69 | 0.68 | 981 |
|
116 |
+
| 21 | 0 | 0 | 0 | 13 |
|
117 |
+
| macro avg | 0.74 | 0.7 | 0.72 | 37345 |
|
118 |
+
| weighted avg | 0.78 | 0.78 | 0.78 | 37345 |
|
119 |
+
|
120 |
+
## Inference platform
|
121 |
+
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.
|
122 |
+
|
123 |
+
## Cooperation
|
124 |
+
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).
|
125 |
+
|
126 |
+
## Debugging and issues
|
127 |
+
This architecture uses the `sentencepiece` tokenizer. In order to run the model before `transformers==4.27` you need to install it manually.
|
128 |
+
|
129 |
+
If you encounter a `RuntimeError` when loading the model using the `from_pretrained()` method, adding `ignore_mismatched_sizes=True` should solve the issue.
|