-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_model.py
104 lines (85 loc) · 3.38 KB
/
generate_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#DATASET LOADING
import evaluate,torch,os
import numpy as np
from datasets import load_dataset
from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
from transformers import AutoTokenizer,DataCollatorWithPadding
dataset = load_dataset("xnli",'en')
#TOKENIZING
base_model = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(base_model)
def preprocess_function(examples):
return tokenizer(examples["premise"], examples["hypothesis"] ,truncation=True)
tokenized_dataset = dataset.map(preprocess_function, batched=True)
#COLLATION
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
#EVALUATION
metric_name= "f1"
metric = evaluate.load(metric_name)
def compute_metrics(eval_pred):
predictions, labels = eval_pred
predictions = np.argmax(predictions, axis=1)
return metric.compute(predictions=predictions, references=labels, average='macro')
id2label = {0: "Entailment", 1: "Neutral",2:'Contradiction'}
label2id = {"Entailment": 0, "Neutral": 1, 'Contradiction':2}
epochs = 1
lr = 2e-5
bs= 16
physical_batch_size = 8
model = AutoModelForSequenceClassification.from_pretrained(
base_model, num_labels=len(id2label), id2label=id2label, label2id=label2id
).to('cuda' if torch.cuda.is_available() else 'cpu')
model_name = f'{base_model}_lr{lr}_epochs{epochs}'
model_path = f"generated_models/{model_name}"
print(f'--------Training model {model_name}--------')
training_args = TrainingArguments(
output_dir=model_path,
learning_rate=lr,
gradient_accumulation_steps=int(bs/physical_batch_size),
per_device_train_batch_size=int(physical_batch_size),
per_device_eval_batch_size=int(physical_batch_size),
num_train_epochs=epochs,
weight_decay=0.01,
evaluation_strategy="epoch",
logging_strategy="epoch",
save_strategy="no",
metric_for_best_model=metric_name,
load_best_model_at_end=False
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_dataset["train"],
eval_dataset=tokenized_dataset["test"],
tokenizer=tokenizer,
data_collator=data_collator,
compute_metrics=compute_metrics,
)
trainer.train()
print('------TRAINING FINISHED----------')
cur_path = os.path.split(os.path.realpath(__file__))[0]
datafile = os.path.join(cur_path, model_path)
if not os.path.exists(datafile):
os.mkdir(datafile)
#trainer.save_model(datafile)
metrics_values = {f'val_{metric_name}':[],'val_loss':[],'tra_loss':[]}
for metrics in trainer.state.log_history:
if f'eval_{metric_name}' in metrics:
metrics_values['val_loss'].append(round(metrics['eval_loss'],3))
metrics_values[f'val_{metric_name}'].append(round(metrics[f'eval_{metric_name}'],3))
elif 'loss' in metrics :
metrics_values['tra_loss'].append(round(metrics['loss'],3))
def print_metrics():
out = model_name + '\n'
out += '\t'.join(['epoch'] + [str(i+1) for i in range(epochs)])
for m in metrics_values:
out += '\n' + '\t'.join([m]+[str(i) for i in metrics_values[m]])
eval_res = max(metrics_values[f'val_{metric_name}'])
print(eval_res)
out += f'\nBest {metric_name} on evaluation is {eval_res}'
test_res = trainer.evaluate(tokenized_dataset["test"])
print(test_res)
out += f'\nBest {metric_name} on testing is {round(test_res[f"eval_{metric_name}"],3)}'
return out
with open(datafile+'/metrics.csv','w') as f:
f.write(print_metrics())