-
Notifications
You must be signed in to change notification settings - Fork 14
/
run_old_race.py
220 lines (150 loc) · 6.99 KB
/
run_old_race.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
# -*- coding: utf-8 -*-
import random
import time
import numpy as np
import argparse
import os
from tensorboardX import SummaryWriter
from tqdm import tqdm
import torch
import torch.optim as optim
import torch.nn as nn
from torchtext import data
from torchtext import datasets
from torchtext import vocab
from Utils.utils import word_tokenize, get_device, epoch_time, classifiction_metric
from Utils.race_embedding_utils import load_race
def train(epoch_num, model, train_dataloader, dev_dataloader, optimizer, criterion, label_list, out_model_file, log_dir, print_step, clip):
model.train()
writer = SummaryWriter(
log_dir=log_dir + '/' + time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime(time.time())))
global_step = 0
best_dev_loss = float('inf')
best_acc = 0.0
for epoch in range(int(epoch_num)):
print(f'---------------- Epoch: {epoch+1:02} ----------')
epoch_loss = 0
train_steps = 0
all_preds = np.array([], dtype=int)
all_labels = np.array([], dtype=int)
for step, batch in enumerate(tqdm(train_dataloader, desc="Iteration")):
optimizer.zero_grad()
logits = model(batch)
loss = criterion(logits.view(-1, len(label_list)), batch.label)
labels = batch.label.detach().cpu().numpy()
preds = np.argmax(logits.detach().cpu().numpy(), axis=1)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), clip)
optimizer.step()
global_step += 1
epoch_loss += loss.item()
train_steps += 1
all_preds = np.append(all_preds, preds)
all_labels = np.append(all_labels, labels)
if global_step % print_step == 0:
train_loss = epoch_loss / train_steps
train_acc, train_report = classifiction_metric(
all_preds, all_labels, label_list)
dev_loss, dev_acc, dev_report = evaluate(
model, dev_dataloader, criterion, label_list)
c = global_step // print_step
writer.add_scalar("loss/train", train_loss, c)
writer.add_scalar("loss/dev", dev_loss, c)
writer.add_scalar("acc/train", train_acc, c)
writer.add_scalar("acc/dev", dev_acc, c)
for label in label_list:
writer.add_scalar(label + ":" + "f1/train",
train_report[label]['f1-score'], c)
writer.add_scalar(label + ":" + "f1/dev",
dev_report[label]['f1-score'], c)
print_list = ['macro avg', 'weighted avg']
for label in print_list:
writer.add_scalar(label + ":" + "f1/train",
train_report[label]['f1-score'], c)
writer.add_scalar(label + ":" + "f1/dev",
dev_report[label]['f1-score'], c)
# if dev_loss < best_dev_loss:
# best_dev_loss = dev_loss
if dev_acc > best_acc:
best_acc = dev_acc
torch.save(model.state_dict(), out_model_file)
model.train()
writer.close()
def evaluate(model, iterator, criterion, label_list):
model.eval()
epoch_loss = 0
all_preds = np.array([], dtype=int)
all_labels = np.array([], dtype=int)
with torch.no_grad():
for batch in iterator:
with torch.no_grad():
logits = model(batch)
loss = criterion(logits.view(-1, len(label_list)), batch.label)
labels = batch.label.detach().cpu().numpy()
preds = np.argmax(logits.detach().cpu().numpy(), axis=1)
all_preds = np.append(all_preds, preds)
all_labels = np.append(all_labels, labels)
epoch_loss += loss.item()
acc, report = classifiction_metric(
all_preds, all_labels, label_list)
return epoch_loss/len(iterator), acc, report
def main(config, model_filename):
if not os.path.exists(config.output_dir):
os.makedirs(config.output_dir)
if not os.path.exists(config.cache_dir):
os.makedirs(config.cache_dir)
model_file = os.path.join(
config.output_dir, model_filename)
# 设备准备
gpu_ids = [int(device_id) for device_id in config.gpu_ids.split()]
device, n_gpu = get_device(gpu_ids[0])
if n_gpu > 1:
n_gpu = len(gpu_ids)
# 设定随机种子
random.seed(config.seed)
torch.manual_seed(config.seed)
np.random.seed(config.seed)
if n_gpu > 0:
torch.cuda.manual_seed_all(config.seed)
torch.backends.cudnn.deterministic = True # cudnn 使用确定性算法,保证每次结果一样
# 数据准备
id_field = data.RawField()
id_field.is_target = False
text_field = data.Field(tokenize='spacy', lower=True,
include_lengths=True)
label_field = data.LabelField(dtype=torch.long)
train_iterator, dev_iterator, test_iterator = load_race(
config.data_path, id_field, text_field, label_field, config.train_batch_size, config.dev_batch_size, config.test_batch_size, device, config.glove_word_file, config.cache_dir)
# 词向量
word_emb = text_field.vocab.vectors
if config.model_name == "GAReader":
from GAReader.GAReader import GAReader
model = GAReader(
config.glove_word_dim, config.output_dim, config.hidden_size,
config.rnn_num_layers, config.ga_layers, config.bidirectional,
config.dropout, word_emb)
# optimizer = optim.Adam(model.parameters(), lr=config.lr)
optimizer = optim.SGD(model.parameters(), lr=config.lr)
criterion = nn.CrossEntropyLoss()
model = model.to(device)
criterion = criterion.to(device)
if config.do_train:
train(config.epoch_num, model, train_iterator, dev_iterator, optimizer, criterion, ['0', '1', '2', '3'], model_file, config.log_dir, config.print_step, config.clip)
model.load_state_dict(torch.load(model_file))
test_loss, test_acc, test_report = evaluate(
model, test_iterator, criterion, ['0', '1', '2', '3'])
print("-------------- Test -------------")
print("\t Loss: {} | Acc: {} | Macro avg F1: {} | Weighted avg F1: {}".format(
test_loss, test_acc, test_report['macro avg']['f1-score'], test_report['weighted avg']['f1-score']))
if __name__ == "__main__":
model_name = "GAReader"
data_dir = "/search/hadoop02/suanfa/songyingxin/data/RACE/all"
# data_dir = "/home/songyingxin/datasets/RACE/demo"
cache_dir = ".cache/"
embedding_folder = "/search/hadoop02/suanfa/songyingxin/data/embedding/glove/"
output_dir = ".old_models/"
log_dir = ".old_log/"
model_filename = "model_adam1.pt"
if model_name == "GAReader":
from GAReader import args, GAReader
main(args.get_args(data_dir, cache_dir, embedding_folder, output_dir, log_dir), model_filename)