pierreguillou commited on
Commit
3cd97ba
1 Parent(s): b691002

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +202 -0
README.md ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - pt
4
+ tags:
5
+ - text2text-generation
6
+ - t5
7
+ - pytorch
8
+ - qa
9
+ datasets:
10
+ - squad
11
+ - squad_v1_pt
12
+ metrics:
13
+ - precision
14
+ - recall
15
+ - f1
16
+ - accuracy
17
+ - squad
18
+ model-index:
19
+ - name: checkpoints
20
+ results:
21
+ - task:
22
+ name: text2text-generation
23
+ type: text2text-generation
24
+ dataset:
25
+ name: squad
26
+ type: squad
27
+ metrics:
28
+ - name: f1
29
+ type: f1
30
+ value: 79.3
31
+ - name: exact-match
32
+ type: exact-match
33
+ value: 67.3983
34
+ widget:
35
+ - text: "question: Quando começou a pandemia de Covid-19 no mundo? context: A pandemia de COVID-19, também conhecida como pandemia de coronavírus, é uma pandemia em curso de COVID-19, uma doença respiratória aguda causada pelo coronavírus da síndrome respiratória aguda grave 2 (SARS-CoV-2). A doença foi identificada pela primeira vez em Wuhan, na província de Hubei, República Popular da China, em 1 de dezembro de 2019, mas o primeiro caso foi reportado em 31 de dezembro do mesmo ano."
36
+ - text: "question: Onde foi descoberta a Covid-19? context: A pandemia de COVID-19, também conhecida como pandemia de coronavírus, é uma pandemia em curso de COVID-19, uma doença respiratória aguda causada pelo coronavírus da síndrome respiratória aguda grave 2 (SARS-CoV-2). A doença foi identificada pela primeira vez em Wuhan, na província de Hubei, República Popular da China, em 1 de dezembro de 2019, mas o primeiro caso foi reportado em 31 de dezembro do mesmo ano."
37
+ ---
38
+
39
+ # T5 base finetuned for Question Answering (QA) on SQUaD v1.1 Portuguese
40
+
41
+ ![Exemple of what can do with a T5 model (for example: Question Answering finetuned on SQUAD v1.1 in Portuguese)]()
42
+
43
+ ## Introduction
44
+
45
+ **t5-base-qa-squad-v1.1-portuguese** is a QA model (Question Answering) in Portuguese that was finetuned on 26/01/2022 in Google Colab from the model [unicamp-dl/ptt5-base-portuguese-vocab](https://huggingface.co/unicamp-dl/ptt5-base-portuguese-vocab) of Neuralmind on the dataset SQUAD v1.1 in portuguese from the [Deep Learning Brasil group](http://www.deeplearningbrasil.com.br/) by using a Test2Text-Generation objective.
46
+
47
+ Due to the small size of T5 base and finetuning dataset, the model overfitted before to reach the end of training. Here are the overall final metrics on the validation dataset:
48
+ - **f1**: 79.3
49
+ - **exact_match**: 67.3983
50
+
51
+ Check our other QA models in Portuguese finetuned on SQUAD v1.1:
52
+ - [Portuguese BERT base cased QA](https://huggingface.co/pierreguillou/bert-base-cased-squad-v1.1-portuguese)
53
+ - [Portuguese BERT large cased QA](https://huggingface.co/pierreguillou/bert-large-cased-squad-v1.1-portuguese)
54
+ - [Portuguese ByT5 small QA](https://huggingface.co/pierreguillou/byt5-small-qa-squad-v1.1-portuguese)
55
+
56
+ ## Blog post
57
+
58
+ [NLP nas empresas | Como eu treinei um modelo T5 na tarefa QA em português no Google Colab]() (26/01/2022)
59
+
60
+ ## Widget & App
61
+
62
+ You can test this model into the widget of this page.
63
+
64
+ Use as well the [QA App | T5 base pt](https://huggingface.co/spaces/pierreguillou/question-answering-portuguese-t5-base) that allows using the model T5 base finetuned on the QA task with the SQuAD v1.1 pt dataset.
65
+
66
+ ## Using the model for inference in production
67
+ ````
68
+ # install pytorch: check https://pytorch.org/
69
+ # !pip install transformers
70
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
71
+
72
+ # model & tokenizer
73
+ model_name = "t5-base-qa-squad-v1.1-portuguese"
74
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
75
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
76
+
77
+ # parameters
78
+ max_target_length=32
79
+ num_beams=1
80
+ early_stopping=True
81
+
82
+ input_text = 'question: Quando foi descoberta a Covid-19? context: A pandemia de COVID-19, também conhecida como pandemia de coronavírus, é uma pandemia em curso de COVID-19, uma doença respiratória aguda causada pelo coronavírus da síndrome respiratória aguda grave 2 (SARS-CoV-2). A doença foi identificada pela primeira vez em Wuhan, na província de Hubei, República Popular da China, em 1 de dezembro de 2019, mas o primeiro caso foi reportado em 31 de dezembro do mesmo ano.'
83
+ label = '1 de dezembro de 2019'
84
+
85
+ inputs = tokenizer(input_text, return_tensors="pt")
86
+
87
+ outputs = model.generate(inputs["input_ids"],
88
+ max_length=max_target_length,
89
+ num_beams=num_beams,
90
+ early_stopping=early_stopping
91
+ )
92
+ pred = tokenizer.decode(outputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
93
+
94
+ print('true answer |', label)
95
+ print('pred |', pred)
96
+ ````
97
+ You can use pipeline, too. However, it seems to have an issue regarding to the max_length of the input sequence.
98
+ ````
99
+ !pip install transformers
100
+ import transformers
101
+ from transformers import pipeline
102
+
103
+ # model
104
+ model_name = "t5-base-qa-squad-v1.1-portuguese"
105
+
106
+ # parameters
107
+ max_target_length=32
108
+ num_beams=1
109
+ early_stopping=True
110
+ clean_up_tokenization_spaces=True
111
+
112
+ input_text = 'question: Quando foi descoberta a Covid-19? context: A pandemia de COVID-19, também conhecida como pandemia de coronavírus, é uma pandemia em curso de COVID-19, uma doença respiratória aguda causada pelo coronavírus da síndrome respiratória aguda grave 2 (SARS-CoV-2). A doença foi identificada pela primeira vez em Wuhan, na província de Hubei, República Popular da China, em 1 de dezembro de 2019, mas o primeiro caso foi reportado em 31 de dezembro do mesmo ano.'
113
+ label = '1 de dezembro de 2019'
114
+
115
+ text2text = pipeline(
116
+ "text2text-generation",
117
+ model=model_name,
118
+ max_length=max_target_length,
119
+ num_beams=num_beams,
120
+ early_stopping=early_stopping,
121
+ clean_up_tokenization_spaces=clean_up_tokenization_spaces
122
+ )
123
+
124
+ pred = text2text(input_text)
125
+
126
+ print('true answer |', label)
127
+ print('pred |', pred)
128
+ ````
129
+ ## Training procedure
130
+
131
+ ### Notebook
132
+
133
+ The notebook of finetuning ([HuggingFace_Notebook_ptt5-base-portuguese-vocab_question_answering_QA_squad_v11_pt.ipynb]()) is in github.
134
+
135
+ ### Hyperparameters
136
+
137
+ # do training and evaluation
138
+ do_train = True
139
+ do_eval= True
140
+
141
+ # batch
142
+ batch_size = 4
143
+ gradient_accumulation_steps = 3
144
+ per_device_train_batch_size = batch_size
145
+ per_device_eval_batch_size = per_device_train_batch_size*16
146
+
147
+ # LR, wd, epochs
148
+ learning_rate = 1e-4
149
+ weight_decay = 0.01
150
+ num_train_epochs = 10
151
+ fp16 = True
152
+
153
+ # logs
154
+ logging_strategy = "steps"
155
+ logging_first_step = True
156
+ logging_steps = 3000 # if logging_strategy = "steps"
157
+ eval_steps = logging_steps
158
+
159
+ # checkpoints
160
+ evaluation_strategy = logging_strategy
161
+ save_strategy = logging_strategy
162
+ save_steps = logging_steps
163
+ save_total_limit = 3
164
+
165
+ # best model
166
+ load_best_model_at_end = True
167
+ metric_for_best_model = "f1" #"loss"
168
+ if metric_for_best_model == "loss":
169
+ greater_is_better = False
170
+ else:
171
+ greater_is_better = True
172
+
173
+ # evaluation
174
+ num_beams = 1
175
+
176
+ ### Training results
177
+
178
+ ````
179
+ Num examples = 87510
180
+ Num Epochs = 10
181
+ Instantaneous batch size per device = 4
182
+ Total train batch size (w. parallel, distributed & accumulation) = 12
183
+ Gradient Accumulation steps = 3
184
+ Total optimization steps = 72920
185
+
186
+ Step Training Loss Exact Match F1
187
+ 3000 0.776100 61.807001 75.114517
188
+ 6000 0.545900 65.260170 77.468930
189
+ 9000 0.460500 66.556291 78.491938
190
+ 12000 0.393400 66.821192 78.745397
191
+ 15000 0.379800 66.603595 78.815515
192
+ 18000 0.298100 67.578051 79.287899
193
+ 21000 0.303100 66.991485 78.979669
194
+ 24000 0.251600 67.275307 78.929923
195
+
196
+ 27000 0.237500 66.972564 79.333612
197
+
198
+ 30000 0.220500 66.915799 79.236574
199
+ 33000 0.182600 67.029328 78.964212
200
+ 36000 0.190600 66.982025 79.086125
201
+
202
+ ````