-
Notifications
You must be signed in to change notification settings - Fork 1
/
solver.py
398 lines (299 loc) · 15.5 KB
/
solver.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
import os
import math
from math import isnan
import re
import pickle
import gensim
import numpy as np
from tqdm import tqdm
from tqdm import tqdm_notebook
from sklearn.metrics import classification_report, accuracy_score, f1_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_recall_fscore_support
from scipy.special import expit
import torch
import torch.nn as nn
from torch.nn import functional as F
torch.manual_seed(123)
torch.cuda.manual_seed_all(123)
from utils import to_gpu, time_desc_decorator, DiffLoss, MSE, SIMSE, CMD, DiffLoss_so
import models
class Solver(object):
def __init__(self, train_config, dev_config, test_config, train_data_loader, dev_data_loader, test_data_loader, is_train=True, model=None):
self.train_config = train_config
self.epoch_i = 0
self.train_data_loader = train_data_loader
self.dev_data_loader = dev_data_loader
self.test_data_loader = test_data_loader
self.is_train = is_train
self.model = model
@time_desc_decorator('Build Graph')
def build(self, cuda=True):
if self.model is None:
self.model = getattr(models, self.train_config.model)(self.train_config)
# Final list
for name, param in self.model.named_parameters():
# Bert freezing customizations
if self.train_config.data == "mosei":
if "bertmodel.encoder.layer" in name:
layer_num = int(name.split("encoder.layer.")[-1].split(".")[0])
if layer_num <= (8):
param.requires_grad = False
elif self.train_config.data == "ur_funny":
if "bert" in name:
param.requires_grad = False
if 'weight_hh' in name:
nn.init.orthogonal_(param)
print('\t' + name, param.requires_grad)
# Initialize weight of Embedding matrix with Glove embeddings
if not self.train_config.use_bert:
if self.train_config.pretrained_emb is not None:
self.model.embed.weight.data = self.train_config.pretrained_emb
self.model.embed.requires_grad = False
if torch.cuda.is_available() and cuda:
self.model.cuda()
if self.is_train:
self.optimizer = self.train_config.optimizer(
filter(lambda p: p.requires_grad, self.model.parameters()),
lr=self.train_config.learning_rate)
@time_desc_decorator('Training Start!')
def train(self):
curr_patience = patience = self.train_config.patience
num_trials = 1
# self.criterion = criterion = nn.L1Loss(reduction="mean")
if self.train_config.data == "ur_funny":
self.criterion = criterion = nn.CrossEntropyLoss(reduction="mean")
else: # mosi and mosei are regression datasets
self.criterion = criterion = nn.MSELoss(reduction="mean")
self.domain_loss_criterion = nn.CrossEntropyLoss(reduction="mean")
self.sp_loss_criterion = nn.CrossEntropyLoss(reduction="mean")
self.loss_diff = DiffLoss()
self.loss_recon = MSE()
# self.loss_cmd = CMD()
self.loss_diff_so = DiffLoss_so()
best_valid_loss = float('inf')
lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(self.optimizer, gamma=0.5)
train_losses = []
valid_losses = []
for e in range(self.train_config.n_epoch):
self.model.train()
train_loss_cls, train_loss_sim, train_loss_diff = [], [], []
train_loss_recon = []
train_loss_sp = []
train_loss = []
for batch in self.train_data_loader:
self.model.zero_grad()
t, v, a, y, l, bert_sent, bert_sent_type, bert_sent_mask = batch
batch_size = t.size(0)
t = to_gpu(t)
v = to_gpu(v)
a = to_gpu(a)
y = to_gpu(y)
l = to_gpu(l)
bert_sent = to_gpu(bert_sent)
bert_sent_type = to_gpu(bert_sent_type)
bert_sent_mask = to_gpu(bert_sent_mask)
y_tilde = self.model(t, v, a, l, bert_sent, bert_sent_type, bert_sent_mask)
if self.train_config.data == "ur_funny":
y = y.squeeze()
cls_loss = criterion(y_tilde, y)
diff_loss = self.get_diff_loss()
domain_loss = self.get_domain_loss()
recon_loss = self.get_recon_loss()
# cmd_loss = self.get_cmd_loss()
cmd_loss = self.get_diff_loss_so()
# print(cmd_loss)
if self.train_config.use_cmd_sim:
similarity_loss = cmd_loss
else:
similarity_loss = domain_loss
loss = cls_loss + \
self.train_config.diff_weight * diff_loss + \
self.train_config.sim_weight * similarity_loss + \
self.train_config.recon_weight * recon_loss
loss.backward()
torch.nn.utils.clip_grad_value_([param for param in self.model.parameters() if param.requires_grad], self.train_config.clip)
self.optimizer.step()
train_loss_cls.append(cls_loss.item())
train_loss_diff.append(diff_loss.item())
train_loss_recon.append(recon_loss.item())
train_loss.append(loss.item())
train_loss_sim.append(similarity_loss.item())
train_losses.append(train_loss)
print(f"Training loss: {round(np.mean(train_loss), 4)}")
valid_loss, valid_acc = self.eval(mode="dev")
print(f"Current patience: {curr_patience}, current trial: {num_trials}.")
if valid_loss <= best_valid_loss:
best_valid_loss = valid_loss
print("Found new best model on dev set!")
if not os.path.exists('checkpoints'): os.makedirs('checkpoints')
torch.save(self.model.state_dict(), f'checkpoints/model_{self.train_config.name}.std')
torch.save(self.optimizer.state_dict(), f'checkpoints/optim_{self.train_config.name}.std')
curr_patience = patience
else:
curr_patience -= 1
if curr_patience <= -1:
print("Running out of patience, loading previous best model.")
num_trials -= 1
curr_patience = patience
self.model.load_state_dict(torch.load(f'checkpoints/model_{self.train_config.name}.std'))
self.optimizer.load_state_dict(torch.load(f'checkpoints/optim_{self.train_config.name}.std'))
lr_scheduler.step()
print(f"Current learning rate: {self.optimizer.state_dict()['param_groups'][0]['lr']}")
if num_trials <= 0:
print("Running out of patience, early stopping.")
break
self.eval(mode="test", to_print=True)
def eval(self,mode=None, to_print=False):
assert(mode is not None)
self.model.eval()
y_true, y_pred = [], []
eval_loss, eval_loss_diff = [], []
if mode == "dev":
dataloader = self.dev_data_loader
elif mode == "test":
dataloader = self.test_data_loader
if to_print:
self.model.load_state_dict(torch.load(
f'checkpoints/model_{self.train_config.name}.std'))
with torch.no_grad():
for batch in dataloader:
self.model.zero_grad()
t, v, a, y, l, bert_sent, bert_sent_type, bert_sent_mask = batch
t = to_gpu(t)
v = to_gpu(v)
a = to_gpu(a)
y = to_gpu(y)
l = to_gpu(l)
bert_sent = to_gpu(bert_sent)
bert_sent_type = to_gpu(bert_sent_type)
bert_sent_mask = to_gpu(bert_sent_mask)
y_tilde = self.model(t, v, a, l, bert_sent, bert_sent_type, bert_sent_mask)
if self.train_config.data == "ur_funny":
y = y.squeeze()
cls_loss = self.criterion(y_tilde, y)
loss = cls_loss
eval_loss.append(loss.item())
y_pred.append(y_tilde.detach().cpu().numpy())
y_true.append(y.detach().cpu().numpy())
eval_loss = np.mean(eval_loss)
y_true = np.concatenate(y_true, axis=0).squeeze()
y_pred = np.concatenate(y_pred, axis=0).squeeze()
accuracy = self.calc_metrics(y_true, y_pred, mode, to_print)
return eval_loss, accuracy
def multiclass_acc(self, preds, truths):
"""
Compute the multiclass accuracy w.r.t. groundtruth
:param preds: Float array representing the predictions, dimension (N,)
:param truths: Float/int array representing the groundtruth classes, dimension (N,)
:return: Classification accuracy
"""
return np.sum(np.round(preds) == np.round(truths)) / float(len(truths))
def calc_metrics(self, y_true, y_pred, mode=None, to_print=False):
"""
Metric scheme adapted from:
https://github.com/yaohungt/Multimodal-Transformer/blob/master/src/eval_metrics.py
"""
if self.train_config.data == "ur_funny":
test_preds = np.argmax(y_pred, 1)
test_truth = y_true
if to_print:
print("Confusion Matrix (pos/neg) :")
print(confusion_matrix(test_truth, test_preds))
print("Classification Report (pos/neg) :")
print(classification_report(test_truth, test_preds, digits=5))
print("Accuracy (pos/neg) ", accuracy_score(test_truth, test_preds))
return accuracy_score(test_truth, test_preds)
else:
test_preds = y_pred
test_truth = y_true
non_zeros = np.array([i for i, e in enumerate(test_truth) if e != 0])
test_preds_a7 = np.clip(test_preds, a_min=-3., a_max=3.)
test_truth_a7 = np.clip(test_truth, a_min=-3., a_max=3.)
test_preds_a5 = np.clip(test_preds, a_min=-2., a_max=2.)
test_truth_a5 = np.clip(test_truth, a_min=-2., a_max=2.)
mae = np.mean(np.absolute(test_preds - test_truth)) # Average L1 distance between preds and truths
corr = np.corrcoef(test_preds, test_truth)[0][1]
mult_a7 = self.multiclass_acc(test_preds_a7, test_truth_a7)
mult_a5 = self.multiclass_acc(test_preds_a5, test_truth_a5)
f_score = f1_score((test_preds[non_zeros] > 0), (test_truth[non_zeros] > 0), average='weighted')
# pos - neg
binary_truth = (test_truth[non_zeros] > 0)
binary_preds = (test_preds[non_zeros] > 0)
if to_print:
print("mae: ", mae)
print("corr: ", corr)
print("mult_acc: ", mult_a7)
print("Classification Report (pos/neg) :")
print(classification_report(binary_truth, binary_preds, digits=5))
print("Accuracy (pos/neg) ", accuracy_score(binary_truth, binary_preds))
# non-neg - neg
binary_truth = (test_truth >= 0)
binary_preds = (test_preds >= 0)
if to_print:
print("Classification Report (non-neg/neg) :")
print(classification_report(binary_truth, binary_preds, digits=5))
print("Accuracy (non-neg/neg) ", accuracy_score(binary_truth, binary_preds))
return accuracy_score(binary_truth, binary_preds)
def get_domain_loss(self,):
if self.train_config.use_cmd_sim:
return 0.0
# Predicted domain labels
domain_pred_t = self.model.domain_label_t
domain_pred_v = self.model.domain_label_v
domain_pred_a = self.model.domain_label_a
# True domain labels
domain_true_t = to_gpu(torch.LongTensor([0]*domain_pred_t.size(0)))
domain_true_v = to_gpu(torch.LongTensor([1]*domain_pred_v.size(0)))
domain_true_a = to_gpu(torch.LongTensor([2]*domain_pred_a.size(0)))
# Stack up predictions and true labels
domain_pred = torch.cat((domain_pred_t, domain_pred_v, domain_pred_a), dim=0)
domain_true = torch.cat((domain_true_t, domain_true_v, domain_true_a), dim=0)
return self.domain_loss_criterion(domain_pred, domain_true)
def get_cmd_loss(self,):
if not self.train_config.use_cmd_sim:
return 0.0
# losses between shared states
loss = self.loss_cmd(self.model.utt_shared_t, self.model.utt_shared_v, 5)
loss += self.loss_cmd(self.model.utt_shared_t, self.model.utt_shared_a, 5)
loss += self.loss_cmd(self.model.utt_shared_a, self.model.utt_shared_v, 5)
loss = loss/3.0
return loss
def get_diff_loss(self):
shared_t = self.model.utt_shared_t
shared_v = self.model.utt_shared_v
shared_a = self.model.utt_shared_a
private_t = self.model.utt_private_t
private_v = self.model.utt_private_v
private_a = self.model.utt_private_a
# Between private and shared
loss = self.loss_diff(private_t, shared_t)
loss += self.loss_diff(private_v, shared_v)
loss += self.loss_diff(private_a, shared_a)
# Across privates
loss += self.loss_diff(private_a, private_t)
loss += self.loss_diff(private_a, private_v)
loss += self.loss_diff(private_t, private_v)
return loss
def get_diff_loss_so(self):
# losses between shared states
loss = self.loss_diff_so(self.model.utt_shared_t, self.model.utt_shared_v, self.train_config.correlation_a)
loss += self.loss_diff_so(self.model.utt_shared_t, self.model.utt_shared_a, self.train_config.correlation_v)
loss += self.loss_diff_so(self.model.utt_shared_a, self.model.utt_shared_v, self.train_config.correlation_t)
loss = loss/3.0
# shared_orthogonal_t = self.model.shared_orthogonal_t
# shared_orthogonal_v = self.model.shared_orthogonal_v
# shared_orthogonal_a = self.model.shared_orthogonal_a
# Across privates
# across_private_loss = [self.loss_diff_so(shared_orthogonal_t,shared_orthogonal_v,self.train_config.correlation), self.loss_diff_so(shared_orthogonal_v,shared_orthogonal_a,self.train_config.privacy_select), self.loss_diff_so(shared_orthogonal_a,shared_orthogonal_t,self.train_config.privacy_select)]
# loss = np.sum(np.array(across_private_loss) * self.train_config.attack_select)
# loss = self.loss_diff_so(shared_orthogonal_t,shared_orthogonal_v,self.train_config.privacy_select)
# loss += self.loss_diff_so(shared_orthogonal_v,shared_orthogonal_a,self.train_config.privacy_select)
# loss += self.loss_diff_so(shared_orthogonal_a,shared_orthogonal_t,self.train_config.privacy_select)
return loss
def get_recon_loss(self, ):
loss = self.loss_recon(self.model.utt_t_recon, self.model.utt_t_orig)
loss += self.loss_recon(self.model.utt_v_recon, self.model.utt_v_orig)
loss += self.loss_recon(self.model.utt_a_recon, self.model.utt_a_orig)
loss = loss/3.0
return loss