From 149d33a4cf982eb1395b928966aba39c0487ae4a Mon Sep 17 00:00:00 2001 From: "joao.guedes" Date: Sat, 11 Mar 2023 16:53:00 -0300 Subject: [PATCH 1/4] feat: add Random algorithm to GeneralRecommenders --- recbole/model/general_recommender/__init__.py | 1 + recbole/model/general_recommender/random.py | 44 +++++++++++++++++++ tests/model/test_model_auto.py | 6 +++ tests/model/test_model_manual.py | 30 ++++++++----- 4 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 recbole/model/general_recommender/random.py diff --git a/recbole/model/general_recommender/__init__.py b/recbole/model/general_recommender/__init__.py index 59cc41b47..fa4c9ffff 100644 --- a/recbole/model/general_recommender/__init__.py +++ b/recbole/model/general_recommender/__init__.py @@ -21,6 +21,7 @@ from recbole.model.general_recommender.nncf import NNCF from recbole.model.general_recommender.pop import Pop from recbole.model.general_recommender.ract import RaCT +from recbole.model.general_recommender.random import Random from recbole.model.general_recommender.recvae import RecVAE from recbole.model.general_recommender.slimelastic import SLIMElastic from recbole.model.general_recommender.spectralcf import SpectralCF diff --git a/recbole/model/general_recommender/random.py b/recbole/model/general_recommender/random.py new file mode 100644 index 000000000..446d98b68 --- /dev/null +++ b/recbole/model/general_recommender/random.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# @Time : 2023/03/01 +# @Author : João Felipe Guedes +# @Email : guedes.joaofelipe@poli.ufrj.br +# UPDATE + +r""" +Random +################################################ + +""" + +import torch +import random + +from recbole.model.abstract_recommender import GeneralRecommender +from recbole.utils import InputType, ModelType + + +class Random(GeneralRecommender): + """Random is an fundamental model that recommends random items.""" + + input_type = InputType.POINTWISE + type = ModelType.TRADITIONAL + + def __init__(self, config, dataset): + super(Random, self).__init__(config, dataset) + torch.manual_seed(config["seed"] + self.n_users + self.n_items) + self.fake_loss = torch.nn.Parameter(torch.zeros(1)) + + def forward(self): + pass + + def calculate_loss(self, interaction): + return torch.nn.Parameter(torch.zeros(1)) + + def predict(self, interaction): + return torch.rand(1).squeeze(-1) + + def full_sort_predict(self, interaction): + batch_user_num = interaction[self.USER_ID].shape[0] + result = torch.rand(self.n_items, 1).to(torch.float64) + result = torch.repeat_interleave(result.unsqueeze(0), batch_user_num, dim=0) + return result.view(-1) diff --git a/tests/model/test_model_auto.py b/tests/model/test_model_auto.py index dea05cec5..9b7d33af8 100644 --- a/tests/model/test_model_auto.py +++ b/tests/model/test_model_auto.py @@ -30,6 +30,12 @@ def test_pop(self): } quick_test(config_dict) + def test_random(self): + config_dict = { + "model": "Random", + } + quick_test(config_dict) + def test_itemknn(self): config_dict = { "model": "ItemKNN", diff --git a/tests/model/test_model_manual.py b/tests/model/test_model_manual.py index 4ed8f9c46..40c83b74e 100644 --- a/tests/model/test_model_manual.py +++ b/tests/model/test_model_manual.py @@ -26,20 +26,26 @@ class TestSequentialRecommender(unittest.TestCase): # } # quick_test(config_dict) - def test_s3rec(self): - config_dict = { - "model": "S3Rec", - "train_stage": "pretrain", - "save_step": 1, - "train_neg_sample_args": None, - } - quick_test(config_dict) + # def test_s3rec(self): + # config_dict = { + # "model": "S3Rec", + # "train_stage": "pretrain", + # "save_step": 1, + # "train_neg_sample_args": None, + # } + # quick_test(config_dict) + + # config_dict = { + # "model": "S3Rec", + # "train_stage": "finetune", + # "pre_model_path": "./saved/S3Rec-test-1.pth", + # "train_neg_sample_args": None, + # } + # quick_test(config_dict) + def test_random(self): config_dict = { - "model": "S3Rec", - "train_stage": "finetune", - "pre_model_path": "./saved/S3Rec-test-1.pth", - "train_neg_sample_args": None, + "model": "Random", } quick_test(config_dict) From d19eff68a376ec3179b6fae57904330f3bb63611 Mon Sep 17 00:00:00 2001 From: Paitesanshi <45314239+Paitesanshi@users.noreply.github.com> Date: Tue, 10 Oct 2023 09:01:59 +0800 Subject: [PATCH 2/4] FEAT: add docs --- .../user_guide/model/general/random.rst | 38 +++++++++++++++++++ recbole/model/general_recommender/random.py | 2 +- tests/model/test_model_manual.py | 30 ++++++--------- 3 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 docs/source/user_guide/model/general/random.rst diff --git a/docs/source/user_guide/model/general/random.rst b/docs/source/user_guide/model/general/random.rst new file mode 100644 index 000000000..fb6c95fd7 --- /dev/null +++ b/docs/source/user_guide/model/general/random.rst @@ -0,0 +1,38 @@ +Random +=========== + +Introduction +--------------------- + +When discussing recommendation systems, accuracy is often regarded as the most crucial metric. +However, besides accuracy, several other key metrics can evaluate the effectiveness of a recommendation system, such as diversity, coverage, and efficiency. +In this context, the random recommendation algorithm is a valuable baseline. +In terms of implementation, for a given user and item, the random recommendation algorithm provides a random rating. + +Running with RecBole + + + +**A Running Example:** + +Write the following code to a python file, such as `run.py` + +.. code:: python + + from recbole.quick_start import run_recbole + + run_recbole(model='Random', dataset='ml-100k') + +And then: + +.. code:: bash + + python run.py + + +If you want to change parameters, dataset or evaluation settings, take a look at + +- :doc:`../../../user_guide/config_settings` +- :doc:`../../../user_guide/data_intro` +- :doc:`../../../user_guide/train_eval_intro` +- :doc:`../../../user_guide/usage` \ No newline at end of file diff --git a/recbole/model/general_recommender/random.py b/recbole/model/general_recommender/random.py index 446d98b68..e2b8d2e26 100644 --- a/recbole/model/general_recommender/random.py +++ b/recbole/model/general_recommender/random.py @@ -35,7 +35,7 @@ def calculate_loss(self, interaction): return torch.nn.Parameter(torch.zeros(1)) def predict(self, interaction): - return torch.rand(1).squeeze(-1) + return torch.rand(len(interaction)).squeeze(-1) def full_sort_predict(self, interaction): batch_user_num = interaction[self.USER_ID].shape[0] diff --git a/tests/model/test_model_manual.py b/tests/model/test_model_manual.py index 40c83b74e..4ed8f9c46 100644 --- a/tests/model/test_model_manual.py +++ b/tests/model/test_model_manual.py @@ -26,26 +26,20 @@ class TestSequentialRecommender(unittest.TestCase): # } # quick_test(config_dict) - # def test_s3rec(self): - # config_dict = { - # "model": "S3Rec", - # "train_stage": "pretrain", - # "save_step": 1, - # "train_neg_sample_args": None, - # } - # quick_test(config_dict) - - # config_dict = { - # "model": "S3Rec", - # "train_stage": "finetune", - # "pre_model_path": "./saved/S3Rec-test-1.pth", - # "train_neg_sample_args": None, - # } - # quick_test(config_dict) + def test_s3rec(self): + config_dict = { + "model": "S3Rec", + "train_stage": "pretrain", + "save_step": 1, + "train_neg_sample_args": None, + } + quick_test(config_dict) - def test_random(self): config_dict = { - "model": "Random", + "model": "S3Rec", + "train_stage": "finetune", + "pre_model_path": "./saved/S3Rec-test-1.pth", + "train_neg_sample_args": None, } quick_test(config_dict) From 49df3a26be791317eed7f6c8d125df83d3e8b828 Mon Sep 17 00:00:00 2001 From: Paitesanshi <45314239+Paitesanshi@users.noreply.github.com> Date: Tue, 10 Oct 2023 09:13:55 +0800 Subject: [PATCH 3/4] FIX: update model number --- docs/source/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/index.rst b/docs/source/index.rst index bdbcde087..290b360c6 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -11,7 +11,7 @@ Introduction RecBole is a unified, comprehensive and efficient framework developed based on PyTorch. It aims to help the researchers to reproduce and develop recommendation models. -In the lastest release, our library includes 86 recommendation algorithms `[Model List]`_, covering four major categories: +In the lastest release, our library includes 87 recommendation algorithms `[Model List]`_, covering four major categories: - General Recommendation - Sequential Recommendation From 6d2ee9808b140cdc42a1b6509946ba6022fac6f6 Mon Sep 17 00:00:00 2001 From: Paitesanshi <45314239+Paitesanshi@users.noreply.github.com> Date: Tue, 10 Oct 2023 09:28:02 +0800 Subject: [PATCH 4/4] FEAT: add model_intro in docs --- docs/source/user_guide/model_intro.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/user_guide/model_intro.rst b/docs/source/user_guide/model_intro.rst index 16968c406..4d6c2e303 100644 --- a/docs/source/user_guide/model_intro.rst +++ b/docs/source/user_guide/model_intro.rst @@ -42,7 +42,7 @@ task of top-n recommendation. All the collaborative filter(CF) based models are model/general/nceplrec model/general/simplex model/general/ncl - + model/general/random Context-aware Recommendation -------------------------------