Search code examples
tensorflowkeraskeras-layer

ValueError: in case of LSTM with `stateful=True`


I tried to use LSTM network with stateful=True as follows:

import numpy as np, pandas as pd, matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, LSTM
from keras.callbacks import LambdaCallback
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler

raw = np.sin(2*np.pi*np.arange(1024)/float(1024/2))
data = pd.DataFrame(raw)

window_size = 3
data_s = data.copy()
for i in range(window_size):
    data = pd.concat([data, data_s.shift(-(i+1))], axis = 1)   
data.dropna(axis=0, inplace=True)

print (data)
ds = data.values

n_rows = ds.shape[0]
ts = int(n_rows * 0.8)

train_data = ds[:ts,:]
test_data = ds[ts:,:]

train_X = train_data[:,:-1]
train_y = train_data[:,-1]
test_X = test_data[:,:-1]
test_y = test_data[:,-1]

print (train_X.shape)
print (train_y.shape)
print (test_X.shape)
print (test_y.shape)

(816, 3) (816,) (205, 3) (205,)

batch_size = 3
n_feats = 1

train_X = train_X.reshape(train_X.shape[0], batch_size, n_feats)
test_X = test_X.reshape(test_X.shape[0], batch_size, n_feats)
print(train_X.shape, train_y.shape)

regressor = Sequential()
regressor.add(LSTM(units = 64, batch_input_shape=(train_X.shape[0], batch_size, n_feats),
                   activation = 'sigmoid',  
                   stateful=True, return_sequences=True))

regressor.add(Dense(units = 1))

regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')

resetCallback = LambdaCallback(on_epoch_begin=lambda epoch,logs: regressor.reset_states())

regressor.fit(train_X, train_y, batch_size=7, epochs = 1, callbacks=[resetCallback])

previous_inputs = test_X                                    
regressor.reset_states()

previous_predictions = regressor.predict(previous_inputs).reshape(-1)
test_y = test_y.reshape(-1)
plt.plot(test_y, color = 'blue')
plt.plot(previous_predictions, color = 'red')
plt.show()

However, I got:

ValueError: Error when checking target: expected dense_1 to have 3 dimensions, but got array with shape (816, 1)

PS this code has been adapted from https://github.com/danmoller/TestRepo/blob/master/testing%20the%20blog%20code%20-%20train%20and%20pred.ipynb


Solution

  • Two minor bugs:

    Here you have

    regressor.add(LSTM(units = 64, batch_input_shape=(train_X.shape[0], batch_size, n_feats),
                   activation = 'sigmoid',
                   stateful=True, return_sequences=True))
    

    This LSTM will return a 3D vector, but your y is 2D which throws a valuerror. You can fix this with return_sequences=False. I'm not sure why you initially had train_X.shape[0] inside of your batch_input, the number of samples in your entire set shouldn't affect the size of each batch.

        regressor.add(LSTM(units = 64, batch_input_shape=(1, batch_size, n_feats),
                   activation = 'sigmoid',
                   stateful=True, return_sequences=False))
    

    After this you have

    regressor.fit(train_X, train_y, batch_size=7, epochs = 1, callbacks=[resetCallback])
    

    In a stateful network you can only put in a number of inputs that divides the batch size. Since 7 doesn't divide 816 we change this to 1:

    regressor.fit(train_X, train_y, batch_size=1, epochs = 1, callbacks=[resetCallback])
    

    The same goes in your predict. You must specify batch_size=1:

    previous_predictions = regressor.predict(previous_inputs, batch_size=1).reshape(-1)