-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* EASEᴿ Implementation * sorting corrected * corrections * Update __init__.py * Update README.md Co-authored-by: Quoc-Tuan Truong <tqtg@users.noreply.github.com>
- Loading branch information
1 parent
b300716
commit 3f7a04a
Showing
7 changed files
with
208 additions
and
1 deletion.
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
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 @@ | ||
from .recom_ease import EASE |
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,135 @@ | ||
import numpy as np | ||
|
||
from cornac.models.recommender import Recommender | ||
from cornac.exception import ScoreException | ||
|
||
class EASE(Recommender): | ||
"""Embarrassingly Shallow Autoencoders for Sparse Data. | ||
Parameters | ||
---------- | ||
name: string, optional, default: 'EASEᴿ' | ||
The name of the recommender model. | ||
lamb: float, optional, default: 500 | ||
L2-norm regularization-parameter λ ∈ R+. | ||
posB: boolean, optional, default: False | ||
Remove Negative Weights | ||
trainable: boolean, optional, default: True | ||
When False, the model is not trained and Cornac assumes that the model is already \ | ||
trained. | ||
verbose: boolean, optional, default: False | ||
When True, some running logs are displayed. | ||
seed: int, optional, default: None | ||
Random seed for parameters initialization. | ||
References | ||
---------- | ||
* Steck, H. (2019, May). "Embarrassingly shallow autoencoders for sparse data." \ | ||
In The World Wide Web Conference (pp. 3251-3257). | ||
""" | ||
|
||
def __init__( | ||
self, | ||
name="EASEᴿ", | ||
lamb=500, | ||
posB=True, | ||
trainable=True, | ||
verbose=True, | ||
seed=None, | ||
B=None, | ||
U=None, | ||
): | ||
Recommender.__init__(self, name=name, trainable=trainable, verbose=verbose) | ||
self.lamb = lamb | ||
self.posB = posB | ||
self.verbose = verbose | ||
self.seed = seed | ||
self.B = B | ||
self.U = U | ||
|
||
def fit(self, train_set, val_set=None): | ||
"""Fit the model to observations. | ||
Parameters | ||
---------- | ||
train_set: :obj:`cornac.data.Dataset`, required | ||
User-Item preference data as well as additional modalities. | ||
val_set: :obj:`cornac.data.Dataset`, optional, default: None | ||
User-Item preference data for model selection purposes (e.g., early stopping). | ||
Returns | ||
------- | ||
self : object | ||
""" | ||
Recommender.fit(self, train_set, val_set) | ||
|
||
# A rating matrix | ||
self.U = self.train_set.matrix | ||
|
||
# Gram matrix is X^t X, compute dot product | ||
G = self.U.T.dot(self.U).toarray() | ||
|
||
diag_indices = np.diag_indices(G.shape[0]) | ||
|
||
G[diag_indices] = G.diagonal() + self.lamb | ||
|
||
P = np.linalg.inv(G) | ||
|
||
B = P / (-np.diag(P)) | ||
|
||
B[diag_indices] = 0.0 | ||
|
||
# if self.posB remove -ve values | ||
if self.posB: | ||
B[B<0]=0 | ||
|
||
# save B for predictions | ||
self.B=B | ||
|
||
return self | ||
|
||
|
||
def score(self, user_idx, item_idx=None): | ||
"""Predict the scores/ratings of a user for an item. | ||
Parameters | ||
---------- | ||
user_idx: int, required | ||
The index of the user for whom to perform score prediction. | ||
item_idx: int, optional, default: None | ||
The index of the item for which to perform score prediction. | ||
If None, scores for all known items will be returned. | ||
Returns | ||
------- | ||
res : A scalar or a Numpy array | ||
Relative scores that the user gives to the item or to all known items | ||
""" | ||
if item_idx is None: | ||
if self.train_set.is_unk_user(user_idx): | ||
raise ScoreException( | ||
"Can't make score prediction for (user_id=%d)" % user_idx | ||
) | ||
|
||
known_item_scores = self.U[user_idx, :].dot(self.B) | ||
return known_item_scores | ||
else: | ||
if self.train_set.is_unk_user(user_idx) or self.train_set.is_unk_item( | ||
item_idx | ||
): | ||
raise ScoreException( | ||
"Can't make score prediction for (user_id=%d, item_id=%d)" | ||
% (user_idx, item_idx) | ||
) | ||
|
||
user_pred = self.B[item_idx, :].dot(self.U[user_idx, :]) | ||
|
||
return user_pred |
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
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,61 @@ | ||
# Copyright 2018 The Cornac Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================ | ||
"""Example (EASEᴿ) Embarrassingly Shallow Autoencoders for Sparse Data on MovieLens data""" | ||
|
||
import cornac | ||
from cornac.datasets import movielens | ||
from cornac.eval_methods import RatioSplit | ||
|
||
|
||
# Load user-item feedback | ||
data = movielens.load_feedback(variant="1M") | ||
|
||
# Instantiate an evaluation method to split data into train and test sets. | ||
ratio_split = RatioSplit( | ||
data=data, | ||
test_size=0.2, | ||
exclude_unknowns=True, | ||
verbose=True, | ||
seed=123, | ||
rating_threshold=0.8, | ||
) | ||
|
||
ease_original = cornac.models.EASE( | ||
lamb=500, | ||
name="EASEᴿ (B>0)", | ||
posB=True | ||
) | ||
|
||
ease_all = cornac.models.EASE( | ||
lamb=500, | ||
name="EASEᴿ (B>-∞)", | ||
posB=False | ||
) | ||
|
||
|
||
# Instantiate evaluation measures | ||
rec_20 = cornac.metrics.Recall(k=20) | ||
rec_50 = cornac.metrics.Recall(k=50) | ||
ndcg_100 = cornac.metrics.NDCG(k=100) | ||
|
||
|
||
# Put everything together into an experiment and run it | ||
cornac.Experiment( | ||
eval_method=ratio_split, | ||
models=[ease_original, ease_all], | ||
metrics=[rec_20, rec_50, ndcg_100], | ||
user_based=True, #If `False`, results will be averaged over the number of ratings. | ||
save_dir=None | ||
).run() |