-
Notifications
You must be signed in to change notification settings - Fork 622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add RP3Beta #634
Open
deklanw
wants to merge
1
commit into
RUCAIBox:0.2.x
Choose a base branch
from
deklanw:rp3beta
base: 0.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−0
Open
Add RP3Beta #634
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
r""" | ||
RP3Beta | ||
################################################ | ||
Reference: | ||
Paudel, Bibek et al. Updatable, Accurate, Diverse, and Scalable Recommendations for Interactive Applications. https://doi.org/10.1145/2955101 | ||
|
||
Reference code: | ||
https://github.com/MaurizioFD/RecSys2019_DeepLearning_Evaluation/blob/master/GraphBased/RP3betaRecommender.py | ||
""" | ||
|
||
|
||
from recbole.utils.enum_type import ModelType | ||
import numpy as np | ||
import scipy.sparse as sp | ||
import torch | ||
|
||
from recbole.utils import InputType | ||
from recbole.model.abstract_recommender import GeneralRecommender | ||
|
||
|
||
def get_inv_degree_matrix(A): | ||
# add epsilon to degree sums to suppress warning about division by zero when we later divide | ||
degree_sums = A.sum(axis=1).getA1() + 1e-7 | ||
|
||
return sp.diags(1/degree_sums) | ||
|
||
|
||
# for reference, doing it in one computation | ||
# since the resultant matrix is dense, I'll refrain from doing this | ||
def calculate_rp3beta(B, beta): | ||
user_degree_inv = get_inv_degree_matrix(B) | ||
item_degree_inv = get_inv_degree_matrix(B.T) | ||
|
||
# multiplication on left for row-wise scaling | ||
user_transition = user_degree_inv @ B | ||
item_transition = item_degree_inv @ B.T | ||
|
||
P3 = user_transition @ item_transition @ user_transition | ||
|
||
# multiplication on right for column-wise scaling (i.e., we're reweighting by inverse item popularity to a power) | ||
RP3Beta = P3 @ item_degree_inv.power(beta) | ||
|
||
return RP3Beta | ||
|
||
|
||
class RP3Beta(GeneralRecommender): | ||
input_type = InputType.POINTWISE | ||
type = ModelType.TRADITIONAL | ||
|
||
def __init__(self, config, dataset): | ||
super().__init__(config, dataset) | ||
|
||
# need at least one param | ||
self.dummy_param = torch.nn.Parameter(torch.zeros(1)) | ||
|
||
B = dataset.inter_matrix( | ||
form='coo').astype(np.float32) | ||
|
||
self.beta = config['beta'] | ||
|
||
user_degree_inv = get_inv_degree_matrix(B) | ||
item_degree_inv = get_inv_degree_matrix(B.T) | ||
|
||
self.user_transition = user_degree_inv @ B | ||
self.item_transition = item_degree_inv @ B.T | ||
self.item_degree_inv = item_degree_inv | ||
|
||
def forward(self): | ||
pass | ||
|
||
def calculate_loss(self, interaction): | ||
return torch.nn.Parameter(torch.zeros(1)) | ||
|
||
def predict(self, interaction): | ||
user = interaction[self.USER_ID].cpu().numpy() | ||
item = interaction[self.ITEM_ID].cpu().numpy() | ||
|
||
specific_user_transitions = self.user_transition[user] | ||
|
||
# make all item predictions for specified users | ||
user_all_items = specific_user_transitions @ self.item_transition @ self.user_transition @ self.item_degree_inv.power( | ||
self.beta) | ||
|
||
# then narrow down to specific items | ||
# without this copy(): "cannot set WRITEABLE flag..." | ||
item_predictions = user_all_items[range(len(user)), item.copy()] | ||
|
||
return torch.from_numpy(item_predictions.getA1()) | ||
|
||
def full_sort_predict(self, interaction): | ||
user = interaction[self.USER_ID].cpu().numpy() | ||
|
||
specific_user_transitions = self.user_transition[user] | ||
|
||
item_predictions = specific_user_transitions @ self.item_transition @ self.user_transition @ self.item_degree_inv.power( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
self.beta) | ||
|
||
return torch.from_numpy(item_predictions.todense().getA1()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
beta: 0.55 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the whole user_all_items matrix can be computed in advance to avoid multiplication in each batch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@linzihan-backforward Hello, sorry for the (very) late reply. I intentionally avoided computing the entire user_item matrix to save memory. I included the all-at-once implementation at the top for reference, and explained the former. See
RecBole/recbole/model/general_recommender/rp3beta.py
Lines 28 to 43 in e28e150
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It make sense. We will test the algorithm as soon as possible. It will be merged into the future version if everything is OK. Thanks again for your contribution !