English
Recommendation
Jmilagres commited on
Commit
6139274
1 Parent(s): a8e69a1

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +293 -0
README.md CHANGED
@@ -1,3 +1,296 @@
1
  ---
 
 
 
2
  license: apache-2.0
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ language: en
3
+ tags:
4
+ - Clsssification
5
  license: apache-2.0
6
+ datasets:
7
+ - tensorflow
8
+ - numpy
9
+ - keras
10
+ - pandas
11
+ - openpyxl
12
+ - gensin
13
+ - contractions
14
+ - nltk
15
+ - spacy
16
+ thumbnail: https://github.com/Marcosdib/S2Query/Classification_Architecture_model.png
17
  ---
18
+
19
+ ![MCTIimg](https://antigo.mctic.gov.br/mctic/export/sites/institucional/institucional/entidadesVinculadas/conselhos/pag-old/RODAPE_MCTI.png)
20
+
21
+
22
+ # MCTI Text Classification Task (uncased) DRAFT
23
+
24
+ Disclaimer: The Brazilian Ministry of Science, Technology, and Innovation (MCTI) has partially supported this project.
25
+
26
+ The model [NLP MCTI Classification Multi](https://huggingface.co/spaces/unb-lamfo-nlp-mcti/NLP-W2V-CNN-Multi) is part of the project [Research Financing Product Portfolio (FPP)](https://huggingface.co/unb-lamfo-nlp-mcti) focuses
27
+ on the task of Text Classification and explores different machine learning strategies to classify a small amount
28
+ of long, unstructured, and uneven data to find a proper method with good performance. Pre-training and word embedding
29
+ solutions were used to learn word relationships from other datasets with considerable similarity and larger scale.
30
+ Then, using the acquired resources, based on the dataset available in the MCTI, transfer learning plus deep learning
31
+ models were applied to improve the understanding of each sentence.
32
+
33
+ ## According to the abstract,
34
+
35
+ Compared to the 81% baseline accuracy rate based on available datasets and the 85% accuracy rate achieved using a
36
+ Transformer-based approach, the Word2Vec-based approach improved the accuracy rate to 93%, according to
37
+ ["Using transfer learning to classify long unstructured texts with small amounts of labeled data"](https://www.scitepress.org/Link.aspx?doi=10.5220/0011527700003318).
38
+
39
+ ## Model description
40
+
41
+ Nullam congue hendrerit turpis et facilisis. Cras accumsan ante mi, eu hendrerit nulla finibus at. Donec imperdiet,
42
+ nisi nec pulvinar suscipit, dolor nulla sagittis massa, et vehicula ante felis quis nibh. Lorem ipsum dolor sit amet,
43
+ consectetur adipiscing elit. Maecenas viverra tempus risus non ornare. Donec in vehicula est. Pellentesque vulputate
44
+ bibendum cursus. Nunc volutpat vitae neque ut bibendum:
45
+
46
+ - Nullam congue hendrerit turpis et facilisis. Cras accumsan ante mi, eu hendrerit nulla finibus at. Donec imperdiet,
47
+ nisi nec pulvinar suscipit, dolor nulla sagittis massa, et vehicula ante felis quis nibh. Lorem ipsum dolor sit amet,
48
+ consectetur adipiscing elit.
49
+ - Nullam congue hendrerit turpis et facilisis. Cras accumsan ante mi, eu hendrerit nulla finibus at. Donec imperdiet,
50
+ nisi nec pulvinar suscipit, dolor nulla sagittis massa, et vehicula ante felis quis nibh. Lorem ipsum dolor sit amet,
51
+ consectetur adipiscing elit.
52
+
53
+ Nullam congue hendrerit turpis et facilisis. Cras accumsan ante mi, eu hendrerit nulla finibus at. Donec imperdiet,
54
+ nisi nec pulvinar suscipit, dolor nulla sagittis massa, et vehicula ante felis quis nibh. Lorem ipsum dolor sit amet,
55
+ consectetur adipiscing elit. Maecenas viverra tempus risus non ornare. Donec in vehicula est. Pellentesque vulputate
56
+ bibendum cursus. Nunc volutpat vitae neque ut bibendum.
57
+
58
+ ![architeru](https://github.com/marcosdib/S2Query/Classification_Architecture_model.png)
59
+
60
+ ## Model variations
61
+
62
+ With the motivation to increase accuracy obtained with baseline implementation, we implemented a transfer learning
63
+ strategy under the assumption that small data available for training was insufficient for adequate embedding training.
64
+ In this context, we considered two approaches:
65
+
66
+ i) pre-training wordembeddings using similar datasets for text classification;
67
+ ii) using transformers and attention mechanisms (Longformer) to create contextualized embeddings.
68
+
69
+ XXXX has originally been released in base and large variations, for cased and uncased input text. The uncased models
70
+ also strips out an accent markers. Chinese and multilingual uncased and cased versions followed shortly after.
71
+ Modified preprocessing with whole word masking has replaced subpiece masking in a following work, with the release of
72
+ two models.
73
+
74
+ Other 24 smaller models are released afterward.
75
+
76
+ The detailed release history can be found on the [here](https://huggingface.co/unb-lamfo-nlp-mcti) on github.
77
+
78
+ #### Table 1:
79
+ | Model | #params | Language |
80
+ |------------------------------|:-------:|:--------:|
81
+ | [`mcti-base-uncased`] | 110M | English |
82
+ | [`mcti-large-uncased`] | 340M | English |
83
+ | [`mcti-base-cased`] | 110M | English |
84
+ | [`mcti-large-cased`] | 110M | Chinese |
85
+ | [`-base-multilingual-cased`] | 110M | Multiple |
86
+
87
+ #### Table 2:
88
+ | Dataset | Compatibility to base* |
89
+ |--------------------------------------|:----------------------:|
90
+ | Labeled MCTI | 100% |
91
+ | Full MCTI | 100% |
92
+ | BBC News Articles | 56.77% |
93
+ | New unlabeled MCTI | 75.26% |
94
+ ## Intended uses
95
+ You can use the raw model for either masked language modeling or next sentence prediction, but it's mostly intended to
96
+ be fine-tuned on a downstream task. See the [model hub](https://www.google.com) to look for
97
+ fine-tuned versions of a task that interests you.
98
+ Note that this model is primarily aimed at being fine-tuned on tasks that use the whole sentence (potentially masked)
99
+ to make decisions, such as sequence classification, token classification or question answering. For tasks such as text
100
+ generation you should look at model like XXX.
101
+ ### How to use
102
+ You can use this model directly with a pipeline for masked language modeling:
103
+ ```python
104
+ >>> from transformers import pipeline
105
+ >>> unmasker = pipeline('fill-mask', model='bert-base-uncased')
106
+ >>> unmasker("Hello I'm a [MASK] model.")
107
+ [{'sequence': "[CLS] hello i'm a fashion model. [SEP]",
108
+ 'score': 0.1073106899857521,
109
+ 'token': 4827,
110
+ 'token_str': 'fashion'},
111
+ {'sequence': "[CLS] hello i'm a fine model. [SEP]",
112
+ 'score': 0.027095865458250046,
113
+ 'token': 2986,
114
+ 'token_str': 'fine'}]
115
+ ```
116
+ Here is how to use this model to get the features of a given text in PyTorch:
117
+ ```python
118
+ from transformers import BertTokenizer, BertModel
119
+ tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
120
+ model = BertModel.from_pretrained("bert-base-uncased")
121
+ text = "Replace me by any text you'd like."
122
+ encoded_input = tokenizer(text, return_tensors='pt')
123
+ output = model(**encoded_input)
124
+ ```
125
+ and in TensorFlow:
126
+ ```python
127
+ from transformers import BertTokenizer, TFBertModel
128
+ tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
129
+ model = TFBertModel.from_pretrained("bert-base-uncased")
130
+ text = "Replace me by any text you'd like."
131
+ encoded_input = tokenizer(text, return_tensors='tf')
132
+ output = model(encoded_input)
133
+ ```
134
+ ### Limitations and bias
135
+ This model is uncased: it does not make a difference between english
136
+ and English.
137
+ Even if the training data used for this model could be characterized as fairly neutral, this model can have biased
138
+ predictions:
139
+ ```python
140
+ >>> from transformers import pipeline
141
+ >>> unmasker = pipeline('fill-mask', model='bert-base-uncased')
142
+ >>> unmasker("The man worked as a [MASK].")
143
+ [{'sequence': '[CLS] the man worked as a carpenter. [SEP]',
144
+ 'score': 0.09747550636529922,
145
+ 'token': 10533,
146
+ 'token_str': 'carpenter'},
147
+ {'sequence': '[CLS] the man worked as a salesman. [SEP]',
148
+ 'score': 0.037680890411138535,
149
+ 'token': 18968,
150
+ 'token_str': 'salesman'}]
151
+ >>> unmasker("The woman worked as a [MASK].")
152
+ [{'sequence': '[CLS] the woman worked as a nurse. [SEP]',
153
+ 'score': 0.21981462836265564,
154
+ 'token': 6821,
155
+ 'token_str': 'nurse'},
156
+ {'sequence': '[CLS] the woman worked as a cook. [SEP]',
157
+ 'score': 0.03042375110089779,
158
+ 'token': 5660,
159
+ 'token_str': 'cook'}]
160
+ ```
161
+ This bias will also affect all fine-tuned versions of this model.
162
+ ## Training data
163
+ The BERT model was pretrained on [BookCorpus](https://yknzhu.wixsite.com/mbweb), a dataset consisting of 11,038
164
+ unpublished books and [English Wikipedia](https://en.wikipedia.org/wiki/English_Wikipedia) (excluding lists, tables and
165
+ headers).
166
+ ## Training procedure
167
+ ### Preprocessing
168
+ Pre-processing was used to standardize the texts for the English language, reduce the number of insignificant tokens and
169
+ optimize the training of the models.
170
+ The following assumptions were considered:
171
+ - The Data Entry base is obtained from the result of goal 4.
172
+ - Labeling (Goal 4) is considered true for accuracy measurement purposes;
173
+ - Preprocessing experiments compare accuracy in a shallow neural network (SNN);
174
+ - Pre-processing was investigated for the classification goal.
175
+ From the Database obtained in Meta 4, stored in the project's [GitHub](https://github.com/mcti-sefip/mcti-sefip-ppfcd2020/blob/scraps-desenvolvimento/Rotulagem/db_PPF_validacao_para%20UNB_%20FINAL.xlsx), a Notebook was developed in [Google Colab](https://colab.research.google.com)
176
+ to implement the [pre-processing code](https://github.com/mcti-sefip/mcti-sefip-ppfcd2020/blob/pre-processamento/Pre_Processamento/MCTI_PPF_Pr%C3%A9_processamento.ipynb), which also can be found on the project's GitHub.
177
+ Several Python packages were used to develop the preprocessing code:
178
+ #### Table 3: Python packages used
179
+ | Objective | Package |
180
+ |--------------------------------------------------------|--------------|
181
+ | Resolve contractions and slang usage in text | [contractions](https://pypi.org/project/contractions) |
182
+ | Natural Language Processing | [nltk](https://pypi.org/project/nltk) |
183
+ | Others data manipulations and calculations included in Python 3.10: io, json, math, re (regular expressions), shutil, time, unicodedata; | [numpy](https://pypi.org/project/numpy) |
184
+ | Data manipulation and analysis | [pandas](https://pypi.org/project/pandas) |
185
+ | http library | [requests](https://pypi.org/project/requests) |
186
+ | Training model | [scikit-learn](https://pypi.org/project/scikit-learn) |
187
+ | Machine learning | [tensorflow](https://pypi.org/project/tensorflow) |
188
+ | Machine learning | [keras](https://keras.io/) |
189
+ | Translation from multiple languages to English | [translators](https://pypi.org/project/translators) |
190
+ As detailed in the notebook on [GitHub](https://github.com/mcti-sefip/mcti-sefip-ppfcd2020/blob/pre-processamento/Pre_Processamento/MCTI_PPF_Pr%C3%A9_processamento), in the pre-processing, code was created to build and evaluate 8 (eight) different
191
+ bases, derived from the base of goal 4, with the application of the methods shown in Figure 2.
192
+ #### Table 4: Preprocessing methods evaluated
193
+ | id | Experiments |
194
+ |--------|------------------------------------------------------------------------|
195
+ | Base | Original Texts |
196
+ | xp1 | Expand Contractions |
197
+ | xp2 | Expand Contractions + Convert text to lowercase |
198
+ | xp3 | Expand Contractions + Remove Punctuation |
199
+ | xp4 | Expand Contractions + Remove Punctuation + Convert text to lowercase |
200
+ | xp5 | xp4 + Stemming |
201
+ | xp6 | xp4 + Lemmatization |
202
+ | xp7 | xp4 + Stemming + Stopwords Removal |
203
+ | xp8 | ap4 + Lemmatization + Stopwords Removal |
204
+ First, the treatment of punctuation and capitalization was evaluated. This phase resulted in the construction and
205
+ evaluation of the first four bases (xp1, xp2, xp3, xp4).
206
+ Then, the content simplification was evaluated, from the xp4 base, considering stemming (xp5), stemming (xp6),
207
+ stemming + stopwords removal (xp7), and stemming + stopwords removal (xp8).
208
+ All eight bases were evaluated to classify the eligibility of the opportunity, through the training of a shallow
209
+ neural network (SNN – Shallow Neural Network). The metrics for the eight bases were evaluated. The results are
210
+ shown in Table 5.
211
+ #### Table 5: Results obtained in Preprocessing
212
+ | id | Experiment | acurácia | f1-score | recall | precision | Média(s) | N_tokens | max_lenght |
213
+ |--------|------------------------------------------------------------------------|----------|----------|--------|-----------|----------|----------|------------|
214
+ | Base | Original Texts | 89,78% | 84,20% | 79,09% | 90,95% | 417,772 | 23788 | 5636 |
215
+ | xp1 | Expand Contractions | 88,71% | 81,59% | 71,54% | 97,33% | 414,715 | 23768 | 5636 |
216
+ | xp2 | Expand Contractions + Convert text to lowercase | 90,32% | 85,64% | 77,19% | 97,44% | 368,375 | 20322 | 5629 |
217
+ | xp3 | Expand Contractions + Remove Punctuation | 91,94% | 87,73% | 79,66% | 98,72% | 386,650 | 22121 | 4950 |
218
+ | xp4 | Expand Contractions + Remove Punctuation + Convert text to lowercase | 90,86% | 86,61% | 80,85% | 94,25% | 326,830 | 18616 | 4950 |
219
+ | xp5 | xp4 + Stemming | 91,94% | 87,68% | 78,47% | 100,00% | 257,960 | 14319 | 4950 |
220
+ | xp6 | xp4 + Lemmatization | 89,78% | 85,06% | 79,66% | 91,87% | 282,645 | 16194 | 4950 |
221
+ | xp7 | xp4 + Stemming + Stopwords Removal | 92,47% | 88,46% | 79,66% | 100,00% | 210,320 | 14212 | 2817 |
222
+ | xp8 | ap4 + Lemmatization + Stopwords Removal | 92,47% | 88,46% | 79,66% | 100,00% | 225,580 | 16081 | 2726 |
223
+ Even so, between these two excellent options, one can judge which one to choose. XP7: It has less training time,
224
+ less number of unique tokens. XP8: It has smaller maximum sizes. In this case, the criterion used for the choice
225
+ was the computational cost required to train the vector representation models (word-embedding, sentence-embeddings,
226
+ document-embedding). The training time is so close that it did not have such a large weight for the analysis.
227
+ As a last step, a spreadsheet was generated for the model (xp8) with the fields opo_pre and opo_pre_tkn, containing the preprocessed text in sentence format and tokens, respectively. This [database](https://github.com/mcti-sefip/mcti-sefip-ppfcd2020/blob/pre-processamento/Pre_Processamento/oportunidades_final_pre_processado.xlsx) was made
228
+ available on the project's GitHub with the inclusion of columns opo_pre (text) and opo_pre_tkn (tokenized).
229
+ ### Pretraining
230
+ The model was trained on 4 cloud TPUs in Pod configuration (16 TPU chips total) for one million steps with a batch size
231
+ of 256. The sequence length was limited to 128 tokens for 90% of the steps and 512 for the remaining 10%. The optimizer
232
+ used is Adam with a learning rate of 1e-4, \\(\beta_{1} = 0.9\\) and \\(\beta_{2} = 0.999\\), a weight decay of 0.01,
233
+ learning rate warmup for 10,000 steps and linear decay of the learning rate after.
234
+ ## Evaluation results
235
+ ### Model training with Word2Vec embeddings
236
+ Now we have a pre-trained model of word2vec embeddings that has already learned relevant meaningsfor our classification problem.
237
+ We can couple it to our classification models (Fig. 4), realizing transferlearning and then training the model with the labeled
238
+ data in a supervised manner. The new coupled model can be seen in Figure 5 under word2vec model training. The Table 3 shows the
239
+ obtained results with related metrics. With this implementation, we achieved new levels of accuracy with 86% for the CNN
240
+ architecture and 88% for the LSTM architecture.
241
+ #### Table 6: Results from Pre-trained WE + ML models
242
+ | ML Model | Accuracy | F1 Score | Precision | Recall |
243
+ |:--------:|:---------:|:---------:|:---------:|:---------:|
244
+ | NN | 0.8269 | 0.8545 | 0.8392 | 0.8712 |
245
+ | DNN | 0.7115 | 0.7794 | 0.7255 | 0.8485 |
246
+ | CNN | 0.8654 | 0.9083 | 0.8486 | 0.9773 |
247
+ | LSTM | 0.8846 | 0.9139 | 0.9056 | 0.9318 |
248
+ ### Transformer-based implementation
249
+ Another way we used pre-trained vector representations was by use of a Longformer (Beltagy et al., 2020). We chose it because
250
+ of the limitation of the first generation of transformers and BERT-based architectures involving the size of the sentences:
251
+ the maximum of 512 tokens. The reason behind that limitation is that the self-attention mechanism scale quadratically with the
252
+ input sequence length O(n2) (Beltagy et al., 2020). The Longformer allowed the processing sequences of a thousand characters
253
+ without facing the memory bottleneck of BERT-like architectures and achieved SOTA in several benchmarks.
254
+ For our text length distribution in Figure 3, if we used a Bert-based architecture with a maximum length of 512, 99 sentences
255
+ would have to be truncated and probably miss some critical information. By comparison, with the Longformer, with a maximum
256
+ length of 4096, only eight sentences will have their information shortened.
257
+ To apply the Longformer, we used the pre-trained base (available on the link) that was previously trained with a combination
258
+ of vast datasets as input to the model, as shown in figure 5 under Longformer model training. After coupling to our classification
259
+ models, we realized supervised training of the whole model. At this point, only transfer learning was applied since more
260
+ computational power was needed to realize the fine-tuning of the weights. The results with related metrics can be viewed in table 4.
261
+ This approach achieved adequate accuracy scores, above 82% in all implementation architectures.
262
+ #### Table 7: Results from Pre-trained Longformer + ML models
263
+ | ML Model | Accuracy | F1 Score | Precision | Recall |
264
+ |:--------:|:---------:|:---------:|:---------:|:---------:|
265
+ | NN | 0.8269 | 0.8754 |0.7950 | 0.9773 |
266
+ | DNN | 0.8462 | 0.8776 |0.8474 | 0.9123 |
267
+ | CNN | 0.8462 | 0.8776 |0.8474 | 0.9123 |
268
+ | LSTM | 0.8269 | 0.8801 |0.8571 | 0.9091 |
269
+ ## Checkpoints
270
+ - Examples
271
+ - Implementation Notes
272
+ - Usage Example
273
+ - >>>
274
+ - >>> ...
275
+ ## Config
276
+ ## Tokenizer
277
+ ## Benchmarks
278
+ ### BibTeX entry and citation info
279
+ ```bibtex
280
+ @conference{webist22,
281
+ author ={Carlos Rocha. and Marcos Dib. and Li Weigang. and Andrea Nunes. and Allan Faria. and Daniel Cajueiro.
282
+ and Maísa {Kely de Melo}. and Victor Celestino.},
283
+ title ={Using Transfer Learning To Classify Long Unstructured Texts with Small Amounts of Labeled Data},
284
+ booktitle ={Proceedings of the 18th International Conference on Web Information Systems and Technologies - WEBIST,},
285
+ year ={2022},
286
+ pages ={201-213},
287
+ publisher ={SciTePress},
288
+ organization ={INSTICC},
289
+ doi ={10.5220/0011527700003318},
290
+ isbn ={978-989-758-613-2},
291
+ issn ={2184-3252},
292
+ }
293
+ ```
294
+ <a href="https://huggingface.co/exbert/?model=bert-base-uncased">
295
+ <img width="300px" src="https://cdn-media.huggingface.co/exbert/button.png">
296
+ </a>