-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrainer.py
466 lines (412 loc) · 19.2 KB
/
trainer.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import os
from collections import OrderedDict
from logging import getLogger
from time import time
import numpy as np
import torch
import torch.optim as optim
from torch.nn.utils.clip_grad import clip_grad_norm_
from tqdm import tqdm
import torch.cuda.amp as amp
from evaluator.collector import Collector
from evaluator.evaluator import Evaluator
from utils.enum_type import EvaluatorType
from utils.logger import set_color
from utils.utils import calculate_valid_score, get_tensorboard, get_local_time, ensure_dir, get_gpu_usage, \
early_stopping, dict2str
class BiSeqRecTrainer(object):
def __init__(self, config, model):
self.config = config
self.model = model
self.logger = getLogger()
self.tensorboard = get_tensorboard(self.logger)
self.learner = config["learner"]
self.learning_rate = config["learning_rate"]
self.epochs = config["epochs"]
self.eval_step = min(config["eval_step"], self.epochs)
self.stopping_step = config["stopping_step"]
self.clip_grad_norm = config["clip_grad_norm"]
self.valid_metric = config["valid_metric"].lower()
self.valid_metric_bigger = config["valid_metric_bigger"]
self.test_batch_size = config["eval_batch_size"]
self.gpu_available = torch.cuda.is_available() and config["use_gpu"]
self.device = config["device"]
self.checkpoint_dir = config["checkpoint_dir"]
self.enable_amp = config["enable_amp"]
self.enable_scaler = torch.cuda.is_available() and config["enable_scaler"]
ensure_dir(self.checkpoint_dir)
saved_model_file = "{}-{}.pth".format(self.config["model"], get_local_time())
self.saved_model_file = os.path.join(self.checkpoint_dir, saved_model_file)
self.weight_decay = config["weight_decay"]
self.start_epoch = 0
self.cur_step = 0
self.best_valid_score = -np.inf if self.valid_metric_bigger else np.inf
self.best_valid_result = None
self.train_loss_dict = dict()
self.optimizer = self._build_optimizer()
self.eval_collector = Collector(config)
self.evaluator = Evaluator(config)
self.tot_item_num = None
def _build_optimizer(self, **kwargs):
r"""Init the Optimizer
Args:
params (torch.nn.Parameter, optional): The parameters to be optimized.
Defaults to ``self.model.parameters()``.
learner (str, optional): The name of used optimizer. Defaults to ``self.learner``.
learning_rate (float, optional): Learning rate. Defaults to ``self.learning_rate``.
weight_decay (float, optional): The L2 regularization weight. Defaults to ``self.weight_decay``.
Returns:
torch.optim: the optimizer
"""
params = kwargs.pop("params", self.model.parameters())
learner = kwargs.pop("learner", self.learner)
learning_rate = kwargs.pop("learning_rate", self.learning_rate)
weight_decay = kwargs.pop("weight_decay", self.weight_decay)
if (
self.config["reg_weight"]
and weight_decay
and weight_decay * self.config["reg_weight"] > 0
):
self.logger.warning(
"The parameters [weight_decay] and [reg_weight] are specified simultaneously, "
"which may lead to double regularization."
)
if learner.lower() == "adam":
optimizer = optim.Adam(params, lr=learning_rate, weight_decay=weight_decay)
elif learner.lower() == "sgd":
optimizer = optim.SGD(params, lr=learning_rate, weight_decay=weight_decay)
elif learner.lower() == "adagrad":
optimizer = optim.Adagrad(
params, lr=learning_rate, weight_decay=weight_decay
)
elif learner.lower() == "rmsprop":
optimizer = optim.RMSprop(
params, lr=learning_rate, weight_decay=weight_decay
)
elif learner.lower() == "sparse_adam":
optimizer = optim.SparseAdam(params, lr=learning_rate)
if weight_decay > 0:
self.logger.warning(
"Sparse Adam cannot argument received argument [{weight_decay}]"
)
else:
self.logger.warning(
"Received unrecognized optimizer, set default Adam optimizer"
)
optimizer = optim.Adam(params, lr=learning_rate)
return optimizer
def _train_epoch(self, train_data, epoch_idx, loss_func=None, show_progress=False):
r"""Train the model in an epoch
Args:
train_data (DataLoader): The train data.
epoch_idx (int): The current epoch id.
loss_func (function): The loss function of :attr:`model`. If it is ``None``, the loss function will be
:attr:`self.model.calculate_loss`. Defaults to ``None``.
show_progress (bool): Show the progress of training epoch. Defaults to ``False``.
Returns:
float/tuple: The sum of loss returned by all batches in this epoch. If the loss in each batch contains
multiple parts and the model return these multiple parts loss instead of the sum of loss, it will return a
tuple which includes the sum of loss in each part.
"""
self.model.train()
loss_func = loss_func or self.model.calculate_loss
total_loss = None
iter_data = (
tqdm(
train_data,
total=len(train_data),
ncols=100,
desc=set_color(f"Train {epoch_idx:>5}", "pink"),
)
if show_progress
else train_data
)
scaler = amp.GradScaler(enabled=self.enable_scaler)
for batch_idx, interaction in enumerate(iter_data):
interaction = self.to_device(interaction)
self.optimizer.zero_grad()
with torch.autocast(device_type=self.device.type, enabled=self.enable_amp):
losses = loss_func(interaction)
if isinstance(losses, tuple):
loss = sum(losses)
loss_tuple = tuple(per_loss.item() for per_loss in losses)
total_loss = (
loss_tuple
if total_loss is None
else tuple(map(sum, zip(total_loss, loss_tuple)))
)
else:
loss = losses
total_loss = (
losses.item() if total_loss is None else total_loss + losses.item()
)
self._check_nan(loss)
scaler.scale(loss).backward()
if self.clip_grad_norm:
clip_grad_norm_(self.model.parameters(), **self.clip_grad_norm)
scaler.step(self.optimizer)
scaler.update()
if self.gpu_available and show_progress:
iter_data.set_postfix_str(
set_color("GPU RAM: " + get_gpu_usage(self.device), "yellow")
)
return total_loss
def _valid_epoch(self, valid_data, show_progress=False):
valid_result_all = self.evaluate(valid_data, load_best_model=False, show_progress=show_progress)
valid_u_score = calculate_valid_score(valid_result_all[0], self.valid_metric)
valid_i_score = calculate_valid_score(valid_result_all[1], self.valid_metric)
valid_score = (valid_u_score + valid_i_score) / 2
valid_result = OrderedDict()
valid_result['For user'] = valid_result_all[0]
valid_result['For item'] = valid_result_all[1]
return valid_score, valid_result
def _save_checkpoint(self, epoch, verbose=True, **kwargs):
r"""Store the model parameters information and training information.
Args:
epoch (int): the current epoch id
"""
saved_model_file = kwargs.pop("saved_model_file", self.saved_model_file)
state = {
"config": self.config,
"epoch": epoch,
"cur_step": self.cur_step,
"best_valid_score": self.best_valid_score,
"state_dict": self.model.state_dict(),
"optimizer": self.optimizer.state_dict(),
}
torch.save(state, saved_model_file, pickle_protocol=4)
if verbose:
self.logger.info(
set_color("Saving current", "blue") + f": {saved_model_file}"
)
def _check_nan(self, loss):
if torch.isnan(loss):
raise ValueError("Training loss is nan")
def _generate_train_loss_output(self, epoch_idx, s_time, e_time, losses):
des = self.config["loss_decimal_place"] or 4
train_loss_output = (
set_color("epoch %d training", "green")
+ " ["
+ set_color("time", "blue")
+ ": %.2fs, "
) % (epoch_idx, e_time - s_time)
if isinstance(losses, tuple):
des = set_color("train_loss%d", "blue") + ": %." + str(des) + "f"
train_loss_output += ", ".join(
des % (idx + 1, loss) for idx, loss in enumerate(losses)
)
else:
des = "%." + str(des) + "f"
train_loss_output += set_color("train loss", "blue") + ": " + des % losses
return train_loss_output + "]"
def _add_train_loss_to_tensorboard(self, epoch_idx, losses, tag="Loss/Train"):
if isinstance(losses, tuple):
for idx, loss in enumerate(losses):
self.tensorboard.add_scalar(tag + str(idx), loss, epoch_idx)
else:
self.tensorboard.add_scalar(tag, losses, epoch_idx)
def _add_hparam_to_tensorboard(self, best_valid_result):
# base hparam
hparam_dict = {
"learner": self.config["learner"],
"learning_rate": self.config["learning_rate"],
"train_batch_size": self.config["train_batch_size"],
}
hparam_dict.update(
{
para: val
for para, val in self.config.final_config_dict.items()
}
)
for k in hparam_dict:
if hparam_dict[k] is not None and not isinstance(
hparam_dict[k], (bool, str, float, int)
):
hparam_dict[k] = str(hparam_dict[k])
self.tensorboard.add_hparams(
hparam_dict, {"hparam/best_valid_result": best_valid_result}
)
def fit(
self,
train_data,
valid_data=None,
verbose=True,
saved=True,
show_progress=False,
callback_fn=None,
):
r"""Train the model based on the train data and the valid data.
Args:
train_data (DataLoader): the train data
valid_data (DataLoader, optional): the valid data, default: None.
If it's None, the early_stopping is invalid.
verbose (bool, optional): whether to write training and evaluation information to logger, default: True
saved (bool, optional): whether to save the model parameters, default: True
show_progress (bool): Show the progress of training epoch and evaluate epoch. Defaults to ``False``.
callback_fn (callable): Optional callback function executed at end of epoch.
Includes (epoch_idx, valid_score) input arguments.
Returns:
(float, dict): best valid score and best valid result. If valid_data is None, it returns (-1, None)
"""
if saved and self.start_epoch >= self.epochs:
self._save_checkpoint(-1, verbose=verbose)
self.eval_collector.data_collect(train_data)
valid_step = 0
for epoch_idx in range(self.start_epoch, self.epochs):
# train
training_start_time = time()
train_loss = self._train_epoch(
train_data, epoch_idx, show_progress=show_progress
)
self.train_loss_dict[epoch_idx] = (
sum(train_loss) if isinstance(train_loss, tuple) else train_loss
)
training_end_time = time()
train_loss_output = self._generate_train_loss_output(
epoch_idx, training_start_time, training_end_time, train_loss
)
if verbose:
self.logger.info(train_loss_output)
self._add_train_loss_to_tensorboard(epoch_idx, train_loss)
# eval
if self.eval_step <= 0 or not valid_data:
if saved:
self._save_checkpoint(epoch_idx, verbose=verbose)
continue
if (epoch_idx + 1) % self.eval_step == 0:
valid_start_time = time()
valid_score, valid_result = self._valid_epoch(
valid_data, show_progress=show_progress
)
(
self.best_valid_score,
self.cur_step,
stop_flag,
update_flag,
) = early_stopping(
valid_score,
self.best_valid_score,
self.cur_step,
max_step=self.stopping_step,
bigger=self.valid_metric_bigger,
)
valid_end_time = time()
valid_score_output = (
set_color("epoch %d evaluating", "green")
+ " ["
+ set_color("time", "blue")
+ ": %.2fs, "
+ set_color("valid_score", "blue")
+ ": %f]"
) % (epoch_idx, valid_end_time - valid_start_time, valid_score)
valid_result_str = "\n".join([str(direct) + " : " + dict2str(value)
for direct, value in valid_result.items()])
valid_result_output = (
set_color("valid result", "blue") + ": \n" + valid_result_str
)
if verbose:
self.logger.info(valid_score_output)
self.logger.info(valid_result_output)
self.tensorboard.add_scalar("Vaild_score", valid_score, epoch_idx)
if update_flag:
if saved:
self._save_checkpoint(epoch_idx, verbose=verbose)
self.best_valid_result = valid_result
if callback_fn:
callback_fn(epoch_idx, valid_score)
if stop_flag:
stop_output = "Finished training, best eval result in epoch %d" % (
epoch_idx - self.cur_step * self.eval_step
)
if verbose:
self.logger.info(stop_output)
break
valid_step += 1
self._add_hparam_to_tensorboard(self.best_valid_score)
return self.best_valid_score, self.best_valid_result
def _batch_eval(self, batched_data):
pos_interaction, all_interaction, row_idx, positive_u, positive_i = batched_data
pos_interaction = self.to_device(pos_interaction)
all_interaction = self.to_device(all_interaction)
try:
origin_scores = self.model.neg_sample_predict(pos_interaction, all_interaction,
item_field=self.config["ITEM_ID_FIELD"])
except Exception:
origin_scores = self.model.predict(all_interaction)
if self.config["eval_type"] == EvaluatorType.VALUE:
return all_interaction, origin_scores, positive_u, positive_i
elif self.config["eval_type"] == EvaluatorType.RANKING:
col_idx = all_interaction[self.config["ITEM_ID_FIELD"]]
batch_user_num = positive_u[-1] + 1
scores = torch.full(
(batch_user_num, self.tot_item_num), -np.inf, device=self.device
)
scores[row_idx, col_idx] = origin_scores
return all_interaction, scores, positive_u, positive_i
@torch.no_grad()
def evaluate_one_direct(
self, eval_data, load_best_model=True, model_file=None, show_progress=False
):
r"""Evaluate the model based on the eval data.
Args:
eval_data (DataLoader): the eval data
load_best_model (bool, optional): whether load the best model in the training process, default: True.
It should be set True, if users want to test the model after training.
model_file (str, optional): the saved model file, default: None. If users want to test the previously
trained model file, they can set this parameter.
show_progress (bool): Show the progress of evaluate epoch. Defaults to ``False``.
Returns:
collections.OrderedDict: eval result, key is the eval metric and value in the corresponding metric value.
"""
if not eval_data:
return
if load_best_model:
checkpoint_file = model_file or self.saved_model_file
checkpoint = torch.load(checkpoint_file, map_location=self.device)
self.model.load_state_dict(checkpoint["state_dict"])
message_output = "Loading model structure and parameters from {}".format(
checkpoint_file
)
self.logger.info(message_output)
self.model.eval()
if self.config["eval_type"] == EvaluatorType.RANKING:
if "user" in eval_data._dataset.phase:
self.tot_item_num = eval_data._dataset.item_num
else:
self.tot_item_num = eval_data._dataset.user_num
iter_data = (
tqdm(
eval_data,
total=len(eval_data),
ncols=100,
desc=set_color(f"Evaluate ", "pink"),
)
if show_progress
else eval_data
)
for batch_idx, batched_data in enumerate(iter_data):
interaction, scores, positive_u, positive_i = self._batch_eval(batched_data)
if self.gpu_available and show_progress:
iter_data.set_postfix_str(
set_color("GPU RAM: " + get_gpu_usage(self.device), "yellow")
)
self.eval_collector.eval_batch_collect(
scores, interaction, positive_u, positive_i
)
self.eval_collector.model_collect(self.model)
struct = self.eval_collector.get_data_struct()
result = self.evaluator.evaluate(struct)
return result
@torch.no_grad()
def evaluate(self, eval_data, load_best_model=True, model_file=None, show_progress=False):
test_result_u = self.evaluate_one_direct(eval_data[0], load_best_model=load_best_model,
model_file=model_file, show_progress=show_progress)
self.config.change_direction()
test_result_i = self.evaluate_one_direct(eval_data[1], load_best_model=load_best_model,
model_file=model_file, show_progress=show_progress)
self.config.change_direction()
return test_result_u, test_result_i
def to_device(self, data):
for key, value in data.items():
data[key] = value.to(self.device)
return data