1
$\begingroup$

I am trying to train a model on a big data sequence like this [0.2 0.1 0.1 ..... 0.4 0.8] . I create X vectors with length 60 for inputs and Y scaler numbers as labels(It means the LSTM reads first 60 numbers as input(one row in X_train) and the 61'th number as the output label(rows in y_train)).

model = Sequential()
model.add(LSTM(units = 50, return_sequences = True , input_shape = (X_train.shape[1], 1)))
model.add(Dense(units = 1))
model.compile(optimizer = 'adam', loss = 'mean_squared_error')
model.fit(X_train, y_train, epochs = 100, batch_size = 32)

And:

X_train.shape = (1000,60,1) , y_train.shape = (1000,)

It's OK till here, but the problem is in the prediction part. What I am trying to do is create a (60*1) input vector and use it to predict the next number in my sequence. Then adding the new predicted number to my sequence to predict the next number(second predicted number) and so on. For that goal, I created a new model, retrieved the weights from previous model, then fed new_model only with one (60*1) vector to predict the next number. then added the predicted number to the sequence and shift input vectors one number to the right(to use new predicted number for the next number prediction).

new_model = Sequential()
new_model.add(LSTM(units = 50, return_sequences = True , input_shape = (1 , 60 , 1 )))
new_model.add(Dense(1))
old_weights = model.get_weights()
new_model.set_weights(old_weights)
new_model.compile(optimizer = 'adam', loss = 'mean_squared_error')

inputs = []
for i in range(10):
    inputs = dataset_total[len(dataset_total) - 60:].values
    inputs = np.reshape(inputs, (1 , 60, 1))
    predicted = new_model.predict(inputs)
    inputs.append(predicted)

But what do I get is such errors:

ValueError: Input 0 is incompatible with layer lstm_61: expected ndim=3, found ndim=4

I don't know how to solve this problem!

There is also a similar but without related answer here(for clarification): How to Predict the future values of time horizon with Keras?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.