Skip to content

TensorFlow nightly: ValueError: Cannot convert '('c', 'o', 'u', 'n', 't', 'e', 'r')' to a shape. Found invalid entry 'c' of type '<class 'str'>' #754

@donhuvy

Description

@donhuvy

I follow this guide https://www.tensorflow.org/recommenders and https://colab.research.google.com/github/tensorflow/recommenders/blob/main/docs/examples/quickstart.ipynb#scrollTo=4FyfuZX-gTKS

My environment: Windows 11 x64, PyCharm 2024.3.5 (Professional Edition), Jupyter notebook inside PyCharm, TensorFlow nightly, CUDA 12.8 .

Microsoft Windows [Version 10.0.26100.3476]
(c) Microsoft Corporation. All rights reserved.

C:\Users\ADMIN>nvidia-smi
Thu Apr  3 21:03:07 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 572.83                 Driver Version: 572.83         CUDA Version: 12.8     |
|-----------------------------------------+------------------------+----------------------+
| GPU  Name                  Driver-Model | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA GeForce RTX 5090      WDDM  |   00000000:01:00.0  On |                  N/A |
|  0%   36C    P8             27W /  575W |    1471MiB /  32607MiB |      0%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI              PID   Type   Process name                        GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|    0   N/A  N/A            2584    C+G   ...Chrome\Application\chrome.exe      N/A      |
|    0   N/A  N/A            3208    C+G   ...rm 2024.3.5\bin\pycharm64.exe      N/A      |
|    0   N/A  N/A            4412    C+G   ...4__8wekyb3d8bbwe\ms-teams.exe      N/A      |
|    0   N/A  N/A            6588    C+G   ...xyewy\ShellExperienceHost.exe      N/A      |
|    0   N/A  N/A            6708    C+G   ...adeonsoftware\AMDRSSrcExt.exe      N/A      |
|    0   N/A  N/A            7548    C+G   ...crosoft OneDrive\OneDrive.exe      N/A      |
|    0   N/A  N/A            9988    C+G   ...IA app\CEF\NVIDIA Overlay.exe      N/A      |
|    0   N/A  N/A           10140    C+G   ....0.3124.93\msedgewebview2.exe      N/A      |
|    0   N/A  N/A           10576    C+G   ...Chrome\Application\chrome.exe      N/A      |
|    0   N/A  N/A           14612    C+G   ....0.3124.93\msedgewebview2.exe      N/A      |
|    0   N/A  N/A           17040    C+G   ...ntrolPanel\SystemSettings.exe      N/A      |
|    0   N/A  N/A           20456    C+G   ....0.3124.93\msedgewebview2.exe      N/A      |
|    0   N/A  N/A           23296    C+G   ...8bbwe\PhoneExperienceHost.exe      N/A      |
|    0   N/A  N/A           25528    C+G   ...8bbwe\Microsoft.CmdPal.UI.exe      N/A      |
|    0   N/A  N/A           26808    C+G   ...indows\System32\ShellHost.exe      N/A      |
|    0   N/A  N/A           28164    C+G   C:\Windows\explorer.exe               N/A      |
|    0   N/A  N/A           29748    C+G   ...IA app\CEF\NVIDIA Overlay.exe      N/A      |
|    0   N/A  N/A           31764    C+G   ...s\PowerToys.PowerLauncher.exe      N/A      |
|    0   N/A  N/A           32252    C+G   ...em32\ApplicationFrameHost.exe      N/A      |
|    0   N/A  N/A           36836    C+G   ...y\StartMenuExperienceHost.exe      N/A      |
|    0   N/A  N/A           37448    C+G   ...yb3d8bbwe\WindowsTerminal.exe      N/A      |
|    0   N/A  N/A           37464    C+G   ..._cw5n1h2txyewy\SearchHost.exe      N/A      |
|    0   N/A  N/A           38692    C+G   ...4__8wekyb3d8bbwe\ms-teams.exe      N/A      |
+-----------------------------------------------------------------------------------------+

C:\Users\ADMIN>

I have

!pip install --upgrade tensorflow_hub
import tensorflow_hub as hub

model = hub.KerasLayer("https://tfhub.dev/google/nnlm-en-dim128/2")
embeddings = model(["The rain in Spain.", "falls", "mainly", "In the plain!"])
print(embeddings.shape)

!pip install -q tensorflow-recommenders
!pip install -q --upgrade tensorflow-datasets

!pip install tensorflow-recommenders
!pip install --upgrade tensorflow-datasets

from typing import Dict, Text

import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
import tensorflow_recommenders as tfrs

ratings = tfds.load('movielens/100k-ratings', split="train")
movies = tfds.load('movielens/100k-movies', split="train")

ratings = ratings.map(lambda x: {"movie_title": x["movie_title"], "user_id": x["user_id"]})
movies = movies.map(lambda x: x["movie_title"])

user_ids_vocabulary = tf.keras.layers.StringLookup(mask_token=None)
user_ids_vocabulary.adapt(ratings.map(lambda x: x["user_id"]))
movie_titles_vocabulary = tf.keras.layers.StringLookup(mask_token=None)
movie_titles_vocabulary.adapt(movies)

