tensorflow / tensorflow/recommenders

Calling Predictions from Models

Open
#273 8 comments 2 reactions 1 assignee View on GitHub

@maciejkula is already working on this.

Since Apr 20, 2021.

question
Dominant language
Python
Stars
2k
Forks
300
PR merge metrics
No merged PRs in 30d

Description

For all the co-lab examples (except for basic_retrieval), there isn't a clear way to get predictions from the models. For basic_retrieval, the use of the FacorizedTopK metric tfrs.tasks.Retrieval(metrics=tfrs.metrics.FactorizedTopK(songs.batch(64).map(song_model)) , and use of brute force to get the index tfrs.layers.factorized_top_k.BruteForce(model.user_model) is presented and works. However, for basic_ranking, context_features, deep_recommenders, multitask, and dcn, the code example ends at evaluation of test/dev sets.

So, the question is how does one do predictions? How does one infer from the model?

An option I have tried is calling model.predict() or model(), but that gives an error. The error codes below are from code I have run after running through the entire basic_ranking model co-lab notebook.

for i in train.take(1):
  print(i)
  model.predict(i)
{'movie_title': <tf.Tensor: shape=(), dtype=string, numpy=b'Postman, The (1997)'>, 'user_id': <tf.Tensor: shape=(), dtype=string, numpy=b'681'>, 'user_rating': <tf.Tensor: shape=(), dtype=float32, numpy=4.0>}
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-68-be1677278cba> in <module>()
      1 for i in train.take(1):
      2   print(i)
----> 3   model.predict(i)
      4   model(i)

4 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py in __getitem__(self, key)
    887       else:
    888         if self._v2_behavior:
--> 889           return self._dims[key].value
    890         else:
    891           return self._dims[key]

IndexError: list index out of range

And,

for i in train.take(1):
  print(i)
  model(i)
{'movie_title': <tf.Tensor: shape=(), dtype=string, numpy=b'Postman, The (1997)'>, 'user_id': <tf.Tensor: shape=(), dtype=string, numpy=b'681'>, 'user_rating': <tf.Tensor: shape=(), dtype=float32, numpy=4.0>}
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-69-fb4f109a1076> in <module>()
      1 for i in train.take(1):
      2   print(i)
----> 3   model(i)

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py in call(self, inputs, training, mask)
    444         a list of tensors if there are more than one outputs.
    445     """
--> 446     raise NotImplementedError('When subclassing the `Model` class, you should '
    447                               'implement a `call` method.')
    448 

NotImplementedError: When subclassing the `Model` class, you should implement a `call` method.

Thanks for your help!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.