Skip to content

Commit

Permalink
Merge pull request #4 from JasonTam/eval-use-precompute
Browse files Browse the repository at this point in the history
Eval use precomputed representations
  • Loading branch information
JasonTam authored May 17, 2017
2 parents 88f6bee + 48073bd commit e7c2ed7
Show file tree
Hide file tree
Showing 7 changed files with 1,418 additions and 653 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test_precision = precision_at_k(model, data['test'], k=5).mean()
1. [Learning to Rank Sketchfab Models with LightFM](http://blog.ethanrosenthal.com/2016/11/07/implicit-mf-part-2/)
2. [Metadata Embeddings for User and Item Cold-start Recommendations](http://building-babylon.net/2016/01/26/metadata-embeddings-for-user-and-item-cold-start-recommendations/)
3. [Recommendation Systems - Learn Python for Data Science](https://www.youtube.com/watch?v=9gBC9R-msAk)
4. [Using LightFM to Recommend Projects to Consultants] (https://medium.com/product-at-catalant-technologies/using-lightfm-to-recommend-projects-to-consultants-44084df7321c#.gu887ky51)
4. [Using LightFM to Recommend Projects to Consultants](https://medium.com/product-at-catalant-technologies/using-lightfm-to-recommend-projects-to-consultants-44084df7321c#.gu887ky51)

## How to cite
Please cite LightFM if it helps your research. You can use the following BibTeX entry:
Expand Down
102 changes: 62 additions & 40 deletions lightfm/_lightfm_fast.pyx.template
Original file line number Diff line number Diff line change
Expand Up @@ -1233,12 +1233,15 @@ def predict_lightfm(CSRMatrix item_features,
int[::1] item_ids,
double[::1] predictions,
FastLightFM lightfm,
int num_threads):
int num_threads,
bint use_precomputed,
flt[:, ::1] user_reprs,
flt[:, ::1] item_reprs):
"""
Generate predictions.
"""

cdef int i, no_examples
cdef int i, no_examples, factor_dim
cdef flt *user_repr
cdef flt *it_repr

Expand All @@ -1250,21 +1253,25 @@ def predict_lightfm(CSRMatrix item_features,
it_repr = <flt *>malloc(sizeof(flt) * (lightfm.no_components + 1))

for i in {range_block}(no_examples):

compute_representation(user_features,
lightfm.user_features,
lightfm.user_biases,
lightfm,
user_ids[i],
lightfm.user_scale,
user_repr)
compute_representation(item_features,
lightfm.item_features,
lightfm.item_biases,
lightfm,
item_ids[i],
lightfm.item_scale,
it_repr)
if use_precomputed:
for factor_dim in {range_block}(lightfm.no_components + 1):
user_repr[factor_dim] = user_reprs[user_ids[i], factor_dim]
it_repr[factor_dim] = item_reprs[item_ids[i], factor_dim]
else:
compute_representation(user_features,
lightfm.user_features,
lightfm.user_biases,
lightfm,
user_ids[i],
lightfm.user_scale,
user_repr)
compute_representation(item_features,
lightfm.item_features,
lightfm.item_biases,
lightfm,
item_ids[i],
lightfm.item_scale,
it_repr)

predictions[i] = compute_prediction_from_repr(user_repr,
it_repr,
Expand All @@ -1280,11 +1287,14 @@ def predict_ranks(CSRMatrix item_features,
CSRMatrix train_interactions,
flt[::1] ranks,
FastLightFM lightfm,
int num_threads):
int num_threads,
bint use_precomputed,
flt[:, ::1] user_reprs,
flt[:, ::1] item_reprs):
"""
"""

cdef int i, j, user_id, item_id, predictions_size, row_start, row_stop
cdef int i, j, user_id, item_id, predictions_size, row_start, row_stop, factor_dim
cdef flt *user_repr
cdef flt *it_repr
cdef flt *predictions
Expand Down Expand Up @@ -1315,27 +1325,35 @@ def predict_ranks(CSRMatrix item_features,
# No test interactions for this user
continue

compute_representation(user_features,
lightfm.user_features,
lightfm.user_biases,
lightfm,
user_id,
lightfm.user_scale,
user_repr)
if use_precomputed:
for factor_dim in {range_block}(lightfm.no_components + 1):
user_repr[factor_dim] = user_reprs[user_id, factor_dim]
else:
compute_representation(user_features,
lightfm.user_features,
lightfm.user_biases,
lightfm,
user_id,
lightfm.user_scale,
user_repr)

# Compute predictions for the items whose
# ranks we want to know
for i in range(row_stop - row_start):

item_id = test_interactions.indices[row_start + i]

compute_representation(item_features,
lightfm.item_features,
lightfm.item_biases,
lightfm,
item_id,
lightfm.item_scale,
it_repr)
if use_precomputed:
for factor_dim in range(lightfm.no_components + 1):
it_repr[factor_dim] = item_reprs[item_id, factor_dim]
else:
compute_representation(item_features,
lightfm.item_features,
lightfm.item_biases,
lightfm,
item_id,
lightfm.item_scale,
it_repr)

item_ids[i] = item_id
predictions[i] = compute_prediction_from_repr(user_repr,
Expand All @@ -1350,13 +1368,17 @@ def predict_ranks(CSRMatrix item_features,
# This depends on how we want to evaluate (so it's not necessary)
continue

compute_representation(item_features,
lightfm.item_features,
lightfm.item_biases,
lightfm,
item_id,
lightfm.item_scale,
it_repr)
if use_precomputed:
for factor_dim in range(lightfm.no_components + 1):
it_repr[factor_dim] = item_reprs[item_id, factor_dim]
else:
compute_representation(item_features,
lightfm.item_features,
lightfm.item_biases,
lightfm,
item_id,
lightfm.item_scale,
it_repr)
prediction = compute_prediction_from_repr(user_repr,
it_repr,
lightfm.no_components)
Expand Down
Loading

0 comments on commit e7c2ed7

Please sign in to comment.