Is “adding the predictions to the real data for new training and prediction” a good idea for LSTM?
$begingroup$
Considering we have trained our model with a lot of data for "many-to-one" prediction. Then we like to forecast the future data of next 10 days. So we use last 60 of existent data and predict the single next day. From here there are 2 approaches:
We can put our
model.predict()
function in afor
loop for 10 times and do predictions like this(adding our predictions to end of our real data).We can put all of our model(consisting training part, not just predict part), in a
for
loop and this means we train our model 10 times whenever we do a new prediction and adding it to our real data.
EDIT:
Thinking you have X_train = (100,60,1)
array that means 100 examples, 60 time-steps(hidden units) and 1 feature for each example. Also you have y_train
array of size (100,1,1)
that means 100 labels with time-steps = 1 and 1 feature. Then you train your network to read 60 of inputs and predict the next single output. Also you create a X_test
array like this: X_test = X_train[len(X_train - 60):]
that means you use last 60 numbers of your series to predict the next number. So you use the new_number = model.predict(X_test)
for that and you predict the time-step 61 that is not a real number. It's your prediction. Then you want to continue your predictions. So what do you do is adding the 61'th predicted number to the last of your X_test = np.append(X_test, new_number)
and do new number = model.predict(X_test)
again. But the difference is that the last number in your new X_test
is your previous prediction. And you keep this way for 10 times to predict 10 next numbers. (This was the first approach).
The other approach(2) has a difference. After doingnew_number = model.predict(X_test)
for the first time, you add the predicted number to x_train
instead of X_test
, like this X_train = np.append(X_train, new_number)
and train your model again model.fit(X_train , y_train)
with the new predicted number. Then you use new number = model.predict(X_test)
and again adding predicted number into the X_train
, then train your model again(this time, with 2 new predicted numbers that you have added to the end of your X_train
) and so on for 10 times!
lstm training prediction
$endgroup$
add a comment |
$begingroup$
Considering we have trained our model with a lot of data for "many-to-one" prediction. Then we like to forecast the future data of next 10 days. So we use last 60 of existent data and predict the single next day. From here there are 2 approaches:
We can put our
model.predict()
function in afor
loop for 10 times and do predictions like this(adding our predictions to end of our real data).We can put all of our model(consisting training part, not just predict part), in a
for
loop and this means we train our model 10 times whenever we do a new prediction and adding it to our real data.
EDIT:
Thinking you have X_train = (100,60,1)
array that means 100 examples, 60 time-steps(hidden units) and 1 feature for each example. Also you have y_train
array of size (100,1,1)
that means 100 labels with time-steps = 1 and 1 feature. Then you train your network to read 60 of inputs and predict the next single output. Also you create a X_test
array like this: X_test = X_train[len(X_train - 60):]
that means you use last 60 numbers of your series to predict the next number. So you use the new_number = model.predict(X_test)
for that and you predict the time-step 61 that is not a real number. It's your prediction. Then you want to continue your predictions. So what do you do is adding the 61'th predicted number to the last of your X_test = np.append(X_test, new_number)
and do new number = model.predict(X_test)
again. But the difference is that the last number in your new X_test
is your previous prediction. And you keep this way for 10 times to predict 10 next numbers. (This was the first approach).
The other approach(2) has a difference. After doingnew_number = model.predict(X_test)
for the first time, you add the predicted number to x_train
instead of X_test
, like this X_train = np.append(X_train, new_number)
and train your model again model.fit(X_train , y_train)
with the new predicted number. Then you use new number = model.predict(X_test)
and again adding predicted number into the X_train
, then train your model again(this time, with 2 new predicted numbers that you have added to the end of your X_train
) and so on for 10 times!
lstm training prediction
$endgroup$
$begingroup$
Using TimeDistributed() wrapper of Keras, you will be able to predict upcoming 10 at once. Have a look at machinelearningmastery.com/… and quora.com/What-is-time-distributed-dense-layer-in-Keras
$endgroup$
– Ugur MULUK
yesterday
$begingroup$
@UgurMULUK: I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
$begingroup$
Is many-to-one prediction is mandatory for you? The reason I ask is that LSTM passes the predictions of each recurrent neuron to the next one. If you can train your network as many-to-10 by using the labels of next 10 with previous 60, your LSTM model will automatically predict the next 10 prediction by predicting each of the days from forward by using the predictions of the previous future prediction. Have a look at the first image here: karpathy.github.io/2015/05/21/rnn-effectiveness The 4th RNN structure in that image is what I think as would work for your case.
$endgroup$
– Ugur MULUK
1 hour ago
$begingroup$
@UgurMULUK: You mean LSTM automatically does what I explained in approach 2?
$endgroup$
– user145959
1 hour ago
add a comment |
$begingroup$
Considering we have trained our model with a lot of data for "many-to-one" prediction. Then we like to forecast the future data of next 10 days. So we use last 60 of existent data and predict the single next day. From here there are 2 approaches:
We can put our
model.predict()
function in afor
loop for 10 times and do predictions like this(adding our predictions to end of our real data).We can put all of our model(consisting training part, not just predict part), in a
for
loop and this means we train our model 10 times whenever we do a new prediction and adding it to our real data.
EDIT:
Thinking you have X_train = (100,60,1)
array that means 100 examples, 60 time-steps(hidden units) and 1 feature for each example. Also you have y_train
array of size (100,1,1)
that means 100 labels with time-steps = 1 and 1 feature. Then you train your network to read 60 of inputs and predict the next single output. Also you create a X_test
array like this: X_test = X_train[len(X_train - 60):]
that means you use last 60 numbers of your series to predict the next number. So you use the new_number = model.predict(X_test)
for that and you predict the time-step 61 that is not a real number. It's your prediction. Then you want to continue your predictions. So what do you do is adding the 61'th predicted number to the last of your X_test = np.append(X_test, new_number)
and do new number = model.predict(X_test)
again. But the difference is that the last number in your new X_test
is your previous prediction. And you keep this way for 10 times to predict 10 next numbers. (This was the first approach).
The other approach(2) has a difference. After doingnew_number = model.predict(X_test)
for the first time, you add the predicted number to x_train
instead of X_test
, like this X_train = np.append(X_train, new_number)
and train your model again model.fit(X_train , y_train)
with the new predicted number. Then you use new number = model.predict(X_test)
and again adding predicted number into the X_train
, then train your model again(this time, with 2 new predicted numbers that you have added to the end of your X_train
) and so on for 10 times!
lstm training prediction
$endgroup$
Considering we have trained our model with a lot of data for "many-to-one" prediction. Then we like to forecast the future data of next 10 days. So we use last 60 of existent data and predict the single next day. From here there are 2 approaches:
We can put our
model.predict()
function in afor
loop for 10 times and do predictions like this(adding our predictions to end of our real data).We can put all of our model(consisting training part, not just predict part), in a
for
loop and this means we train our model 10 times whenever we do a new prediction and adding it to our real data.
EDIT:
Thinking you have X_train = (100,60,1)
array that means 100 examples, 60 time-steps(hidden units) and 1 feature for each example. Also you have y_train
array of size (100,1,1)
that means 100 labels with time-steps = 1 and 1 feature. Then you train your network to read 60 of inputs and predict the next single output. Also you create a X_test
array like this: X_test = X_train[len(X_train - 60):]
that means you use last 60 numbers of your series to predict the next number. So you use the new_number = model.predict(X_test)
for that and you predict the time-step 61 that is not a real number. It's your prediction. Then you want to continue your predictions. So what do you do is adding the 61'th predicted number to the last of your X_test = np.append(X_test, new_number)
and do new number = model.predict(X_test)
again. But the difference is that the last number in your new X_test
is your previous prediction. And you keep this way for 10 times to predict 10 next numbers. (This was the first approach).
The other approach(2) has a difference. After doingnew_number = model.predict(X_test)
for the first time, you add the predicted number to x_train
instead of X_test
, like this X_train = np.append(X_train, new_number)
and train your model again model.fit(X_train , y_train)
with the new predicted number. Then you use new number = model.predict(X_test)
and again adding predicted number into the X_train
, then train your model again(this time, with 2 new predicted numbers that you have added to the end of your X_train
) and so on for 10 times!
lstm training prediction
lstm training prediction
edited yesterday
user145959
asked yesterday
user145959user145959
535
535
$begingroup$
Using TimeDistributed() wrapper of Keras, you will be able to predict upcoming 10 at once. Have a look at machinelearningmastery.com/… and quora.com/What-is-time-distributed-dense-layer-in-Keras
$endgroup$
– Ugur MULUK
yesterday
$begingroup$
@UgurMULUK: I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
$begingroup$
Is many-to-one prediction is mandatory for you? The reason I ask is that LSTM passes the predictions of each recurrent neuron to the next one. If you can train your network as many-to-10 by using the labels of next 10 with previous 60, your LSTM model will automatically predict the next 10 prediction by predicting each of the days from forward by using the predictions of the previous future prediction. Have a look at the first image here: karpathy.github.io/2015/05/21/rnn-effectiveness The 4th RNN structure in that image is what I think as would work for your case.
$endgroup$
– Ugur MULUK
1 hour ago
$begingroup$
@UgurMULUK: You mean LSTM automatically does what I explained in approach 2?
$endgroup$
– user145959
1 hour ago
add a comment |
$begingroup$
Using TimeDistributed() wrapper of Keras, you will be able to predict upcoming 10 at once. Have a look at machinelearningmastery.com/… and quora.com/What-is-time-distributed-dense-layer-in-Keras
$endgroup$
– Ugur MULUK
yesterday
$begingroup$
@UgurMULUK: I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
$begingroup$
Is many-to-one prediction is mandatory for you? The reason I ask is that LSTM passes the predictions of each recurrent neuron to the next one. If you can train your network as many-to-10 by using the labels of next 10 with previous 60, your LSTM model will automatically predict the next 10 prediction by predicting each of the days from forward by using the predictions of the previous future prediction. Have a look at the first image here: karpathy.github.io/2015/05/21/rnn-effectiveness The 4th RNN structure in that image is what I think as would work for your case.
$endgroup$
– Ugur MULUK
1 hour ago
$begingroup$
@UgurMULUK: You mean LSTM automatically does what I explained in approach 2?
$endgroup$
– user145959
1 hour ago
$begingroup$
Using TimeDistributed() wrapper of Keras, you will be able to predict upcoming 10 at once. Have a look at machinelearningmastery.com/… and quora.com/What-is-time-distributed-dense-layer-in-Keras
$endgroup$
– Ugur MULUK
yesterday
$begingroup$
Using TimeDistributed() wrapper of Keras, you will be able to predict upcoming 10 at once. Have a look at machinelearningmastery.com/… and quora.com/What-is-time-distributed-dense-layer-in-Keras
$endgroup$
– Ugur MULUK
yesterday
$begingroup$
@UgurMULUK: I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
$begingroup$
@UgurMULUK: I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
$begingroup$
Is many-to-one prediction is mandatory for you? The reason I ask is that LSTM passes the predictions of each recurrent neuron to the next one. If you can train your network as many-to-10 by using the labels of next 10 with previous 60, your LSTM model will automatically predict the next 10 prediction by predicting each of the days from forward by using the predictions of the previous future prediction. Have a look at the first image here: karpathy.github.io/2015/05/21/rnn-effectiveness The 4th RNN structure in that image is what I think as would work for your case.
$endgroup$
– Ugur MULUK
1 hour ago
$begingroup$
Is many-to-one prediction is mandatory for you? The reason I ask is that LSTM passes the predictions of each recurrent neuron to the next one. If you can train your network as many-to-10 by using the labels of next 10 with previous 60, your LSTM model will automatically predict the next 10 prediction by predicting each of the days from forward by using the predictions of the previous future prediction. Have a look at the first image here: karpathy.github.io/2015/05/21/rnn-effectiveness The 4th RNN structure in that image is what I think as would work for your case.
$endgroup$
– Ugur MULUK
1 hour ago
$begingroup$
@UgurMULUK: You mean LSTM automatically does what I explained in approach 2?
$endgroup$
– user145959
1 hour ago
$begingroup$
@UgurMULUK: You mean LSTM automatically does what I explained in approach 2?
$endgroup$
– user145959
1 hour ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Are you saying you would like to predict 10 days ahead in this instance?
If this is the case, the LSTM model is able to do this and iterating in the way you are suggesting is unnecessary and could give you unreliable results.
For instance, consider a dataset whereby we are attempting to predict one-step ahead:
# Training and Test data partition
train_size = int(len(dataset) * 0.8)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
previous = 1
X_train, Y_train = create_dataset(train, previous)
X_test, Y_test = create_dataset(test, previous)
# reshape input to be [samples, time steps, features]
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
# Generate LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, previous)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, Y_train, epochs=100, batch_size=1, verbose=2)
# Generate predictions
trainpred = model.predict(X_train)
testpred = model.predict(X_test)
# Convert predictions back to normal values
trainpred = scaler.inverse_transform(trainpred)
Y_train = scaler.inverse_transform([Y_train])
testpred = scaler.inverse_transform(testpred)
Y_test = scaler.inverse_transform([Y_test])
# calculate RMSE
trainScore = math.sqrt(mean_squared_error(Y_train[0], trainpred[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(Y_test[0], testpred[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
In this batch of code, you can see that we have set the previous parameter equal to 1, meaning that the time step being considered by the model is t-1.
In this particular instance, here are the training and test predictions compared to the actual series:
Now, the same model is run, but this time the previous parameter is set to 10. In other words, the previous 10 days are being considered as one time step, and the model is forecasting for time t+10 in this instance. Here is another sample prediction comparing the test set with the actual. A fuller example of this is provided here:
In this regard, my advice would be to define the time series you wish to forecast and then work off that basis. Using iterations only complicates the situation, and could even cause issues with prediction.
$endgroup$
$begingroup$
I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f46270%2fis-adding-the-predictions-to-the-real-data-for-new-training-and-prediction-a-g%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Are you saying you would like to predict 10 days ahead in this instance?
If this is the case, the LSTM model is able to do this and iterating in the way you are suggesting is unnecessary and could give you unreliable results.
For instance, consider a dataset whereby we are attempting to predict one-step ahead:
# Training and Test data partition
train_size = int(len(dataset) * 0.8)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
previous = 1
X_train, Y_train = create_dataset(train, previous)
X_test, Y_test = create_dataset(test, previous)
# reshape input to be [samples, time steps, features]
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
# Generate LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, previous)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, Y_train, epochs=100, batch_size=1, verbose=2)
# Generate predictions
trainpred = model.predict(X_train)
testpred = model.predict(X_test)
# Convert predictions back to normal values
trainpred = scaler.inverse_transform(trainpred)
Y_train = scaler.inverse_transform([Y_train])
testpred = scaler.inverse_transform(testpred)
Y_test = scaler.inverse_transform([Y_test])
# calculate RMSE
trainScore = math.sqrt(mean_squared_error(Y_train[0], trainpred[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(Y_test[0], testpred[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
In this batch of code, you can see that we have set the previous parameter equal to 1, meaning that the time step being considered by the model is t-1.
In this particular instance, here are the training and test predictions compared to the actual series:
Now, the same model is run, but this time the previous parameter is set to 10. In other words, the previous 10 days are being considered as one time step, and the model is forecasting for time t+10 in this instance. Here is another sample prediction comparing the test set with the actual. A fuller example of this is provided here:
In this regard, my advice would be to define the time series you wish to forecast and then work off that basis. Using iterations only complicates the situation, and could even cause issues with prediction.
$endgroup$
$begingroup$
I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
add a comment |
$begingroup$
Are you saying you would like to predict 10 days ahead in this instance?
If this is the case, the LSTM model is able to do this and iterating in the way you are suggesting is unnecessary and could give you unreliable results.
For instance, consider a dataset whereby we are attempting to predict one-step ahead:
# Training and Test data partition
train_size = int(len(dataset) * 0.8)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
previous = 1
X_train, Y_train = create_dataset(train, previous)
X_test, Y_test = create_dataset(test, previous)
# reshape input to be [samples, time steps, features]
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
# Generate LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, previous)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, Y_train, epochs=100, batch_size=1, verbose=2)
# Generate predictions
trainpred = model.predict(X_train)
testpred = model.predict(X_test)
# Convert predictions back to normal values
trainpred = scaler.inverse_transform(trainpred)
Y_train = scaler.inverse_transform([Y_train])
testpred = scaler.inverse_transform(testpred)
Y_test = scaler.inverse_transform([Y_test])
# calculate RMSE
trainScore = math.sqrt(mean_squared_error(Y_train[0], trainpred[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(Y_test[0], testpred[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
In this batch of code, you can see that we have set the previous parameter equal to 1, meaning that the time step being considered by the model is t-1.
In this particular instance, here are the training and test predictions compared to the actual series:
Now, the same model is run, but this time the previous parameter is set to 10. In other words, the previous 10 days are being considered as one time step, and the model is forecasting for time t+10 in this instance. Here is another sample prediction comparing the test set with the actual. A fuller example of this is provided here:
In this regard, my advice would be to define the time series you wish to forecast and then work off that basis. Using iterations only complicates the situation, and could even cause issues with prediction.
$endgroup$
$begingroup$
I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
add a comment |
$begingroup$
Are you saying you would like to predict 10 days ahead in this instance?
If this is the case, the LSTM model is able to do this and iterating in the way you are suggesting is unnecessary and could give you unreliable results.
For instance, consider a dataset whereby we are attempting to predict one-step ahead:
# Training and Test data partition
train_size = int(len(dataset) * 0.8)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
previous = 1
X_train, Y_train = create_dataset(train, previous)
X_test, Y_test = create_dataset(test, previous)
# reshape input to be [samples, time steps, features]
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
# Generate LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, previous)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, Y_train, epochs=100, batch_size=1, verbose=2)
# Generate predictions
trainpred = model.predict(X_train)
testpred = model.predict(X_test)
# Convert predictions back to normal values
trainpred = scaler.inverse_transform(trainpred)
Y_train = scaler.inverse_transform([Y_train])
testpred = scaler.inverse_transform(testpred)
Y_test = scaler.inverse_transform([Y_test])
# calculate RMSE
trainScore = math.sqrt(mean_squared_error(Y_train[0], trainpred[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(Y_test[0], testpred[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
In this batch of code, you can see that we have set the previous parameter equal to 1, meaning that the time step being considered by the model is t-1.
In this particular instance, here are the training and test predictions compared to the actual series:
Now, the same model is run, but this time the previous parameter is set to 10. In other words, the previous 10 days are being considered as one time step, and the model is forecasting for time t+10 in this instance. Here is another sample prediction comparing the test set with the actual. A fuller example of this is provided here:
In this regard, my advice would be to define the time series you wish to forecast and then work off that basis. Using iterations only complicates the situation, and could even cause issues with prediction.
$endgroup$
Are you saying you would like to predict 10 days ahead in this instance?
If this is the case, the LSTM model is able to do this and iterating in the way you are suggesting is unnecessary and could give you unreliable results.
For instance, consider a dataset whereby we are attempting to predict one-step ahead:
# Training and Test data partition
train_size = int(len(dataset) * 0.8)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
previous = 1
X_train, Y_train = create_dataset(train, previous)
X_test, Y_test = create_dataset(test, previous)
# reshape input to be [samples, time steps, features]
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
# Generate LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, previous)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, Y_train, epochs=100, batch_size=1, verbose=2)
# Generate predictions
trainpred = model.predict(X_train)
testpred = model.predict(X_test)
# Convert predictions back to normal values
trainpred = scaler.inverse_transform(trainpred)
Y_train = scaler.inverse_transform([Y_train])
testpred = scaler.inverse_transform(testpred)
Y_test = scaler.inverse_transform([Y_test])
# calculate RMSE
trainScore = math.sqrt(mean_squared_error(Y_train[0], trainpred[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(Y_test[0], testpred[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
In this batch of code, you can see that we have set the previous parameter equal to 1, meaning that the time step being considered by the model is t-1.
In this particular instance, here are the training and test predictions compared to the actual series:
Now, the same model is run, but this time the previous parameter is set to 10. In other words, the previous 10 days are being considered as one time step, and the model is forecasting for time t+10 in this instance. Here is another sample prediction comparing the test set with the actual. A fuller example of this is provided here:
In this regard, my advice would be to define the time series you wish to forecast and then work off that basis. Using iterations only complicates the situation, and could even cause issues with prediction.
answered yesterday
Michael GroganMichael Grogan
1964
1964
$begingroup$
I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
add a comment |
$begingroup$
I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
$begingroup$
I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
$begingroup$
I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
add a comment |
Thanks for contributing an answer to Data Science Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f46270%2fis-adding-the-predictions-to-the-real-data-for-new-training-and-prediction-a-g%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Using TimeDistributed() wrapper of Keras, you will be able to predict upcoming 10 at once. Have a look at machinelearningmastery.com/… and quora.com/What-is-time-distributed-dense-layer-in-Keras
$endgroup$
– Ugur MULUK
yesterday
$begingroup$
@UgurMULUK: I have edited my question and tried to explain more in EDIT section. Please read it. Maybe it would help to clear what I ask.
$endgroup$
– user145959
yesterday
$begingroup$
Is many-to-one prediction is mandatory for you? The reason I ask is that LSTM passes the predictions of each recurrent neuron to the next one. If you can train your network as many-to-10 by using the labels of next 10 with previous 60, your LSTM model will automatically predict the next 10 prediction by predicting each of the days from forward by using the predictions of the previous future prediction. Have a look at the first image here: karpathy.github.io/2015/05/21/rnn-effectiveness The 4th RNN structure in that image is what I think as would work for your case.
$endgroup$
– Ugur MULUK
1 hour ago
$begingroup$
@UgurMULUK: You mean LSTM automatically does what I explained in approach 2?
$endgroup$
– user145959
1 hour ago