Radhika Bhrara
Radhika Bhrara

Reputation: 11

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 60, 5), found shape=(None, 60, 7)

regressor.add(LSTM(units = 60, activation = 'relu', return_sequences = True, input_shape = (X_train.shape[1], 5)))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 70, activation = 'relu', return_sequences = True, input_shape = (X_train.shape[1], 5)))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 90, activation = 'relu', return_sequences = True, input_shape = (X_train.shape[1], 5)))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units = 140, activation = 'relu', return_sequences = True, input_shape = (X_train.shape[1], 5)))
regressor.add(Dropout(0.2))

regressor.add(Dense(units =1))
regressor.summary()


regressor.compile(optimizer = 'adam', loss='mean_absolute_error')
regressor.fit(X_train, Y_train, epochs = 20, batch_size =50)[enter image description here][1]

On running this code ; the value error raised while I was preparing my model for prediction . Help me to rectify it

Epoch 1/20
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-26-535f5f5c29a7> in <module>()
----> 1 regressor.fit(X_train, Y_train, epochs = 20, batch_size =50)

1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
    65     except Exception as e:  # pylint: disable=broad-except
    66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
    68     finally:
    69       del filtered_tb

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
  1145           except Exception as e:  # pylint:disable=broad-except
  1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
  1148             else:
  1149               raise

ValueError: in user code:

   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
       return step_function(self, iterator)
   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
       outputs = model.distribute_strategy.run(run_step, args=(data,))
   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
       outputs = model.train_step(data)
   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
       y_pred = self(x, training=True)
   File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
       raise e.with_traceback(filtered_tb) from None
   File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
       raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

   ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 60, 5), found shape=(None, 60, 7)

On running the code ; I came across this error on my python interpretor. Let me know the correct compatibility!

Upvotes: 1

Views: 776

Answers (1)

bui
bui

Reputation: 1651

The error pretty much says it all. The first LSTM layer in your model expects a batch of time series, each having 60 timesteps and 5 features per timestep, but somehow you fed it a batch of time series each having 60 steps and 7 features. You might check your X_train.shape[2] to see if it is 7.

Also, the way you're using the output of LSTM layers is incorrect. You might want to go through this answer and official tensorflow documentation to see what are the outputs of a LSTM layer with return_sequences set to True.

Upvotes: 1

Related Questions