-
Notifications
You must be signed in to change notification settings - Fork 1
/
trainer.py
228 lines (202 loc) · 9.69 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
from time import time
import torch
from torch.nn.utils.clip_grad import clip_grad_norm_
from tqdm import tqdm
import torch.cuda.amp as amp
from recbole.trainer import Trainer
from recbole.utils import early_stopping, dict2str, set_color, get_gpu_usage
class MyTrainer(Trainer):
def __init__(self, config, model):
super(MyTrainer, self).__init__(config, model)
self.warm_up_step = config['warm_up_step'] if config['warm_up_step'] is not None else 0
self.warm_up_step_loss = config['warm_up_step_loss'] if config['warm_up_step_loss'] is not None else 0
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
)
if not self.config["single_spec"] and train_data.shuffle:
train_data.sampler.set_epoch(epoch_idx)
scaler = amp.GradScaler(enabled=self.enable_scaler)
for batch_idx, interaction in enumerate(iter_data):
self.batch_scale = len(train_data)
interaction = interaction.to(self.device)
self.optimizer.zero_grad()
sync_loss = 0
if not self.config["single_spec"]:
self.set_reduce_hook()
sync_loss = self.sync_grad_loss()
with torch.autocast(device_type=self.device.type, enabled=self.enable_amp):
losses = loss_func(interaction)
if isinstance(losses, tuple):
if epoch_idx < self.warm_up_step_loss:
losses = losses[:3]
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 + sync_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 _generate_train_loss_output(self, epoch_idx, s_time, e_time, total_train_time, losses):
des = self.config["loss_decimal_place"] or 4
train_loss_output = (
set_color("epoch %d training", "green")
+ " ["
+ set_color("time", "blue")
+ ": %.2fs, "
+ set_color("total_train_time", "blue")
+ ": %.2fs, "
) % (epoch_idx, e_time - s_time, total_train_time)
if isinstance(losses, tuple):
des = set_color("train_loss%d", "blue") + ": %." + str(des) + "f"
train_loss_output += ", ".join(
des % (idx + 1, loss / self.batch_scale) for idx, loss in enumerate(losses)
)
else:
des = "%." + str(des) + "f"
train_loss_output += set_color("train loss", "blue") + ": " + des % (losses / self.batch_scale)
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 / self.batch_scale, epoch_idx)
else:
self.tensorboard.add_scalar(tag, losses / self.batch_scale, epoch_idx)
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)
if self.config["train_neg_sample_args"].get("dynamic", False):
train_data.get_model(self.model)
valid_step = 0
# my configuration
total_train_time = 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()
total_train_time += (training_end_time - training_start_time)
train_loss_output = self._generate_train_loss_output(
epoch_idx, training_start_time, training_end_time, total_train_time, train_loss
)
if verbose:
self.logger.info(train_loss_output)
self._add_train_loss_to_tensorboard(epoch_idx, train_loss)
self.wandblogger.log_metrics(
{"epoch": epoch_idx, "train_loss": train_loss, "train_step": epoch_idx},
head="train",
)
# 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_output = (
set_color("valid result", "blue") + ": \n" + dict2str(valid_result)
)
if verbose:
self.logger.info(valid_score_output)
self.logger.info(valid_result_output)
self.tensorboard.add_scalar("Vaild_score", valid_score, epoch_idx)
self.wandblogger.log_metrics(
{**valid_result, "valid_step": valid_step}, head="valid"
)
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 and epoch_idx > self.warm_up_step:
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