DeDeckerThomas commited on
Commit
c69a9c7
β€’
1 Parent(s): 03b17bd

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +219 -0
README.md CHANGED
@@ -1,3 +1,222 @@
1
  ---
 
 
2
  license: mit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+
3
+ language: en
4
  license: mit
5
+ tags:
6
+ - keyphrase-extraction
7
+ datasets:
8
+ - midas/kpcrowd
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-kbir-inspec
18
+ results:
19
+ - task:
20
+ type: keyphrase-extraction
21
+ name: Keyphrase Extraction
22
+ dataset:
23
+ type: midas/kpcrowd
24
+ name: kpcrowd
25
+ metrics:
26
+ - type: seqeval
27
+ value: 0.427
28
+ name: F1-score
29
  ---
30
+ # πŸ”‘ Keyphrase Extraction model: KBIR-inspec
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 KBIR model on the KPCrowd dataset. KBIR or Keyphrase Boundary Infilling with Replacement is a pre-trained model which utilizes a multi-task learning setup for optimizing a combined loss of Masked Language Modeling (MLM), Keyphrase Boundary Infilling (KBI) and Keyphrase Replacement Classification (KRC).
38
+ You can find more information about the architecture in this paper: https://arxiv.org/abs/2112.08547.
39
+
40
+ The model is fine-tuned as a token classification problem where the text is labeled using the BIO scheme.
41
+
42
+ | Label | Description |
43
+ | ----- | ------------------------------- |
44
+ | B-KEY | At the beginning of a keyphrase |
45
+ | I-KEY | Inside a keyphrase |
46
+ | O | Outside a keyphrase |
47
+
48
+ Kulkarni, Mayank, Debanjan Mahata, Ravneet Arora, and Rajarshi Bhowmik. "Learning Rich Representation of Keyphrases from Text." arXiv preprint arXiv:2112.08547 (2021).
49
+
50
+ Sahrawat, Dhruva, Debanjan Mahata, Haimin Zhang, Mayank Kulkarni, Agniv Sharma, Rakesh Gosangi, Amanda Stent, Yaman Kumar, Rajiv Ratn Shah, and Roger Zimmermann. "Keyphrase extraction as sequence labeling using contextualized embeddings." In European Conference on Information Retrieval, pp. 328-335. Springer, Cham, 2020.
51
+
52
+ ## βœ‹ Intended uses & limitations
53
+ ### πŸ›‘ Limitations
54
+ * This keyphrase extraction model is very dataset-specific. It's not recommended to use this model for other domains, but you are free to test it out.
55
+ * Only works for English documents.
56
+ * For a custom model, please consult the training notebook for more information (link incoming).
57
+
58
+ ### ❓ How to use
59
+ ```python
60
+ from transformers import (
61
+ TokenClassificationPipeline,
62
+ AutoModelForTokenClassification,
63
+ AutoTokenizer,
64
+ )
65
+ from transformers.pipelines import AggregationStrategy
66
+ import numpy as np
67
+
68
+ # Define keyphrase extraction pipeline
69
+ class KeyphraseExtractionPipeline(TokenClassificationPipeline):
70
+ def __init__(self, model, *args, **kwargs):
71
+ super().__init__(
72
+ model=AutoModelForTokenClassification.from_pretrained(model),
73
+ tokenizer=AutoTokenizer.from_pretrained(model),
74
+ *args,
75
+ **kwargs
76
+ )
77
+
78
+ def postprocess(self, model_outputs):
79
+ results = super().postprocess(
80
+ model_outputs=model_outputs,
81
+ aggregation_strategy=AggregationStrategy.SIMPLE,
82
+ )
83
+ return np.unique([result.get("word").strip() for result in results])
84
+
85
+ ```
86
+
87
+ ```python
88
+ # Load pipeline
89
+ model_name = "DeDeckerThomas/keyphrase-extraction-kbir-kpcrowd"
90
+ extractor = KeyphraseExtractionPipeline(model=model_name)
91
+ ```
92
+ ```python
93
+ # Inference
94
+ text = """
95
+ Keyphrase extraction is a technique in text analysis where you extract the important keyphrases from a text.
96
+ Since this is a time-consuming process, Artificial Intelligence is used to automate it.
97
+ Currently, classical machine learning methods, that use statistics and linguistics, are widely used for the extraction process.
98
+ The fact that these methods have been widely used in the community has the advantage that there are many easy-to-use libraries.
99
+ Now with the recent innovations in deep learning methods (such as recurrent neural networks and transformers, GANS, …),
100
+ keyphrase extraction can be improved. These new methods also focus on the semantics and context of a document, which is quite an improvement.
101
+ """.replace(
102
+ "\n", ""
103
+ )
104
+
105
+ keyphrases = extractor(text)
106
+
107
+ print(keyphrases)
108
+ ```
109
+
110
+ ```
111
+ # Output
112
+ ['Artificial Intelligence' 'GANS' 'Keyphrase extraction'
113
+ 'classical machine learning' 'deep learning methods'
114
+ 'keyphrase extraction' 'linguistics' 'recurrent neural networks'
115
+ 'semantics' 'statistics' 'text analysis' 'transformers']
116
+ ```
117
+
118
+ ## πŸ“š Training Dataset
119
+ KPCrowd is a keyphrase a broadcast news transcription dataset consisting of 500 English broadcast news stories from 10 different categories (art and culture, business, crime, fashion, health, politics us, politics world, science, sports, technology) with 50 docs per category. This dataset is annotated by multiple annotators that were required to look at the same news story and assign a set of keyphrases from the text itself.
120
+
121
+ You can find more information here: https://huggingface.co/datasets/midas/kpcrowd and https://github.com/LIAAD/KeywordExtractor-Datasets.
122
+
123
+ ## πŸ‘·β€β™‚οΈ Training procedure
124
+ For more in detail information, you can take a look at the training notebook (link incoming).
125
+
126
+ ### Training parameters
127
+
128
+ | Parameter | Value |
129
+ | --------- | ------|
130
+ | Learning Rate | 1e-4 |
131
+ | Epochs | 50 |
132
+ | Early Stopping Patience | 3 |
133
+
134
+ ### Preprocessing
135
+ 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.
136
+ ```python
137
+ # Labels
138
+ label_list = ["B", "I", "O"]
139
+ lbl2idx = {"B": 0, "I": 1, "O": 2}
140
+ idx2label = {0: "B", 1: "I", 2: "O"}
141
+
142
+ def preprocess_fuction(all_samples_per_split):
143
+ tokenized_samples = tokenizer.batch_encode_plus(
144
+ all_samples_per_split[dataset_document_column],
145
+ padding="max_length",
146
+ truncation=True,
147
+ is_split_into_words=True,
148
+ max_length=max_length,
149
+ )
150
+ total_adjusted_labels = []
151
+ for k in range(0, len(tokenized_samples["input_ids"])):
152
+ prev_wid = -1
153
+ word_ids_list = tokenized_samples.word_ids(batch_index=k)
154
+ existing_label_ids = all_samples_per_split[dataset_biotags_column][k]
155
+ i = -1
156
+ adjusted_label_ids = []
157
+
158
+ for wid in word_ids_list:
159
+ if wid is None:
160
+ adjusted_label_ids.append(lbl2idx["O"])
161
+ elif wid != prev_wid:
162
+ i = i + 1
163
+ adjusted_label_ids.append(lbl2idx[existing_label_ids[i]])
164
+ prev_wid = wid
165
+ else:
166
+ adjusted_label_ids.append(
167
+ lbl2idx[
168
+ f"{'I' if existing_label_ids[i] == 'B' else existing_label_ids[i]}"
169
+ ]
170
+ )
171
+
172
+ total_adjusted_labels.append(adjusted_label_ids)
173
+ tokenized_samples["labels"] = total_adjusted_labels
174
+ return tokenized_samples
175
+ ```
176
+
177
+ ### Postprocessing
178
+ 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.
179
+ ```python
180
+ # Define post_process functions
181
+ def concat_tokens_by_tag(keyphrases):
182
+ keyphrase_tokens = []
183
+ for id, label in keyphrases:
184
+ if label == "B":
185
+ keyphrase_tokens.append([id])
186
+ elif label == "I":
187
+ if len(keyphrase_tokens) > 0:
188
+ keyphrase_tokens[len(keyphrase_tokens) - 1].append(id)
189
+ return keyphrase_tokens
190
+
191
+
192
+ def extract_keyphrases(example, predictions, tokenizer, index=0):
193
+ keyphrases_list = [
194
+ (id, idx2label[label])
195
+ for id, label in zip(
196
+ np.array(example["input_ids"]).squeeze().tolist(), predictions[index]
197
+ )
198
+ if idx2label[label] in ["B", "I"]
199
+ ]
200
+
201
+ processed_keyphrases = concat_tokens_by_tag(keyphrases_list)
202
+ extracted_kps = tokenizer.batch_decode(
203
+ processed_keyphrases,
204
+ skip_special_tokens=True,
205
+ clean_up_tokenization_spaces=True,
206
+ )
207
+ return np.unique([kp.strip() for kp in extracted_kps])
208
+
209
+ ```
210
+ ## πŸ“ Evaluation results
211
+
212
+ 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.
213
+ The model achieves the following results on the Inspec test set:
214
+
215
+ | Dataset | P@5 | R@5 | F1@5 | P@10 | R@10 | F1@10 | P@M | R@M | F1@M |
216
+ |:-----------------:|:----:|:----:|:----:|:----:|:----:|:-----:|:----:|:----:|:----:|
217
+ | Inspec Test Set | 0.47 | 0.07 | 0.12 | 0.46 | 0.13 | 0.20 | 0.37 | 0.33 | 0.33 |
218
+
219
+ For more information on the evaluation process, you can take a look at the keyphrase extraction evaluation notebook.
220
+
221
+ ## 🚨 Issues
222
+ Please feel free to contact Thomas De Decker for any problems with this model.