Specter07
Specter07

Reputation: 201

ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (1012, 1)

I understand this question has been asked over and over again but i tried every possible solution yet i cant seem to get rid of this error.

I'm trying to predict stock prices using the layers below based on a database of 27 features, 1012 samples for training and 125 for testing. - x_train.shape: (1012, 4, 27) - y_train.shape: (1012,)

I'm using the following code:

def Dynamic_Trainer(rate, activ1, activ2):
    lstm_model = Sequential()
    lstm_model.add(LSTM(26, batch_input_shape=(BATCH_SIZE, TIME_STEPS, X_train.shape[2]), dropout=0.0, recurrent_dropout=0.0,
             stateful=True, kernel_initializer='random_uniform', return_sequences=True))
    lstm_model.add(Dropout(rate))
    lstm_model.add(Dense(26, activation=activ1))
    lstm_model.add(Dropout(rate))
    lstm_model.add(Dense(1, activation=activ2))
    lstm_model.compile(loss='mean_squared_error', optimizer='rmsprop')
    print('XTRAIN:', X_train.shape)
    print('YTRAIN', y_train.shape)
    # Initializing The Training
    Dynamic_Trainer.history = lstm_model.fit(X_train, y_train, epochs=EPOCHS, verbose=2, batch_size=BATCH_SIZE,
                                             shuffle=False, validation_data=(Reformat_Matrix(x_val, BATCH_SIZE),
                                                                             Reformat_Matrix(y_val, BATCH_SIZE)))

I get this error: ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (1012, 1)

I don't understand what i am doing wrong since when i print the shapes of i get: x_train: (1012, 4, 27) y_train: (1012,) Which is to my knowledge the right shape.

Upvotes: 1

Views: 385

Answers (1)

Natthaphon Hongcharoen
Natthaphon Hongcharoen

Reputation: 2430

The return_sequences=True is the culprit. With it True it will return tensor rank 3, maybe (1012, TIME_STEPS, 26), which you can put it to another RNN layers.

But here you want to go straight to output so change this to False.

From comment, it seems you have more than one LSTM, the last one need return_sequences=False to have rank 2 tensor as output, the (1012, 1) from error log, while others need return_sequences=True to return tensor as rank 3 for the next RNN layers.

Upvotes: 1

Related Questions