Skip to content

"Working with RNNs" guide includes code that throws an exception #2142

@Undeceiver

Description

@Undeceiver

Issue Type

Documentation Bug

Source

source

Keras Version

3.10.0

Custom Code

No

OS Platform and Distribution

Windows 10

Python version

3.12.7

GPU model and memory

No response

Current Behavior?

The code at https://www.tensorflow.org/guide/keras/working_with_rnns#define_a_custom_cell_that_supports_nested_inputoutput (Definition and use of NestedCell class) throws an exception having to do with unexpected tuple shapes. I am not experienced enough with this to be able to tell for sure what the cause is, but I suspect it has to do with changes that have been made in how shapes are handled in keras since this guide was written. It's probably quite easy to fix the example to make it work on latest versions, but I wouldn't know it at this point in time.

I am only sending this to improve the documentation. I have gotten what I wanted from this guide and this is not a problem for me at the present time.

The exception:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[24], line 18
     15 input_1 = keras.Input((None, i1))
     16 input_2 = keras.Input((None, i2, i3))
---> 18 outputs = rnn((input_1, input_2))
     20 model = keras.models.Model([input_1, input_2], outputs)
     22 model.compile(optimizer="adam", loss="mse", metrics=["accuracy"])

File ~\AppData\Roaming\Python\Python312\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

Cell In[23], line 14, in NestedCell.build(self, input_shapes)
     11 def build(self, input_shapes):
     12     # expect input_shape to contain 2 items, [(batch, i1), (batch, i2, i3)]        
     13     i1 = input_shapes[0][1]
---> 14     i2 = input_shapes[1][1]
     15     i3 = input_shapes[1][2]
     17     self.kernel_1 = self.add_weight(
     18         shape=(i1, self.unit_1), initializer="uniform", name="kernel_1"
     19     )

IndexError: tuple index out of range

Standalone code to reproduce the issue or tutorial link

https://www.tensorflow.org/guide/keras/working_with_rnns#define_a_custom_cell_that_supports_nested_inputoutput

Relevant log output

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions