Issue
I have the following python code:
layer_width= 32
model= keras.Sequential()
# keras batch_size is the first dimension of data, while we are using the second dimension to create the mini-batches
model.add(keras.Input(shape= (batch_size, n_feat), batch_size= 72))
model.add(layers.Masking(mask_value= -1))
model.add(layers.LSTM(layer_width, activation= 'relu', recurrent_dropout= .5,))
model.add(layers.Dense(n_y))
model.compile(optimizer='adam', loss='mean_squared_error')
this works as long as n_y
= 1 and y is limited to one dimension (except for batch_size
and n_feat
). I'm using the following code to feed one-dimensional target data to the network:
data= my_code.DataGenerator(train, y_train[:, :, 0:1], batch_size= batch_size)
[x.shape for x in next(iter(data))]
# [(72, 256, 22), (72, 256, 1)]
history= model.fit(data, epochs= 1)
this works without errors. However, y_train has actually 6 dimensions in its 3rd axis, and I wish to train the network to predict all of these. If I set n_y
to 6 and I feed the whole of y_train
to the data generator, I get this error:
----> 2 history= model.fit(data, epochs= 1)
File ~/.local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File ~/.local/lib/python3.10/site-packages/tensorflow/python/eager/execute.py:60, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
53 # Convert any objects of type core_types.Tensor to Tensor.
54 inputs = [
55 tensor_conversion_registry.convert(t)
56 if isinstance(t, core_types.Tensor)
57 else t
58 for t in inputs
59 ]
---> 60 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
61 inputs, attrs, num_outputs)
62 except core._NotOkStatusException as e:
63 if name is not None:
InvalidArgumentError: Graph execution error:
Detected at node mean_squared_error/remove_squeezable_dimensions/Squeeze defined at (most recent call last):
File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
File "/home/carlo/.local/lib/python3.10/site-packages/ipykernel_launcher.py", line 17, in <module>
File "/home/carlo/.local/lib/python3.10/site-packages/traitlets/config/application.py", line 1053, in launch_instance
File "/home/carlo/.local/lib/python3.10/site-packages/ipykernel/kernelapp.py", line 737, in start
File "/home/carlo/.local/lib/python3.10/site-packages/tornado/platform/asyncio.py", line 195, in start
File "/usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
File "/usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
File "/home/carlo/.local/lib/python3.10/site-packages/ipykernel/kernelbase.py", line 524, in dispatch_queue
File "/home/carlo/.local/lib/python3.10/site-packages/ipykernel/kernelbase.py", line 513, in process_one
File "/home/carlo/.local/lib/python3.10/site-packages/ipykernel/kernelbase.py", line 418, in dispatch_shell
File "/home/carlo/.local/lib/python3.10/site-packages/ipykernel/kernelbase.py", line 758, in execute_request
File "/home/carlo/.local/lib/python3.10/site-packages/ipykernel/ipkernel.py", line 426, in do_execute
File "/home/carlo/.local/lib/python3.10/site-packages/ipykernel/zmqshell.py", line 549, in run_cell
File "/home/carlo/.local/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3046, in run_cell
File "/home/carlo/.local/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3101, in _run_cell
File "/home/carlo/.local/lib/python3.10/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
File "/home/carlo/.local/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3306, in run_cell_async
File "/home/carlo/.local/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3488, in run_ast_nodes
File "/home/carlo/.local/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3548, in run_code
File "/tmp/ipykernel_5961/154309858.py", line 2, in <module>
File "/home/carlo/.local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
File "/home/carlo/.local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1783, in fit
File "/home/carlo/.local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1377, in train_function
File "/home/carlo/.local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1360, in step_function
File "/home/carlo/.local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1349, in run_step
File "/home/carlo/.local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1127, in train_step
File "/home/carlo/.local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1185, in compute_loss
File "/home/carlo/.local/lib/python3.10/site-packages/keras/src/engine/compile_utils.py", line 277, in __call__
File "/home/carlo/.local/lib/python3.10/site-packages/keras/src/losses.py", line 143, in __call__
File "/home/carlo/.local/lib/python3.10/site-packages/keras/src/losses.py", line 263, in call
File "/home/carlo/.local/lib/python3.10/site-packages/keras/src/utils/losses_utils.py", line 200, in squeeze_or_expand_dimensions
File "/home/carlo/.local/lib/python3.10/site-packages/keras/src/utils/losses_utils.py", line 139, in remove_squeezable_dimensions
Can not squeeze dim[2], expected a dimension of 1, got 6
[[{{node mean_squared_error/remove_squeezable_dimensions/Squeeze}}]] [Op:__inference_train_function_29864]
why is that?
Solution
I am not sure why the code worked when y was one-dimensional, but I made it work with multidimensional y by setting True the pass_sequences
argument of the LSTM layer. Otherwise that layer only passes the last values predicted of each series, and it would take a different data generator to make the training work (one with output shapes [(72, 256, 22), (72, 1, n_y)]
, note that I did not test this different data generator).
Answered By - carlo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.