0

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!

Taran
  • 265
  • 1
  • 11
  • Do you have one prediction for a set of timesteps or a prediction for every timestep? – DJK Dec 05 '17 at 23:31
  • I have a scalar target value per time-step. So in this case I have 82 targets I'd like to use for each batch of 82 x 40 training matrix – Taran Dec 06 '17 at 00:19
  • 1
    your last LSTM `retrun_sequences=True` – DJK Dec 06 '17 at 00:22
  • @djk47463 that worked! Could you explain why it did? – Taran Dec 06 '17 at 13:46
  • It is the setting for a many-to-many LSTM structure, theres a ton on posts on SO that cover this. The default setting is `False` which is a many-to-one LSTM structure which is typical for LSTMs, but for your case the setup is a bit different. – DJK Dec 06 '17 at 19:21
  • 1
    i.e. [this post](https://stackoverflow.com/questions/43034960/many-to-one-and-many-to-many-lstm-examples-in-keras) – DJK Dec 06 '17 at 19:22

0 Answers0