I have an array X_train = (1110,25,2)
and a y_train = (1110,5,2)
. It means I use arrays with length of 25 for inputs and length of 5 for labels. But when I use:
model = Sequential()
model.add(LSTM(units = 25, return_sequences = True, input_shape = (25, 2)))
model.add(Dropout(0.2))
model.add(Dense(units = 2))
model.compile(optimizer = 'adam', loss = 'mean_squared_error')
model.fit(X_train, y_train, epochs = 100 , batch_size = 25)
It gives me this error in the last line of the code:
ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (1110, 5, 2) [Finished in 5.1s with exit code 1]
The code works if I change the length of y_train
to the 1, but I like to test longer y labels to train. What is the problem and how can I fix it?
EDIT:
I create X_train
and y_train
arrays with this code:
for i in range((len(training_set)%30) + 30 , len(training_set) - days ):
X_train.append(training_set_scaled[i-30:i-5])
y_train.append(training_set_scaled[i-5:i])
X_train, y_train = np.array(X_train), np.array(y_train)
This is the result of model.summary()
model.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_45 (LSTM) (None, 25, 25) 2800
_________________________________________________________________
dropout_45 (Dropout) (None, 25, 25) 0
_________________________________________________________________
lstm_46 (LSTM) (None, 25, 25) 5100
_________________________________________________________________
dropout_46 (Dropout) (None, 25, 25) 0
_________________________________________________________________
lstm_47 (LSTM) (None, 25, 25) 5100
_________________________________________________________________
dropout_47 (Dropout) (None, 25, 25) 0
_________________________________________________________________
lstm_48 (LSTM) (None, 25) 5100
_________________________________________________________________
dropout_48 (Dropout) (None, 25) 0
_________________________________________________________________
dense_12 (Dense) (None, 2) 52
=================================================================
Total params: 18,152
Trainable params: 18,152
Non-trainable params: 0
_________________________________________________________________
EDIT2:
I have tried to solve my problem with RepeatVector()
function in encoder-decoder
approach with the following code:
model = Sequential()
model.add(LSTM(units = 25, return_sequences = True, input_shape = (25, 2)))
model.add(Dropout(0.2))
model.add(LSTM(units = 25, return_sequences = True))
model.add(Dropout(0.2))
model.add(LSTM(units = 25)) #, return_sequences = True))
model.add(Dropout(0.2))
model.add(RepeatVector(5))
model.add(LSTM(units = 5 ,return_sequences = True))
model.add(Dropout(0.2))
model.add(LSTM(units = 5 ,return_sequences = True ))
model.add(Dropout(0.2))
model.add(Dense(units = 2))