ageron / handson-ml3

A series of Jupyter notebooks that walk you through the fundamentals of Machine Learning and Deep Learning in Python using Scikit-Learn, Keras and TensorFlow 2.
Apache License 2.0
7.44k stars 3k forks source link

[BUG] Cpt 16 Q8 GRU does not support ragged tensor #157

Open Guo749 opened 1 month ago

Guo749 commented 1 month ago

Thanks for helping us improve this project!

Describe the bug Please provide a clear and concise description of what the bug is, and specify the notebook name and the cell number at which the problem occurs (or the chapter and page in the book).

When executing code in q8 cpt 16, there is one issue

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

Cannot index into an inner ragged dimension.

Arguments received by GRU.call():
  • sequences=tf.Tensor(shape=(None, None, 5), dtype=float32)
  • initial_state=None
  • mask=None
  • training=True

To Reproduce Please copy the code that fails here, using code blocks like this:

np.random.seed(42)
tf.random.set_seed(42)

embedding_size = 5

model = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=[None], dtype=tf.int32, ragged=True),
    tf.keras.layers.Embedding(input_dim=len(POSSIBLE_CHARS),
                              output_dim=embedding_size),
    tf.keras.layers.GRU(30),
    tf.keras.layers.Dense(1, activation="sigmoid")
])
optimizer = tf.keras.optimizers.SGD(learning_rate=0.02, momentum = 0.95,
                                    nesterov=True)
model.compile(loss="binary_crossentropy", optimizer=optimizer,
              metrics=["accuracy"])
history = model.fit(X_train, y_train, epochs=20,
                    validation_data=(X_valid, y_valid))

And if you got an exception, please copy the full stacktrace here:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-14-6882d7d9cf12>](https://localhost:8080/#) in <cell line: 17>()
     15 model.compile(loss="binary_crossentropy", optimizer=optimizer,
     16               metrics=["accuracy"])
---> 17 history = model.fit(X_train, y_train, epochs=20,
     18                     validation_data=(X_valid, y_valid))

1 frames
[/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py](https://localhost:8080/#) in error_handler(*args, **kwargs)
    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

[/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py](https://localhost:8080/#) in error_handler(*args, **kwargs)
    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

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

Cannot index into an inner ragged dimension.

Arguments received by GRU.call():
  • sequences=tf.Tensor(shape=(None, None, 5), dtype=float32)
  • initial_state=None
  • mask=None
  • training=True

Expected behavior A clear and concise description of what you expected to happen.

shoud train successfully

Screenshots If applicable, add screenshots to help explain your problem.

https://screenbud.com/shot/f025e559-e87e-4751-b852-aa714fbdacf7

Versions (please complete the following information):

Additional context Add any other context about the problem here.