Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example A Transformer-based recommendation system does not work when using Keras 3.4.1 #1899

Open
vladimirvlasevsky opened this issue Jul 29, 2024 · 1 comment
Assignees

Comments

@vladimirvlasevsky
Copy link

vladimirvlasevsky commented Jul 29, 2024

Issue Type

Bug

Source

source

Keras Version

Keras 3.4.1

Custom Code

No

OS Platform and Distribution

Windows 10

Python version

3.12.3

GPU model and memory

No response

Current Behavior?

I want to reproduce this example using Keras 3.4.1, but I get an error message. I noticed that this example works when using Keras 3.2.1.

Standalone code to reproduce the issue or tutorial link

The original code (https://github.com/keras-team/keras-io/blob/master/examples/structured_data/movielens_recommendations_transformers.py) is used, no changes.

Relevant log output

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[1], line 539
    536 train_dataset = get_dataset_from_csv("train_data.csv", shuffle=True, batch_size=265)
    538 # Fit the model with the training data.
--> 539 model.fit(train_dataset, epochs=5)
    541 # Read the test data.
    542 test_dataset = get_dataset_from_csv("test_data.csv", batch_size=265)

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\utils\traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    119     filtered_tb = _process_traceback_frames(e.__traceback__)
    120     # To get the full stack trace, call:
    121     # `keras.config.disable_traceback_filtering()`
--> 122     raise e.with_traceback(filtered_tb) from None
    123 finally:
    124     del filtered_tb

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\models\functional.py:244, in Functional._adjust_input_rank(self, flat_inputs)
    242             adjusted.append(ops.expand_dims(x, axis=-1))
    243             continue
--> 244     raise ValueError(
    245         f"Invalid input shape for input {x}. Expected shape "
    246         f"{ref_shape}, but input has incompatible shape {x.shape}"
    247     )
    248 # Add back metadata.
    249 for i in range(len(flat_inputs)):

ValueError: Exception encountered when calling Functional.call().

Invalid input shape for input Tensor("functional_1/Cast_1:0", shape=(None,), dtype=float32). Expected shape (None, 3), but input has incompatible shape (None,)

Arguments received by Functional.call():
  • inputs=OrderedDict({'user_id': 'tf.Tensor(shape=(None,), dtype=string)', 'sequence_movie_ids': 'tf.Tensor(shape=(None, None), dtype=string)', 'sequence_ratings': 'tf.Tensor(shape=(None, None), dtype=float32)', 'sex': 'tf.Tensor(shape=(None,), dtype=string)', 'age_group': 'tf.Tensor(shape=(None,), dtype=string)', 'occupation': 'tf.Tensor(shape=(None,), dtype=string)', 'target_movie_id': 'tf.Tensor(shape=(None,), dtype=string)'})
  • training=True
  • mask=OrderedDict({'user_id': 'None', 'sequence_movie_ids': 'None', 'sequence_ratings': 'None', 'sex': 'None', 'age_group': 'None', 'occupation': 'None', 'target_movie_id': 'None'})
@jsoung
Copy link

jsoung commented Nov 21, 2024

FWIW, this can be worked around by returning an OrderedDict from the method create_model_inputs().

def create_model_inputs():
    input_features = OrderedDict()

    input_features["user_id"] = keras.Input(
        name="user_id", shape=(1,), dtype="string"
    )
    input_features["sequence_movie_ids"] = keras.Input(
        name="sequence_movie_ids", shape=(sequence_length - 1,), dtype="string"
    )
    input_features["sequence_ratings"] = keras.Input(
        name="sequence_ratings", shape=(sequence_length - 1,), dtype=tf.float32
    )
    input_features["sex"] = keras.Input(name="sex", shape=(1,), dtype="string")
    input_features["age_group"] = keras.Input(
        name="age_group", shape=(1,), dtype="string"
    )
    input_features["occupation"] = keras.Input(
        name="occupation", shape=(1,), dtype="string"
    )
    input_features["target_movie_id"] = keras.Input(
        name="target_movie_id", shape=(1,), dtype="string"
    )

    return input_features

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants