recommenders icon indicating copy to clipboard operation
recommenders copied to clipboard

model.predict not working for sequential retrieval tutorial. How to predict N next items?

Open meethariprasad opened this issue 2 years ago • 1 comments

Dear All,

I tried https://www.tensorflow.org/recommenders/examples/sequential_retrieval with calling: model.predict(cached_test) at the end.

https://colab.research.google.com/drive/1Goe4evEy-IchJ0MLv0emUA7UDZjEE6Ch?usp=sharing

Recieved following error. How to predict next N items using this sequential model?

NotImplementedError: Exception encountered when calling layer "model" (type Model).

Unimplemented `tf.keras.Model.call()`: if you intend to create a `Model` with the Functional API, please provide `inputs` and `outputs` arguments. Otherwise, subclass `Model` with an overridden `call()` method.

Call arguments received by layer "model" (type Model):
  • inputs={'context_movie_id': 'tf.Tensor(shape=(None, 10), dtype=string)', 'label_movie_id': 'tf.Tensor(shape=(None, 1), dtype=string)'}
  • training=False
  • mask=None

meethariprasad avatar Dec 27 '22 04:12 meethariprasad

@meethariprasad

brute_force = tfrs.layers.factorized_top_k.BruteForce(model._query_model)
brute_force.index_from_dataset(
    movies.batch(128).map(lambda title: (title, model._candidate_model(title)))
)

user_batch = [batch for batch in cached_train.take(1)][0]
_, titles = brute_force(user_batch, k=3)
titles

This is a solution to your problem. Model is a subclass of tfrs.Model and that may be part of the error. You could also try modifying the returns or something; however, the solution above is worthwhile.

You could get movie titles instead by modifying the dataloading to utilize listwise movielens and choosing to propagate the movie_titles to the StringLookup. Otherwise you'd have to edit the source file that reorganizes the data for the Sequence Tutorial.

AlexanderLavelle avatar Jan 17 '23 20:01 AlexanderLavelle