andreaceto commited on
Commit
2dc819c
·
verified ·
1 Parent(s): 2c854d6

Update how_to_use.md

Browse files
Files changed (1) hide show
  1. how_to_use.md +82 -2
how_to_use.md CHANGED
@@ -126,7 +126,9 @@ For model inference you will need to execute the same steps on new input text.
126
 
127
  ---
128
 
129
- ## Multitask Model definition
 
 
130
  To use the model you will need to define a `multitask_model.py` with the custom model class built upon our base model.
131
 
132
  ```python
@@ -205,4 +207,82 @@ class MultitaskModel(PreTrainedModel):
205
  "intent_logits": intent_logits,
206
  "ner_logits": ner_logits,
207
  }
208
- ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  ---
128
 
129
+ ## Multitask Model
130
+
131
+ ### 1. Multitask Model class
132
  To use the model you will need to define a `multitask_model.py` with the custom model class built upon our base model.
133
 
134
  ```python
 
207
  "intent_logits": intent_logits,
208
  "ner_logits": ner_logits,
209
  }
210
+ ```
211
+
212
+ ---
213
+
214
+ ### 2. Load Tokenizer
215
+
216
+ ```
217
+ model_name = "distilbert-base-uncased"
218
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
219
+ ```
220
+
221
+ ---
222
+
223
+ ### 3. Custom Metrics Function
224
+ This function is essential for a multitask model. It will be called by the Trainer at the end of each epoch to calculate both intent accuracy and NER F1-score.
225
+
226
+ ```python
227
+ def compute_metrics(eval_pred):
228
+ # Unpack predictions and labels
229
+ predictions, label_values = eval_pred
230
+ intent_preds, ner_preds = predictions
231
+ intent_labels, ner_labels = label_values
232
+
233
+ # --- Intent Metrics ---
234
+ intent_preds = np.argmax(intent_preds, axis=1)
235
+ intent_accuracy = accuracy_score(intent_labels, intent_preds)
236
+ intent_f1 = f1_score(intent_labels, intent_preds, average='weighted')
237
+
238
+ # --- NER Metrics ---
239
+ ner_preds = np.argmax(ner_preds, axis=2)
240
+
241
+ # Remove padding tokens (where label is -100) and convert IDs to labels
242
+ true_ner_labels = []
243
+ true_ner_predictions = []
244
+ id2ner = processed_datasets['train'].features['labels'].feature.names
245
+
246
+ for i in range(len(ner_labels)):
247
+ true_labels_row = []
248
+ true_predictions_row = []
249
+ for j in range(len(ner_labels[i])):
250
+ if ner_labels[i][j] != -100:
251
+ true_labels_row.append(id2ner[ner_labels[i][j]])
252
+ true_predictions_row.append(id2ner[ner_preds[i][j]])
253
+ true_ner_labels.append(true_labels_row)
254
+ true_ner_predictions.append(true_predictions_row)
255
+
256
+ ner_f1 = ner_f1_score(true_ner_labels, true_ner_predictions, mode='strict', scheme=IOB2)
257
+
258
+ return {
259
+ "intent_accuracy": intent_accuracy,
260
+ "intent_f1": intent_f1,
261
+ "ner_f1": ner_f1
262
+ }
263
+ ```
264
+
265
+ ---
266
+
267
+ ### 4. Instantiate the model
268
+ We now create an instance of our `MultitaskModel`, passing it a configuration object that includes the number of labels for each head.
269
+
270
+ ```
271
+ # Get label mappings from the dataset features
272
+ id2intent = processed_datasets['train'].features['intent_label'].names
273
+ intent2id = {name: i for i, name in enumerate(id2intent)}
274
+ id2ner = processed_datasets['train'].features['labels'].feature.names
275
+ ner2id = {name: i for i, name in enumerate(id2ner)}
276
+
277
+ # Load the model config and add our custom parameters
278
+ config = AutoConfig.from_pretrained(
279
+ model_name,
280
+ id2label_intent=id2intent,
281
+ label2id_intent=intent2id,
282
+ id2label_ner=id2ner,
283
+ label2id_ner=ner2id
284
+ )
285
+
286
+ # Instantiate our custom model with the new config
287
+ model = MultitaskModel(config, num_intent_labels=len(id2intent), num_ner_labels=len(id2ner))
288
+ ```