Skip to content Skip to sidebar Skip to footer

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 featur

Solution 1:

Your second LSTM layer also returns sequences and Dense layers by default apply the kernel to every timestep also producing a sequence:

# (bs, 45, 2)
model.add( LSTM( 512, input_shape=(45, 2), return_sequences=True))
# (bs, 45, 512)
model.add( LSTM( 512, return_sequences=True))
# (bs, 45, 512)
model.add( (Dense(1)))
# (bs, 45, 1)

So your output is shape (bs, 45, 1). To solve the problem you need to set return_sequences=False in your second LSTM layer which will compress sequence:

# (bs, 45, 2)
model.add( LSTM( 512, input_shape=(45, 2), return_sequences=True))
# (bs, 45, 512)
model.add( LSTM( 512, return_sequences=False)) # SET HERE# (bs, 512)
model.add( (Dense(1)))
# (bs, 1)

And you'll get the desired output. Note bs is the batch size.

Solution 2:

I had a similar problem, found the answer here:

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

Solution 3:

The 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 the validation_data argument in the model.fit command. An example of this is:

model.fit(x=X_input ,y=y_input,batch_size=2,validation_data=(X_output, y_output),epochs=10)

where both X_input, y_input are training data arrays with shape (126,711,1) and (126,1) respectively and X_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.

Post a Comment for "Error When Checking Target: Expected Dense_1 To Have 3 Dimensions, But Got Array With Shape (118, 1)"