How to predict future values with LSTM (RNN)?

4 views (last 30 days)
Ful0
Ful0 on 26 Mar 2021
Edited: Ful0 on 1 Apr 2021
Hello everyone,
Can anyone explain me how to predict future values with LSTM?
I would like to compare the prediction of a NARXNET and a LSTM net, but I can't understand from the matlab examples how to train an LSTM network with one input (11000 value of water demand) and one different output(11000 values of energy consumption). Once I train the net, I should be able to find 500 new values of energy consumption if I give to the net 500 new value of water demand.
Can anyone help me with this issue, please?
Thank you so much
Ful
  2 Comments
Gaurav Garg
Gaurav Garg on 30 Mar 2021
Hi,
Can you clarify a few points -
  1. What is the input size?
  2. What is the network you have made?
  3. What is the exact issue being faced by you? Is it a syntax error?
Ful0
Ful0 on 31 Mar 2021
Edited: Ful0 on 31 Mar 2021
Hello Sir,
Thank you for your answer.
I just change my network in order to simulate the example presents in Matlab Documentation, but with my data. It is not anymore what I ask in my previous question, but I try to make it simpler.
I want to forecast the hourly water demand in a water system. My data are over 15 mouths.
Below my code:
start_toolkit;
d=epanet('Prova reti neurali_15 mesi_ONOFF.inp');
% Load sequence data
A=d.getComputedTimeSeries;
% Portata utenza
portata_utenze = A.Demand(:,1);
data = portata_utenze';
figure (1)
plot(data)
xlabel("h")
ylabel("Portata [l/s]")
title("Portata richiesta dalla utenza")
numTimeStepsTrain = floor(0.9*numel(data));
dataTrain = data(1:numTimeStepsTrain+1);
dataTest = data(numTimeStepsTrain+1:end);
mu = mean(dataTrain);
sig = std(dataTrain);
dataTrainStandardized = (dataTrain - mu) / sig;
XTrain = dataTrainStandardized(1:end-1);
YTrain = dataTrainStandardized(2:end);
numFeatures = 1;
numResponses = 1;
numHiddenUnits = 200;
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs',100, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');
net = trainNetwork(XTrain,YTrain,layers,options);
dataTestStandardized = (dataTest - mu) / sig;
XTest = dataTestStandardized(1:end-1);
net = predictAndUpdateState(net,XTrain);
[net,YPred] = predictAndUpdateState(net,YTrain(end));
numTimeStepsTest = numel(XTest);
for i = 2:numTimeStepsTest
[net,YPred(:,i)] = predictAndUpdateState(net,YPred(:,i-1),'ExecutionEnvironment','cpu');
end
YPred = sig*YPred + mu;
YTest = dataTest(2:end);
rmse = sqrt(mean((YPred-YTest).^2))
figure (2)
plot(dataTrain(1:end-1))
hold on
idx = numTimeStepsTrain:(numTimeStepsTrain+numTimeStepsTest);
plot(idx,[data(numTimeStepsTrain) YPred],'.-')
hold off
xlabel("h")
ylabel("Portata [l/s]")
title("Previsione")
legend(["Reali" "Previsti"])
figure (3)
subplot(2,1,1)
plot(YTest)
hold on
plot(YPred,'.-')
hold off
legend(["Reali" "Previsti"])
ylabel("Portata [l/s]")
title("Previsione")
subplot(2,1,2)
stem(YPred - YTest)
xlabel("h")
ylabel("Error")
title("RMSE = " + rmse)
I can't understand why the figures aren't similar to the ones in the example.
Below my figure (3)

Sign in to comment.

Answers (1)

Gaurav Garg
Gaurav Garg on 1 Apr 2021
Hi,
Since the input dataset and models used by you and examples in MATLAB are different, you are expected to see different results.
Moreover, RMSE results plotted by you are not the way they should be. Kindly look at the following link for more info on RMSE.
  1 Comment
Ful0
Ful0 on 1 Apr 2021
Edited: Ful0 on 1 Apr 2021
I don't understand what are the errors in my code.
Why it can't predict 3 weeks of water demand if the net is trained using 60 weeks of water demand past values?
Thank's for the link suggested, but it doesn't help me to find the error in my previous code.

Sign in to comment.

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!