0
votes

I have a sequence and I would like to do the simplest LSTM possible to predict the rest of the sequence. Meaning I want to start by using only the previous step to predict the next one and then add more steps. I want to use the predicted values as inputs also. So I believe what I want is to achieve many to many as mentioned in the answers there Understanding Keras LSTMs .

I have read other questions on the topic on stackoverflow but still didn't manage to make it work. In my code, I'm using the tutorial here https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/ and the function create_dataset to create two arrays with only a shift of one step.

Here is my code and the error I got.

"Here I'm scaling my data as advised"
scaler = MinMaxScaler(feature_range=(0, 1))
Rot = scaler.fit_transform(Rot)

"I'm creating the model using batch_size=1 but I'm not sure why this is necessary"

batch_size = 1
model = Sequential()
model.add(LSTM(1,batch_input_shape=(batch_size,1,1),stateful=True,return_sequences=True,input_shape=(None,1)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')

"I want to use only the previous value for now"
look_back = 1
"as len(Rot) = 41000 I'm taking 36000 for training"
train_size = 36000
X,Y = create_dataset(Rot[:train_size,:],look_back)
X = numpy.reshape(X,(X.shape[0], X.shape[1], 1))
Y = numpy.reshape(Y,(X.shape[0], X.shape[1], 1))

And now I train my network as advised by @Daniel Möller. 

epochs = 10

for epoch in range(epochs):
    model.reset_states()
    model.train_on_batch(X,Y)

" And I get this error "
" PartialTensorShape: Incompatible shapes during merge: [35998,1] vs. [1,1]
     [[{{node lstm_11/TensorArrayStack/TensorArrayGatherV3}}]]."

Do you know why I have such an error as it seems I did everything as in the topic mentioned above ?

1

1 Answers

1
votes

In this LSTM network batch_size=1, because it is stateful. When stateful=True, the train_set size and test_set size when divided by batch_size should have a modulo of zero.

batch_input_shape=(batch_size,1,1) is already defined, then why again,input_shape=(None,1)

When return_sequences=True, another LSTM is following the existing LSTM layer. But here it is not.