1

I'm trying example 10.2.3 from the book Deep Learning with Python by François Chollet. I'm getting an error on the line history = model.fit(...) using Tensorflow 2.17. However, I don't get an error if I downgrade to Tensorflow 2.15.

What has changed between these versions that is affecting this example? And how should the implementation change for Tensorflow 2.17+?

Code:

from tensorflow import keras
from tensorflow.keras import layers

inputs = keras.Input(shape=(sequence_length, raw_data.shape[-1]))
x = layers.Flatten()(inputs)
x = layers.Dense(16, activation='relu')(x)
outputs = layers.Dense(1)(x)

model = keras.Model(inputs, outputs)
callbacks = [
    keras.callbacks.ModelCheckpoint('jena_dense.keras', save_best_only=True)
]
model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])

history = model.fit(
    train_dataset, epochs=10, validation_data=val_dataset, callbacks=callbacks)

model = keras.models.load_model('jena_dense.keras')
print(f"Test MAE: {model.evaluate(test_dataset)[1]:.2f}")

Error:

--------------------------------------------------------------------------- InvalidArgumentError                      Traceback (most recent call last) Cell In[10], line 15
     10 callbacks = [
     11     keras.callbacks.ModelCheckpoint('jena_dense.keras', save_best_only=True)
     12 ]
     13 model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
---> 15 history = model.fit(
     16     train_dataset, epochs=10, validation_data=val_dataset, callbacks=callbacks)
     18 model = keras.models.load_model('jena_dense.keras')
     19 print(f"Test MAE: {model.evaluate(test_dataset)[1]:.2f}")

File ~\anaconda3\envs\pqn\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 ~\anaconda3\envs\pqn\Lib\site-packages\tensorflow\python\eager\execute.py:53, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     51 try:
     52   ctx.ensure_initialized()
---> 53   tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     54                                       inputs, attrs, num_outputs)
     55 except core._NotOkStatusException as e:
     56   if name is not None:

InvalidArgumentError: Graph execution error:

Detected at node functional_1/flatten_1/Reshape defined at (most recent call last):   File "<frozen runpy>", line 198, in
_run_module_as_main

  File "<frozen runpy>", line 88, in _run_code

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\ipykernel_launcher.py", line 17, in <module>

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\traitlets\config\application.py", line 1075, in launch_instance

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\ipykernel\kernelapp.py", line 701, in start

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\tornado\platform\asyncio.py", line 205, in start

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\asyncio\windows_events.py", line 322, in run_forever

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\asyncio\base_events.py", line 641, in run_forever

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\asyncio\base_events.py", line 1986, in _run_once

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\asyncio\events.py", line 88, in _run

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\ipykernel\kernelbase.py", line 534, in dispatch_queue

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\ipykernel\kernelbase.py", line 523, in process_one

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\ipykernel\kernelbase.py", line 429, in dispatch_shell

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\ipykernel\kernelbase.py", line 767, in execute_request

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\ipykernel\ipkernel.py", line 429, in do_execute

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\ipykernel\zmqshell.py", line 549, in run_cell

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\IPython\core\interactiveshell.py", line 3075, in run_cell

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\IPython\core\interactiveshell.py", line 3130, in _run_cell

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\IPython\core\async_helpers.py", line 128, in _pseudo_sync_runner

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\IPython\core\interactiveshell.py", line 3334, in run_cell_async

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\IPython\core\interactiveshell.py", line 3517, in run_ast_nodes

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\IPython\core\interactiveshell.py", line 3577, in run_code

  File "C:\Users\Mike\AppData\Local\Temp\ipykernel_16136\1294295077.py", line 15, in <module>

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\utils\traceback_utils.py", line 117, in error_handler

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 320, in fit

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 121, in one_step_on_iterator

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 108, in one_step_on_data

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 51, in train_step

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\utils\traceback_utils.py", line 117, in error_handler

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\layers\layer.py", line 901, in __call__

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\utils\traceback_utils.py", line 117, in error_handler

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\ops\operation.py", line 46, in __call__

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\utils\traceback_utils.py", line 156, in error_handler

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\models\functional.py", line 175, in call

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\ops\function.py", line 171, in _run_through_graph

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\models\functional.py", line 560, in call

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\utils\traceback_utils.py", line 117, in error_handler

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\layers\layer.py", line 901, in __call__

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\utils\traceback_utils.py", line 117, in error_handler

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\ops\operation.py", line 46, in __call__

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\utils\traceback_utils.py", line 156, in error_handler

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\layers\reshaping\flatten.py", line 54, in call

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\ops\numpy.py", line 4440, in reshape

  File "C:\Users\Mike\anaconda3\envs\pqn\Lib\site-packages\keras\src\backend\tensorflow\numpy.py", line 1789, in reshape

Only one input size may be -1, not both 0 and 1      [[{{node functional_1/flatten_1/Reshape}}]] [Op:__inference_one_step_on_iterator_9970]
2
  • I don't have a good answer since I haven't worked with Keras 3 yet, but I have seen this question pop up multiple times recently. It seems to be related to the move to Keras 3. You could confirm which Keras version you have installed (this is installed as a separate library when you install TF so you can check via pip). Looking into what has changed with Keras 3 may be a good starting point. Commented Nov 27, 2024 at 11:16
  • Please make sure your dataset is properly batched and the input shape is defined, before training your model. Commented Dec 5, 2024 at 10:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.