-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_hier.py
290 lines (264 loc) · 10.2 KB
/
main_hier.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
import os
import dill as pickle
import numpy as np
from tqdm import tqdm
import torch
import torch.nn as nn
from models.common_layer import NoamOpt
from models import HELMoEncoder, HLstmModel, HUTransformer, HDeepMoji
from utils import constant
from utils.data_reader import extract_elmo, prepare_data, prepare_data_loaders
from utils.utils import getMetrics
def evaluate(model, criterion, loader, verbose=True):
model.eval()
pred = []
gold = []
for X_1, X_2, X_3, x1_len, x2_len, x3_len, y, ind, X_text in loader:
if x1_len is None:
pred_prob = model(X_1, X_2, X_3)
else:
pred_prob = model(X_1, X_2, X_3, x1_len, x2_len, x3_len)
# pred_prob = model(X_1,X_2,X_3,x1_len,x2_len,x3_len)
pred.append(pred_prob[0].detach().cpu().numpy())
gold.append(y.cpu().numpy())
pred = np.concatenate(pred)
gold = np.concatenate(gold)
accuracy, microPrecision, microRecall, microF1 = getMetrics(pred, gold, verbose)
return microF1
def predict(model, criterion, loader, split=0):
label2emotion = ["others", "happy", "sad", "angry"]
model.eval()
file = constant.save_path + "test_{}.txt".format(split)
with open(file, "w") as the_file:
the_file.write("id\tturn1\tturn2\tturn3\tlabel\n")
preds_dict = {}
indices = []
count = 0
for X_1, X_2, X_3, x1_len, x2_len, x3_len, _, ind, X_text in loader:
if x1_len is None:
pred_prob = model(X_1, X_2, X_3)
else:
pred_prob = model(X_1, X_2, X_3, x1_len, x2_len, x3_len)
# pred_prob = model(X_1, X_2, X_3, x1_len, x2_len, x3_len)
preds = pred_prob[1].data.max(1)[1] # max func return (max, argmax)
for idx, text, pred in zip(ind, X_text, preds):
preds_dict[idx] = "{}\t{}\t{}\t{}\t{}\n".format(
idx, text[0], text[1], text[2], label2emotion[pred.item()]
)
indices.append(idx)
sorted_indices = np.argsort(-np.array(indices))[::-1]
for idx in range(len(sorted_indices)):
the_file.write(preds_dict[idx])
# print("FILE {} SAVED".format(file))
def save_model(model, split):
model_save_path = os.path.join(constant.save_path, 'model_{}'.format(split) )
args = {'model':model.state_dict(), 'config':constant.arg}
torch.save(args, model_save_path)
print("Model saved in:",model_save_path)
def load_model(model, split):
model_save_path = os.path.join(constant.save_path, 'model_{}'.format(split) )
state = torch.load(model_save_path, map_location= lambda storage, location: storage)
model = model.load_state_dict(state['model'])
constant.arg = state['config']
return model
def train(
model,
data_loader_train,
data_loader_val,
data_loader_test,
vocab,
patient=10,
split=0,
verbose=True,
):
"""
Training loop
Inputs:
model: the model to be trained
data_loader_train: training data loader
data_loader_val: validation data loader
vocab: vocabulary list
Output:
avg_best: best f1 score on validation data
"""
if constant.USE_CUDA:
device = torch.device("cuda:{}".format(constant.device))
model.to(device)
criterion = nn.CrossEntropyLoss()
if constant.noam:
opt = NoamOpt(
constant.emb_dim,
1,
4000,
torch.optim.Adam(model.parameters(), lr=0, betas=(0.9, 0.98), eps=1e-9),
)
else:
opt = torch.optim.Adam(model.parameters(), lr=constant.lr)
## TRAINING LOOP
avg_best = 0
cnt = 0
for e in range(constant.max_epochs):
model.train()
loss_log = []
f1_log = []
pbar = tqdm(enumerate(data_loader_train), total=len(data_loader_train))
for i, (X_1, X_2, X_3, x1_len, x2_len, x3_len, y, ind, X_text) in pbar:
if constant.noam:
opt.optimizer.zero_grad()
else:
opt.zero_grad()
if x1_len is None:
pred_prob = model(X_1, X_2, X_3)
else:
pred_prob = model(X_1, X_2, X_3, x1_len, x2_len, x3_len)
if constant.double_supervision:
loss = (1-constant.super_ratio)*criterion(pred_prob[0],y) + constant.super_ratio*criterion(pred_prob[2],y)
else:
loss = criterion(pred_prob[0], y)
if constant.act:
R_t = pred_prob[2][0]
N_t = pred_prob[2][1]
p_t = R_t + N_t
avg_p_t = torch.sum(torch.sum(p_t, dim=1) / p_t.size(1)) / p_t.size(0)
loss += constant.act_loss_weight * avg_p_t.item()
loss.backward()
opt.step()
## logging
loss_log.append(loss.item())
accuracy, microPrecision, microRecall, microF1 = getMetrics(
pred_prob[0].detach().cpu().numpy(), y.cpu().numpy()
)
f1_log.append(microF1)
pbar.set_description(
"(Epoch {}) TRAIN MICRO:{:.4f} TRAIN LOSS:{:.4f}".format(
(e + 1), np.mean(f1_log), np.mean(loss_log)
)
)
## LOG
if e % 1 == 0:
microF1 = evaluate(model, criterion, data_loader_val, verbose)
if microF1 > avg_best:
avg_best = microF1
save_model(model, split)
predict(
model, criterion, data_loader_test, split
) ## print the prediction with the highest Micro-F1
cnt = 0
else:
cnt += 1
if cnt == patient:
break
if avg_best == 1.0:
break
correct = 0
loss_nb = 0
return avg_best
if __name__ == "__main__":
elmo_pre = extract_elmo(emoji=constant.emoji) if constant.use_elmo_pre else None
data_loaders_tr, data_loaders_val, data_loaders_test, vocab = prepare_data_loaders(
num_split=constant.num_split,
batch_size=constant.batch_size,
hier=True,
elmo=constant.elmo,
elmo_pre=elmo_pre,
use_elmo_pre=constant.use_elmo_pre,
deepmoji=(constant.model=="DEEPMOJI"),
dev_with_label=constant.dev_with_label,
include_test=constant.include_test
)
results = []
for i in range(constant.num_split):
data_loader_tr = data_loaders_tr[i]
data_loader_val = data_loaders_val[i]
data_loader_test = data_loaders_test[i]
print("###### EXPERIMENT {} ######".format(i + 1))
print("(EXPERIMENT %d) Create the model" % (i + 1))
if constant.model == "LSTM":
model = HLstmModel(
vocab=vocab,
embedding_size=constant.emb_dim,
hidden_size=constant.hidden_dim,
num_layers=constant.n_layers,
is_bidirectional=constant.bidirec,
input_dropout=constant.drop,
layer_dropout=constant.drop,
attentive=constant.attn,
multiattentive=constant.multiattn,
num_heads=constant.heads,
total_key_depth=constant.depth,
total_value_depth=constant.depth,
super_ratio=constant.super_ratio,
double_supervision=constant.double_supervision,
context=constant.context,
pool=constant.pool,
pool_kernel=constant.pool_kernel,
pool_stride=constant.pool_stride
)
elif constant.model == "UTRS":
model = HUTransformer(
vocab=vocab,
embedding_size=constant.emb_dim,
hidden_size=constant.hidden_dim,
num_layers=constant.hop,
num_heads=constant.heads,
total_key_depth=constant.depth,
total_value_depth=constant.depth,
filter_size=constant.filter,
act=constant.act,
input_dropout=constant.input_dropout,
layer_dropout=constant.layer_dropout,
attention_dropout=constant.attention_dropout,
relu_dropout=constant.relu_dropout,
)
elif constant.model == "ELMO":
I = 3072 if constant.use_elmo_pre else 1024
if constant.emoji:
I += 300
model = HELMoEncoder(
I=I,
H=constant.hidden_dim,
L=constant.n_layers,
bi=constant.bidirec,
C=4,
mlp=constant.mlp,
pre=constant.use_elmo_pre,
attentive=constant.attn,
multiattentive=constant.multiattn,
num_heads=constant.heads,
double_supervision=constant.double_supervision,
)
elif constant.model == "DEEPMOJI":
model = HDeepMoji(
vocab=vocab,
# embedding_size=constant.emb_dim,
hidden_size=constant.hidden_dim,
num_layers=constant.n_layers,
max_length=700,
input_dropout=constant.drop,
layer_dropout=constant.drop,
is_bidirectional=constant.bidirec,
attentive=constant.attn,
)
else:
print("Model is not defined")
exit(0)
print(model)
if not os.path.exists(constant.save_path):
os.makedirs(constant.save_path)
avg_best = train(
model,
data_loader_tr,
data_loader_val,
data_loader_test,
vocab,
patient=constant.patient,
split=i,
)
results.append(avg_best)
print("(EXPERIMENT %d) Best F1 VAL: %3.5f" % ((i + 1), avg_best))
file_summary = constant.save_path + "summary.txt"
with open(file_summary, "w") as the_file:
header = "\t".join(["SPLIT_{}".format(i) for i, _ in enumerate(results)])
the_file.write(header + "\tAVG\n")
ris = "\t".join(["{:.4f}".format(e) for i, e in enumerate(results)])
the_file.write(ris + "\t{:.4f}\n".format(np.mean(results)))