-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEmoTrain.py
286 lines (242 loc) · 9.1 KB
/
EmoTrain.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
"""
Train on Emotion dataset.
"""
import os
import time
import numpy as np
import torch
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import Utils
import math
def emotrain(wordenc, sentenc, contenc, dec, data_loader, tr_emodict, emodict, args, focus_emo):
"""
:data_loader input the whole field
"""
# start time
time_st = time.time()
decay_rate = args.decay
# dataloaders
train_loader = data_loader['train']
dev_loader = data_loader['dev']
feats, labels = train_loader['feat'], train_loader['label']
lr_w, lr_s, lr_c, lr_d = [args.lr] * 4
#if args.load_model:
# lr_c /= 2
# lr_s /= 2
# lr_w /= 2
wordenc_opt = optim.Adam(wordenc.parameters(), lr=lr_w)
sentenc_opt = optim.Adam(sentenc.parameters(), lr=lr_s)
contenc_opt = optim.Adam(contenc.parameters(), lr=lr_c)
dec_opt = optim.Adam(dec.parameters(), lr=lr_d)
# weight for loss
weight_rate = 0.5
#if args.dataset in ['MOSI', 'IEMOCAP4v2']:
# weight_rate = 0
weights = torch.from_numpy(loss_weight(tr_emodict, emodict, focus_emo, rate=weight_rate)).float()
print("Dataset {} Weight rate {} \nEmotion rates {} \nLoss weights {}\n".format(
args.dataset, weight_rate, emodict.word2count, weights))
wordenc.train()
sentenc.train()
contenc.train()
dec.train()
over_fitting = 0
cur_best = -1e10
glob_steps = 0
report_loss = 0
for epoch in range(1, args.epochs + 1):
feats, labels = Utils.shuffle_lists(feats, labels)
print("===========Epoch==============")
print("-{}-{}".format(epoch, Utils.timeSince(time_st)))
for bz in range(len(labels)):
# tensorize a dialog list
feat, lens = Utils.ToTensor(feats[bz], is_len=True)
label = Utils.ToTensor(labels[bz])
feat = Variable(feat)
label = Variable(label)
if args.gpu != None:
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
device = torch.device("cuda: 0")
wordenc.cuda(device)
sentenc.cuda(device)
contenc.cuda(device)
dec.cuda(device)
feat = feat.cuda(device)
label = label.cuda(device)
weights = weights.cuda(device)
w_embed = wordenc(feat)
s_embed = sentenc(w_embed, lens)[0]
#log_prob = dec(s_embed)
s_context = contenc(s_embed)
s_concat = torch.cat([s_embed, s_context], dim=-1)
log_prob = dec(s_concat)
#log_prob = dec(s_context)
#print(log_prob, label)
loss = comput_class_loss(log_prob, label, weights)
loss.backward()
report_loss += loss.item()
glob_steps += 1
# gradient clip
torch.nn.utils.clip_grad_norm_(wordenc.parameters(), max_norm=5)
torch.nn.utils.clip_grad_norm_(sentenc.parameters(), max_norm=5)
torch.nn.utils.clip_grad_norm_(contenc.parameters(), max_norm=5)
torch.nn.utils.clip_grad_norm_(dec.parameters(), max_norm=5)
wordenc_opt.step()
sentenc_opt.step()
contenc_opt.step()
dec_opt.step()
wordenc_opt.zero_grad()
sentenc_opt.zero_grad()
contenc_opt.zero_grad()
dec_opt.zero_grad()
if glob_steps % args.report_loss == 0:
print("Steps: {} Loss: {} LR: {}".format(glob_steps, report_loss/args.report_loss, dec_opt.param_groups[0]['lr']))
report_loss = 0
# validate
pAccs = emoeval(wordenc=wordenc, sentenc=sentenc, contenc=contenc, dec=dec, data_loader=dev_loader,
tr_emodict=tr_emodict, emodict=emodict, args=args, focus_emo=focus_emo)
print("Validate: ACCs-F1s-WA-UWA-F1-val {}".format(pAccs))
last_best = pAccs[-2]
#if args.dataset in ['MOSI', 'IEMOCAP4v2']:
# last_best = pAccs[-2]
if last_best > cur_best:
Utils.revmodel_saver(wordenc, args.save_dir, 'wordenc', args.dataset, args.load_model)
Utils.revmodel_saver(sentenc, args.save_dir, 'sentenc', args.dataset, args.load_model)
Utils.revmodel_saver(contenc, args.save_dir, 'contenc', args.dataset, args.load_model)
Utils.revmodel_saver(dec, args.save_dir, 'dec', args.dataset, args.load_model)
cur_best = last_best
over_fitting = 0
else:
over_fitting += 1
if wordenc_opt.param_groups[0]['lr'] < 2e-5:
decay_rate = 1.0
wordenc_opt.param_groups[0]['lr'] *= decay_rate
sentenc_opt.param_groups[0]['lr'] *= decay_rate
contenc_opt.param_groups[0]['lr'] *= decay_rate
dec_opt.param_groups[0]['lr'] *= decay_rate
if over_fitting >= args.patience:
break
def comput_class_loss(log_prob, target, weights):
""" classification loss """
loss = F.nll_loss(log_prob, target.view(target.size(0)), weight=weights, reduction='sum')
loss /= target.size(0)
return loss
def loss_weight(tr_ladict, ladict, focus_dict, rate=1.0):
""" loss weights """
min_emo = float(min([tr_ladict.word2count[w] for w in focus_dict]))
weight = [math.pow(min_emo / tr_ladict.word2count[k], rate) if k in focus_dict
else 0 for k,v in ladict.word2count.items()]
weight = np.array(weight)
weight /= np.sum(weight)
return weight
def emoeval(wordenc, sentenc, contenc, dec, data_loader, tr_emodict, emodict, args, focus_emo):
""" data_loader only input 'dev' """
wordenc.eval()
sentenc.eval()
contenc.eval()
dec.eval()
# weight for loss
weight_rate = 0 # eval state without weights
#if args.dataset in ['MOSI', 'IEMOCAP4v2']:
# weight_rate = 0
weights = torch.from_numpy(loss_weight(tr_emodict, emodict, focus_emo, rate=weight_rate)).float()
acc = np.zeros([emodict.n_words], dtype=np.long) # recall
num = np.zeros([emodict.n_words], dtype=np.long) # gold
preds = np.zeros([emodict.n_words], dtype=np.long) # precision, only count those in focus_emo
focus_idx = [emodict.word2index[emo] for emo in focus_emo]
feats, labels = data_loader['feat'], data_loader['label']
val_loss = 0
for bz in range(len(labels)):
feat, lens = Utils.ToTensor(feats[bz], is_len=True)
label = Utils.ToTensor(labels[bz])
feat = Variable(feat)
label = Variable(label)
if args.gpu != None:
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
device = torch.device("cuda: 0")
wordenc.cuda(device)
sentenc.cuda(device)
contenc.cuda(device)
dec.cuda(device)
feat = feat.cuda(device)
label = label.cuda(device)
weights = weights.cuda(device)
w_embed = wordenc(feat)
s_embed = sentenc(w_embed, lens)[0]
#log_prob = dec(s_embed)
s_context = contenc(s_embed)
s_concat = torch.cat([s_embed, s_context], dim=-1)
log_prob = dec(s_concat)
# log_prob = dec(s_context)
# print(log_prob, label)
# val loss
loss = comput_class_loss(log_prob, label, weights)
val_loss += loss.item()
# accuracy
emo_predidx = torch.argmax(log_prob, dim=1)
emo_true = label.view(label.size(0))
for lb in range(emo_true.size(0)):
idx = emo_true[lb].item()
num[idx] += 1
if emo_true[lb] == emo_predidx[lb]:
acc[idx] += 1
# count for precision
if emo_true[lb] in focus_idx and emo_predidx[lb] in focus_idx:
preds[emo_predidx[lb]] += 1
pacc = [acc[i] for i in range(emodict.n_words) if emodict.index2word[i] in focus_emo]
pnum = [num[i] for i in range(emodict.n_words) if emodict.index2word[i] in focus_emo]
pwACC = sum(pacc) / sum(pnum) * 100
ACCs = [np.round(acc[i] / num[i] * 100, 2) if num[i] != 0 else 0 for i in range(emodict.n_words)]
pACCs = [ACCs[i] for i in range(emodict.n_words) if emodict.index2word[i] in focus_emo]
paACC = sum(pACCs) / len(pACCs)
pACCs = [ACCs[emodict.word2index[w]] for w in focus_emo] # recall
TP = [acc[emodict.word2index[w]] for w in focus_emo]
pPREDs = [preds[emodict.word2index[w]] for w in focus_emo]
pPRECs = [np.round(tp/p*100,2) if p>0 else 0 for tp,p in zip(TP,pPREDs)] # precision
pF1s = [np.round(2*r*p/(r+p),2) if r+p>0 else 0 for r,p in zip(pACCs,pPRECs)]
F1 = sum(pF1s)/len(pF1s)
Total = [pACCs] + [pF1s] + [np.round(pwACC,2), np.round(paACC,2), np.round(F1,2), -val_loss]
wordenc.train()
sentenc.train()
contenc.train()
dec.train()
return Total
# for case study
def Casestudy(wordenc, sentenc, contenc, dec, data_loader, emodict, args):
wordenc.eval()
sentenc.eval()
contenc.eval()
dec.eval()
feats, labels = data_loader['feat'], data_loader['label']
label_preds = []
for bz in range(len(labels)):
feat, lens = Utils.ToTensor(feats[bz], is_len=True)
label = Utils.ToTensor(labels[bz])
feat = Variable(feat)
label = Variable(label)
if args.gpu != None:
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
device = torch.device("cuda: 0")
wordenc.cuda(device)
sentenc.cuda(device)
contenc.cuda(device)
dec.cuda(device)
feat = feat.cuda(device)
label = label.cuda(device)
w_embed = wordenc(feat)
s_embed = sentenc(w_embed, lens)[0]
s_context = contenc(s_embed)
s_concat = torch.cat([s_embed, s_context], dim=-1)
log_prob = dec(s_concat)
emo_pred = torch.argmax(log_prob, dim=1)
emo_true = label.view(label.size(0))
label_pred = []
for lb in range(emo_true.size(0)):
true_idx = emo_true[lb].item()
pred_idx = emo_pred[lb].item()
label_pred.append((emodict.index2word[true_idx], emodict.index2word[pred_idx]))
label_preds.append(label_pred)
path = '{}_finetune?{}_case.json'.format(args.dataset, str(args.load_model))
Utils.saveToJson(path, label_preds)
return 1