From 9a6cdd23cd5e0796a9b03485b6657d121d2a7770 Mon Sep 17 00:00:00 2001 From: Yupeng Hou Date: Mon, 11 Jan 2021 11:39:48 +0000 Subject: [PATCH 1/7] FIX: bugs of module init in GRU4Rec & KSR --- recbole/model/sequential_recommender/gru4rec.py | 4 ++-- recbole/model/sequential_recommender/ksr.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/recbole/model/sequential_recommender/gru4rec.py b/recbole/model/sequential_recommender/gru4rec.py index 94d542174..0ea93e331 100644 --- a/recbole/model/sequential_recommender/gru4rec.py +++ b/recbole/model/sequential_recommender/gru4rec.py @@ -70,8 +70,8 @@ def _init_weights(self, module): if isinstance(module, nn.Embedding): xavier_normal_(module.weight) elif isinstance(module, nn.GRU): - xavier_uniform_(self.gru_layers.weight_hh_l0) - xavier_uniform_(self.gru_layers.weight_ih_l0) + xavier_uniform_(module.weight_hh_l0) + xavier_uniform_(module.weight_ih_l0) def forward(self, item_seq, item_seq_len): item_seq_emb = self.item_embedding(item_seq) diff --git a/recbole/model/sequential_recommender/ksr.py b/recbole/model/sequential_recommender/ksr.py index 3941f1814..bf48c6719 100644 --- a/recbole/model/sequential_recommender/ksr.py +++ b/recbole/model/sequential_recommender/ksr.py @@ -84,8 +84,8 @@ def _init_weights(self, module): if isinstance(module, nn.Embedding): xavier_normal_(module.weight) elif isinstance(module, nn.GRU): - xavier_uniform_(self.gru_layers.weight_hh_l0) - xavier_uniform_(self.gru_layers.weight_ih_l0) + xavier_uniform_(module.weight_hh_l0) + xavier_uniform_(module.weight_ih_l0) def _get_kg_embedding(self, head): """Difference: From e8a4e4fb2eb986be46a6861153783b8361d2c42b Mon Sep 17 00:00:00 2001 From: Yupeng Hou Date: Mon, 11 Jan 2021 12:55:07 +0000 Subject: [PATCH 2/7] FIX: utilize ensure dir to create dir --- recbole/data/utils.py | 5 ++--- recbole/utils/logger.py | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/recbole/data/utils.py b/recbole/data/utils.py index 56be8c3fe..3f44d9a7d 100644 --- a/recbole/data/utils.py +++ b/recbole/data/utils.py @@ -19,7 +19,7 @@ from recbole.config import EvalSetting from recbole.data.dataloader import * from recbole.sampler import KGSampler, Sampler, RepeatableSampler -from recbole.utils import ModelType +from recbole.utils import ModelType, ensure_dir def create_dataset(config): @@ -216,8 +216,7 @@ def save_datasets(save_path, name, dataset): raise ValueError(f'Length of name {name} should equal to length of dataset {dataset}.') for i, d in enumerate(dataset): cur_path = os.path.join(save_path, name[i]) - if not os.path.isdir(cur_path): - os.makedirs(cur_path) + ensure_dir(cur_path) d.save(cur_path) diff --git a/recbole/utils/logger.py b/recbole/utils/logger.py index 5c9b50319..a5e079c2a 100644 --- a/recbole/utils/logger.py +++ b/recbole/utils/logger.py @@ -11,7 +11,7 @@ import logging import os -from recbole.utils.utils import get_local_time +from recbole.utils import get_local_time, ensure_dir def init_logger(config): @@ -30,8 +30,7 @@ def init_logger(config): """ LOGROOT = './log/' dir_name = os.path.dirname(LOGROOT) - if not os.path.exists(dir_name): - os.makedirs(dir_name) + ensure_dir(dir_name) logfilename = '{}-{}.log'.format(config['model'], get_local_time()) From b893cf018fe50580d81c4da786228c98a0066c68 Mon Sep 17 00:00:00 2001 From: Yupeng Hou Date: Mon, 11 Jan 2021 13:00:44 +0000 Subject: [PATCH 3/7] FIX: rm self.batch_size from abstract recommender --- recbole/model/abstract_recommender.py | 2 -- recbole/model/general_recommender/dgcf.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/recbole/model/abstract_recommender.py b/recbole/model/abstract_recommender.py index 64b202fb5..d5d7f8dc0 100644 --- a/recbole/model/abstract_recommender.py +++ b/recbole/model/abstract_recommender.py @@ -91,7 +91,6 @@ def __init__(self, config, dataset): self.n_items = dataset.num(self.ITEM_ID) # load parameters info - self.batch_size = config['train_batch_size'] self.device = config['device'] @@ -145,7 +144,6 @@ def __init__(self, config, dataset): self.n_relations = dataset.num(self.RELATION_ID) # load parameters info - self.batch_size = config['train_batch_size'] self.device = config['device'] diff --git a/recbole/model/general_recommender/dgcf.py b/recbole/model/general_recommender/dgcf.py index bdaec9e2e..3560dd2b9 100644 --- a/recbole/model/general_recommender/dgcf.py +++ b/recbole/model/general_recommender/dgcf.py @@ -74,7 +74,7 @@ def __init__(self, config, dataset): self.n_layers = config['n_layers'] self.reg_weight = config['reg_weight'] self.cor_weight = config['cor_weight'] - n_batch = dataset.dataset.inter_num // self.batch_size + 1 + n_batch = dataset.dataset.inter_num // config['train_batch_size'] + 1 self.cor_batch_size = int(max(self.n_users / n_batch, self.n_items / n_batch)) # ensure embedding can be divided into intent assert self.embedding_size % self.n_factors == 0 From b53475b30cc4c497576f847835908b2f87509ba4 Mon Sep 17 00:00:00 2001 From: Yupeng Hou Date: Mon, 11 Jan 2021 13:11:53 +0000 Subject: [PATCH 4/7] FIX: remove useless comment from trainer --- recbole/trainer/trainer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recbole/trainer/trainer.py b/recbole/trainer/trainer.py index b761d9926..3af49161f 100644 --- a/recbole/trainer/trainer.py +++ b/recbole/trainer/trainer.py @@ -64,7 +64,7 @@ class Trainer(AbstractTrainer): 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. - More information can be found in [placeholder]. `model` is the instantiated object of a Model Class. + `model` is the instantiated object of a Model Class. """ From ba3cd4d86e827b4e9c72fc8e40b8bbe73b26e127 Mon Sep 17 00:00:00 2001 From: Yupeng Hou Date: Mon, 11 Jan 2021 13:21:20 +0000 Subject: [PATCH 5/7] FIX: bugs of import error in logger --- recbole/utils/logger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recbole/utils/logger.py b/recbole/utils/logger.py index a5e079c2a..76559710f 100644 --- a/recbole/utils/logger.py +++ b/recbole/utils/logger.py @@ -11,7 +11,7 @@ import logging import os -from recbole.utils import get_local_time, ensure_dir +from recbole.utils.utils import get_local_time, ensure_dir def init_logger(config): From 295249375d095d7d578da7c3743719b33e193c23 Mon Sep 17 00:00:00 2001 From: Yupeng Hou Date: Tue, 12 Jan 2021 02:40:56 +0000 Subject: [PATCH 6/7] FIX: move plt import from head to specific func --- recbole/trainer/trainer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recbole/trainer/trainer.py b/recbole/trainer/trainer.py index 3af49161f..9d2c3a8ae 100644 --- a/recbole/trainer/trainer.py +++ b/recbole/trainer/trainer.py @@ -16,7 +16,6 @@ from logging import getLogger from time import time -import matplotlib.pyplot as plt import numpy as np import torch import torch.optim as optim @@ -422,6 +421,7 @@ def plot_train_loss(self, show=True, save_path=None): 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 epochs = list(self.train_loss_dict.keys()) epochs.sort() values = [float(self.train_loss_dict[epoch]) for epoch in epochs] From b70dcdfc9e63270e96978940e6c4a04644bc5617 Mon Sep 17 00:00:00 2001 From: Yupeng Hou Date: Tue, 12 Jan 2021 03:20:10 +0000 Subject: [PATCH 7/7] FIX: utilize dlapi to simplify codes --- recbole/data/dataloader/abstract_dataloader.py | 7 ------- recbole/data/dataset/dataset.py | 6 ++++++ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/recbole/data/dataloader/abstract_dataloader.py b/recbole/data/dataloader/abstract_dataloader.py index d722212d2..73e642472 100644 --- a/recbole/data/dataloader/abstract_dataloader.py +++ b/recbole/data/dataloader/abstract_dataloader.py @@ -55,13 +55,6 @@ def __init__(self, config, dataset, batch_size=1, dl_format=InputType.POINTWISE, if self.real_time is None: self.real_time = True - self.join = self.dataset.join - self.history_item_matrix = self.dataset.history_item_matrix - self.history_user_matrix = self.dataset.history_user_matrix - self.inter_matrix = self.dataset.inter_matrix - self.get_user_feature = self.dataset.get_user_feature - self.get_item_feature = self.dataset.get_item_feature - for dataset_attr in self.dataset._dataloader_apis: try: flag = hasattr(self.dataset, dataset_attr) diff --git a/recbole/data/dataset/dataset.py b/recbole/data/dataset/dataset.py index a5d1893b5..22c5c8b60 100644 --- a/recbole/data/dataset/dataset.py +++ b/recbole/data/dataset/dataset.py @@ -1173,6 +1173,7 @@ def _check_field(self, *field_names): if getattr(self, field_name, None) is None: raise ValueError(f'{field_name} isn\'t set.') + @dlapi.set() def join(self, df): """Given interaction feature, join user/item feature into it. @@ -1429,6 +1430,7 @@ def save(self, filepath): if df is not None: df.to_csv(os.path.join(filepath, f'{name}.csv')) + @dlapi.set() def get_user_feature(self): """ Returns: @@ -1440,6 +1442,7 @@ def get_user_feature(self): else: return self.user_feat + @dlapi.set() def get_item_feature(self): """ Returns: @@ -1536,6 +1539,7 @@ def _create_graph(self, tensor_feat, source_field, target_field, form='dgl', val else: raise NotImplementedError(f'Graph format [{form}] has not been implemented.') + @dlapi.set() def inter_matrix(self, form='coo', value_field=None): """Get sparse matrix that describe interactions between user_id and item_id. @@ -1617,6 +1621,7 @@ def _history_matrix(self, row, value_field=None): return torch.LongTensor(history_matrix), torch.FloatTensor(history_value), torch.LongTensor(history_len) + @dlapi.set() def history_item_matrix(self, value_field=None): """Get dense matrix describe user's history interaction records. @@ -1641,6 +1646,7 @@ def history_item_matrix(self, value_field=None): """ return self._history_matrix(row='user', value_field=value_field) + @dlapi.set() def history_user_matrix(self, value_field=None): """Get dense matrix describe item's history interaction records.