class MovieLensModel(tfrs.Model):

  def __init__(self, user_model: tf.keras.Model, movie_model: tf.keras.Model, task: tfrs.tasks.Retrieval):
    super().__init__()
    self.user_model = user_model
    self.movie_model = movie_model
    self.task = task

  def compute_loss(self, features: Dict[Text, tf.Tensor], training=False) -> tf.Tensor:
    user_embeddings = self.user_model(features["user_id"])
    movie_embeddings = self.movie_model(features["movie_title"])
    return self.task(user_embeddings, movie_embeddings)

user_model = tf.keras.Sequential([user_ids_vocabulary, tf.keras.layers.Embedding(user_ids_vocabulary.vocabulary_size(), 64)])
movie_model = tf.keras.Sequential([movie_titles_vocabulary, tf.keras.layers.Embedding(movie_titles_vocabulary.vocabulary_size(), 64)])

!pip show tensorflow
import tensorflow as tf
print(tf.__version__)

print(user_ids_vocabulary.get_vocabulary())
print(movie_titles_vocabulary.get_vocabulary())

task = tfrs.tasks.Retrieval(metrics=tfrs.metrics.FactorizedTopK(movies.batch(128).map(movie_model)))

error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[1], line 55
     52 print(user_ids_vocabulary.get_vocabulary())
     53 print(movie_titles_vocabulary.get_vocabulary())
---> 55 task = tfrs.tasks.Retrieval(metrics=tfrs.metrics.FactorizedTopK(movies.batch(128).map(movie_model)))

File ~\PyCharmMiscProject\.venv\Lib\site-packages\tensorflow_recommenders\metrics\factorized_top_k.py:79, in FactorizedTopK.__init__(self, candidates, ks, name)
     75 super().__init__(name=name)
     77 if isinstance(candidates, tf.data.Dataset):
     78   candidates = (
---> 79       layers.factorized_top_k.Streaming(k=max(ks))
     80       .index_from_dataset(candidates)
     81   )
     83 self._ks = ks
     84 self._candidates = candidates

File ~\PyCharmMiscProject\.venv\Lib\site-packages\tensorflow_recommenders\layers\factorized_top_k.py:376, in Streaming.__init__(self, query_model, k, handle_incomplete_batches, num_parallel_calls, sorted_order)
    373 self._num_parallel_calls = num_parallel_calls
    374 self._sorted = sorted_order
--> 376 self._counter = self.add_weight("counter", dtype=tf.int32, trainable=False)

File ~\PyCharmMiscProject\.venv\Lib\site-packages\keras\src\layers\layer.py:547, in Layer.add_weight(self, shape, initializer, dtype, trainable, autocast, regularizer, constraint, aggregation, name)
    545 initializer = initializers.get(initializer)
    546 with backend.name_scope(self.name, caller=self):
--> 547     variable = backend.Variable(
    548         initializer=initializer,
    549         shape=shape,
    550         dtype=dtype,
    551         trainable=trainable,
    552         autocast=autocast,
    553         aggregation=aggregation,
    554         name=name,
    555     )
    556 # Will be added to layer.losses
    557 variable.regularizer = regularizers.get(regularizer)

File ~\PyCharmMiscProject\.venv\Lib\site-packages\keras\src\backend\common\variables.py:185, in Variable.__init__(self, initializer, shape, dtype, trainable, autocast, aggregation, name)
    183 else:
    184     if callable(initializer):
--> 185         self._shape = self._validate_shape(shape)
    186         self._initialize_with_initializer(initializer)
    187     else:

File ~\PyCharmMiscProject\.venv\Lib\site-packages\keras\src\backend\common\variables.py:207, in Variable._validate_shape(self, shape)
    206 def _validate_shape(self, shape):
--> 207     shape = standardize_shape(shape)
    208     if None in shape:
    209         raise ValueError(
    210             "Shapes used to initialize variables must be "
    211             "fully-defined (no `None` dimensions). Received: "
    212             f"shape={shape} for variable path='{self.path}'"
    213         )

File ~\PyCharmMiscProject\.venv\Lib\site-packages\keras\src\backend\common\variables.py:582, in standardize_shape(shape)
    580     continue
    581 if not is_int_dtype(type(e)):
--> 582     raise ValueError(
    583         f"Cannot convert '{shape}' to a shape. "
    584         f"Found invalid entry '{e}' of type '{type(e)}'. "
    585     )
    586 if e < 0:
    587     raise ValueError(
    588         f"Cannot convert '{shape}' to a shape. "
    589         "Negative dimensions are not allowed."
    590     )

ValueError: Cannot convert '('c', 'o', 'u', 'n', 't', 'e', 'r')' to a shape. Found invalid entry 'c' of type '<class 'str'>'.

enter image description here

My Jupyter notebook https://gist.github.com/donhuvy/9447a2aea4cd182007198f28d4b7b413 . How to fix it?

Related https://stackoverflow.com/questions/79553173/tensorflow-nightly-valueerror-cannot-convert-c-o-u-n-t-e

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions