Reputation: 117
I'm trying to predict the opening price for the next day. I'm able to get the formatting correct to feed in the input i.e. ('Open','High' columns per day for n time). However when I format into a 3D array my shape is as follows:
(1200, 60, 2)
The X_train has 1200 samples, with 60 timestep (previous 60 days of historical data) and 2 features (open and high)
However, My issue arises when its reaches the keras coding part when implementing layers. This is my code I am using:
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 2)))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.2))
regressor.add(Dense(units = 1))
The problem arises with the last line. I want the output to be only 1. So essentially I want the Open and High values of the input series to be used to work out the final singular output of just the Open price. However, by setting the Dense(units = 1)
, creates this error:
ValueError: Error when checking target: expected dense_1 to have shape (1,) but got array with shape (2,)
To fix this I have tried to change it to 2 Dense(units=2)
, however the final output produces 2 lines on the graph one for open and one for high which is not what I want. That's 2 outputs where I want 1. I'm not sure what to do with this scenario.
regressor.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (None, 60, 50) 10600
_________________________________________________________________
dropout_1 (Dropout) (None, 60, 50) 0
_________________________________________________________________
lstm_2 (LSTM) (None, 60, 50) 20200
_________________________________________________________________
dropout_2 (Dropout) (None, 60, 50) 0
_________________________________________________________________
lstm_3 (LSTM) (None, 60, 50) 20200
_________________________________________________________________
dropout_3 (Dropout) (None, 60, 50) 0
_________________________________________________________________
lstm_4 (LSTM) (None, 50) 20200
_________________________________________________________________
dropout_4 (Dropout) (None, 50) 0
_________________________________________________________________
dense_1 (Dense) (None, 2) 102
=================================================================
Total params: 71,302
Trainable params: 71,302
Non-trainable params: 0
Upvotes: 1
Views: 1578
Reputation: 14094
When the dense layer is having a shape error it might be your label tensor that is not matching. Check that y_train
has shape [1200, 1] so that you can use a dense of 1.
Upvotes: 1