I’m trying to build and train a stateful LSTM model using Keras in TensorFlow, but I keep encountering a ValueError when specifying the batch_input_shape parameter.
The error message:
ValueError: Unrecognized keyword arguments passed to LSTM: {'batch_input_shape': (1, 1, 14)}
Here’s a simplified version of my code:
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
# Load your data
file_path = 'path_to_your_file.csv'
data = pd.read_csv(file_path)
# Create 'date' column with the first day of each month
data['date'] = pd.to_datetime(data['tahun'].astype(str) + '-' + data['bulan'].astype(str) + '-01')
data['date'] = data['date'] + pd.offsets.MonthEnd(0)
data.set_index('date', inplace=True)
# Group by 'date' and sum the 'amaun_rm' column
df_sales = data.groupby('date')['amaun_rm'].sum().reset_index()
# Create a new dataframe to model the difference
df_diff = df_sales.copy()
df_diff['prev_amaun_rm'] = df_diff['amaun_rm'].shift(1)
df_diff = df_diff.dropna()
df_diff['diff'] = df_diff['amaun_rm'] - df_diff['prev_amaun_rm']
# Create new dataframe from transformation from time series to supervised
df_supervised = df_diff.drop(['prev_amaun_rm'], axis=1)
for inc in range(1, 13):
field_name = 'lag_' + str(inc)
df_supervised[field_name] = df_supervised['diff'].shift(inc)
# Adding moving averages
df_supervised['ma_3'] = df_supervised['amaun_rm'].rolling(window=3).mean().shift(1)
df_supervised['ma_6'] = df_supervised['amaun_rm'].rolling(window=6).mean().shift(1)
df_supervised['ma_12'] = df_supervised['amaun_rm'].rolling(window=12).mean().shift(1)
df_supervised = df_supervised.dropna().reset_index(drop=True)
df_supervised = df_supervised.fillna(df_supervised.mean())
# Split the data into train and test sets
train_set, test_set = df_supervised[0:-6].values, df_supervised[-6:].values
scaler = MinMaxScaler(feature_range=(-1, 1))
scaler = scaler.fit(train_set)
train_set_scaled = scaler.transform(train_set)
test_set_scaled = scaler.transform(test_set)
# Split into input and output
X_train, y_train = train_set_scaled[:, 1:], train_set_scaled[:, 0]
X_test, y_test = test_set_scaled[:, 1:], test_set_scaled[:, 0]
X_train = X_train.reshape((X_train.shape[0], 1, X_train.shape[1]))
X_test = X_test.reshape((X_test.shape[0], 1, X_test.shape[1]))
# Check the shape of X_train
print("X_train shape:", X_train.shape) # Should output (44, 1, 14)
# Define the LSTM model
model = Sequential()
model.add(LSTM(4, stateful=True, batch_input_shape=(1, X_train.shape[1], X_train.shape[2])))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
# Train the model
model.fit(X_train, y_train, epochs=100, batch_size=1, verbose=1, shuffle=False)
# Summarize the model
model.summary()
What I have tried:
- I verified the shape of
X_train
which is(44, 1, 14)
. - I attempted to use
input_shape
instead ofbatch_input_shape
, which led to different errors. - I ensured that the versions of TensorFlow and Keras are compatible.
System Information:
-
Python version: 3.12
-
TensorFlow version: 2.17.0
-
Keras version: 3.4.1
Question:
How can I correctly specify the batch_input_shape
for my stateful LSTM model in Keras to avoid this error? Are there any specific version requirements or additional configurations needed?