recommenders
recommenders copied to clipboard
tfrs.metrics.FactorizedTopK
hi when i used this code i seen this error
self.task = tfrs.tasks.Retrieval(
metrics=tfrs.metrics.FactorizedTopK(
candidates=(
movies.batch(128)
.cache()
.map(lambda title: (title, self.movie_model(title)))
)
)
)
error:
ValueError: Cannot convert '('c', 'o', 'u', 'n', 't', 'e', 'r')' to a shape. Found invalid entry 'c' of type '<class 'str'>'. Exception ignored in: <function AtomicFunction.del at 0x7d6fc6688d60>
Hello @soheil-asgari I got the same error and solved it. It is a dependency issue with the latest version of TensorFlow. In my case, in order to solve it, I installed TFRS without dependencies (pip install tensorflow-recommenders --no-deps), because I already had the needed version of tensorflow (2.11.0) and other dependencies.
Here's another similar approach: https://stackoverflow.com/questions/78144515/error-initializing-factorizedtopk-in-tensorflow-recommenders-on-sagemaker-cann
Hello @Lopera47 I also installed the version (2.11.0) of TensorFlow, but when I install scann, it automatically updates the version of TensorFlow to the latest version.
@soheil-asgari are you explicitly using Scann?, in my case I'm not. Maybe, you can try installing Scann without dependencies to not make it update TensorFlow.
This is a bug in the implementation of the tfrs.layers.factorized_top_k
module.
In recommenders/tensorflow_recommenders/layers/factorized_top_k.py, we find:
self._counter = self.add_weight("counter", dtype=tf.int32, trainable=False)
Note that the first argument passed to add_weight
is the “counter” string.
In the Keras 2.15.0 implementation of add_weight, we find:
def add_weight(
self,
name=None,
shape=None,
dtype=None,
initializer=None,
regularizer=None,
trainable=None,
constraint=None,
use_resource=None,
synchronization=tf.VariableSynchronization.AUTO,
aggregation=tf.VariableAggregation.NONE,
**kwargs,
):
Note that first argument is the name of the weight variable. Passing “counter” as the name in this manner works fine in Keras 2.15.0, and we won’t get the ValueError: Cannot convert '('c', 'o', 'u', 'n', 't', 'e', 'r')' to a shape
error.
But the Keras 3.1.1 implementation of add_weight expects shape
as the first argument:
def add_weight(
self,
shape=None,
initializer=None,
dtype=None,
trainable=True,
regularizer=None,
constraint=None,
name=None,
):
So when it goes on to check for a valid shape, the shape is set to “counter”, which is not valid, and it results in ValueError: Cannot convert '('c', 'o', 'u', 'n', 't', 'e', 'r')' to a shape
.
The bug, therefore, is in TensorFlow Recommenders’ implementation of the tfrs.layers.factorized_top_k
module.
I can confirm that is a bug with tensorflow2.16.0 and tensorflow-recommenders0.7.3 with the output error:
Cannot convert '('c', 'o', 'u', 'n', 't', 'e', 'r')' to a shape
I only was able to perform a training by downgrading tensorflow to 2.15.1 version, but i can't save model with signature_inputs, the kernel get in infinty loop, this behavior is refered on this issue
Seems to me that this issue should be marked as a bug in TensorFlow Recommenders (see my diagnosis of exactly where the bug in the code is) and fixed ASAP. Maybe @soheil-asgari can add the Bug label?
Hello @rlcauvin, unfortunately, I cannot add label bugs
I'm facing the same error. Also with tensorflow version 2.15. But only for tensorflow-recommenders version 0.7.2. Any ideas?
For now, I've worked around the TensorFlow Recommenders bug by including the following code in my notebook before installing any TensorFlow related packages:
import os
os.environ['TF_USE_LEGACY_KERAS'] = '1'
My notebook installs tensorflow-recommenders
version 0.7.3 and tensorflow
version 2.16.1.
I've been unable to install and import tensorflow-ranking
, however, without reverting to a prior version of tensorflow
.
Alright! Thank you @rlcauvin :)
what worked for me is
python3.7
and my requirements.txt
is
Tensorflow==2.10.0
tensorflow_recommenders==0.7.2
tensorflow-datasets
scann