Skip to content
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

check user and item matrix for nan entries also in bpr gpu version #731

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions implicit/cpu/matrix_factorization_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from scipy.sparse import csr_matrix, lil_matrix

from ..recommender_base import ModelFitError, RecommenderBase
from ..recommender_base import RecommenderBase
from .topk import topk


Expand Down Expand Up @@ -246,10 +246,7 @@ def item_norms(self):
return self._item_norms

def _check_fit_errors(self):
is_nan = np.any(np.isnan(self.user_factors), axis=None)
is_nan |= np.any(np.isnan(self.item_factors), axis=None)
if is_nan:
raise ModelFitError("NaN encountered in factors")
self._check_factors(self.user_factors, self.item_factors)


def _filter_items_from_sparse_matrix(items, query_items):
Expand Down
2 changes: 2 additions & 0 deletions implicit/gpu/bpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def fit(self, user_items, show_progress=True, callback=None):
if callback:
callback(_epoch, time.time() - s, correct, skipped)

self._check_fit_errors()

def to_cpu(self) -> implicit.cpu.bpr.BayesianPersonalizedRanking:
"""Converts this model to an equivalent version running on the cpu"""
ret = implicit.cpu.bpr.BayesianPersonalizedRanking(
Expand Down
3 changes: 3 additions & 0 deletions implicit/gpu/matrix_factorization_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ def similar_items(

similar_items.__doc__ = RecommenderBase.similar_items.__doc__

def _check_fit_errors(self):
self._check_factors(self.user_factors.to_numpy(), self.item_factors.to_numpy())

def recalculate_user(self, userid, user_items):
raise NotImplementedError("recalculate_user is not supported with this model")

Expand Down
7 changes: 7 additions & 0 deletions implicit/recommender_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,10 @@ def rank_items(self, userid, user_items, selected_items, recalculate_user=False)
items=selected_items,
filter_already_liked_items=False,
)

@staticmethod
def _check_factors(user_factors, item_factors):
is_nan = np.any(np.isnan(user_factors), axis=None)
is_nan |= np.any(np.isnan(item_factors), axis=None)
if is_nan:
raise ModelFitError("NaN encountered in factors")