forked from RUCAIBox/RecBole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
1131 lines (944 loc) · 47.6 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# @Time : 2020/6/26
# @Author : Shanlei Mu
# @Email : [email protected]
# UPDATE:
# @Time : 2020/8/7, 2020/9/26, 2020/9/26, 2020/10/01, 2020/9/16
# @Author : Zihan Lin, Yupeng Hou, Yushuo Chen, Shanlei Mu, Xingyu Pan
# UPDATE:
# @Time : 2020/10/8, 2020/10/15, 2020/11/20, 2021/2/20, 2021/3/3, 2021/3/5
# @Author : Hui Wang, Xinyan Fan, Chen Yang, Yibo Li, Lanling Xu, Haoran Cheng
r"""
recbole.trainer.trainer
################################
"""
import os
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
from recbole.data.interaction import Interaction
from recbole.evaluator import ProxyEvaluator
from recbole.utils import ensure_dir, get_local_time, early_stopping, calculate_valid_score, dict2str, \
DataLoaderType, KGDataLoaderState
from recbole.utils.utils import set_color
###
"""
# @Time : 2021/09/10
# @Author : Juyong Jiang
# @Email : [email protected]
"""
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
from torch.nn.parallel import DistributedDataParallel
###
class AbstractTrainer(object):
r"""Trainer Class is used to manage the training and evaluation processes of recommender system models.
AbstractTrainer is an abstract class in which the fit() and evaluate() method should be implemented according
to different training and evaluation strategies.
"""
def __init__(self, config, model):
self.config = config
self.model = model
def fit(self, train_data):
r"""Train the model based on the train data.
"""
raise NotImplementedError('Method [next] should be implemented.')
def evaluate(self, eval_data):
r"""Evaluate the model based on the eval data.
"""
raise NotImplementedError('Method [next] should be implemented.')
class Trainer(AbstractTrainer):
r"""The basic Trainer for basic training and evaluation strategies in recommender systems. This class defines common
functions for training and evaluation processes of most recommender system models, including fit(), evaluate(),
resume_checkpoint() and some other features helpful for model training and evaluation.
Generally speaking, this class can serve most recommender system models, If the training process of the model is to
simply optimize a single loss without involving any complex training strategies, such as adversarial learning,
pre-training and so on.
Initializing the Trainer needs two parameters: `config` and `model`. `config` records the parameters information
for controlling training and evaluation, such as `learning_rate`, `epochs`, `eval_step` and so on.
`model` is the instantiated object of a Model Class.
"""
def __init__(self, config, model):
super(Trainer, self).__init__(config, model)
self.logger = getLogger()
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.device = config['device']
self.checkpoint_dir = config['checkpoint_dir']
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.draw_loss_pic = config['draw_loss_pic']
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.model.parameters())
self.eval_type = config['eval_type']
self.evaluator = ProxyEvaluator(config)
self.item_tensor = None
self.tot_item_num = None
###@Juyong Jiang
#you can turn on or off(None) this setting in your `config.yaml`
self.multi_gpus = config['multi_gpus']
if torch.cuda.device_count() > 1 and self.multi_gpus:
self._build_distribute(backend="nccl")
print("Let's use", torch.cuda.device_count(), "GPUs to train ", self.config['model'], "...")
def _build_optimizer(self, params):
r"""Init the Optimizer
Returns:
torch.optim: the optimizer
"""
if self.config['reg_weight'] and self.weight_decay and self.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 self.learner.lower() == 'adam':
optimizer = optim.Adam(params, lr=self.learning_rate, weight_decay=self.weight_decay)
elif self.learner.lower() == 'sgd':
optimizer = optim.SGD(params, lr=self.learning_rate, weight_decay=self.weight_decay)
elif self.learner.lower() == 'adagrad':
optimizer = optim.Adagrad(params, lr=self.learning_rate, weight_decay=self.weight_decay)
elif self.learner.lower() == 'rmsprop':
optimizer = optim.RMSprop(params, lr=self.learning_rate, weight_decay=self.weight_decay)
elif self.learner.lower() == 'sparse_adam':
optimizer = optim.SparseAdam(params, lr=self.learning_rate)
if self.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=self.learning_rate)
return optimizer
###@Juyong Jiang
def _build_distribute(self, backend):
# 1 set backend
torch.distributed.init_process_group(backend=backend)
# 2 get distributed id
local_rank = torch.distributed.get_rank()
torch.cuda.set_device(local_rank)
device_dis = torch.device("cuda", local_rank)
# 3, 4 assign model to be distributed
self.model.to(device_dis)
self.model = DistributedDataParallel(self.model,
device_ids=[local_rank],
output_device=local_rank).module
return self.model
def _trans_dataload(self, interaction):
data_dict = {}
#using pytorch dataload to re-wrap dataset
def sub_trans(dataset):
dis_loader = DataLoader(dataset=dataset,
batch_size=dataset.shape[0],
sampler=DistributedSampler(dataset, shuffle=False))
for data in dis_loader:
batch_data = data
return batch_data
#change `interaction` datatype to a python `dict` object.
#for some methods, you may need transfer more data unit like the following way.
data_dict[self.config['USER_ID_FIELD']] = sub_trans(interaction[self.config['USER_ID_FIELD']])
data_dict[self.config['ITEM_ID_FIELD']] = sub_trans(interaction[self.config['ITEM_ID_FIELD']])
data_dict[self.config['TIME_FIELD']] = sub_trans(interaction[self.config['TIME_FIELD']])
data_dict[self.config['ITEM_LIST_LENGTH_FIELD']] = sub_trans(interaction[self.config['ITEM_LIST_LENGTH_FIELD']])
data_dict['item_id_list'] = sub_trans(interaction['item_id_list'])
data_dict['timestamp_list'] = sub_trans(interaction['timestamp_list'])
return data_dict
###
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(
enumerate(train_data),
total=len(train_data),
desc=set_color(f"Train {epoch_idx:>5}", 'pink'),
) if show_progress else enumerate(train_data)
)
for batch_idx, interaction in iter_data:
interaction = interaction.to(self.device)
###@Juyong Jiang
#in fact, it costs ignorable time to transfer the dataset.
if torch.cuda.device_count() > 1 and self.multi_gpus:
# import time
# start_ct = time.time()
interaction = self._trans_dataload(interaction)
# end_ct = time.time()
# print('Dataset Converting Time: ', end_ct-start_ct)
###
self.optimizer.zero_grad()
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)
loss.backward()
if self.clip_grad_norm:
clip_grad_norm_(self.model.parameters(), **self.clip_grad_norm)
self.optimizer.step()
return total_loss
def _valid_epoch(self, valid_data, show_progress=False):
r"""Valid the model with valid data
Args:
valid_data (DataLoader): the valid data.
show_progress (bool): Show the progress of evaluate epoch. Defaults to ``False``.
Returns:
float: valid score
dict: valid result
"""
valid_result = self.evaluate(valid_data, load_best_model=False, show_progress=show_progress)
valid_score = calculate_valid_score(valid_result, self.valid_metric)
return valid_score, valid_result
def _save_checkpoint(self, epoch):
r"""Store the model parameters information and training information.
Args:
epoch (int): the current epoch id
"""
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, self.saved_model_file)
def resume_checkpoint(self, resume_file):
r"""Load the model parameters information and training information.
Args:
resume_file (file): the checkpoint file
"""
resume_file = str(resume_file)
checkpoint = torch.load(resume_file)
self.start_epoch = checkpoint['epoch'] + 1
self.cur_step = checkpoint['cur_step']
self.best_valid_score = checkpoint['best_valid_score']
# load architecture params from checkpoint
if checkpoint['config']['model'].lower() != self.config['model'].lower():
self.logger.warning(
'Architecture configuration given in config file is different from that of checkpoint. '
'This may yield an exception while state_dict is being loaded.'
)
self.model.load_state_dict(checkpoint['state_dict'])
# load optimizer state from checkpoint only when optimizer type is not changed
self.optimizer.load_state_dict(checkpoint['optimizer'])
message_output = 'Checkpoint loaded. Resume training from epoch {}'.format(self.start_epoch)
self.logger.info(message_output)
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 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)
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)
# eval
if self.eval_step <= 0 or not valid_data:
if saved:
self._save_checkpoint(epoch_idx)
update_output = set_color('Saving current', 'blue') + ': %s' % self.saved_model_file
if verbose:
self.logger.info(update_output)
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)
if update_flag:
if saved:
self._save_checkpoint(epoch_idx)
update_output = set_color('Saving current best', 'blue') + ': %s' % self.saved_model_file
if verbose:
self.logger.info(update_output)
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
if self.draw_loss_pic:
save_path = '{}-{}-train_loss.pdf'.format(self.config['model'], get_local_time())
self.plot_train_loss(save_path=os.path.join(save_path))
return self.best_valid_score, self.best_valid_result
def _full_sort_batch_eval(self, batched_data):
interaction, history_index, swap_row, swap_col_after, swap_col_before = batched_data
try:
# Note: interaction without item ids
scores = self.model.full_sort_predict(interaction.to(self.device))
except NotImplementedError:
new_inter = interaction.to(self.device).repeat_interleave(self.tot_item_num)
batch_size = len(new_inter)
new_inter.update(self.item_tensor[:batch_size])
if batch_size <= self.test_batch_size:
scores = self.model.predict(new_inter)
else:
scores = self._spilt_predict(new_inter, batch_size)
scores = scores.view(-1, self.tot_item_num)
scores[:, 0] = -np.inf
if history_index is not None:
scores[history_index] = -np.inf
swap_row = swap_row.to(self.device)
swap_col_after = swap_col_after.to(self.device)
swap_col_before = swap_col_before.to(self.device)
scores[swap_row, swap_col_after] = scores[swap_row, swap_col_before]
return interaction, scores
@torch.no_grad()
def evaluate(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:
dict: eval result, key is the eval metric and value in the corresponding metric value.
"""
if not eval_data:
return
if load_best_model:
if model_file:
checkpoint_file = model_file
else:
checkpoint_file = self.saved_model_file
checkpoint = torch.load(checkpoint_file)
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 eval_data.dl_type == DataLoaderType.FULL:
if self.item_tensor is None:
self.item_tensor = eval_data.get_item_feature().to(self.device).repeat(eval_data.step)
self.tot_item_num = eval_data.dataset.item_num
batch_matrix_list = []
iter_data = (
tqdm(
enumerate(eval_data),
total=len(eval_data),
desc=set_color(f"Evaluate ", 'pink'),
) if show_progress else enumerate(eval_data)
)
for batch_idx, batched_data in iter_data:
if eval_data.dl_type == DataLoaderType.FULL:
interaction, scores = self._full_sort_batch_eval(batched_data)
else:
interaction = batched_data
batch_size = interaction.length
if batch_size <= self.test_batch_size:
scores = self.model.predict(interaction.to(self.device))
else:
scores = self._spilt_predict(interaction, batch_size)
batch_matrix = self.evaluator.collect(interaction, scores)
batch_matrix_list.append(batch_matrix)
result = self.evaluator.evaluate(batch_matrix_list, eval_data)
return result
def _spilt_predict(self, interaction, batch_size):
spilt_interaction = dict()
for key, tensor in interaction.interaction.items():
spilt_interaction[key] = tensor.split(self.test_batch_size, dim=0)
num_block = (batch_size + self.test_batch_size - 1) // self.test_batch_size
result_list = []
for i in range(num_block):
current_interaction = dict()
for key, spilt_tensor in spilt_interaction.items():
current_interaction[key] = spilt_tensor[i]
result = self.model.predict(Interaction(current_interaction).to(self.device))
if len(result.shape) == 0:
result = result.unsqueeze(0)
result_list.append(result)
return torch.cat(result_list, dim=0)
def plot_train_loss(self, show=True, save_path=None):
r"""Plot the train loss in each epoch
Args:
show (bool, optional): Whether to show this figure, default: True
save_path (str, optional): The data path to save the figure, default: None.
If it's None, it will not be saved.
"""
import matplotlib.pyplot as plt
import time
epochs = list(self.train_loss_dict.keys())
epochs.sort()
values = [float(self.train_loss_dict[epoch]) for epoch in epochs]
plt.plot(epochs, values)
my_x_ticks = np.arange(0, len(epochs), int(len(epochs) / 10))
plt.xticks(my_x_ticks)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title(self.config['model'] + ' ' + time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time())))
if show:
plt.show()
if save_path:
plt.savefig(save_path)
class KGTrainer(Trainer):
r"""KGTrainer is designed for Knowledge-aware recommendation methods. Some of these models need to train the
recommendation related task and knowledge related task alternately.
"""
def __init__(self, config, model):
super(KGTrainer, self).__init__(config, model)
self.train_rec_step = config['train_rec_step']
self.train_kg_step = config['train_kg_step']
def _train_epoch(self, train_data, epoch_idx, loss_func=None, show_progress=False):
if self.train_rec_step is None or self.train_kg_step is None:
interaction_state = KGDataLoaderState.RSKG
elif epoch_idx % (self.train_rec_step + self.train_kg_step) < self.train_rec_step:
interaction_state = KGDataLoaderState.RS
else:
interaction_state = KGDataLoaderState.KG
train_data.set_mode(interaction_state)
if interaction_state in [KGDataLoaderState.RSKG, KGDataLoaderState.RS]:
return super()._train_epoch(train_data, epoch_idx, show_progress=show_progress)
elif interaction_state in [KGDataLoaderState.KG]:
return super()._train_epoch(
train_data, epoch_idx, loss_func=self.model.calculate_kg_loss, show_progress=show_progress
)
return None
class KGATTrainer(Trainer):
r"""KGATTrainer is designed for KGAT, which is a knowledge-aware recommendation method.
"""
def __init__(self, config, model):
super(KGATTrainer, self).__init__(config, model)
def _train_epoch(self, train_data, epoch_idx, loss_func=None, show_progress=False):
# train rs
train_data.set_mode(KGDataLoaderState.RS)
rs_total_loss = super()._train_epoch(train_data, epoch_idx, show_progress=show_progress)
# train kg
train_data.set_mode(KGDataLoaderState.KG)
kg_total_loss = super()._train_epoch(
train_data, epoch_idx, loss_func=self.model.calculate_kg_loss, show_progress=show_progress
)
# update A
self.model.eval()
with torch.no_grad():
self.model.update_attentive_A()
return rs_total_loss, kg_total_loss
class S3RecTrainer(Trainer):
r"""S3RecTrainer is designed for S3Rec, which is a self-supervised learning based sequential recommenders.
It includes two training stages: pre-training ang fine-tuning.
"""
def __init__(self, config, model):
super(S3RecTrainer, self).__init__(config, model)
def save_pretrained_model(self, epoch, saved_model_file):
r"""Store the model parameters information and training information.
Args:
epoch (int): the current epoch id
saved_model_file (str): file name for saved pretrained model
"""
state = {
'config': self.config,
'epoch': epoch,
'state_dict': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
}
torch.save(state, saved_model_file)
def pretrain(self, train_data, verbose=True, show_progress=False):
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)
if (epoch_idx + 1) % self.config['save_step'] == 0:
saved_model_file = os.path.join(
self.checkpoint_dir,
'{}-{}-{}.pth'.format(self.config['model'], self.config['dataset'], str(epoch_idx + 1))
)
self.save_pretrained_model(epoch_idx, saved_model_file)
update_output = set_color('Saving current', 'blue') + ': %s' % saved_model_file
if verbose:
self.logger.info(update_output)
return self.best_valid_score, self.best_valid_result
def fit(self, train_data, valid_data=None, verbose=True, saved=True, show_progress=False, callback_fn=None):
if self.model.train_stage == 'pretrain':
return self.pretrain(train_data, verbose, show_progress)
elif self.model.train_stage == 'finetune':
return super().fit(train_data, valid_data, verbose, saved, show_progress, callback_fn)
else:
raise ValueError("Please make sure that the 'train_stage' is 'pretrain' or 'finetune' ")
class MKRTrainer(Trainer):
r"""MKRTrainer is designed for MKR, which is a knowledge-aware recommendation method.
"""
def __init__(self, config, model):
super(MKRTrainer, self).__init__(config, model)
self.kge_interval = config['kge_interval']
def _train_epoch(self, train_data, epoch_idx, loss_func=None, show_progress=False):
rs_total_loss, kg_total_loss = 0., 0.
# train rs
self.logger.info('Train RS')
train_data.set_mode(KGDataLoaderState.RS)
rs_total_loss = super()._train_epoch(
train_data, epoch_idx, loss_func=self.model.calculate_rs_loss, show_progress=show_progress
)
# train kg
if epoch_idx % self.kge_interval == 0:
self.logger.info('Train KG')
train_data.set_mode(KGDataLoaderState.KG)
kg_total_loss = super()._train_epoch(
train_data, epoch_idx, loss_func=self.model.calculate_kg_loss, show_progress=show_progress
)
return rs_total_loss, kg_total_loss
class TraditionalTrainer(Trainer):
r"""TraditionalTrainer is designed for Traditional model(Pop,ItemKNN), which set the epoch to 1 whatever the config.
"""
def __init__(self, config, model):
super(TraditionalTrainer, self).__init__(config, model)
self.epochs = 1 # Set the epoch to 1 when running memory based model
class DecisionTreeTrainer(AbstractTrainer):
"""DecisionTreeTrainer is designed for DecisionTree model.
"""
def __init__(self, config, model):
super(DecisionTreeTrainer, self).__init__(config, model)
self.logger = getLogger()
self.label_field = config['LABEL_FIELD']
self.convert_token_to_onehot = self.config['convert_token_to_onehot']
# evaluator
self.eval_type = config['eval_type']
self.epochs = config['epochs']
self.eval_step = min(config['eval_step'], self.epochs)
self.valid_metric = config['valid_metric'].lower()
self.evaluator = ProxyEvaluator(config)
# model saved
self.checkpoint_dir = config['checkpoint_dir']
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)
def _interaction_to_sparse(self, dataloader):
r"""Convert data format from interaction to sparse or numpy
Args:
dataloader (DecisionTreeDataLoader): DecisionTreeDataLoader dataloader.
Returns:
cur_data (sparse or numpy): data.
interaction_np[self.label_field] (numpy): label.
"""
interaction = dataloader.dataset[:]
interaction_np = interaction.numpy()
cur_data = np.array([])
columns = []
for key, value in interaction_np.items():
value = np.resize(value, (value.shape[0], 1))
if key != self.label_field:
columns.append(key)
if cur_data.shape[0] == 0:
cur_data = value
else:
cur_data = np.hstack((cur_data, value))
if self.convert_token_to_onehot == True:
from scipy import sparse
from scipy.sparse import dok_matrix
convert_col_list = dataloader.dataset.convert_col_list
hash_count = dataloader.dataset.hash_count
new_col = cur_data.shape[1] - len(convert_col_list)
for key, values in hash_count.items():
new_col = new_col + values
onehot_data = dok_matrix((cur_data.shape[0], new_col))
cur_j = 0
new_j = 0
for key in columns:
if key in convert_col_list:
for i in range(cur_data.shape[0]):
onehot_data[i, int(new_j + cur_data[i, cur_j])] = 1
new_j = new_j + hash_count[key] - 1
else:
for i in range(cur_data.shape[0]):
onehot_data[i, new_j] = cur_data[i, cur_j]
cur_j = cur_j + 1
new_j = new_j + 1
cur_data = sparse.csc_matrix(onehot_data)
return cur_data, interaction_np[self.label_field]
def _interaction_to_lib_datatype(self, dataloader):
pass
def _valid_epoch(self, valid_data):
r"""
Args:
valid_data (DecisionTreeDataLoader): DecisionTreeDataLoader, which is the same with GeneralDataLoader.
"""
valid_result = self.evaluate(valid_data)
valid_score = calculate_valid_score(valid_result, self.valid_metric)
return valid_result, valid_score
def fit(self, train_data, valid_data=None, verbose=True, saved=True, show_progress=False):
# load model
if self.boost_model is not None:
self.model.load_model(self.boost_model)
self.best_valid_score = 0.
self.best_valid_result = 0.
for epoch_idx in range(self.epochs):
self._train_at_once(train_data, valid_data)
if (epoch_idx + 1) % self.eval_step == 0:
# evaluate
valid_start_time = time()
valid_result, valid_score = self._valid_epoch(valid_data)
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.best_valid_score = valid_score
self.best_valid_result = valid_result
return self.best_valid_score, self.best_valid_result
def evaluate(self, eval_data):
pass
class xgboostTrainer(DecisionTreeTrainer):
"""xgboostTrainer is designed for XGBOOST.
"""
def __init__(self, config, model):
super(xgboostTrainer, self).__init__(config, model)
self.xgb = __import__('xgboost')
self.boost_model = config['xgb_model']
self.silent = config['xgb_silent']
self.nthread = config['xgb_nthread']
# train params
self.params = config['xgb_params']
self.num_boost_round = config['xgb_num_boost_round']
self.evals = ()
self.early_stopping_rounds = config['xgb_early_stopping_rounds']
self.evals_result = {}
self.verbose_eval = config['xgb_verbose_eval']
self.callbacks = None
def _interaction_to_lib_datatype(self, dataloader):
r"""Convert data format from interaction to DMatrix
Args:
dataloader (DecisionTreeDataLoader): xgboost dataloader.
Returns:
DMatrix: Data in the form of 'DMatrix'.
"""
data, label = self._interaction_to_sparse(dataloader)
return self.xgb.DMatrix(data=data, label=label, silent=self.silent, nthread=self.nthread)
def _train_at_once(self, train_data, valid_data):
r"""
Args:
train_data (DecisionTreeDataLoader): DecisionTreeDataLoader, which is the same with GeneralDataLoader.
valid_data (DecisionTreeDataLoader): DecisionTreeDataLoader, which is the same with GeneralDataLoader.
"""
self.dtrain = self._interaction_to_lib_datatype(train_data)
self.dvalid = self._interaction_to_lib_datatype(valid_data)
self.evals = [(self.dtrain, 'train'), (self.dvalid, 'valid')]
self.model = self.xgb.train(
self.params,
self.dtrain,
self.num_boost_round,
self.evals,
early_stopping_rounds=self.early_stopping_rounds,
evals_result=self.evals_result,
verbose_eval=self.verbose_eval,
xgb_model=self.boost_model,
callbacks=self.callbacks
)
self.model.save_model(self.saved_model_file)
self.boost_model = self.saved_model_file
def evaluate(self, eval_data, load_best_model=True, model_file=None, show_progress=False):
self.eval_pred = torch.Tensor()
self.eval_true = torch.Tensor()
self.deval = self._interaction_to_lib_datatype(eval_data)
self.eval_true = torch.Tensor(self.deval.get_label())
self.eval_pred = torch.Tensor(self.model.predict(self.deval))
batch_matrix_list = [[torch.stack((self.eval_true, self.eval_pred), 1)]]
result = self.evaluator.evaluate(batch_matrix_list, eval_data)
return result
class RaCTTrainer(Trainer):
r"""RaCTTrainer is designed for RaCT, which is an actor-critic reinforcement learning based general recommenders.
It includes three training stages: actor pre-training, critic pre-training and actor-critic training.
"""
def __init__(self, config, model):
super(RaCTTrainer, self).__init__(config, model)
self.pretrain_epochs = self.config['pretrain_epochs']
def save_pretrained_model(self, epoch, saved_model_file):
r"""Store the model parameters information and training information.
Args:
epoch (int): the current epoch id
saved_model_file (str): file name for saved pretrained model
"""
state = {
'config': self.config,
'epoch': epoch,
'state_dict': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
}
torch.save(state, saved_model_file)
def pretrain(self, train_data, verbose=True, show_progress=False):
for epoch_idx in range(self.start_epoch, self.pretrain_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)
if (epoch_idx + 1) % self.pretrain_epochs == 0:
saved_model_file = os.path.join(
self.checkpoint_dir,
'{}-{}-{}.pth'.format(self.config['model'], self.config['dataset'], str(epoch_idx + 1))
)
self.save_pretrained_model(epoch_idx, saved_model_file)
update_output = 'Saving current: %s' % saved_model_file
if verbose:
self.logger.info(update_output)
return self.best_valid_score, self.best_valid_result
def fit(self, train_data, valid_data=None, verbose=True, saved=True, show_progress=False, callback_fn=None):
if self.model.train_stage == 'actor_pretrain':
return self.pretrain(train_data, verbose, show_progress)
elif self.model.train_stage == "critic_pretrain":
return self.pretrain(train_data, verbose, show_progress)
elif self.model.train_stage == 'finetune':
return super().fit(train_data, valid_data, verbose, saved, show_progress, callback_fn)
else:
raise ValueError("Please make sure that the 'train_stage' is 'pretrain' or 'finetune' ")
class lightgbmTrainer(DecisionTreeTrainer):
"""lightgbmTrainer is designed for lightgbm.
"""
def __init__(self, config, model):
super(lightgbmTrainer, self).__init__(config, model)
self.lgb = __import__('lightgbm')
self.boost_model = config['lgb_model']
self.silent = config['lgb_silent']
# train params
self.params = config['lgb_params']
self.num_boost_round = config['lgb_num_boost_round']
self.evals = ()
self.early_stopping_rounds = config['lgb_early_stopping_rounds']
self.evals_result = {}
self.verbose_eval = config['lgb_verbose_eval']
self.learning_rates = config['lgb_learning_rates']
self.callbacks = None
def _interaction_to_lib_datatype(self, dataloader):
r"""Convert data format from interaction to Dataset
Args:
dataloader (DecisionTreeDataLoader): xgboost dataloader.
Returns:
dataset(lgb.Dataset): Data in the form of 'lgb.Dataset'.
"""
data, label = self._interaction_to_sparse(dataloader)
return self.lgb.Dataset(data=data, label=label, silent=self.silent)
def _train_at_once(self, train_data, valid_data):
r"""
Args:
train_data (DecisionTreeDataLoader): DecisionTreeDataLoader, which is the same with GeneralDataLoader.
valid_data (DecisionTreeDataLoader): DecisionTreeDataLoader, which is the same with GeneralDataLoader.
"""
self.dtrain = self._interaction_to_lib_datatype(train_data)
self.dvalid = self._interaction_to_lib_datatype(valid_data)
self.evals = [self.dtrain, self.dvalid]
self.model = self.lgb.train(
self.params,
self.dtrain,
self.num_boost_round,
self.evals,
early_stopping_rounds=self.early_stopping_rounds,
evals_result=self.evals_result,
verbose_eval=self.verbose_eval,
learning_rates=self.learning_rates,
init_model=self.boost_model,
callbacks=self.callbacks
)
self.model.save_model(self.saved_model_file)
self.boost_model = self.saved_model_file
def evaluate(self, eval_data, load_best_model=True, model_file=None, show_progress=False):
self.eval_pred = torch.Tensor()
self.eval_true = torch.Tensor()
self.deval_data, self.deval_label = self._interaction_to_sparse(eval_data)
self.eval_true = torch.Tensor(self.deval_label)