keras icon indicating copy to clipboard operation
keras copied to clipboard

Unrecognized keyword arguments passed to LSTM: {'batch_input_shape'

Open Ineedsomehelpah opened this issue 1 year ago • 4 comments
trafficstars

model = Sequential() model.add(LSTM(4, batch_input_shape=(1, X_train.shape[1], X_train.shape[2]), stateful=True)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(X_train, y_train, epochs=100, batch_size=1, verbose=1, shuffle=False)

ValueError: Unrecognized keyword arguments passed to LSTM: {'batch_input_shape': (1, 1, 7)}

My version : TensorFlow version: 2.17.0 Keras version: 3.4.1

I've seen similar issue raised on stackoverflow. I was able to run the code 2 weeks ago without error. What new keyword argument should I use?

https://stackoverflow.com/questions/78805181/valueerror-unrecognized-keyword-arguments-passed-to-lstm-batch-input-shape

Ineedsomehelpah avatar Aug 09 '24 19:08 Ineedsomehelpah

That's the usage in Keras 2. In Keras 3, you can refer to: https://github.com/keras-team/keras/blob/933579d3c4a585f236982d05d3a74921f9567415/keras/src/layers/rnn/rnn.py#L105-L110

Grvzard avatar Aug 10 '24 11:08 Grvzard

@Ineedsomehelpah

Just adding up to the previous comment. Note that:

  1. Input layer was missing the description for the parameter batch_shape in 3.4.1. And there is still the need to update RNN's description if I understand correctly, otherwise it'd still fail.
  2. . You could do:

import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense, Input

X_train = keras.random.normal((50,20,3))
y_train = keras.random.normal((50, 1))

model = Sequential()
model.add(Input(batch_shape=X_train.shape))
model.add(LSTM(units=4, stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, y_train, epochs=100, batch_size=1,)

newresu avatar Aug 11 '24 07:08 newresu

Here is the code with slight modification that works with batch_input_shape

import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense, Input, InputLayer

X_train = keras.random.normal((50,20,3))
y_train = keras.random.normal((50, 1))

model = Sequential()
model.add(InputLayer(batch_input_shape=X_train.shape))
model.add(LSTM(units=4, stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, y_train, epochs=100, batch_size=1,)

sachinprasadhs avatar Aug 14 '24 06:08 sachinprasadhs

Really appreciate all your feedback, it's working for me now. Thanks all

Ineedsomehelpah avatar Aug 26 '24 21:08 Ineedsomehelpah

Thanks for confirming, could you please close the issue as well.

sachinprasadhs avatar Aug 29 '24 21:08 sachinprasadhs

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar Aug 29 '24 21:08 google-ml-butler[bot]

Here is the code with slight modification that works with batch_input_shape

import keras from keras.models import Sequential from keras.layers import LSTM, Dense, Input, InputLayer

X_train = keras.random.normal((50,20,3)) y_train = keras.random.normal((50, 1))

model = Sequential() model.add(InputLayer(batch_input_shape=X_train.shape)) model.add(LSTM(units=4, stateful=True)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(X_train, y_train, epochs=100, batch_size=1,)

Hello! Could you create a similar example with SimpleRNN layer using functional API instead? I am having issues reproducing the logic using tensorflow 2.18 and keras 3.8. and functional API, when training the model. So replace LSTM with RNN with stateful=True and keep the dense layer. However, the official documentation states the following:

Else for functional model with 1 or more Input layers:
    `batch_shape=(...)` to all the first layers in your model.
This is the expected shape of your inputs
*including the batch size*.
It should be a tuple of integers, e.g. `(32, 10, 100)`.
- Specify `shuffle=False` when calling `fit()`.

To reset the states of your model, call `.reset_states()` on either
a specific layer, or on your entire model.

Not sure how .reset_states() is to be combined with stateful=True and whether it makes sense? Thank you for your time.

NickBanana7 avatar Mar 22 '25 08:03 NickBanana7

I am unable to resolve this problem, i hadnot used this parameter explicitly anywhere, but on saving it automatically comes to the binary file of my model, which reflects as an error when I am loading that model.

AnushkJain2201 avatar Apr 10 '25 19:04 AnushkJain2201