forked from twjiang/MIMO_CFE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
338 lines (291 loc) · 12.6 KB
/
train.py
File metadata and controls
338 lines (291 loc) · 12.6 KB
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
import sys, os, io
import random
import json
import torch
import logging
import argparse
import gensim
import struct
import math
import itertools
import copy
import time
import warnings
if sys.version_info[0] == 2:
import cPickle as pickle
else:
import pickle
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.autograd as autograd
from torch.autograd import Variable
from sklearn.utils import shuffle
from pytorch_pretrained_bert import BertTokenizer, BertModel
from utils import *
from MIMO import *
from data_center import *
warnings.filterwarnings('ignore')
parser = argparse.ArgumentParser(description='Implement of SISO, SIMO, MISO, MIMO for Conditional Statement Extraction')
# Model parameters.
parser.add_argument('--train', type=str, default='data/stmts-train.tsv',
help='location of the labeled training set')
parser.add_argument('--eval', type=str, default='data/stmts-eval.tsv',
help='location of the evaluation set')
parser.add_argument('--model_name', type=str, default='MIMO_BERT_LSTM',
help='the model to be trained')
parser.add_argument('--language_model', type=str, default='resources/model.pt',
help='language model checkpoint to use')
parser.add_argument('--wordembed', type=str, default='resources/pubmed-vectors=50.bin',
help='wordembedding file for words')
parser.add_argument('--out_model', type=str, default='./models/supervised_model',
help='location of the saved model')
parser.add_argument('--out_file', type=str, default='./results/evaluation_supervised_model',
help='location of the saved results')
parser.add_argument('--config', type=str, default='',
help='gates for three input sequence, i.e. LM(gate1, gate2, gate3), POS(gate1, gate2, gate3), CAP(gate1, gate2, gate3)')
parser.add_argument('--seed', type=int, default=824,
help='random seed')
parser.add_argument('--epochs', type=int, default=1000)
parser.add_argument('--nu_datasets', type=int, default=6)
parser.add_argument('--num_pass', type=int, default=5,
help='num of pass for evaluation')
parser.add_argument('--cuda', action='store_true',
help='use CUDA')
parser.add_argument('--pretrain', action='store_true')
parser.add_argument('--is_semi', action='store_true')
parser.add_argument('--udata', type=str, default='./udata/stmts-demo-unlabeled-pubmed',
help='location of the unlabeled data')
parser.add_argument('--AR', action='store_true')
parser.add_argument('--TC', action='store_true')
parser.add_argument('--TCDEL', action='store_true')
parser.add_argument('--SH', action='store_true')
parser.add_argument('--DEL', action='store_true')
parser.add_argument('--run_eval', action='store_true')
args = parser.parse_args()
if torch.cuda.is_available():
if not args.cuda:
print("WARNING: You have a CUDA device, so you should probably run with --cuda")
else:
device_id = torch.cuda.current_device()
print('using device', device_id, torch.cuda.get_device_name(device_id))
device = torch.device("cuda" if args.cuda else "cpu")
print('DEVICE:', device)
if __name__ == '__main__':
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
logging.debug(args)
models = []
multi_head = None
multi_head_two = None
max_f1 = 0 # max macro-f1 of validation
max_std = 0 # max std of macro-f1 of validation
batch_size = 35
dim = 50 # dimension of WE
input_size = dim # input size of encoder
hidden_dim = 300 # the number of LSTM units in encoder layer
bert_hidden_dim = 768
dataCenter = DataCenter(args.train, args.eval)
_weight_classes_fact = []
for _id in range(len(dataCenter.ID2Tag_fact)):
if args.is_semi:
_weight_classes_fact.append(1.0)
else:
_weight_classes_fact.append((1.0/dataCenter.Tag2Num[dataCenter.ID2Tag_fact[_id]])*1000)
weight_classes_fact = torch.FloatTensor(_weight_classes_fact)
print(weight_classes_fact)
weight_classes_fact = weight_classes_fact.to(device)
_weight_classes_condition = []
for _id in range(len(dataCenter.ID2Tag_condition)):
if args.is_semi:
_weight_classes_condition.append(1.0)
else:
_weight_classes_condition.append((1.0/dataCenter.Tag2Num[dataCenter.ID2Tag_condition[_id]])*1000)
weight_classes_condition = torch.FloatTensor(_weight_classes_condition)
print(weight_classes_condition)
weight_classes_condition = weight_classes_condition.to(device)
out_model_name = args.out_model+'_'+args.model_name
out_file = args.out_file+'_'+args.model_name
LM_model = None
LM_corpus = None
tokenizer = None
if args.model_name in ['MIMO_LSTM', 'MIMO_LSTM_TF', 'MIMO_BERT_LSTM', 'MIMO_BERT_LSTM_TF']:
config_list = [bool(int(i)) for i in args.config]
assert len(config_list) == 9
lm_config = config_list[:3]
postag_config = config_list[3:6]
cap_config = config_list[6:9]
print('lm config', lm_config)
print('postag config', postag_config)
print('cap config', cap_config)
# decodeing config for multi_heads
configs = []
if True in lm_config:
configs.append([lm_config, [False]*3, [False]*3])
if True in postag_config:
configs.append([[False]*3, postag_config, [False]*3])
if True in cap_config:
configs.append([[False]*3, [False]*3, cap_config])
print(configs)
if len(configs) > 1:
print('There are more than one featrues, thus multi_heads will be used.')
if len(configs) == 0:
print('The model without any input-features is to be trained.')
configs.append([[False]*3, [False]*3, [False]*3])
if not args.model_name.startswith('MIMO_BERT'):
wv = Gensim(args.wordembed, dim)
word2vec = wv.word2vec_dict
PAD = '<pad>'
WordEmbedding = [word2vec[PAD].view(1, -1),]
Word2ID = dict()
ID2Word = dict()
Word2ID[PAD] = 0
ID2Word[0] = PAD
for word in word2vec:
if word == PAD or word in Word2ID:
continue
_id = len(WordEmbedding)
Word2ID[word] = _id
ID2Word[_id] = word
WordEmbedding.append(word2vec[word].view(1, -1))
WordEmbedding = torch.cat(WordEmbedding)
for config in configs:
print('creating model by', config)
lm_config, postag_config, cap_config = config
if args.model_name == 'MIMO_LSTM':
mimo = MIMO_LSTM(WordEmbedding, Word2ID, dataCenter.POS2ID, dataCenter.CAP2ID, dim, input_size, hidden_dim, len(dataCenter.Tag2ID_fact), len(dataCenter.Tag2ID_condition), lm_config, postag_config, cap_config, device)
elif args.model_name == 'MIMO_LSTM_TF':
mimo = MIMO_LSTM_TF(WordEmbedding, Word2ID, dataCenter.POS2ID, dataCenter.CAP2ID, dim, input_size, hidden_dim, len(dataCenter.Tag2ID_fact), len(dataCenter.Tag2ID_condition), lm_config, postag_config, cap_config, device)
elif args.model_name == 'MIMO_BERT_LSTM':
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
assert '[UNK]' in tokenizer.vocab
mimo = MIMO_BERT_LSTM(dataCenter.POS2ID, dataCenter.CAP2ID, bert_hidden_dim, len(dataCenter.Tag2ID_fact), len(dataCenter.Tag2ID_condition), lm_config, postag_config, cap_config, device)
else:
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
assert '[UNK]' in tokenizer.vocab
mimo = MIMO_BERT_LSTM_TF(dataCenter.POS2ID, dataCenter.CAP2ID, bert_hidden_dim, len(dataCenter.Tag2ID_fact), len(dataCenter.Tag2ID_condition), lm_config, postag_config, cap_config, device)
config_str = ''.join([str(int(i)) for i in np.reshape(config, 9)])
if (not args.pretrain) or len(configs) > 1 or args.is_semi:
print('loading pretrained model ...')
name = 'models/pre_supervised_model_'+args.model_name+'_'+config_str+'.torch'
if not args.pretrain and len(configs) > 1:
name = 'models/supervised_model_'+args.model_name+'_'+config_str
print(name)
try:
mimo = torch.load(name)
except:
print('please train the single figure model first:', config_str)
sys.exit(1)
if len(configs) > 1:
for param in mimo.parameters():
param.requires_grad = False
if args.run_eval:
if args.pretrain:
name = 'models/pre_supervised_model_'+args.model_name+'_'+config_str+'.torch'
print(name)
else:
name = 'models/supervised_model_'+args.model_name+'_'+config_str
print(name)
mimo = torch.load(name)
mimo.to(device)
models.append(mimo)
assert len(models) == len(configs)
if len(configs) > 1:
_hidden_dim = bert_hidden_dim if 'BERT' in args.model_name else hidden_dim*2
multi_head = Multi_head_Net(_hidden_dim, len(dataCenter.Tag2ID_fact))
multi_head.to(device)
if args.run_eval:
print('loading multi_head model ...')
if args.pretrain:
multi_head = torch.load('models/pre_supervised_model_'+args.model_name+'_'+args.config+'.torch_multi_head')
else:
multi_head = torch.load('models/supervised_model_'+args.model_name+'_'+args.config+'_multi_head')
with open(args.language_model, 'rb') as f:
LM_model = torch.load(f)
LM_model.eval()
LM_corpus = Corpus()
out_model_name += ('_'+args.config)
out_file += ('_'+args.config)
elif args.model_name == 'MIMO_BERT':
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
assert '[UNK]' in tokenizer.vocab
mimo = MIMO_BERT('bert-base-uncased')
mimo.to(device)
mimo_extractor_fact = None
mimo_extractor_cond = None
if not args.pretrain:
_hidden_dim = bert_hidden_dim if 'BERT' in args.model_name else hidden_dim*2
mimo_extractor_fact = Extractor(_hidden_dim, len(dataCenter.Tag2ID_fact), 'fact')
mimo_extractor_cond = Extractor(_hidden_dim, len(dataCenter.Tag2ID_condition), 'cond')
if args.run_eval:
print('loading mo_extractor ...')
mimo_extractor_fact = torch.load('models/supervised_model_'+args.model_name+'_'+args.config+'_extractor_fact')
mimo_extractor_cond = torch.load('models/supervised_model_'+args.model_name+'_'+args.config+'_extractor_cond')
mimo_extractor_fact.to(device)
mimo_extractor_cond.to(device)
if args.is_semi:
out_model_name += '_SeT'
out_file += '_SeT'
data_file = './auto_ldata/labeled'
if args.AR:
out_model_name += '_AR'
out_file += '_AR'
data_file += '_AR'
if args.TC:
out_model_name += '_TC'
out_file += '_TC'
data_file += '_TC'
if args.TCDEL:
out_model_name += '_TCDEL'
out_file += '_TCDEL'
data_file += '_TCDEL'
if args.SH:
out_model_name += '_SH'
out_file += '_SH'
data_file += '_SH'
if args.DEL:
out_model_name += '_DEL'
out_file += '_DEL'
data_file += '_DEL'
udata_file = args.udata+'_part-1.tsv'
data_file += '_'+udata_file.split('/')[-1]
nu_datasets = args.nu_datasets
else:
nu_datasets = 1
out_file += '.txt'
if args.pretrain:
out_model_name = out_model_name.replace('supervised', 'pre_supervised') + '.torch'
out_file = out_file.replace('evaluation', 'pre_evaluation')
print('out_model_name =', out_model_name)
print('out_file =', out_file)
for index in range(nu_datasets):
if args.is_semi:
udata_file = udata_file.replace('part'+str(index-1), 'part'+str(index))
data_file = data_file.replace('part'+str(index-1), 'part'+str(index))
print('udata_file =', udata_file)
print('data_file =', data_file)
dataCenter.loading_dataset(None, None, udata_file)
auto_labeling(models, dataCenter, device, data_file, args.AR, args.TC, args.TCDEL, args.SH, args.DEL, LM_model=LM_model, LM_corpus=LM_corpus, tokenizer=tokenizer, mimo_extractor_fact=mimo_extractor_fact, mimo_extractor_cond=mimo_extractor_cond, pretrain=args.pretrain, multi_head=multi_head, multi_head_two=multi_head_two)
dataCenter.loading_dataset(None, None, data_file)
for epoch in range(args.epochs):
if not args.run_eval:
print('[epoch-%d] training ..' % epoch)
models_update = apply_model(models, batch_size, dataCenter, device, weight_classes_fact, weight_classes_condition, LM_model=LM_model, LM_corpus=LM_corpus, tokenizer=tokenizer, mimo_extractor_fact=mimo_extractor_fact, mimo_extractor_cond=mimo_extractor_cond, pretrain=args.pretrain, multi_head=multi_head, multi_head_two=multi_head_two, is_semi=args.is_semi, eval_pack=[out_file, max_f1, max_std, out_model_name, args.num_pass])
if args.is_semi:
print('empty_cache')
torch.cuda.empty_cache()
for model in models:
print("loading model parameters...")
model = torch.load(out_model_name+model.name)
print("loading done.")
max_f1 = 0
max_std = 0
else:
models_update = []
args.num_pass = 1
if not args.is_semi:
print('validation ...')
max_f1, max_std = evaluation(models, out_file, dataCenter, 0, 0, max_f1, max_std, out_model_name, args.num_pass, False, write_prediction=True, file_name2='./predicts/'+out_file.split('/')[-1], LM_model=LM_model, just_PR=False, LM_corpus=LM_corpus, tokenizer=tokenizer, mimo_extractor_fact=mimo_extractor_fact, mimo_extractor_cond=mimo_extractor_cond, pretrain=args.pretrain, multi_head=multi_head, models_update=models_update, multi_head_two=multi_head_two)