recommenders icon indicating copy to clipboard operation
recommenders copied to clipboard

tfrs.metrics.FactorizedTopK

Open soheil-asgari opened this issue 11 months ago • 11 comments

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>

soheil-asgari avatar Apr 03 '24 18:04 soheil-asgari

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

Lopera47 avatar Apr 05 '24 17:04 Lopera47

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 avatar Apr 05 '24 17:04 soheil-asgari

@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.

Lopera47 avatar Apr 05 '24 19:04 Lopera47

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.

rlcauvin avatar Apr 06 '24 18:04 rlcauvin

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

Chm-vinicius avatar Apr 10 '24 00:04 Chm-vinicius

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?

rlcauvin avatar Apr 24 '24 15:04 rlcauvin

Hello @rlcauvin, unfortunately, I cannot add label bugs

soheil-asgari avatar Apr 25 '24 19:04 soheil-asgari

I'm facing the same error. Also with tensorflow version 2.15. But only for tensorflow-recommenders version 0.7.2. Any ideas?

celha avatar May 14 '24 15:05 celha

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.

rlcauvin avatar May 15 '24 13:05 rlcauvin

Alright! Thank you @rlcauvin :)

celha avatar May 15 '24 14:05 celha

what worked for me is

python3.7

and my requirements.txt is

Tensorflow==2.10.0
tensorflow_recommenders==0.7.2
tensorflow-datasets
scann

y-71 avatar Aug 03 '24 23:08 y-71