Tensorflow Recommenders Incompatible Packages

I have tried a lot of examples of code of Tensorflow Recommenders and it didn’t work always the problem in the packages of TensorFlow and Tensorflow recommenders, and I starting to give up on it!

1 Like

For now, you will need to use legacy Keras by executing the following in your code before installing any TensorFlow related packages:

import os
os.environ['TF_USE_LEGACY_KERAS'] = '1'

More here.

1 Like

Thank you for your support!

Here is my code and I have an error, can you help me with this, please? I’m using Tensorflow Recommenders, the retrieval approach, I tried with the error even though I checked what you said before because it seems like many people have the same error.

Prepare user and item features from your dataframe

user_features = df[[‘restaurant_id’, ‘type’, ‘category’, ‘season’]]
item_features = df[[‘main_taxon_id’, ‘taxon_name’, ‘product_category’, ‘rating’, ‘quantity’]]

Ensure IDs are of type string

user_features[‘restaurant_id’] = user_features[‘restaurant_id’].astype(str)
item_features[‘main_taxon_id’] = item_features[‘main_taxon_id’].astype(str)

Create TensorFlow datasets for training

ratings = tf.data.Dataset.from_tensor_slices(dict(user_features))
items = tf.data.Dataset.from_tensor_slices(dict(item_features))

Define the model

class Model(tfrs.Model):
def init(self):
super().init()

    # Set up user and item representations
    self.user_model = tf.keras.layers.Embedding(
        input_dim=len(user_features['restaurant_id'].unique()), 
        output_dim=64
    )
    self.item_model = tf.keras.layers.Embedding(
        input_dim=len(item_features['main_taxon_id'].unique()), 
        output_dim=64
    )

    # Define the task with a custom mapping for candidates
    self.task = tfrs.tasks.Retrieval(
        metrics=tfrs.metrics.FactorizedTopK(
            candidates=items.batch(128).map(lambda c: (c["main_taxon_id"], self.item_model(c["main_taxon_id"])))
        )
    )

def compute_loss(self, features: Dict[Text, tf.Tensor], training=False) -> tf.Tensor:
    # Get user and item embeddings
    user_embeddings = self.user_model(features["restaurant_id"])
    item_embeddings = self.item_model(features["main_taxon_id"])

    # Compute the retrieval task loss
    return self.task(user_embeddings, item_embeddings)

Initialize and compile the model

model = Model()
model.compile(optimizer=tf.keras.optimizers.Adagrad(0.5))

Shuffle data and split between train and test

tf.random.set_seed(42)
shuffled = ratings.shuffle(100_000, seed=42, reshuffle_each_iteration=False)

train = shuffled.take(80_000)
test = shuffled.skip(80_000).take(20_000)

Train the model

model.fit(train.batch(4096), epochs=5)

Evaluate the model

model.evaluate(test.batch(4096), return_dict=True)

##The error:

See the caveats in the documentation: Indexing and selecting data — pandas 2.2.3 documentation
item_features[‘main_taxon_id’] = item_features[‘main_taxon_id’].astype(str)
Traceback (most recent call last):
File “/home/eya_laouini/recsys/my_Retrieval_TFRS.py”, line 321, in
model = Model()
^^^^^^^
File “/home/eya_laouini/recsys/my_Retrieval_TFRS.py”, line 307, in init
metrics=tfrs.metrics.FactorizedTopK(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/eya_laouini/.local/lib/python3.12/site-packages/tensorflow_recommenders/metrics/factorized_top_k.py”, line 79, in init
layers.factorized_top_k.Streaming(k=max(ks))
File “/home/eya_laouini/.local/lib/python3.12/site-packages/tensorflow_recommenders/layers/factorized_top_k.py”, line 376, in init
self._counter = self.add_weight(“counter”, dtype=tf.int32, trainable=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/dist-packages/keras/src/layers/layer.py”, line 541, in add_weight
variable = backend.Variable(
^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/dist-packages/keras/src/backend/common/variables.py”, line 162, in init
self._shape = self._validate_shape(shape)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/dist-packages/keras/src/backend/common/variables.py”, line 184, in _validate_shape
shape = standardize_shape(shape)
^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/dist-packages/keras/src/backend/common/variables.py”, line 535, in standardize_shape
raise ValueError(
ValueError: Cannot convert ‘(‘c’, ‘o’, ‘u’, ‘n’, ‘t’, ‘e’, ‘r’)’ to a shape. Found invalid entry ‘c’ of type ‘<class ‘str’>’.

Did you include the directive to use legacy Keras, and does your code execute the directive before installing any TensorFlow related packages?

how can i check this please?

The “directive” to which I’m referring is the code I previously suggested you include:

import os
os.environ['TF_USE_LEGACY_KERAS'] = '1'

Did you include it in your code, and does it execute in your code before the installation of any TensorFlow related packages?

If you share your code in a collab notebook, it might help us collaboratively diagnose it.

1 Like

I appreciate your support!