-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
443 lines (335 loc) · 16.6 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import os
import torch
import wandb
from tqdm import tqdm, trange
from torcheval.metrics.functional import multiclass_f1_score, multiclass_accuracy
class ModelTrainer():
def __init__(self, args,
model, loss_fn, optimizer, tokenizer,
train_dataloader, valid_dataloader=None, test_dataloader=None,
scheduler = None,
label_dict=None, verbalizer_value=None):
self.args = args
self.model = model
self.loss_fn = loss_fn
self.tokenizer = tokenizer
self.optimizer = optimizer
self.scheduler = scheduler
self.train_dataloader = train_dataloader
self.valid_dataloader = valid_dataloader
self.test_dataloader = test_dataloader
if verbalizer_value!=None and args.pet:
self.verbalizer_value = list(verbalizer_value.values())
else:
self.verbalizer_value = None
self.label_dict = label_dict
self.neutral_label = label_dict['neutral']
# save directory 생성
if not os.path.exists(self.args.save_path):
os.makedirs(self.args.save_path)
def train(self):
self.model.to(self.args.device)
# 학습 및 검증 수행
for epoch in trange(self.args.epochs):
if self.args.model == "speech_only":
self._train_speech()
if self.args.val_ratio:
self._validation_speech()
else:
self._train()
if self.args.val_ratio:
self._validation()
if (epoch+1) % 30 == 0:
if self.args.mm_type == "concat" and self.args.model in ["compressing", "attention"]:
torch.save(self.model.state_dict(), f"{self.args.save_path}/e{epoch+1}_{self.args.model}_{self.args.mm_type}_seed{self.args.seed}.pt")
else:
torch.save(self.model.state_dict(), f"{self.args.save_path}/e{epoch+1}_{self.args.model}_seed{self.args.seed}.pt")
# cuda cache 삭제
torch.cuda.empty_cache()
if self.args.val_ratio:
del self.model, self.train_dataloader, self.valid_dataloader
else:
del self.model, self.train_dataloader
def test(self):
self.model.to(self.args.device)
# Inference 수행
if self.args.model == "speech_only":
m_f1, mic_f1, w_f1, acc = self._test_speech()
else:
m_f1, mic_f1, w_f1, acc = self._test()
name = self.args.test_model_path.split("/")[1]
e = name.split("_")[0][1:]
m = name.split("_")[1]
s = name.split("_")[-1][4:-3]
print(f"Epoch: {e}, Seed: {s}, Model: {m}, Macro-F1: {m_f1: .4f}, Micro-F1: {mic_f1: .4f}, Weighted-F1: {w_f1: .4f}, ACC: {acc: .4f}")
return e, s, m, m_f1, mic_f1, w_f1, acc
def _train(self):
"""
학습 수행: speech_only를 제외한 모든 모델들의 학습 수행
"""
self.model.train()
train_epoch_loss = 0
output_list = []
label_list = []
pbar = tqdm(self.train_dataloader)
for step, batch in enumerate(pbar):
self.optimizer.zero_grad()
label = batch['label'].to(self.args.device)
audio_tensor = batch['audio_emb'].to(self.args.device)
input_ids = batch["input_ids"].to(self.args.device)
attention_mask = batch["attention_mask"].to(self.args.device)
token_type_ids = batch["token_type_ids"].to(self.args.device)
output = self.model(
input_ids,
attention_mask,
token_type_ids,
audio_tensor
)
logit = output['class_logit']
# PET 적용
if self.args.pet:
output = output['prediction_scores']
_, mask_pos = torch.where(input_ids==self.tokenizer.mask_token_id)
self.verbalizer_idx = self.tokenizer(self.verbalizer_value,
add_special_tokens=False,
return_tensors='pt').input_ids.squeeze().to(self.args.device)
# Verbalizer Label 토큰에 대한 logit값
logit = torch.stack([pred_score[mask_idx, :][self.verbalizer_idx] for pred_score, mask_idx in zip(output, mask_pos)])
loss = self.loss_fn(logit, label)
else:
logit = output['class_logit']
loss = self.loss_fn(logit, label)
loss.backward()
self.optimizer.step()
if self.scheduler:
self.scheduler.step()
step_loss = loss.detach().cpu().item()
train_epoch_loss += step_loss
wandb.log({'loss':step_loss})
output_list.append(logit.detach().cpu())
label_list.append(label.detach().cpu())
pbar.set_postfix({'loss': step_loss,
"lr": self.optimizer.param_groups[0]["lr"]})
train_epoch_loss /= (step+1)
m_f1 = self._macro_f1_score(output_list, label_list)
mic_f1 = self._micro_f1_score(output_list, label_list)
w_f1 = self._weighted_f1_score(output_list, label_list)
acc = self._accuracy_score(output_list, label_list)
wandb.log({'train_macro_f1_score':m_f1,
'train_weighted_f1_score':w_f1,
'train_micro_f1_score':mic_f1,
'train_accuracy':acc})
print(f"Train Loss: {train_epoch_loss: .4f} \nTrain Acc: {acc :.4f} \
\nTrain Macro-F1: {m_f1:.4f} \nTrain Weighted-F1: {w_f1:.4f} \nTrain Micro-F1: {mic_f1:.4f}")
pbar.close()
def _train_speech(self):
"""
학습 수행: speech_only model을 학습
"""
self.model.train()
train_epoch_loss = 0
output_list = []
label_list = []
pbar = tqdm(self.train_dataloader)
for step, batch in enumerate(pbar):
self.optimizer.zero_grad()
label = batch['label'].to(self.args.device)
audio_tensor = batch['audio_emb'].to(self.args.device)
output = self.model(audio_tensor)
logit = output['class_logit']
loss = self.loss_fn(logit, label)
loss.backward()
self.optimizer.step()
if self.scheduler:
self.scheduler.step()
step_loss = loss.detach().cpu().item()
train_epoch_loss += step_loss
wandb.log({'loss':step_loss})
output_list.append(logit.detach().cpu())
label_list.append(label.detach().cpu())
pbar.set_postfix({'loss': step_loss,
"lr": self.optimizer.param_groups[0]["lr"]})
train_epoch_loss /= (step+1)
m_f1 = self._macro_f1_score(output_list, label_list)
mic_f1 = self._micro_f1_score(output_list, label_list)
w_f1 = self._weighted_f1_score(output_list, label_list)
acc = self._accuracy_score(output_list, label_list)
wandb.log({'train_macro_f1_score':m_f1,
'train_weighted_f1_score':w_f1,
'train_micro_f1_score':mic_f1,
'train_accuracy':acc})
print(f"Train Loss: {train_epoch_loss: .4f} \nTrain Acc: {acc :.4f} \
\nTrain Macro-F1: {m_f1:.4f} \nTrain Weighted-F1: {w_f1:.4f} \nTrain Micro-F1: {mic_f1:.4f}")
pbar.close()
def _validation(self):
"""
검증 수행: speech_only를 제외한 모든 모델들의 검증 수행
"""
self.model.eval()
val_epoch_loss=0
output_list=[]
label_list=[]
with torch.no_grad():
pbar=tqdm(self.valid_dataloader)
for step, batch in enumerate(pbar):
label = batch['label'].to(self.args.device)
audio_tensor = batch['audio_emb'].to(self.args.device)
input_ids = batch["input_ids"].to(self.args.device)
attention_mask = batch["attention_mask"].to(self.args.device)
token_type_ids = batch["token_type_ids"].to(self.args.device)
output = self.model(
input_ids,
attention_mask,
token_type_ids,
audio_tensor
)
# PET 적용
if self.args.pet:
output = output['prediction_scores']
_, mask_pos = torch.where(input_ids==self.tokenizer.mask_token_id)
self.verbalizer_idx = self.tokenizer(self.verbalizer_value,
add_special_tokens=False,
return_tensors='pt').input_ids.squeeze().to(self.args.device)
# Verbalizer Label 토큰에 대한 logit값
logit = torch.stack([pred_score[mask_idx, :][self.verbalizer_idx] for pred_score, mask_idx in zip(output, mask_pos)])
valid_step_loss = self.loss_fn(logit, label)
else:
logit = output['class_logit']
valid_step_loss = self.loss_fn(logit, label)
val_epoch_loss += valid_step_loss.detach().cpu().item()
output_list.append(logit.detach().cpu())
label_list.append(label.detach().cpu())
m_f1 = self._macro_f1_score(output_list, label_list)
w_f1 = self._weighted_f1_score(output_list, label_list)
mic_f1 = self._micro_f1_score(output_list, label_list)
acc = self._accuracy_score(output_list, label_list)
val_epoch_loss /= (step+1)
wandb.log({'val_epoch_loss':val_epoch_loss,
'val_macro_f1_score':m_f1,
'val_weighted_f1_score':w_f1,
'val_micro_f1_score':mic_f1,
'val_accuracy':acc})
print(f"Valid Loss: {val_epoch_loss: .4f} \nValid Acc: {acc :.4f} \
\nValid Macro-F1: {m_f1:.4f} \nValid Weighted-F1: {w_f1:.4f} \nValid Micro-F1: {mic_f1:.4f}")
return val_epoch_loss
def _validation_speech(self):
"""
검증 수행: speech_only model을 검증
"""
self.model.eval()
val_epoch_loss=0
output_list=[]
label_list=[]
with torch.no_grad():
pbar=tqdm(self.valid_dataloader)
for step, batch in enumerate(pbar):
label = batch['label'].to(self.args.device)
audio_tensor = batch['audio_emb'].to(self.args.device)
output = self.model(audio_tensor)
logit = output['class_logit']
valid_step_loss = self.loss_fn(logit, label)
val_epoch_loss += valid_step_loss.detach().cpu().item()
output_list.append(logit.detach().cpu())
label_list.append(label.detach().cpu())
m_f1 = self._macro_f1_score(output_list, label_list)
w_f1 = self._weighted_f1_score(output_list, label_list)
mic_f1 = self._micro_f1_score(output_list, label_list)
acc = self._accuracy_score(output_list, label_list)
val_epoch_loss /= (step+1)
wandb.log({'val_epoch_loss':val_epoch_loss,
'val_macro_f1_score':m_f1,
'val_weighted_f1_score':w_f1,
'val_micro_f1_score':mic_f1,
'val_accuracy':acc})
print(f"Valid Loss: {val_epoch_loss: .4f} \nValid Acc: {acc :.4f} \
\nValid Macro-F1: {m_f1:.4f} \nValid Weighted-F1: {w_f1:.4f} \nValid Micro-F1: {mic_f1:.4f}")
return val_epoch_loss
def _test(self):
"""
Inference 수행: speech_only를 제외한 모든 모델들의 예측 수행
"""
self.model.eval()
output_list=[]
label_list=[]
with torch.no_grad():
pbar=tqdm(self.test_dataloader)
for _, batch in enumerate(pbar):
label = batch['label'].to(self.args.device)
audio_tensor = batch['audio_emb'].to(self.args.device)
input_ids = batch["input_ids"].to(self.args.device)
attention_mask = batch["attention_mask"].to(self.args.device)
token_type_ids = batch["token_type_ids"].to(self.args.device)
output = self.model(
input_ids,
attention_mask,
token_type_ids,
audio_tensor
)
# PET 적용
if self.args.pet:
output = output['prediction_scores']
_, mask_pos = torch.where(input_ids==self.tokenizer.mask_token_id)
self.verbalizer_idx = self.tokenizer(self.verbalizer_value,
add_special_tokens=False,
return_tensors='pt').input_ids.squeeze().to(self.args.device)
# Verbalizer Label 토큰에 대한 logit값
output = torch.stack([pred_score[mask_idx, :][self.verbalizer_idx] for pred_score, mask_idx in zip(output, mask_pos)])
else:
output = output['class_logit']
output_list.append(output.detach().cpu())
label_list.append(label.detach().cpu())
m_f1 = self._macro_f1_score(output_list, label_list)
w_f1 = self._weighted_f1_score(output_list, label_list)
mic_f1 = self._micro_f1_score(output_list, label_list)
acc = self._accuracy_score(output_list, label_list)
return m_f1, mic_f1, w_f1, acc
def _test_speech(self):
"""
Inference 수행: speech_only model의 예측 수행
"""
self.model.eval()
output_list=[]
label_list=[]
with torch.no_grad():
pbar=tqdm(self.test_dataloader)
for _, batch in enumerate(pbar):
label = batch['label'].to(self.args.device)
audio_tensor = batch['audio_emb'].to(self.args.device)
output = self.model(audio_tensor)
output = output['class_logit']
output_list.append(output.detach().cpu())
label_list.append(label.detach().cpu())
m_f1 = self._macro_f1_score(output_list, label_list)
w_f1 = self._weighted_f1_score(output_list, label_list)
mic_f1 = self._micro_f1_score(output_list, label_list)
acc = self._accuracy_score(output_list, label_list)
return m_f1, mic_f1, w_f1, acc
def _macro_f1_score(self, logit_list, label_list):
logits = torch.cat(logit_list)
labels = torch.cat(label_list)
m_f1_score = multiclass_f1_score(logits, labels,
num_classes=self.args.num_labels,
average="macro").detach().cpu().item()
return m_f1_score
def _weighted_f1_score(self, logit_list, label_list):
logits = torch.cat(logit_list)
labels = torch.cat(label_list)
w_f1_score = multiclass_f1_score(logits, labels,
num_classes=self.args.num_labels,
average="weighted").detach().cpu().item()
return w_f1_score
def _micro_f1_score(self, logit_list, label_list):
labels = torch.cat(label_list)
label_pos =torch.where(labels!=self.neutral_label)
labels = labels[label_pos]
logits = torch.cat(logit_list)[label_pos]
logits = torch.argmax(logits, dim=1)
micro_f1_score = multiclass_f1_score(logits, labels,
average="micro").detach().cpu().item()
return micro_f1_score
def _accuracy_score(self, logit_list, label_list):
logits = torch.cat(logit_list)
labels = torch.cat(label_list)
acc_score = multiclass_accuracy(logits, labels,
num_classes=self.args.num_labels).detach().cpu().item()
return acc_score