-
Notifications
You must be signed in to change notification settings - Fork 16
/
train.py
370 lines (287 loc) · 12.3 KB
/
train.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
'''
This script handling the training process.
'''
import argparse
import math
import time
import metrics
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import Constants
from model import RNNModel
from model import RRModel
from Optim import ScheduledOptim
from DataLoader import DataLoader
def get_performance(crit, pred, gold, smoothing=False, num_class=None):
''' Apply label smoothing if needed '''
# TODO: Add smoothing
if smoothing:
assert bool(num_class)
eps = 0.1
gold = gold * (1 - eps) + (1 - gold) * eps / num_class
raise NotImplementedError
loss = crit(pred, gold.contiguous().view(-1))
pred = pred.max(1)[1]
gold = gold.contiguous().view(-1)
n_correct = pred.data.eq(gold.data)
n_correct = n_correct.masked_select(gold.ne(Constants.PAD).data).sum().float()
return loss, n_correct
def get_performance_rl(pred, pred_probs, tgt, expect):
# reward = -|(log) tgt - (log) pred|
# loss = -log prob * (reward-expect)
# update expect reward prob*reward
pred_probs = torch.cat([pred_probs,torch.zeros(pred.size(0),1).cuda()],dim=1) #manully add EOS prob log(1)=0
pred_size=torch.argmax((pred==Constants.EOS).float(),dim=1)#.cpu().numpy()
pred_size=pred_size+(pred_size==0).long()*1000#pred.size(1) #no eos, the first is start
tgt_size=torch.argmax((tgt==Constants.EOS).float(),dim=1)#.cpu().numpy()
reward = pred_size.float().log()-tgt_size.float().log()
reward = - reward.mul(reward) # square of log-transformed loss
reward_sum = reward.sum().cpu().numpy()
reward = reward.cpu().numpy()
pred_size = pred_size.cpu().numpy()
seq_prob = Variable(torch.zeros(pred_probs.size(0))).cuda()
for i in range(pred_probs.size(0)):
for j in range(min(pred_size[i]+1,pred_probs.size(1))):
seq_prob[i] += pred_probs[i][j]
expect=0#-0.8
loss = -seq_prob.mul(torch.Tensor(reward-expect).cuda()).sum()
expect = seq_prob.exp().mul(torch.Tensor(reward).cuda()).sum().detach().cpu().numpy()
return loss, expect, reward_sum
def train_epoch(model, training_data, crit, optimizer,RL_setting,expect,):
''' Epoch operation in training phase'''
model.train()
total_loss = 0.0
n_total_words = 0.0
n_total_correct = 0.0
expect_new = 0.0
reward = 0.0
batch_num = 0.0
for batch in tqdm(
training_data, mininterval=2,
desc=' - (Training) ', leave=False):
# prepare data
tgt = batch
gold = tgt[:, 1:]
n_words = gold.data.ne(Constants.PAD).sum().float()
n_total_words += n_words
batch_num += tgt.size(0)
# additional RL training
if RL_setting:
optimizer.zero_grad()
pred_ids, pred_probs = model(tgt,RL_train=True)
# backward
loss_rl, expect_batch, reward_batch = get_performance_rl(pred_ids, pred_probs, tgt, expect) # with start ids
loss_rl.backward()
expect_new += expect_batch
reward += reward_batch
# update parameters
optimizer.step()
optimizer.update_learning_rate()
#if not RL_setting:
# forward
optimizer.zero_grad()
pred, *_ = model(tgt,RL_train=False)
# backward
loss, n_correct = get_performance(crit, pred, gold)
loss.backward()
# update parameters
optimizer.step()
optimizer.update_learning_rate()
# note keeping
n_total_correct += n_correct
total_loss += loss.data[0]
return total_loss/n_total_words, n_total_correct/n_total_words, expect_new/batch_num, reward/batch_num
def eval_epoch(model, validation_data, crit):
''' Epoch operation in evaluation phase '''
model.eval()
total_loss = 0
n_total_words = 0
n_total_correct = 0
reward = 0.0
batch_num = 0.0
for batch in tqdm(
validation_data, mininterval=2,
desc=' - (Validation) ', leave=False):
# prepare data
tgt = batch
gold = tgt[:, 1:]
# forward
pred, *_ = model(tgt,RL_train=False)
loss, n_correct = get_performance(crit, pred, gold)
# note keeping
n_words = gold.data.ne(Constants.PAD).sum().float()
n_total_words += n_words
n_total_correct += n_correct
total_loss += loss.data[0]
pred_ids, pred_probs = model(tgt,RL_train=True)
# backward
loss_rl, expect_batch, reward_batch = get_performance_rl(pred_ids, pred_probs, tgt, expect=0) # with start ids
reward += reward_batch
batch_num += tgt.size(0)
return total_loss/n_total_words, n_total_correct/n_total_words, reward/batch_num
def test_epoch(model, test_data, k_list=[1,5,10,20,50,100]):
''' Epoch operation in evaluation phase '''
model.eval()
scores = {}
for k in k_list:
scores['hits@' + str(k)] = 0
scores['map@' + str(k)] = 0
n_total_words = 0
reward = 0.0
batch_num = 0.0
for batch in tqdm(
test_data, mininterval=2,
desc=' - (Test) ', leave=False):
# prepare data
tgt = batch
gold = tgt[:, 1:]
# forward
pred, *_ = model(tgt,RL_train=False)
scores_batch, scores_len = metrics.portfolio(pred.detach().cpu().numpy(), gold.contiguous().view(-1).detach().cpu().numpy(), k_list)
n_total_words += scores_len
for k in k_list:
scores['hits@' + str(k)] += scores_batch['hits@' + str(k)] * scores_len
scores['map@' + str(k)] += scores_batch['map@' + str(k)] * scores_len
pred_ids, pred_probs = model(tgt,RL_train=True)
for i in range(10):
loss_rl, expect_batch, reward_batch = get_performance_rl(pred_ids, pred_probs, tgt, expect=0) # with start ids
reward += reward_batch
batch_num += tgt.size(0)
for k in k_list:
scores['hits@' + str(k)] = scores['hits@' + str(k)] / n_total_words
scores['map@' + str(k)] = scores['map@' + str(k)] / n_total_words
return scores, reward / batch_num
def train(model, training_data, validation_data, test_data, crit, optimizer, opt):
''' Start training '''
log_train_file = None
log_valid_file = None
if opt.log:
log_train_file = opt.log + '.train.log'
log_valid_file = opt.log + '.valid.log'
print('[Info] Training performance will be written to file: {} and {}'.format(
log_train_file, log_valid_file))
with open(log_train_file, 'w') as log_tf, open(log_valid_file, 'w') as log_vf:
log_tf.write('epoch,loss,ppl,accuracy\n')
log_vf.write('epoch,loss,ppl,accuracy\n')
valid_accus = []
expect = 0
for epoch_i in range(opt.epoch):
print('[ Epoch', epoch_i, ']')
if epoch_i<opt.warmup:
RL_setting = False
else:
RL_setting = True
start = time.time()
train_loss, train_accu, expect, reward = train_epoch(model, training_data, crit, optimizer,RL_setting,expect)
print(' - (Training) ppl: {ppl: 8.5f}, accuracy: {accu:3.3f} %, '\
'elapse: {elapse:3.3f} min'.format(
ppl=math.exp(min(train_loss, 100)), accu=100*train_accu,
elapse=(time.time()-start)/60))
# validation
start = time.time()
valid_loss, valid_accu, reward = eval_epoch(model, validation_data, crit)
print(' - (Validation) ppl: {ppl: 8.5f}, accuracy: {accu:3.3f} %, Reward: {rwd:3.8f} , '\
'elapse: {elapse:3.3f} min'.format(
ppl=math.exp(min(valid_loss, 100)), accu=100*valid_accu, rwd=reward,
elapse=(time.time()-start)/60))
valid_accus += [valid_accu]
model_state_dict = model.state_dict()
checkpoint = {
'model': model_state_dict,
'settings': opt,
'epoch': epoch_i}
if epoch_i>=opt.warmup-1 or epoch_i % 5==4:
# test
scores, reward_test = test_epoch(model, test_data)
print(' - (Test) ')
for metric in scores.keys():
print(metric+' '+str(scores[metric]))
print('reward '+str(reward_test)+'\n')
if opt.save_model:
if opt.save_mode == 'all':
model_name = opt.save_model + '_accu_{accu:3.3f}.chkpt'.format(accu=100*valid_accu)
torch.save(checkpoint, model_name)
elif opt.save_mode == 'best':
model_name = opt.save_model + '.chkpt'
if valid_accu >= max(valid_accus):
torch.save(checkpoint, model_name)
print(' - [Info] The checkpoint file has been updated.')
if log_train_file and log_valid_file:
with open(log_train_file, 'a') as log_tf, open(log_valid_file, 'a') as log_vf:
log_tf.write('{epoch},{loss: 8.5f},{ppl: 8.5f},{accu:3.3f}\n'.format(
epoch=epoch_i, loss=train_loss,
ppl=math.exp(min(train_loss, 100)), accu=100*train_accu))
log_vf.write('{epoch},{loss: 8.5f},{ppl: 8.5f},{accu:3.3f}\n'.format(
epoch=epoch_i, loss=valid_loss,
ppl=math.exp(min(valid_loss, 100)), accu=100*valid_accu))
def main():
torch.set_num_threads(4)
''' Main function'''
parser = argparse.ArgumentParser()
#parser.add_argument('-data', required=True)
parser.add_argument('-epoch', type=int, default=50)
parser.add_argument('-batch_size', type=int, default=16)
#parser.add_argument('-d_word_vec', type=int, default=512)
parser.add_argument('-d_model', type=int, default=64)
parser.add_argument('-d_inner_hid', type=int, default=64)
parser.add_argument('-n_warmup_steps', type=int, default=1000)
parser.add_argument('-dropout', type=float, default=0.1)
parser.add_argument('-embs_share_weight', action='store_true')
parser.add_argument('-proj_share_weight', action='store_true')
parser.add_argument('-log', default=None)
parser.add_argument('-save_model', default='model')
parser.add_argument('-save_mode', type=str, choices=['all', 'best'], default='best')
parser.add_argument('-no_cuda', action='store_true')
parser.add_argument('-network', type=int, default=0) # use social network; need features or deepwalk embeddings as initial input
parser.add_argument('-pos_emb', type=int, default=1)
parser.add_argument('-warmup', type=int, default=10) # warmup epochs
parser.add_argument('-notes', default='')
parser.add_argument('-data_name', default='twitter')
opt = parser.parse_args()
opt.cuda = not opt.no_cuda
opt.d_word_vec = opt.d_model
if opt.network==1:
opt.network = True
else:
opt.network = False
if opt.pos_emb==1:
opt.pos_emb = True
else:
opt.pos_emb = False
print(opt.notes)
#========= Preparing DataLoader =========#
train_data = DataLoader(opt.data_name, data=0, load_dict=True, batch_size=opt.batch_size, cuda=opt.cuda, loadNE=opt.network)
valid_data = DataLoader(opt.data_name, data=1, batch_size=opt.batch_size, cuda=opt.cuda, loadNE=opt.network)
test_data = DataLoader(opt.data_name, data=2, batch_size=opt.batch_size, cuda=opt.cuda, loadNE=opt.network)
opt.user_size = train_data.user_size
if opt.network:
opt.net = train_data._adj_list
opt.net_dict = train_data._adj_dict_list
opt.embeds = train_data._embeds
#========= Preparing Model =========#
#print(opt)
decoder = RNNModel('GRUCell', opt)
RLLearner = RRModel(decoder)
#print(transformer)
optimizer = ScheduledOptim(
optim.Adam(
RLLearner.parameters(),
betas=(0.9, 0.98), eps=1e-09),
opt.d_model, opt.n_warmup_steps)
def get_criterion(user_size):
''' With PAD token zero weight '''
weight = torch.ones(user_size)
weight[Constants.PAD] = 0
weight[Constants.EOS] = 1
return nn.CrossEntropyLoss(weight, size_average=False)
crit = get_criterion(train_data.user_size)
if opt.cuda:
decoder = decoder.cuda()
RLLearner = RLLearner.cuda()
crit = crit.cuda()
train(RLLearner, train_data, valid_data, test_data, crit, optimizer, opt)
if __name__ == '__main__':
main()