poltextlab commited on
Commit
bdd94ab
1 Parent(s): a6253c8

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +131 -0
README.md ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ---
3
+ ---
4
+ license: mit
5
+ language:
6
+ - fr
7
+ tags:
8
+ - zero-shot-classification
9
+ - text-classification
10
+ - pytorch
11
+ metrics:
12
+ - accuracy
13
+ - f1-score
14
+ ---
15
+ # poltextlab/xlm-roberta-large-french-cap-v3
16
+ ## Model description
17
+ An `xlm-roberta-large` model finetuned on french training data 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-french-cap-v3',
50
+ num_labels=22,
51
+ problem_type="multi_label_classification",
52
+ ignore_mismatched_sizes=True
53
+ )
54
+
55
+ training_args = TrainingArguments(
56
+ output_dir='.',
57
+ per_device_train_batch_size=8,
58
+ per_device_eval_batch_size=8
59
+ )
60
+
61
+ trainer = Trainer(
62
+ model=model,
63
+ args=training_args
64
+ )
65
+
66
+ probs = trainer.predict(test_dataset=dataset).predictions
67
+ predicted = pd.DataFrame(np.argmax(probs, axis=1)).replace({0: CAP_NUM_DICT}).rename(
68
+ columns={0: 'predicted'}).reset_index(drop=True)
69
+
70
+ ```
71
+
72
+ ### Fine-tuning procedure
73
+ `poltextlab/xlm-roberta-large-french-cap-v3` was fine-tuned using the Hugging Face Trainer class with the following hyperparameters:
74
+ ```python
75
+ training_args = TrainingArguments(
76
+ output_dir=f"../model/{model_dir}/tmp/",
77
+ logging_dir=f"../logs/{model_dir}/",
78
+ logging_strategy='epoch',
79
+ num_train_epochs=10,
80
+ per_device_train_batch_size=8,
81
+ per_device_eval_batch_size=8,
82
+ learning_rate=5e-06,
83
+ seed=42,
84
+ save_strategy='epoch',
85
+ evaluation_strategy='epoch',
86
+ save_total_limit=1,
87
+ load_best_model_at_end=True
88
+ )
89
+ ```
90
+ We also incorporated an EarlyStoppingCallback in the process with a patience of 2 epochs.
91
+
92
+ ## Model performance
93
+ The model was evaluated on a test set of 2280 examples (10% of the available data).<br>
94
+ Model accuracy is **0.71**.
95
+ | label | precision | recall | f1-score | support |
96
+ |:-------------|------------:|---------:|-----------:|----------:|
97
+ | 0 | 0.71 | 0.72 | 0.71 | 200 |
98
+ | 1 | 0.59 | 0.44 | 0.5 | 62 |
99
+ | 2 | 0.82 | 0.74 | 0.78 | 80 |
100
+ | 3 | 0.66 | 0.75 | 0.7 | 64 |
101
+ | 4 | 0.72 | 0.57 | 0.63 | 186 |
102
+ | 5 | 0.75 | 0.76 | 0.76 | 125 |
103
+ | 6 | 0.7 | 0.6 | 0.65 | 85 |
104
+ | 7 | 0.88 | 0.82 | 0.85 | 45 |
105
+ | 8 | 0.7 | 0.74 | 0.72 | 57 |
106
+ | 9 | 0.74 | 0.86 | 0.79 | 58 |
107
+ | 10 | 0.82 | 0.77 | 0.8 | 154 |
108
+ | 11 | 0.55 | 0.65 | 0.59 | 105 |
109
+ | 12 | 0.76 | 0.64 | 0.7 | 87 |
110
+ | 13 | 0.58 | 0.59 | 0.59 | 106 |
111
+ | 14 | 0.8 | 0.8 | 0.8 | 87 |
112
+ | 15 | 0.7 | 0.72 | 0.71 | 46 |
113
+ | 16 | 0.57 | 0.71 | 0.63 | 59 |
114
+ | 17 | 0.64 | 0.79 | 0.71 | 204 |
115
+ | 18 | 0.78 | 0.78 | 0.78 | 359 |
116
+ | 19 | 0 | 0 | 0 | 7 |
117
+ | 20 | 0.76 | 0.7 | 0.73 | 104 |
118
+ | 21 | 0 | 0 | 0 | 0 |
119
+ | macro avg | 0.65 | 0.64 | 0.64 | 2280 |
120
+ | weighted avg | 0.72 | 0.71 | 0.71 | 2280 |
121
+
122
+ ## Inference platform
123
+ 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.
124
+
125
+ ## Cooperation
126
+ 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).
127
+
128
+ ## Debugging and issues
129
+ This architecture uses the `sentencepiece` tokenizer. In order to run the model before `transformers==4.27` you need to install it manually.
130
+
131
+ If you encounter a `RuntimeError` when loading the model using the `from_pretrained()` method, adding `ignore_mismatched_sizes=True` should solve the issue.