I'm training a model to predict the stock price and input data is close price. I use 45 days data to predict the 46th day's close price and a economic Indicator to be second feature, here is the model:
model = Sequential()
model.add( LSTM( 512, input_shape=(45, 2), return_sequences=True))
model.add( LSTM( 512, return_sequences=True))
model.add( (Dense(1)))
model.compile(loss='mse', optimizer='adam')
history = model.fit( X_train, y_train, batch_size = batchSize, epochs=epochs, shuffle = False)
When I run this I get the following error:
ValueError: Error when checking target: expected dense_1 to have 3 dimensions, but got array with shape (118, 1)
However, I print
the shape of data and they are:
X_train:(118, 45, 2)
y_train:(118, 1)
I have no idea why the model is expecting a 3 dimensional output when y_train is (118, 1). Where am I wrong and what should I do?
I had a similar problem, found the answer here:
I added
model.add(Flatten())
before the last Dense layerThe Shape of the training data should be in the format of:
(num_samples,num_features,num_signals/num_vectors)
. Following this convention, try passing the training data in the form of an array with the reshaped size in convention described above, along with that ensure to add thevalidation_data
argument in themodel.fit
command. An example of this is:where both
X_input
,y_input
are training data arrays with shape (126,711,1) and (126,1) respectively andX_output
,y_output
are validation/test data arrays with shapes (53,711,1) and (53,1) respectively.In case you find a shuffling error try setting the value of shuffle argument as True after following the above methodology.
Your second LSTM layer also returns sequences and Dense layers by default apply the kernel to every timestep also producing a sequence:
So your output is shape
(bs, 45, 1)
. To solve the problem you need to setreturn_sequences=False
in your second LSTM layer which will compress sequence:And you'll get the desired output. Note
bs
is the batch size.