Negative values in NARX NN for multi-step ahead prediction

조회 수: 4 (최근 30일)
Ali
Ali 2019년 3월 4일
편집: Ali 2019년 3월 4일
i am getting negative values in performance (MAPE,MSE etc) as shown in attached figure in multi-step ahead prediction.the trained network performs well on training data and giving awkward results on Test data.My script is:
%% 1. Importing data
clc
clear all
data3=xlsread ('HomeA-meter_2016.xlsx','C:C');
%%
data=data3(1:8000)';
%%
% Partition the training and test data. Train on the first 75% of the sequence
% and test on the last 25%
numTimeStepsTrain = floor(0.75*numel(data));
dataTrain = data(1:numTimeStepsTrain+1);
dataTest = data(numTimeStepsTrain+1:end);
%% Standardize Data
% For a better fit and to prevent the training from diverging, standardize the
% training data to have zero mean and unit variance.
mu = mean(dataTrain);
sig = std(dataTrain);
dataTrainStandardized = (dataTrain - mu) / sig;
dataTestStandardized = (dataTest - mu) / sig;
% N=300;
XTrain = dataTrainStandardized(1:end-1);
YTrain = dataTrainStandardized(2:end);
dataTestStandardized = (dataTest - mu) / sig;
XTest = dataTestStandardized(1:end-1);
YTest = dataTest(2:end);
% Training data
X = tonndata(XTrain,true,false);
T = tonndata(YTrain ,true,false);
% Test data
X2 = tonndata(XTest,true,false);
T2 = tonndata(YTest ,true,false);
% data=normalize(data2);
%
%%
%% 2. Data preparation
inputSeries = X;
targetSeries = T;
% 2nd group: this is the new data used for testing trained network.
inputSeriesVal = X2;
targetSeriesVal = T2 ; % This is generally not available
%% 3. Network Architecture
delay = 2;
neuronsHiddenLayer = 20;
% Network Creation
net = narxnet(1:delay,1:delay,neuronsHiddenLayer);
%% 4. Training the network
[Xs,Xi,Ai,Ts] = preparets(net,inputSeries,{},targetSeries);
net.divideFcn = 'dividetrain'; % Divide data in blocks
net.divideMode = 'time'; % Divide up every value
% % net.divideParam.trainRatio = 75/100;
% % net.divideParam.valRatio = 25/100;
% % net.divideParam.testRatio = 00/100;
net.trainParam.epochs=1000;
net = train(net,Xs,Ts,Xi,Ai);
% view(net)
Y = net(Xs,Xi,Ai);
% Performance for the series-parallel implementation, only
% one-step-ahead prediction
perf = perform(net,Ts,Y);
%% Prediction on Training data
figure(1)
error=(cell2mat(Ts)-cell2mat(Y));
rmse = sqrt((mean(error).^2))
MAPE_Training=mean(abs(cell2mat(Ts)-cell2mat(Y))./cell2mat(Ts))*100
%% 5. Multi-step ahead prediction
[Xs1,Xio,Aio] = preparets(net,inputSeries(1:end-delay),{},targetSeries(1:end-delay));
[Y1,Xfo,Afo] = net(Xs1,Xio,Aio);
[netc,Xic,Aic] = closeloop(net,Xfo,Afo);
[yPred,Xfc,Afc] = netc(inputSeriesVal,Xic,Aic);
multiStepPerformance = perform(net,yPred,targetSeriesVal);
%% Prediction on Test data
YTest=cell2mat(targetSeriesVal);
Y2Pred=cell2mat(yPred);
% Unstandardize the predictions using the parameters calculated earlier.
YPred = sig*Y2Pred + mu;
error2=(YTest-YPred);
rmse2 = sqrt((mean(error2).^2))
MAPE_Test=mean(abs(YTest-YPred)./YTest)*100
figure(2)
plot(cell2mat(inputSeriesVal),'linewidth',1);
hold on
plot(cell2mat(yPred),'linewidth',1)
xlabel("Date and Time (Number of samples)")
ylabel("EDemand ")
title("Prediction on Test data (XTest,YTest)")
legend(["Real data" "NARX prediction (Testing) "])
xlim([0 100])
hold on
%%
YTest=cell2mat(targetSeriesVal);
%
Y2Pred=cell2mat(yPred);
% Unstandardize the predictions using the parameters calculated earlier.
YPred = sig*Y2Pred + mu;
%
% rmse = sqrt(mean((YTest-YPred).^2))
numTimeStepsTest = numel(XTest);
%% Future prediction
% Plot the training time series with the forecasted values.
figure (4)
plot(dataTrain(1:end-1))
hold on
idx = numTimeStepsTrain:(numTimeStepsTrain+numTimeStepsTest);
plot(idx,[data(numTimeStepsTrain) YPred],'r' , 'linewidth' , 2)
% plot(idx,[data(numTimeStepsTest) YPred],'r' , 'linewidth' , 2) % Ali
% xlim([375000 385000]) Home C
% xlim([175000 195000]) %Home B
% xlim([183000 186000]) %Home B
xlim([5900 6100]) %Home B
hold off
xlabel("Date and Time (Number of samples)")
ylabel("Demand ")
title("Forecast future steps")
legend(["Real data" "Future prediction "])
%
rmse_test = sqrt(mean((YTest-YPred).^2))
MAPE_Test=mean(abs(YTest-YPred)./YTest)*100
%%

답변 (0개)

카테고리

Help CenterFile Exchange에서 Sequence and Numeric Feature Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by