-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.py
230 lines (196 loc) · 8.46 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
import sys
import os.path
import mindspore
from mindspore import Tensor, nn, Model, context
from mindspore import load_checkpoint, load_param_into_net
from mindspore import ops as P
from mindspore.ops import functional as F
from mindspore.ops import composite as C
from mindspore.common.parameter import ParameterTuple
from mindspore.train.callback import LossMonitor, CheckpointConfig, ModelCheckpoint, TimeMonitor
from mindspore.nn.loss.loss import _Loss
import numpy as np
from tqdm import tqdm
import config
import data
import model
import utils
import mindspore.context as context
import json
import math
class TrainOneStepCell(nn.Cell):
"""
Network training package class.
Wraps the network with an optimizer. The resulting Cell be trained without inputs.
Backward graph will be created in the construct function to do parameter updating. Different
parallel modes are available to run the training.
Args:
network (Cell): The training network.
optimizer (Cell): Optimizer for updating the weights.
sens (Number): The scaling number to be filled as the input of backpropagation. Default value is 1.0.
Outputs:
Tensor, a scalar Tensor with shape :math:`()`.
Examples:
>>> net = Net()
>>> loss_fn = nn.SoftmaxCrossEntropyWithLogits()
>>> optim = nn.Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> loss_net = nn.WithLossCell(net, loss_fn)
>>> train_net = nn.TrainOneStepCell(loss_net, optim)
"""
def __init__(self, network, optimizer, sens=1.0):
super(TrainOneStepCell, self).__init__(auto_prefix=False)
self.network = network
self.network.add_flags(defer_inline=True)
self.weights = ParameterTuple(network.trainable_params())
self.optimizer = optimizer
self.grad = C.GradOperation(get_by_list=True)
self.sens = sens
def construct(self, v, q, a, item, q_len):
weights = self.weights
loss = self.network(v, q, a, item, q_len)
sens = P.Fill()(P.DType()(loss), P.Shape()(loss), self.sens)
grads = self.grad(self.network, weights)(v, q, a, item, q_len)
return F.depend(loss, self.optimizer(grads))
class NLLLoss(_Loss):
'''
NLLLoss function
'''
def __init__(self, reduction='mean'):
super(NLLLoss, self).__init__(reduction)
self.reduce_sum = P.ReduceSum()
self.log_softmax = P.LogSoftmax(axis=0)
def construct(self, logits, label):
nll = -self.log_softmax(logits)
loss = self.reduce_sum(nll * label / 10, axis=1).mean()
return self.get_loss(loss)
class OutLossAccuracyWrapper(nn.Cell):
"""
The highest level cell for evaluation, wrapped with NLL Loss and accuracy. (use it directly)
Output:
output: a Tensor of shape (batch_size, config.max_answers) (logits)
loss: a scalar value
accuracy: a Tensor of shape (batch_size, 1)
"""
def __init__(self, backbone):
super(OutLossAccuracyWrapper, self).__init__()
self.net = backbone
self._loss_fn = NLLLoss()
def construct(self, v, q, a, item, q_len):
output = self.net(v, q, q_len)
loss = self._loss_fn(output, a)
accuracy = utils.batch_accuracy(output, a)
return output, loss, accuracy
class WithLossCell(nn.Cell):
"""
The cell wrapped with NLL loss, for train only
"""
def __init__(self, backbone):
super(WithLossCell, self).__init__(auto_prefix=False)
self._loss_fn = NLLLoss()
self._backbone = backbone
self.reduce_sum = P.ReduceSum()
def construct(self, v, q, a, item, q_len):
out = self._backbone(v, q, q_len)
loss = self._loss_fn(out, a)
return loss
class TrainNetWrapper(nn.Cell):
"""
The highest level train cell. (use it directly)
"""
def __init__(self, backbone):
super(TrainNetWrapper, self).__init__(auto_prefix=False)
self.net = backbone
loss_net = WithLossCell(backbone)
optimizer = nn.Adam(params=net.trainable_params(), learning_rate=config.initial_lr)
self.loss_train_net = TrainOneStepCell(loss_net, optimizer)
def construct(self, v, q, a, item, q_len):
loss = self.loss_train_net(v, q, a, item, q_len)
output = self.net(v, q, q_len)
accuracy = utils.batch_accuracy(output, a)
# print(accuracy)
return loss, accuracy
def run(net, loader, tracker, train=False, prefix='', epoch=0):
""" Run an epoch over the given loader """
arg_max = P.Argmax(axis=1, output_type=mindspore.int32)
cat = P.Concat(axis=0) # Warning: `Concat` a list of tensors is not supported in mindspore 1.1.x
if train:
net.set_train()
tracker_class, tracker_params = tracker.MovingMeanMonitor, {'momentum': 0.99}
else:
net.set_train(False)
tracker_class, tracker_params = tracker.MeanMonitor, {}
answ = []
idxs = []
accs = []
tq = tqdm(loader, desc='{} E{:03d}'.format(prefix, epoch), ncols=0, total=math.ceil(len(loader.source) / config.batch_size))
loss_tracker = tracker.track('{}_loss'.format(prefix), tracker_class(**tracker_params))
acc_tracker = tracker.track('{}_acc'.format(prefix), tracker_class(**tracker_params))
for v, q, a, idx, q_len in tq:
if train:
loss, acc = net(v, q, a, idx, q_len)
else:
output, loss, acc = net(v, q, a, idx, q_len)
answer = arg_max(output)
answ.append(answer.view(-1))
accs.append(acc.view(-1))
idxs.append(idx.view(-1))
# Update loss and accuracy in console line
loss_tracker.append(loss.asnumpy())
for a in acc.asnumpy():
acc_tracker.append(a)
fmt = '{:.4f}'.format
tq.set_postfix(loss=fmt(loss_tracker.mean.value), acc=fmt(acc_tracker.mean.value))
if not train:
# Cast to python types for JSON serialization
answ = list(map(int, list(cat(answ).asnumpy())))
accs = list(cat(accs).asnumpy().astype(float))
idxs = list(map(int, list(cat(idxs).asnumpy())))
return answ, accs, idxs
if __name__ == '__main__':
if config.device == 'GPU': os.environ['CUDA_VISIBLE_DEVICES'] = '1' # select GPU if necessary
context.set_context(mode=context.PYNATIVE_MODE, device_target=config.device)
from datetime import datetime
name = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
target_name = os.path.join('logs', '{}.ckpt'.format(name))
print('will save to {}'.format(target_name))
config_as_dict = {k: v for k, v in vars(config).items() if not k.startswith('__')}
train_loader = data.get_loader(train=True)
val_loader = data.get_loader(val=True)
net = model.Net(train_loader.source.num_tokens)
if config.pretrained:
param_dict = load_checkpoint(config.pretrained_model_path)
if param_dict is not None: print("Successfully loaded pretrained model from {}.".format(config.pretrained_model_path))
load_param_into_net(net, param_dict)
tracker = utils.Tracker()
train_net = TrainNetWrapper(net) # for train
eval_net = OutLossAccuracyWrapper(net) # for evaluation
step = 0
for epoch in range(config.epochs):
# train_loader = data.get_loader(train=True) # not sure if it matters?
"""
Wrapped train with `tqdm`
"""
run(train_net, train_loader, tracker, train=True, prefix='train', epoch=epoch)
r = run(eval_net, val_loader, tracker, train=False, prefix='val', epoch=epoch)
# Calculate the validate accuracy mean of each batch
val_acc = []
for acc_list in tracker.to_dict()['val_acc']:
val_acc.append(np.mean(acc_list).astype(float))
results = {
'name': name,
# 'tracker': tracker.to_dict(),
'accuracy': val_acc,
'config': config_as_dict,
'eval': {
'answers': r[0],
'accuracies': r[1],
'idx': r[2],
},
'vocab': train_loader.source.vocab,
}
# Save model as CKPT every 5 epochs
if epoch % 5 == 0: mindspore.save_checkpoint(train_net.net, ckpt_file_name=os.path.join('logs', '{}.ckpt'.format(name)))
# Save train meta info as JSON
with open(os.path.join('logs', 'TrainRecord_{}.json'.format(name)), 'w') as fp:
fp.write(json.dumps(results))
# train_loader.reset() # not sure if it matters?