I have a Fast ai collaborative filtering model. I would like to predict on this model for a new tuple. I am having trouble with the predict function
From their documentation,
Signature: learn.predict(item, rm_type_tfms=None, with_input=False)
Docstring: Prediction on `item`, fully decoded, loss function decoded and probabilities
File: ~/playground/virtualenv/lib/python3.8/site-packages/fastai/learner.py
Type: method
How do I define the Item that I need to pass. Lets say for a movielens dataset, for a user already with in the dataset, we would like to recommend a set of movies, how do we pass the userID?
Not familiar with fastai’s collaborative filtering modules, and I received the same error as you, but a hacky workaround is:
row = pd.DataFrame({'user': [user_id, 0],
'item': [item_id, 0]})
# Turn your data into a DataLoader.
# Note how unlike in learn.predict, 'row' isn't placed in a list ([row])
row = learn.dls.test_dl(row)
# learn.get_preds returns a tuple of (predictions, labels)
# The first item in 'predictions' corresponds to our data point
pred = learn.get_preds(dl=row)[0][0]
I’m not sure whether this is a bug, or if learn.predict for collaborative models is meant to be used differently, but whichever the case, I think the code above should work fine for your use case.
Based on your response, to get predictions from the learner in Collaborative Filtering Deep Dive | Kaggle, I tried 2 entries of the same data as given below and it worked.
IndexError: too many indices for tensor of dimension 0
Not really sure why learn.get_preds expects atleast 2 rows. (Maybe somehow related to the fact that there is #na# entry in dls.classes ?)
Anyways we have a solution for this issue