Views
No views yet
eval_loss1thainer_training_args = TrainingArguments(
2 output_dir=path.join("finetuned_models", "NN-NER-FT"),
3 overwrite_output_dir=True,
4 evaluation_strategy="steps",
5 eval_steps=100,
6 save_strategy="steps",
7 save_steps=100,
8 save_total_limit=5,
9 per_device_train_batch_size=16,
10 per_device_eval_batch_size=16,
11 learning_rate=3e-5,
12 warmup_ratio=0.1,
13 weight_decay=0.01,
14 adam_beta1=0.9,
15 adam_beta2=0.999,
16 adam_epsilon=1e-8,
17 num_train_epochs=3,
18 fp16=True,
19 load_best_model_at_end=True,
20 metric_for_best_model="eval_loss"
21)1def thainer_f1_metric(eval_pred):
2 predictions = eval_pred.predictions.argmax(axis=2)
3 labels = eval_pred.label_ids
4 predictions = [
5 [thainer_id2label[p] for p, l in zip(p_row, l_row) if l != -100]
6 for p_row, l_row in zip(predictions, labels)
7 ]
8 labels = [
9 [thainer_id2label[l] for l in l_row if l != -100]
10 for l_row in labels
11 ]
12 result = seqeval_metric(y_pred=predictions, y_true=labels, output_dict=True)
13 tag_set = {tag[2:] for tag in thainer_id2label.values() if tag != "O"}
14 return {
15 "micro_average_f1": result["micro avg"]["f1-score"],
16 "macro_average_f1": result["macro avg"]["f1-score"],
17 "class_f1": {
18 tag: result[tag]["f1-score"]
19 for tag in result if tag in tag_set
20 }
21 }
22
23thainer_trainer = Trainer(
24 model=thainer_model,
25 args=thainer_training_args,
26 train_dataset=thainer["train"],
27 eval_dataset=thainer["validation"],
28 tokenizer=tokenizer,
29 data_collator=thainer_data_collator,
30 compute_metrics=thainer_f1_metric
31)