DeDeckerThomas commited on
Commit
bc2d179
1 Parent(s): e7c6323

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +212 -3
README.md CHANGED
@@ -1,3 +1,212 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+
3
+ language: en
4
+ license: mit
5
+ tags:
6
+ - keyphrase-extraction
7
+ datasets:
8
+ - midas/kptimes
9
+ metrics:
10
+ - seqeval
11
+ widget:
12
+ - text: "Keyphrase extraction is a technique in text analysis where you extract the important keyphrases from a text. Since this is a time-consuming process, Artificial Intelligence is used to automate it. Currently, classical machine learning methods, that use statistics and linguistics, are widely used for the extraction process. The fact that these methods have been widely used in the community has the advantage that there are many easy-to-use libraries. Now with the recent innovations in deep learning methods (such as recurrent neural networks and transformers, GANS, …), keyphrase extraction can be improved. These new methods also focus on the semantics and context of a document, which is quite an improvement."
13
+ example_title: "Example 1"
14
+ - text: "In this work, we explore how to learn task specific language models aimed towards learning rich representation of keyphrases from text documents. We experiment with different masking strategies for pre-training transformer language models (LMs) in discriminative as well as generative settings. In the discriminative setting, we introduce a new pre-training objective - Keyphrase Boundary Infilling with Replacement (KBIR), showing large gains in performance (up to 9.26 points in F1) over SOTA, when LM pre-trained using KBIR is fine-tuned for the task of keyphrase extraction. In the generative setting, we introduce a new pre-training setup for BART - KeyBART, that reproduces the keyphrases related to the input text in the CatSeq format, instead of the denoised original input. This also led to gains in performance (up to 4.33 points inF1@M) over SOTA for keyphrase generation. Additionally, we also fine-tune the pre-trained language models on named entity recognition(NER), question answering (QA), relation extraction (RE), abstractive summarization and achieve comparable performance with that of the SOTA, showing that learning rich representation of keyphrases is indeed beneficial for many other fundamental NLP tasks."
15
+ example_title: "Example 2"
16
+ model-index:
17
+ - name: DeDeckerThomas/keyphrase-extraction-distilbert-kptimes
18
+ results:
19
+ - task:
20
+ type: keyphrase-extraction
21
+ name: Keyphrase Extraction
22
+ dataset:
23
+ type: midas/kptimes
24
+ name: kptimes
25
+ metrics:
26
+ - type: seqeval
27
+ value: 0.539
28
+ name: F1-score
29
+ ---
30
+ # 🔑 Keyphrase Extraction model: distilbert-kptimes
31
+ Keyphrase extraction is a technique in text analysis where you extract the important keyphrases from a text. Since this is a time-consuming process, Artificial Intelligence is used to automate it.
32
+ Currently, classical machine learning methods, that use statistics and linguistics, are widely used for the extraction process. The fact that these methods have been widely used in the community has the advantage that there are many easy-to-use libraries.
33
+ Now with the recent innovations in deep learning methods (such as recurrent neural networks and transformers, GANS, …), keyphrase extraction can be improved. These new methods also focus on the semantics and context of a document, which is quite an improvement.
34
+
35
+
36
+ ## 📓 Model Description
37
+ This model is a fine-tuned distilbert model on the kptimes dataset. More information can be found here: https://huggingface.co/distilbert-base-uncased.
38
+
39
+ The model is fine-tuned as a token classification problem where the text is labeled using the BIO scheme.
40
+
41
+ | Label | Description |
42
+ | ----- | ------------------------------- |
43
+ | B-KEY | At the beginning of a keyphrase |
44
+ | I-KEY | Inside a keyphrase |
45
+ | O | Outside a keyphrase |
46
+
47
+ ## ✋ Intended uses & limitations
48
+ ### 🛑 Limitations
49
+ <!--* This keyphrase extraction model is very domain-specific and will perform very well on abstracts of scientific papers. It's not recommended to use this model for other domains, but you are free to test it out.-->
50
+ * Only works for English documents.
51
+ * For a custom model, please consult the training notebook for more information (link incoming).
52
+
53
+ ### ❓ How to use
54
+ ```python
55
+ from transformers import (
56
+ TokenClassificationPipeline,
57
+ AutoModelForTokenClassification,
58
+ AutoTokenizer,
59
+ )
60
+ from transformers.pipelines import AggregationStrategy
61
+ import numpy as np
62
+
63
+ # Define keyphrase extraction pipeline
64
+ class KeyphraseExtractionPipeline(TokenClassificationPipeline):
65
+ def __init__(self, model, *args, **kwargs):
66
+ super().__init__(
67
+ model=AutoModelForTokenClassification.from_pretrained(model),
68
+ tokenizer=AutoTokenizer.from_pretrained(model),
69
+ *args,
70
+ **kwargs
71
+ )
72
+
73
+ def postprocess(self, model_outputs):
74
+ results = super().postprocess(
75
+ model_outputs=model_outputs,
76
+ aggregation_strategy=AggregationStrategy.SIMPLE,
77
+ )
78
+ return np.unique([result.get("word").strip() for result in results])
79
+
80
+ ```
81
+
82
+ ```python
83
+ # Load pipeline
84
+ model_name = "DeDeckerThomas/keyphrase-extraction-distilbert-kptimes"
85
+ extractor = KeyphraseExtractionPipeline(model=model_name)
86
+ ```
87
+ ```python
88
+ # Inference
89
+ text = """
90
+ Keyphrase extraction is a technique in text analysis where you extract the important keyphrases from a text.
91
+ Since this is a time-consuming process, Artificial Intelligence is used to automate it.
92
+ Currently, classical machine learning methods, that use statistics and linguistics, are widely used for the extraction process.
93
+ The fact that these methods have been widely used in the community has the advantage that there are many easy-to-use libraries.
94
+ Now with the recent innovations in deep learning methods (such as recurrent neural networks and transformers, GANS, …),
95
+ keyphrase extraction can be improved. These new methods also focus on the semantics and context of a document, which is quite an improvement.
96
+ """.replace(
97
+ "\n", ""
98
+ )
99
+
100
+ keyphrases = extractor(text)
101
+
102
+ print(keyphrases)
103
+ ```
104
+
105
+ ```
106
+ # Output
107
+ ['Artificial Intelligence' 'GANS' 'Keyphrase extraction'
108
+ 'classical machine learning' 'deep learning methods'
109
+ 'keyphrase extraction' 'linguistics' 'recurrent neural networks'
110
+ 'semantics' 'statistics' 'text analysis' 'transformers']
111
+ ```
112
+
113
+ ## 📚 Training Dataset
114
+ KPTimes is a keyphrase extraction/generation dataset consisting of 279,923 news articles from NY Times and 10K from JPTimes and annotated by professional indexers or editors.
115
+
116
+ You can find more information here: https://huggingface.co/datasets/midas/kptimes
117
+
118
+ ## 👷‍♂️ Training procedure
119
+ For more in detail information, you can take a look at the training notebook (link incoming).
120
+
121
+ ### Training parameters
122
+
123
+ | Parameter | Value |
124
+ | --------- | ------|
125
+ | Learning Rate | 1e-4 |
126
+ | Epochs | 50 |
127
+ | Early Stopping Patience | 3 |
128
+
129
+ ### Preprocessing
130
+ The documents in the dataset are already preprocessed into list of words with the corresponding labels. The only thing that must be done is tokenization and the realignment of the labels so that they correspond with the right subword tokens.
131
+ ```python
132
+ def preprocess_fuction(all_samples_per_split):
133
+ tokenized_samples = tokenizer.batch_encode_plus(
134
+ all_samples_per_split[dataset_document_column],
135
+ padding="max_length",
136
+ truncation=True,
137
+ is_split_into_words=True,
138
+ max_length=max_length,
139
+ )
140
+ total_adjusted_labels = []
141
+ for k in range(0, len(tokenized_samples["input_ids"])):
142
+ prev_wid = -1
143
+ word_ids_list = tokenized_samples.word_ids(batch_index=k)
144
+ existing_label_ids = all_samples_per_split[dataset_biotags_column][k]
145
+ i = -1
146
+ adjusted_label_ids = []
147
+
148
+ for wid in word_ids_list:
149
+ if wid is None:
150
+ adjusted_label_ids.append(lbl2idx["O"])
151
+ elif wid != prev_wid:
152
+ i = i + 1
153
+ adjusted_label_ids.append(lbl2idx[existing_label_ids[i]])
154
+ prev_wid = wid
155
+ else:
156
+ adjusted_label_ids.append(
157
+ lbl2idx[
158
+ f"{'I' if existing_label_ids[i] == 'B' else existing_label_ids[i]}"
159
+ ]
160
+ )
161
+
162
+ total_adjusted_labels.append(adjusted_label_ids)
163
+ tokenized_samples["labels"] = total_adjusted_labels
164
+ return tokenized_samples
165
+ ```
166
+
167
+ ### Postprocessing
168
+ For the post-processing, you will need to filter out the B and I labeled tokens and concat the consecutive B and Is. As last you strip the keyphrase to ensure all spaces are removed.
169
+ ```python
170
+ # Define post_process functions
171
+ def concat_tokens_by_tag(keyphrases):
172
+ keyphrase_tokens = []
173
+ for id, label in keyphrases:
174
+ if label == "B":
175
+ keyphrase_tokens.append([id])
176
+ elif label == "I":
177
+ if len(keyphrase_tokens) > 0:
178
+ keyphrase_tokens[len(keyphrase_tokens) - 1].append(id)
179
+ return keyphrase_tokens
180
+
181
+
182
+ def extract_keyphrases(example, predictions, tokenizer, index=0):
183
+ keyphrases_list = [
184
+ (id, idx2label[label])
185
+ for id, label in zip(
186
+ np.array(example["input_ids"]).squeeze().tolist(), predictions[index]
187
+ )
188
+ if idx2label[label] in ["B", "I"]
189
+ ]
190
+
191
+ processed_keyphrases = concat_tokens_by_tag(keyphrases_list)
192
+ extracted_kps = tokenizer.batch_decode(
193
+ processed_keyphrases,
194
+ skip_special_tokens=True,
195
+ clean_up_tokenization_spaces=True,
196
+ )
197
+ return np.unique([kp.strip() for kp in extracted_kps])
198
+
199
+ ```
200
+ ## 📝 Evaluation results
201
+
202
+ One of the traditional evaluation methods is the precision, recall and F1-score @k,m where k is the number that stands for the first k predicted keyphrases and m for the average amount of predicted keyphrases.
203
+ The model achieves the following results on the KPTimes test set:
204
+
205
+ | Dataset | P@5 | R@5 | F1@5 | P@10 | R@10 | F1@10 | P@M | R@M | F1@M |
206
+ |:-----------------:|:----:|:----:|:----:|:----:|:----:|:-----:|:----:|:----:|:----:|
207
+ | KPTimes Test Set | 0.19 | 0.36 | 0.23 | 0.10 | 0.37 | 0.15 | 0.35 | 0.37 | 0.33 |
208
+
209
+ For more information on the evaluation process, you can take a look at the keyphrase extraction evaluation notebook.
210
+
211
+ ## 🚨 Issues
212
+ Please feel free to contact Thomas De Decker for any problems with this model.