implicit icon indicating copy to clipboard operation
implicit copied to clipboard

Incremental update with old users but new items

Open levrone1987 opened this issue 2 years ago • 4 comments

I want to use model.partial_fit_items for incremental model update. The users pool stays the same, but new items may arrive. This is the code I have:

mat = csr_matrix((test_df['amount_in_eur'], (test_df['player_index'], test_df['item_index'])))
model.partial_fit_items(test_df['item_index'], mat)

and I get the following error:

    314 """Incrementally updates item factors
    315 
    316 This method updates factors for items specified by itemids, given a
   (...)
    327     Sparse matrix containing the liked users for each item in itemids
    328 """
    329 if len(itemids) != item_users.shape[0]:
--> 330     raise ValueError("item_users must contain 1 row for every user in itemids")
    332 # recalculate factors for each item in the input
    333 item_factors = self.recalculate_item(itemids, item_users)

ValueError: item_users must contain 1 row for every user in itemids

If my assumption that what I want to achieve is possible, please help me write correct code.

levrone1987 avatar Oct 24 '23 13:10 levrone1987

it looks like you're passing a (user, items) matrix to the partial_fit_items - but the method expects a (items, users) matrix.

Does it work if you create it like

mat = csr_matrix((test_df['amount_in_eur'], (test_df['item_index'], test_df['player_index'])))

?

benfred avatar Oct 24 '23 16:10 benfred

Same error @benfred

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[38], line 1
----> 1 model.partial_fit_items(test_df['item_index'], mat)

File ~/miniconda3/envs/whizdom-env/lib/python3.9/site-packages/implicit/cpu/als.py:330, in AlternatingLeastSquares.partial_fit_items(self, itemids, item_users)
    314 """Incrementally updates item factors
    315 
    316 This method updates factors for items specified by itemids, given a
   (...)
    327     Sparse matrix containing the liked users for each item in itemids
    328 """
    329 if len(itemids) != item_users.shape[0]:
--> 330     raise ValueError("item_users must contain 1 row for every user in itemids")
    332 # recalculate factors for each item in the input
    333 item_factors = self.recalculate_item(itemids, item_users)

ValueError: item_users must contain 1 row for every user in itemids

Is my assumption correct: is it possible to retrain the model with some new items (so, same users as in the pretrained model, but new items)? If yes, could you please provide a small working example on how this could be done?

levrone1987 avatar Oct 26 '23 10:10 levrone1987

I tried all variations, but it still does not work @benfred

levrone1987 avatar Oct 31 '23 10:10 levrone1987

The best example I have is probably in the unittest here

https://github.com/benfred/implicit/blob/cb2a66d50d1f1c2779108724c78f7653024332b8/tests/als_test.py#L272-L301

benfred avatar Nov 06 '23 17:11 benfred