I was hoping someone could point in the right direction in being able to return the top n recommendations from a model built in the same way as the retrieval tutorial with context features. (link below)
Using the same code as in the very first tutorial in the series returns an error.
The code:
index = tfrs.layers.factorized_top_k.BruteForce(model.user_model)
recommends movies out of the entire movies dataset.
If for example I run the exact tutorial code shared in the link above and modify the get recommendations code to the following:
index = tfrs.layers.factorized_top_k.BruteForce(model.query_model)
index.index_from_dataset(
tf.data.Dataset.zip((movies.batch(100), movies.batch(100).map(model.candidate_model)))
)
_, titles = index(tf.constant([“42”]))
print(f"Recommendations for user 42: {titles[0, :3]}")
I produce the error:
“TypeError: Only integers, slices (:), ellipsis (...), tf.newaxis (None) and scalar tf.int32/tf.int64 tensors are valid indices, got ‘user_id’”
I got exactly the same problem.
In case you got a solution meanwhile, I would be very grateful if you would share it with me.
I will do the same when I found something.
I believe I solved this and so this might be what you are after, the example works on generating recommendations from the tutorials with the use_timestamp features:
index = tfrs.layers.factorized_top_k.BruteForce(model.query_model)
For the model with context features, I used the following to create the index and query it.
(Hope it’s correct)
index = tfrs.layers.factorized_top_k.BruteForce(model.query_model)
index.index_from_dataset(
candidates.batch(100).map(lambda c: (c['candidate_id'], model.candidate_model(c)))
)
query = {"user_id": np.array(['42'])} # the query is a hash in this case. i'm not sure why the user id needs to be a numpy array though :(
_scores, titles = index(query)
titles[0].numpy().tolist()