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

Use num_threads for batch recommendations in MF models #562

Merged
merged 1 commit into from
Apr 12, 2022
Merged
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
9 changes: 3 additions & 6 deletions implicit/cpu/als.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ class AlternatingLeastSquares(MatrixFactorizationBase):
calculate_training_loss : bool, optional
Whether to log out the training loss at each iteration
num_threads : int, optional
The number of threads to use for fitting the model. This only
applies for the native extensions. Specifying 0 means to default
to the number of cores on the machine.
The number of threads to use for fitting the model and batch recommend calls.
Specifying 0 means to default to the number of cores on the machine.
random_state : int, numpy.random.RandomState or None, optional
The random state for seeding the initial item and user factors.
Default is None.
Expand All @@ -68,8 +67,7 @@ def __init__(
num_threads=0,
random_state=None,
):

super().__init__()
super().__init__(num_threads=num_threads)

# parameters on how to factorize
self.factors = factors
Expand All @@ -81,7 +79,6 @@ def __init__(
self.use_cg = use_cg
self.iterations = iterations
self.calculate_training_loss = calculate_training_loss
self.num_threads = num_threads
self.fit_callback = None
self.cg_steps = 3
self.random_state = random_state
Expand Down
8 changes: 3 additions & 5 deletions implicit/cpu/bpr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ class BayesianPersonalizedRanking(MatrixFactorizationBase):
been liked by the user. This check increases the time needed to train but usually leads
to better predictions.
num_threads : int, optional
The number of threads to use for fitting the model. This only
applies for the native extensions. Specifying 0 means to default
to the number of cores on the machine.
The number of threads to use for fitting the model and batch recommend calls.
Specifying 0 means to default to the number of cores on the machine.
random_state : int, RandomState or None, optional
The random state for seeding the initial item and user factors.
Default is None.
Expand All @@ -111,14 +110,13 @@ class BayesianPersonalizedRanking(MatrixFactorizationBase):
def __init__(self, factors=100, learning_rate=0.01, regularization=0.01, dtype=np.float32,
iterations=100, num_threads=0,
verify_negative_samples=True, random_state=None):
super(BayesianPersonalizedRanking, self).__init__()
super(BayesianPersonalizedRanking, self).__init__(num_threads=num_threads)

self.factors = factors
self.learning_rate = learning_rate
self.iterations = iterations
self.regularization = regularization
self.dtype = np.dtype(dtype)
self.num_threads = num_threads
self.verify_negative_samples = verify_negative_samples
self.random_state = random_state

Expand Down
8 changes: 3 additions & 5 deletions implicit/cpu/lmf.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ class LogisticMatrixFactorization(MatrixFactorizationBase):
The proportion of negative samples. i.e.) "neg_prop = 30" means if user have seen 5 items,
then 5 * 30 = 150 negative samples are used for training.
num_threads : int, optional
The number of threads to use for fitting the model. This only
applies for the native extensions. Specifying 0 means to default
to the number of cores on the machine.
The number of threads to use for fitting the model and batch recommendation calls.
Specifying 0 means to default to the number of cores on the machine.
random_state : int, RandomState or None, optional
The random state for seeding the initial item and user factors.
Default is None.
Expand All @@ -101,14 +100,13 @@ class LogisticMatrixFactorization(MatrixFactorizationBase):
def __init__(self, factors=30, learning_rate=1.00, regularization=0.6, dtype=np.float32,
iterations=30, neg_prop=30, num_threads=0,
random_state=None):
super(LogisticMatrixFactorization, self).__init__()
super(LogisticMatrixFactorization, self).__init__(num_threads=num_threads)

self.factors = factors
self.learning_rate = learning_rate
self.iterations = iterations
self.regularization = regularization
self.dtype = np.dtype(dtype)
self.num_threads = num_threads
self.neg_prop = neg_prop
self.random_state = random_state

Expand Down
16 changes: 14 additions & 2 deletions implicit/cpu/matrix_factorization_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ class MatrixFactorizationBase(RecommenderBase):
Array of latent factors for each item in the training set
user_factors : ndarray
Array of latent factors for each user in the training set
num_threads : int
The number of threads to use for batch recommendation calls and fitting the
model. Setting to 0 will use all CPU cores on the machine
"""

def __init__(self):
def __init__(self, num_threads=0):
# learned parameters
self.item_factors = None
self.user_factors = None

# cache of user, item norms (useful for calculating similar items)
self._user_norms, self._item_norms = None, None
self.num_threads = num_threads

def recommend(
self,
Expand Down Expand Up @@ -78,6 +82,7 @@ def recommend(
N,
filter_query_items=filter_query_items,
filter_items=filter_items,
num_threads=self.num_threads,
)

if np.isscalar(userid):
Expand Down Expand Up @@ -209,7 +214,14 @@ def similar_items(
similar_items.__doc__ = RecommenderBase.similar_items.__doc__

def _get_similarity_score(self, factor, norm, factors, norms, N, filter_items=None):
ids, scores = topk(factors, factor, N, item_norms=norms, filter_items=filter_items)
ids, scores = topk(
factors,
factor,
N,
item_norms=norms,
filter_items=filter_items,
num_threads=self.num_threads,
)
if np.isscalar(norm):
ids, scores = ids[0], scores[0]
scores /= norm
Expand Down