-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_hyper.py
391 lines (315 loc) · 18.1 KB
/
train_hyper.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import argparse
import datetime
import os
import shutil
import time
import dgl
import numpy as np
import torch
from rouge import Rouge
from Hypergraph import Hypergraph
from Tester_hyper import SLTester
from module.dataloader_withedge import ExampleSet, graph_collate_fn
from module.embedding import Word_Embedding
from module.vocabulary import Vocab
from tools.logger import *
import random
_DEBUG_FLAG_ = False
def save_model(model, save_file):
with open(save_file, 'wb') as f:
torch.save(model.state_dict(), f)
logger.info('[INFO] Saving model to %s', save_file)
def setup_training(model, train_loader, valid_loader, valset, hps):
""" Does setup before starting training (run_training)
:param model: the model
:param train_loader: train dataset loader
:param valid_loader: valid dataset loader
:param valset: valid dataset which includes text and summary
:param hps: hps for model
:return:
"""
train_dir = os.path.join(hps.save_root, "train")
if os.path.exists(train_dir) and hps.restore_model != 'None':
logger.info("[INFO] Restoring %s for training...", hps.restore_model)
bestmodel_file = os.path.join(train_dir, hps.restore_model)
model.load_state_dict(torch.load(bestmodel_file))
hps.save_root = hps.save_root + "_reload"
else:
logger.info("[INFO] Create new model for training...")
if os.path.exists(train_dir):
shutil.rmtree(train_dir)
os.makedirs(train_dir)
try:
run_training(model, train_loader, valid_loader, valset, hps, train_dir)
except KeyboardInterrupt:
logger.error("[Error] Caught keyboard interrupt on worker. Stopping supervisor...")
save_model(model, os.path.join(train_dir, "earlystop"))
def run_training(model, train_loader, valid_loader, valset, hps, train_dir):
''' Repeatedly runs training iterations, logging loss to screen and log files
:param model: the model
:param train_loader: train dataset loader
:param valid_loader: valid dataset loader
:param valset: valid dataset which includes text and summary
:param hps: hps for model
:param train_dir: where to save checkpoints
:return:
'''
logger.info("[INFO] Starting run_training")
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=hps.lr)
criterion = torch.nn.CrossEntropyLoss(reduction='none')
best_train_loss = None
best_loss = None
best_F = None
non_descent_cnt = 0
saveNo = 0
for epoch in range(1, hps.n_epochs + 1):
epoch_loss = 0.0
train_loss = 0.0
epoch_start_time = time.time()
for i, (G, index, hyperedges, actual_node_list) in enumerate(train_loader):
iter_start_time = time.time()
model.train()
if hps.cuda:
G = G.to(torch.device("cuda"))
hyperedges = hyperedges.to(torch.device("cuda"))
actual_node_list = actual_node_list.to(torch.device("cuda"))
outputs = model.forward(G, hyperedges, actual_node_list)
snode_id = G.filter_nodes(lambda nodes: nodes.data["dtype"] == 1)
label = G.ndata["label"][snode_id].sum(-1)
G.nodes[snode_id].data["loss"] = criterion(outputs, label).unsqueeze(-1)
loss = dgl.sum_nodes(G, "loss")
loss = loss.mean()
if not (np.isfinite(loss.data.cpu())).numpy():
logger.error("train Loss is not finite. Stopping.")
logger.info(loss)
for name, param in model.named_parameters():
if param.requires_grad:
logger.info(name)
# logger.info(param.grad.data.sum())
raise Exception("train Loss is not finite. Stopping.")
optimizer.zero_grad()
loss.backward()
if hps.grad_clip:
torch.nn.utils.clip_grad_norm_(model.parameters(), hps.max_grad_norm)
optimizer.step()
train_loss += float(loss.data)
epoch_loss += float(loss.data)
if i % 100 == 0:
if _DEBUG_FLAG_:
for name, param in model.named_parameters():
if param.requires_grad:
logger.debug(name)
logger.debug(param.grad.data.sum())
logger.info(' | end of iter {:3d} | time: {:5.2f}s | train loss {:5.4f} | '
.format(i, (time.time() - iter_start_time),float(train_loss / 100)))
train_loss = 0.0
if hps.lr_descent:
new_lr = max(5e-6, hps.lr / (epoch + 1))
for param_group in list(optimizer.param_groups):
param_group['lr'] = new_lr
logger.info("[INFO] The learning rate now is %f", new_lr)
epoch_avg_loss = epoch_loss / len(train_loader)
logger.info(' | end of epoch {:3d} | time: {:5.2f}s | epoch train loss {:5.4f} | '
.format(epoch, (time.time() - epoch_start_time), float(epoch_avg_loss)))
if not best_train_loss or epoch_avg_loss < best_train_loss:
save_file = os.path.join(train_dir, "bestmodel")
logger.info('[INFO] Found new best model with %.3f running_train_loss. Saving to %s', float(epoch_avg_loss),
save_file)
save_model(model, save_file)
best_train_loss = epoch_avg_loss
elif epoch_avg_loss >= best_train_loss:
logger.error("[Error] training loss does not descent. Stopping supervisor...")
save_model(model, os.path.join(train_dir, "earlystop"))
sys.exit(1)
if epoch % 3 == 1:
torch.cuda.empty_cache()
best_loss, best_F, non_descent_cnt, saveNo = run_eval(model, valid_loader, valset, hps, best_loss, best_F, non_descent_cnt, saveNo)
if non_descent_cnt >= 3:
logger.error("[Error] val loss does not descent for three times. Stopping supervisor...")
save_model(model, os.path.join(train_dir, "earlystop"))
return
def run_eval(model, loader, valset, hps, best_loss, best_F, non_descent_cnt, saveNo):
'''
Repeatedly runs eval iterations, logging to screen and writing summaries. Saves the model with the best loss seen so far.
:param model: the model
:param loader: valid dataset loader
:param valset: valid dataset which includes text and summary
:param hps: hps for model
:param best_loss: best valid loss so far
:param best_F: best valid F so far
:param non_descent_cnt: the number of non descent epoch (for early stop)
:param saveNo: the number of saved models (always keep best saveNo checkpoints)
:return:
'''
logger.info("[INFO] Starting eval for this model ...")
eval_dir = os.path.join(hps.save_root, "eval")
if not os.path.exists(eval_dir):
os.makedirs(eval_dir)
model.eval()
iter_start_time = time.time()
with torch.no_grad():
tester = SLTester(model, hps.m)
for i, (G, index, hyperedges, actual_node_list) in enumerate(loader):
if hps.cuda:
G = G.to(torch.device("cuda"))
hyperedges = hyperedges.to(torch.device("cuda"))
actual_node_list = actual_node_list.to(torch.device("cuda"))
tester.evaluation(G, index, valset, hyperedges, actual_node_list)
running_avg_loss = tester.running_avg_loss
if len(tester.hyps) == 0 or len(tester.refer) == 0:
logger.error("During testing, no hyps is selected!")
return
rouge = Rouge()
scores_all = rouge.get_scores(tester.hyps, tester.refer, avg=True)
logger.info('[INFO] End of valid | time: {:5.2f}s | valid loss {:5.4f} | ' .format((time.time() - iter_start_time), float(running_avg_loss)))
res = "Rouge1:\n\tp:%.6f, r:%.6f, f:%.6f\n" % (
scores_all['rouge-1']['p'], scores_all['rouge-1']['r'], scores_all['rouge-1']['f']) \
+ "Rouge2:\n\tp:%.6f, r:%.6f, f:%.6f\n" % (
scores_all['rouge-2']['p'], scores_all['rouge-2']['r'], scores_all['rouge-2']['f']) \
+ "Rougel:\n\tp:%.6f, r:%.6f, f:%.6f\n" % (
scores_all['rouge-l']['p'], scores_all['rouge-l']['r'], scores_all['rouge-l']['f'])
logger.info(res)
tester.getMetric()
F = tester.labelMetric
if best_loss is None or running_avg_loss < best_loss:
bestmodel_save_path = os.path.join(eval_dir, 'bestmodel_%d' % (saveNo % 3)) # this is where checkpoints of best models are saved
if best_loss is not None:
logger.info(
'[INFO] Found new best model with %.6f running_avg_loss. The original loss is %.6f, Saving to %s',
float(running_avg_loss), float(best_loss), bestmodel_save_path)
else:
logger.info(
'[INFO] Found new best model with %.6f running_avg_loss. The original loss is None, Saving to %s',
float(running_avg_loss), bestmodel_save_path)
with open(bestmodel_save_path, 'wb') as f:
torch.save(model.state_dict(), f)
best_loss = running_avg_loss
non_descent_cnt = 0
saveNo += 1
else:
non_descent_cnt += 1
if best_F is None or best_F < F:
bestmodel_save_path = os.path.join(eval_dir, 'bestFmodel') # this is where checkpoints of best models are saved
if best_F is not None:
logger.info('[INFO] Found new best model with %.6f F. The original F is %.6f, Saving to %s', float(F),
float(best_F), bestmodel_save_path)
else:
logger.info('[INFO] Found new best model with %.6f F. The original F is None, Saving to %s', float(F),
bestmodel_save_path)
with open(bestmodel_save_path, 'wb') as f:
torch.save(model.state_dict(), f)
best_F = F
return best_loss, best_F, non_descent_cnt, saveNo
def main():
parser = argparse.ArgumentParser(description='Hypergraph Model')
# Where to find data
parser.add_argument('--data_dir', type=str, default='dataset/arxiv',help='The dataset directory.')
parser.add_argument('--cache_dir', type=str, default='cache/arxiv',help='The processed dataset directory')
parser.add_argument('--embedding_path', type=str, default='glove.42B.300d.txt', help='Path expression to external word embedding.')
# Important settings
parser.add_argument('--model', type=str, default='Hypergraph', help='model structure')
parser.add_argument('--restore_model', type=str, default='None', help='Restore model for further training. [bestmodel/bestFmodel/earlystop/None]')
# Where to save output
parser.add_argument('--save_root', type=str, default='models_arxiv/', help='Root directory for all model.')
parser.add_argument('--log_root', type=str, default='log_arxiv/', help='Root directory for all logging.')
# Hyperparameters
parser.add_argument('--seed', type=int, default=666, help='set the random seed [default: 666]')
parser.add_argument('--gpu', type=str, default='0', help='GPU ID to use. [default: 0]')
parser.add_argument('--cuda', action='store_true', default=True, help='GPU or CPU [default: False]')
parser.add_argument('--vocab_size', type=int, default=50000,help='Size of vocabulary. [default: 50000]')
parser.add_argument('--n_epochs', type=int, default=14, help='Number of epochs [default: 20]')
parser.add_argument('--batch_size', type=int, default=100, help='Mini batch size [default: 32]')
parser.add_argument('--n_iter', type=int, default=1, help='iteration hop [default: 1]')
parser.add_argument('--word_embedding', action='store_true', default=True, help='whether to use Word embedding [default: True]')
parser.add_argument('--word_emb_dim', type=int, default=300, help='Word embedding size [default: 300]')
parser.add_argument('--embed_train', action='store_true', default=False,help='whether to train Word embedding [default: False]')
parser.add_argument('--feat_embed_size', type=int, default=50, help='feature embedding size [default: 50]')
parser.add_argument('--n_layers', type=int, default=1, help='Number of GAT layers [default: 1]')
parser.add_argument('--lstm_hidden_state', type=int, default=128, help='size of lstm hidden state [default: 128]')
parser.add_argument('--lstm_layers', type=int, default=2, help='Number of lstm layers [default: 2]')
parser.add_argument('--bidirectional', action='store_true', default=True, help='whether to use bidirectional LSTM [default: True]')
parser.add_argument('--n_feature_size', type=int, default=128, help='size of node feature [default: 128]')
parser.add_argument('--hidden_size', type=int, default=64, help='hidden size [default: 64]')
parser.add_argument('--ffn_inner_hidden_size', type=int, default=512,help='PositionwiseFeedForward inner hidden size [default: 512]')
parser.add_argument('--n_head', type=int, default=8, help='multihead attention number [default: 8]')
parser.add_argument('--recurrent_dropout_prob', type=float, default=0.1,help='recurrent dropout prob [default: 0.1]')
parser.add_argument('--atten_dropout_prob', type=float, default=0.1, help='attention dropout prob [default: 0.1]')
parser.add_argument('--ffn_dropout_prob', type=float, default=0.1,help='PositionwiseFeedForward dropout prob [default: 0.1]')
parser.add_argument('--use_orthnormal_init', action='store_true', default=True,help='use orthnormal init for lstm [default: True]')
parser.add_argument('--sent_max_len', type=int, default=100,help='max length of sentences (max source text sentence tokens)')
parser.add_argument('--doc_max_timesteps', type=int, default=250,help='max length of documents (max timesteps of documents)')
# Training
parser.add_argument('--lr', type=float, default=0.0008, help='learning rate')
parser.add_argument('--lr_descent', action='store_true', default=True, help='learning rate descent')
parser.add_argument('--grad_clip', action='store_true', default=True, help='for gradient clipping')
parser.add_argument('--max_grad_norm', type=float, default=1.0, help='for gradient clipping max gradient normalization')
parser.add_argument('-m', type=int, default=5, help='decode summary length')
args = parser.parse_args()
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
torch.set_printoptions(threshold=50000)
# File paths
DATA_FILE = os.path.join(args.data_dir, "train.label.jsonl")
VALID_FILE = os.path.join(args.data_dir, "val.label.jsonl")
VOCAL_FILE = os.path.join(args.cache_dir, "vocab")
FILTER_WORD = os.path.join(args.cache_dir, "filter_word.txt")
HEDGE_TRAIN_FILE = os.path.join(args.data_dir, 'train.hedges.jsonl')
HEDGE_VAL_FILE = os.path.join(args.data_dir, 'val.hedges.jsonl')
LOG_PATH = args.log_root
# train_log setting
if not os.path.exists(LOG_PATH):
os.makedirs(LOG_PATH)
nowTime = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
log_path = os.path.join(LOG_PATH, "train_" + nowTime)
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.info("Pytorch %s", torch.__version__)
logger.info("[INFO] Create Vocab, vocab path is %s", VOCAL_FILE)
vocab = Vocab(VOCAL_FILE, args.vocab_size)
embed = torch.nn.Embedding(vocab.size(), args.word_emb_dim, padding_idx=0)
if args.word_embedding:
embed_loader = Word_Embedding(args.embedding_path, vocab)
vectors = embed_loader.load_my_vecs(args.word_emb_dim)
pretrained_weight = embed_loader.add_unknown_words_by_avg(vectors, args.word_emb_dim)
embed.weight.data.copy_(torch.Tensor(pretrained_weight))
embed.weight.requires_grad = args.embed_train
hps = args
logger.info(hps)
train_w2s_path = os.path.join(args.cache_dir, "train.w2s.tfidf.jsonl")
val_w2s_path = os.path.join(args.cache_dir, "val.w2s.tfidf.jsonl")
# bert_path = args.bert_path
if hps.model == "Hypergraph":
model = Hypergraph(hps, embed)
logger.info("[MODEL] Hypergraph ")
dataset = ExampleSet(DATA_FILE, vocab, hps.doc_max_timesteps, hps.sent_max_len, FILTER_WORD, train_w2s_path, HEDGE_TRAIN_FILE)
train_loader = torch.utils.data.DataLoader(dataset, batch_size=hps.batch_size, shuffle=True, num_workers=8, collate_fn=graph_collate_fn)
del dataset
valid_dataset = ExampleSet(VALID_FILE, vocab, hps.doc_max_timesteps, hps.sent_max_len, FILTER_WORD, val_w2s_path, HEDGE_VAL_FILE)
valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=hps.batch_size, shuffle=False, collate_fn=graph_collate_fn, num_workers=8)
else:
logger.error("[ERROR] Invalid Model Type!")
raise NotImplementedError("Model Type has not been implemented")
if args.cuda:
model.to(torch.device("cuda"))
logger.info("[INFO] Use cuda")
setup_training(model, train_loader, valid_loader, valid_dataset, hps)
if __name__ == '__main__':
main()