I am trying to forecast the future time series values of my data using the LSTM function. When I divided my data into training (70%) and testing(30%), LSTM predicts the values accurately, see attached figure, but when I want to forecast say 10000 data points based on my model, the result is very poor.

So How can I forecast the future time series data correctly using LSTM?

I tried my best to write a Matlab code shown below but don't know where I am making mistakes on forecasting future values:

HsAll = History3{:,6};
Hs15 = History3{67209:70128,6};
data = Hs15';

figure
plot(data)
xlabel("Month")
ylabel("Wave height")
title("Time series data over the months")

%Partition the training and test data. 
%Train on the first 90% of the sequence and test on the last 10%.

numTimeStepsTrain = floor(0.7.*numel(data));
dataTrain = data(1:numTimeStepsTrain+1);
dataTest = data(numTimeStepsTrain+1:end);

% Standardize Data

mu = mean(dataTrain);
sig = std(dataTrain);

dataTrainStandardized = (dataTrain - mu) / sig;

% Prepare Predictors and Responses

XTrain = dataTrainStandardized(1:end-1);
YTrain = dataTrainStandardized(2:end);

%Define LSTM Network Architecture

numFeatures = 1;
numResponses = 1;
numHiddenUnits = 10;

layers = [ ...
    sequenceInputLayer(numFeatures)
    lstmLayer(numHiddenUnits)
    fullyConnectedLayer(numResponses)
    regressionLayer];
options = trainingOptions('adam', ...
    'MaxEpochs',100, ...
    'GradientThreshold',5, ...
    'InitialLearnRate',0.005, ...
    'LearnRateSchedule','piecewise', ...
    'LearnRateDropPeriod',125, ...
    'LearnRateDropFactor',0.2, ...
    'Verbose',1, ...
    'Plots','training-progress');

% Train LSTM Network
net = trainNetwork(XTrain,YTrain,layers,options);

% Forecast Future Time Steps
dataTestStandardized = (dataTest - mu) / sig;
XTest = dataTestStandardized(1:end-1);

net = predictAndUpdateState(net,XTrain);

YTest = dataTest(2:end);

%Update Network State with Observed Values
net = resetState(net);
net = predictAndUpdateState(net,XTrain);
YPred = [];
numTimeStepsTest = numel(XTest);
% numTimeStepsTest = 10000;  if this I try then getting bad result.[![enter image description here][1]][1]
% X= (1:numTimeStepsTest);

for i = 1:numTimeStepsTest
    [net,YPred(:,i)] = predictAndUpdateState(net,XTest(:,i),'ExecutionEnvironment','cpu');
end

YPred = sig*YPred + mu;

rmse = sqrt(mean((YPred-YTest).^2))

figure
subplot(2,1,1);
plot(YTest);
hold on
plot(YPred,'.-');
hold off
legend(["Observed" "Predicted"]);
ylabel("Cases")
title("Forecast with Updates");

subplot(2,1,2)
stem(YPred - YTest)
xlabel("Month")
ylabel("Error")
title("RMSE = " + rmse);

figure
plot(dataTrain(1:end-1))
hold on
idx = numTimeStepsTrain:(numTimeStepsTrain+numTimeStepsTest);
plot(idx,[data(numTimeStepsTrain) YPred],'r')
hold off
xlabel("Month")
ylabel("Cases")
title("Forecast")
legend(["Observed" "Forecast"])


% When I am trying to forecast say next 10000 values then it gives me constant values.

The result of training and test values:

Fig 1 is the result of training and test values

0 Answers