-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
213 lines (176 loc) · 7.15 KB
/
trainer.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from collections import defaultdict
import os
import torch
from tqdm import tqdm
from itertools import chain, repeat
from utils.data_utils import y1_set
from src.conll2002_metrics import conll2002_measure
device = torch.device("cuda") if torch.cuda.is_available() else "cpu"
def repeater(dataloader): # for infinite dataloader loop
for loader in repeat(dataloader):
for data in loader:
yield data
def train(model,
model_save_path,
dataloader_train,
dataloader_val,
optim,
scheduler,
eval_steps,
total_steps,
early_stopping_patience,
log_dict,
tokenizer,
use_dep,
use_pos,
):
model.train()
log_dict['eval_results'] = []
repeat_dataloader = repeater(dataloader_train) # for infinite loop
pbar = tqdm(repeat_dataloader, total=total_steps, desc="Start Training")
best_step = 0
best_f1_score = 0
patience = 0
losses = []
coarse_losses = []
for i, features in enumerate(pbar):
if i == total_steps:
print(f"Training step reached set maximum steps: {total_steps}")
break
# optim.zero_grad()
# features = features.to(device)
# loss, _ = model(features, optim, scheduler)
crf_loss, coarse_loss, fine_loss, _ = model(features)
# loss = loss.mean()
# crf_loss = crf_loss.mean()
coarse_loss = coarse_loss.mean()
coarse_losses.append(coarse_loss.detach().cpu().item())
# fine_loss = fine_loss.mean()
loss = crf_loss + 0.05*fine_loss
loss = loss.mean()
losses.append(loss.detach().cpu().item())
# optim.zero_grad()
# loss.backward(retain_graph=True)
# # loss.backward()
# losses.append(loss.detach().cpu().item())
#
# optim.step()
# scheduler.step()
# # scheduler.step(loss)
# first step
optim.zero_grad()
coarse_loss.backward(retain_graph=True)
optim.step()
scheduler.step()
# scheduler.step(coarse_loss)
# second step
optim.zero_grad()
loss.backward(retain_graph=True)
optim.step()
scheduler.step()
# scheduler.step(loss)
# pbar.set_description(f"LOSS: {losses[-1]:.4f}")
pbar.set_description(f"COARSE_LOSS: {coarse_losses[-1]:.4f} LOSS: {losses[-1]:.4f}")
# evaluation
if (i + 1) % eval_steps == 0:
result = {}
eval_f1 = eval(model, dataloader_val, tokenizer=tokenizer, use_dep=use_dep, use_pos=use_pos)['fb1']
print(f"Result(F1-Score) at step {i+1}: {eval_f1}")
result['step'] = i + 1
result['f1-score'] = eval_f1
log_dict['eval_results'].append(result)
# scheduler => f1-score
# scheduler.step(eval_f1)
if eval_f1 > best_f1_score:
"""
when better evaluation f1 score is found:
update best_f1_score and best_step
& save model's parameter
"""
print("Found better model!")
os.makedirs(model_save_path, exist_ok=True)
if os.path.isfile(model_save_path+f'best-model-parameters.pt'):
os.remove(model_save_path+f'best-model-parameters.pt')
torch.save(model.state_dict(), model_save_path+f'best-model-parameters.pt')
best_f1_score = eval_f1
best_step = i
patience = 0
else:
patience += 1
if patience == early_stopping_patience:
print(f"Early stop at step {i+1}")
i += 1
break
model.train()
log_dict['stopped_step'] = i
log_dict['eval_best_step'] = best_step
log_dict['eval_best_f1_score'] = best_f1_score
return best_step, best_f1_score
def eval(model, dataloader, tgt_domain=None, tokenizer=None, out_file=None, use_dep=False, use_pos=False):
"""
evalutation function for validation dataset and test dataset
returns
----------
List of losses, F1 Score
"""
model.eval()
losses = []
total_preds = []
total_targets = []
out_dict = defaultdict(dict)
with torch.no_grad():
pbar = tqdm(dataloader, total=len(dataloader))
for features in pbar:
# _loss, logits = model(features, optim, scheduler)
_loss, coarse_loss, fine_loss, logits = model(features)
loss = _loss.mean()
losses.append(loss.detach().cpu().item())
# pred = torch.argmax(logits, dim=2)
# with gcl logits
pred = torch.argmax(logits[0], dim=2)
true_labels = features['labels']
total_preds.extend(pred.tolist())
total_targets.extend(true_labels.tolist())
remove_char = ['[CLS]', ' ']
for input_id, p in zip(features['input_ids'], pred):
if use_dep:
slot_dep_utter = tokenizer.decode(input_id).split(' [SEP] ')
for rc in remove_char:
slot_dep_utter[0] = slot_dep_utter[0].replace(rc, '')
slot = tokenizer.decode(input_id[(p != 0)])
try:
slot_type, utterance_dep, utterance, _ = slot_dep_utter
except:
slot_type, utterance_dep, utterance = slot_dep_utter
utterance = utterance.split(' [SEP] ')[0]
out_dict[utterance_dep+' -> '+utterance][slot_type] = slot if slot != '' else 'NONE'
else:
slot_utter = tokenizer.decode(input_id).split(' [SEP] ')
for rc in remove_char:
slot_utter[0] = slot_utter[0].replace(rc, '')
slot = tokenizer.decode(input_id[(p != 0)])
try:
slot_type, utterance, _ = slot_utter
except:
slot_type, utterance = slot_utter
utterance = utterance.split(' [SEP] ')[0]
out_dict[utterance][slot_type] = slot if slot != '' else 'NONE'
# below is for check
# rand = torch.randint(query.size()[0], (1,)).item()
# decoded = tokenizer.decode(query[rand])
# print("Query : ", decoded)
# print("Answer : ", targets[rand])
# print("Prediction: ", pred[rand])
total_targets = list(chain.from_iterable(total_targets))
total_preds = list(chain.from_iterable(total_preds))
total_lines = []
for target, pred in zip(total_targets, total_preds):
bin_target = y1_set[target]
bin_pred = y1_set[pred]
total_lines.append("w" + " " + bin_pred + " " + bin_target)
result = conll2002_measure(total_lines)
if out_file is not None:
import json
with open(out_file, "w") as json_file:
json.dump(out_dict, json_file, indent=4)
return result