Return Recommendations from models with Context features

Hi,

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.

index.index_from_dataset(
tf.data.Dataset.zip((movies.batch(100), movies.batch(100).map(model.movie_model)))
)

Get recommendations.

_, titles = index(tf.constant([“42”]))
print(f"Recommendations for user 42: {titles[0, :3]}")

1 Like

Hi Nick, can you share the error please?

cc @Wei_Wei might be able to help here

Hi Igusm,

Thanks for taking a look at this!

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’”

Thanks again!

2 Likes

Hey Nick

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.

Best,

Efrain

1 Like

Hi Efrain,

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)

index.index_from_dataset(

tf.data.Dataset.zip((movies.batch(100), movies.batch(100).map(model.candidate_model)))

)

query = dict(pd.DataFrame({‘user_id’: [“42”],‘timestamp’:[879024327]}).iloc[0].map(lambda x: tf.expand_dims(x,axis=0)))

Get recommendations.

_, titles = index(query)

print(f"Recommendations for user 42: {titles[0, :5]}")

hopefully that works for you!

5 Likes

Hey Nick

Thanks a lot for your help!
Do you also know how to get the recommendations for the multitask and deep cross network model?

Best,

Efrain

Multitask model:

movie_embedding, user_embedding, predicted_rating = model({
      "user_id": np.array(["42"]),
      "movie_title": np.array(["Dances with Wolves (1990)"])
  })
print(predicted_rating)
1 Like

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()
2 Likes