keras-team / keras-io

Keras documentation, hosted live at keras.io
Apache License 2.0
2.78k stars 2.05k forks source link

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

Open vladimirvlasevsky opened 3 months ago

vladimirvlasevsky commented 3 months ago

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 commented 1 day ago

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