Repository navigation
Conversation
The CPU topk function allocated k columns even if there were fewer items, so recommend/similar_items/similar_users with N > n_items returned the remaining slots filled with itemid 0 and a score of 0. ranking_metrics_at_k counted those padded entries as hits (precision > 1 when a user liked item 0), and read past the end of the returned ids when the model returned fewer than K items. Limit k to the number of items in topk, and only look at the returned items in ranking_metrics_at_k.
|
@benfred gentle ping: could you take a look when you have time? The ranking metrics read past the end of the array in some cases, which can give precision > 1; the PR includes a test. |
benfred
left a comment
There was a problem hiding this comment.
thanks!
Mostly looks good, some nitpicks below:
| cdef int query_rows = query.shape[0] | ||
|
|
||
| # we can't return more results than there are items | ||
| k = min(k, items.shape[0]) |
There was a problem hiding this comment.
we should probably also fix this in the GPU code
Line 132 in 8a95dbe
|
|
||
| # the model can return fewer than K items (for instance if K is larger than | ||
| # the number of items), so only look at the items that were actually returned | ||
| num_recs = min(K, ids.shape[1]) |
There was a problem hiding this comment.
maybe just num_recs = min(K, items) ? (The model shouldn't be returning fewer than this).
Mirror the CPU fix: don't allocate/return more than items.rows results from gpu KnnQuery.topk, otherwise the output is padded with zeros (index 0).
|
Thanks! Pushed 2622bff:
The full test suite passes on CPU, including the new test for ALS, BPR and LMF. I don't have a GPU on hand, so the GPU change is untested; could you run |
|
@benfred thanks for re-running CI. The red |
When
Nis larger than the number of items, the CPU matrix factorization models returnNresults anyway, and the extra slots are filled with itemid0and score0:This breaks
ranking_metrics_at_kwhenKis larger than the number of items. Every padded0counts as a hit for users that have item 0 in the test set, so precision can go above 1. For example, on a 300x200 random matrix,ranking_metrics_at_k(model, train, test, K=300)givesprecision=1.49. Also,ranking_metrics_at_kalways loops overKcolumns ofidswith bounds checking disabled, so if a model returns fewer thanKitems, it reads past the end of the array.Changes:
implicit/cpu/topk.pyx: limitkto the number of items. This fixesrecommend,similar_itemsandsimilar_usersfor the CPU models. The existingtest_recommendalready says that asking for too many items "should return only the available items" (recommend N top recommendations #22).implicit/evaluation.pyx: only loop over the items that the model actually returned.I added
test_ranking_metrics_k_larger_than_number_of_itemsfor ALS, BPR and LMF. It checks thatrecommendwithN=100on 50 items returns 50 columns, and that the metrics withK=100are the same as withK=50. It fails on main and passes with this change. The full test suite passes locally on macOS arm64 (167 passed, 54 skipped since I don't have CUDA). I didn't change the GPU code path since I can't test it.