2

In my model

Xtrain shape : (62, 30, 100)
Ytrain shape : (62, 1, 100)
Xtest shape : (16, 30, 100)
Ytest shape : (16, 1, 100)

When I build my model,

model = Sequential()
model.add(LSTM(units=100, return_sequences= True, input_shape=(x_train.shape[1],X_train.shape[2])))
model.add(LSTM(units=100, return_sequences=True))
model.add(Dense(units=100))

model.fit(x_train,y_train,epochs=5,batch_size=13)

when I try to fit it throws a error,

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

I need to predict for the next 1 time stamp for all 100 features. What are the changes needed to be done?

2 Answers2

1

The code posted doesn't seem to be the same that generated the error.

Print your model.summary(). You will see:

  • LSTM 1: (None, 30, 100)
  • LSTM 2: (None, 30, 100)
  • Dense: (None, 30, 100)

You didn't use anything to reduce the number of time steps to 1. Your error message, according to this model should definitely be complaining about trying (None, 30, 100) vs (62, 1, 100).

To eliminate the timesteps, you need return_sequences=False in the last LSTM, so your model becomes:

  • (None, 30, 100)
  • (None, 100)
  • (None, 100)

This way, you can have Ytrain.shape == (62,100)

If you really really need that middle dimension == 1, just use Lambda(lambda x: K.expand_dims(x, 1)) after the dense.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
1

If you want to have the result of each LSTM step to be processed on its own (the most common use of placing a Dense layer after an LSTM or other RNNs), you need to wrap it, like so:

model = Sequential()
model.add(LSTM(units=100, return_sequences= True, input_shape=(x_train.shape[1],X_train.shape[2])))
model.add(LSTM(units=100, return_sequences=True))
model.add(TimeDistributed(Dense(units=100)))

Each output will be served on its own to the Dense layer (of course, it would be the same layer - all weights will be shared between each 'instance' of it).

user2182857
  • 718
  • 8
  • 20