Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 550
Chris Wong Avatar asked Aug 09 '18 09:08

Chris Wong


1 Answers

I had a similar problem, found the answer here:

I added model.add(Flatten()) before the last Dense layer

like image 148
Helen Avatar answered Sep 24 '22 07:09

Helen