So I've seen a few SO posts and Issue posts on the Keras Github about this but none of these solutions seem to work for me so far.
My question relates to the input_shape for an LSTM in Keras. Unlike most examples my problem is a time-series forecasting problem and has nothing to do with the types of classification examples I see all over the place.
I would like to know how to reshape my training and test data sets when fitting an LSTM model in Keras. For my use case I'd like to model an LSTM with 82 time-steps and 40 features per batch
# train_X, test_X, train_y & test_y are numpy arrays
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
> ((57482, 40), (57482,), (12546, 40), (12546,))
# For my use case I'd like to model an LSTM with 82 time-steps and 40 features per batch
# reshape input to be 3D [samples, timesteps, features]
train_X = train_X.reshape((train_X.shape[0]/82, 82, train_X.shape[1]))
test_X = test_X.reshape((test_X.shape[0]/82, 82, test_X.shape[1]))
train_y = train_y.reshape((train_y.shape[0]/82, 82, 1))
test_y = test_y.reshape((test_y.shape[0]/82, 82, 1))
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
> ((701, 82, 40), (701, 82, 1), (153, 82, 40), (153, 82, 1))
When I now create and LSTM and then attempt to fit it, like so,
model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2]), return_sequences=True))
model.add(LSTM(50))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
# fit network
history = model.fit(train_X, train_y, epochs=50, batch_size=1, validation_data=(test_X, test_y), verbose=2, shuffle=False)
I get the following error,
ValueError: Error when checking target: expected dense_15 to have 2 dimensions, but got array with shape (701, 82, 1)
I think I understand the concept of the input_shape and batch_size yet I'm clearly not able to reshape my train and test sets to match what Keras requires. What am I doing wrong?
Thanks for the help!