Skip to content

Commit

Permalink
feat: add Random algorithm to GeneralRecommenders
Browse files Browse the repository at this point in the history
feat: add Random algorithm to GeneralRecommenders
  • Loading branch information
Paitesanshi authored Oct 10, 2023
2 parents 2911096 + e4fe4e9 commit a9259e3
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 88 recommendation algorithms `[Model List]`_, covering four major categories:
In the lastest release, our library includes 89 recommendation algorithms `[Model List]`_, covering four major categories:

- General Recommendation
- Sequential Recommendation
Expand Down
38 changes: 38 additions & 0 deletions docs/source/user_guide/model/general/random.rst
Original file line number Diff line number Diff line change
@@ -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`
1 change: 1 addition & 0 deletions docs/source/user_guide/model_intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +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
model/general/diffrec
model/general/ldiffrec

Expand Down
1 change: 1 addition & 0 deletions recbole/model/general_recommender/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions recbole/model/general_recommender/random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# @Time : 2023/03/01
# @Author : João Felipe Guedes
# @Email : [email protected]
# 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(len(interaction)).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)
6 changes: 6 additions & 0 deletions tests/model/test_model_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a9259e3

Please sign in to comment.