-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_inference.py
229 lines (177 loc) · 6.96 KB
/
train_inference.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
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 11 13:40:34 2021
@author: LYZ
"""
import random
import torch
import torch.nn as nn
from transformers import AdamW
from transformers import get_linear_schedule_with_warmup
import numpy as np
import util.config as config
import data_pre
from tqdm import tqdm
import util.measure as measure
import util.utils as utils
import model.BERT_arg as BERT_arg
import model.BERT_tokentype as BERT_tokentype
import time
import json
# cpu or cuda
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
seed = 68
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
def train(args, dataloader, model, optimizer, scheduler,
attention_mask_list, token_type_ids_list=None):
model.train()
preds_list, labels_list = [], []
total_loss = 0
index = 0
for data in tqdm(dataloader):
attention_mask = attention_mask_list[index]
if token_type_ids_list:
token_type_ids = token_type_ids_list[index]
else:
token_type_ids = None
model.zero_grad()
texts = data[0].to(device)
labels = data[1].to(device)
pos_topic = data[4].to(device)
loss, logits = model(texts, pos_topic=pos_topic,
token_type_ids=token_type_ids,
attention_mask=attention_mask, labels=labels)
# print("======Train", loss.shape, logits.shape)
if torch.cuda.device_count() > 1:
loss = torch.sum(loss) / torch.cuda.device_count()
preds = torch.argmax(logits, dim=1)
total_loss += loss.item()
preds_list += preds.cpu().detach().numpy().tolist()
labels_list += labels.cpu().detach().numpy().tolist()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
scheduler.step()
index += 1
loss = total_loss / len(dataloader)
acc, p, r, f1 = measure.measures(preds_list, labels_list)
return loss, acc, p, r, f1
def evaluate(args, dataloader, model, log_file,
attention_mask_list, token_type_ids_list=None):
model.eval()
preds_list, labels_list = [], []
total_loss = 0
index = 0
for data in tqdm(dataloader):
with torch.no_grad():
attention_mask = attention_mask_list[index]
if token_type_ids_list:
token_type_ids = token_type_ids_list[index]
else:
token_type_ids = None
texts = data[0].to(device)
labels = data[1].to(device)
if args.transfer_type == "2-5":
for i in range(len(labels)):
if labels[i] == 3: labels[i] = 1
else: labels[i] = 0
pos_topic = data[4].to(device)
loss, logits = model(texts, pos_topic=pos_topic,
token_type_ids=token_type_ids,
attention_mask=attention_mask, labels=labels)
if torch.cuda.device_count() > 1:
loss = torch.sum(loss) / torch.cuda.device_count()
preds = torch.argmax(logits, dim=1)
total_loss += loss.item()
preds_list += preds.cpu().detach().numpy().tolist()
labels_list += labels.cpu().detach().numpy().tolist()
index += 1
loss = total_loss / len(dataloader)
# label_path = "./preds/BioMedical_labels.json"
# label_file = open(label_path, "w")
# label_file.write(json.dumps(labels_list, indent=4, ensure_ascii=False))
# label_file.close()
# write predict labels
preds_path = "./preds/" + args.preds_name + ".json"
preds_file = open(preds_path, "w")
preds_file.write(json.dumps(preds_list, indent=4, ensure_ascii=False))
preds_file.close()
sen = "** predict labels --> {}".format(preds_path)
print(sen)
log_file.write(sen + "\n")
acc, p, r, f1 = measure.transfer_measures(args.transfer_type,
preds_list, labels_list)
return loss, preds_list, acc, p, r, f1
def main():
# get config parameters
parser = config.get_config()
args = parser.parse_args()
config.print_config(args)
# get bert config
bert_config = config.get_bert_config(args)
# write log info
log_file = open("./log/" + args.log_name + ".txt", "w")
# load data
print("** Loading Data ...")
start_time = time.time()
train_dataloader, val_dataloader, test_dataloader = data_pre.data_loader(args)
end_time = time.time()
mins, secs = divmod(end_time - start_time, 60)
sen = "** Load Data Done! Time: {}m {:.2f}s".format(mins, secs)
print(sen)
log_file.write(sen + "\n")
# create model
if args.type == "tokentype":
model = BERT_tokentype.BERT_tokentype(args, bert_config)
else:
model = BERT_arg.BERT_arg(args, bert_config)
# muti GPUs
if torch.cuda.device_count() > 1:
sen = "** GPU: Use {} GPUs".format(torch.cuda.device_count())
print(sen)
log_file.write(sen + "\n")
model = nn.DataParallel(model)
else:
sen = "** CPU: Use CPUs"
print(sen)
log_file.write(sen + "\n")
model.to(device)
# load model and predict
model.load_state_dict(torch.load("./output/" + args.model_save_name + ".pt"))
# get test attention mask
print("** Getting Test Attention Mask ...")
start_time = time.time()
attention_mask_list_test = utils.get_attention_mask(test_dataloader, args.arg_encoding)
end_time = time.time()
mins, secs = divmod(end_time - start_time, 60)
sen = "** Getting Test Attention Mask Done! Time: {}m {:.2f}s".format(mins, secs)
print(sen)
log_file.write(sen + "\n")
# get test token type ids
print("** Getting Test Token Type Ids ...")
start_time = time.time()
token_type_ids_list_test = None
if args.type == "tokentype":
token_type_ids_list_test = utils.get_token_type_ids(test_dataloader)
end_time = time.time()
mins, secs = divmod(end_time - start_time, 60)
sen = "** Getting Test Token Type Ids Done! Time: {}m {:.2f}s".format(mins, secs)
print(sen)
log_file.write(sen + "\n")
# test
print("** Testing ...")
test_loss, preds_list, test_acc, test_p, test_r, test_f1 = evaluate(args,
test_dataloader, model, log_file,
attention_mask_list_test,
token_type_ids_list_test)
# print report
measure.transfer_report_log("Test", test_loss, test_acc,
test_p, test_r, test_f1, log_file, args)
# close log file
log_file.close()
if __name__ == "__main__":
main()