NARX net input and feedback delay and optimization of one step ahead prediction
2 次查看(过去 30 天)
显示 更早的评论
I am trying to understand the NARXnet to predict one step ahead value, and then compare the predicted result to the actual result, and make a decision that the actual result is normal or abnormal. I was trying to read the lots of question answer related to narxnet from Greg . I am trying to make decision about input delay and feedback delay, where greg shows a process from nncorr(I don’t have xcorr) . For the simple narxdatasets it works pretty well, but for exchanger_datasets I am getting the siglag so big. I guess there is something wrong in my code. Here is the code which I just collected by reading lots of question answer sessions from greg
close all,clear all, clc, plt=0; tic plt=0 [X,T]=exchanger_dataset; x=cell2mat(X); t = cell2mat(T); %% N=length(x); [trainInd,valInd,testInd] = divideblock(N,0.7,0.15,0.15); %%
ttr=t(trainInd);
xtr=x(:,trainInd);
Ntrn=length(ttr);
ztrn=zscore(ttr,1);
[ O N ] = size(t);
[I N]=size(x);
%%Autocorrelation and significant threshold
rng(0)
for k = 1:100
n = randn(1,Ntrn);
autocorrn = nncorr(ztrn,ztrn,Ntrn-1,'biased');
sortabsautocorrn = sort(abs(autocorrn));
M = floor(0.95*(2*Ntrn-1));
thresh95(k) = sortabsautocorrn(M);
end
sigthresh95 = mean(thresh95)
%%figure Autocorrelation and significant lag
lags = 0:Ntrn-1;
siglags = -1+find(abs(autocorrn(Ntrn:end))>sigthresh95);
plt = plt+1, figure(plt) % Fig 1
hold on
plot( zeros(1,N), 'k--', 'LineWidth', 2 )
plot(sigthresh95*ones(1,Ntrn-1), 'r--', 'LineWidth', 2 )
plot(-sigthresh95*ones(1,Ntrn-1), 'r--', 'LineWidth', 2 )
plot( lags, autocorrn(Ntrn:end), 'LineWidth', 2 )
plot( siglags , autocorrn(Ntrn+ siglags ), 'o', 'LineWidth', 2 )
%%CrossCorelation and significant threshold
rng(0)
for k = 1:100
n = zscore(randn(1,Ntrn),1);
crosscorrn = nncorr(xtr,ztrn,Ntrn-1,'biased');
crosscorrzn =[crosscorrn(1:Ntrn-1) crosscorrn(Ntrn:end)];
sortabscrosscorrn = sort(abs(crosscorrzn));
M1 = floor(0.95*(2*Ntrn-1)) ;
thresh951(k) = sortabscrosscorrn(M1);
end
sigthresh951 = mean(thresh951);
%%plotting cross correlation
lagscr = 0:Ntrn-1;
siglagscroscros = -1+find(abs(crosscorrzn(Ntrn:end))>sigthresh951);
plt = plt+1, figure(plt) % Fig 2
hold on
plot( zeros(1,N), 'k--', 'LineWidth', 2 )
plot(sigthresh951*ones(1,Ntrn-1), 'r--', 'LineWidth', 2 )
plot(-sigthresh951*ones(1,Ntrn-1), 'r--', 'LineWidth', 2 )
plot( lagscr, crosscorrzn(Ntrn:end), 'LineWidth', 2 )
plot( siglagscroscros, crosscorrzn(Ntrn+siglagscroscros ), 'o', 'LineWidth', 2 );
%%
Ntrneq = prod(size(ttr));
%%
%%
%%%%%%%%%%%I can’t understand hig to select from such a value, that’s why just select default value
FD = 1:2;
ID=1:2;
NFD = length(FD);
NID=length(ID);
LDB = max([ID,FD]);
Hub = -1+ceil( (Ntrneq-O) / (NID*I+NFD*O+O+1)) ;
Hmax = floor(Hub)/10;
Hmin = 0;
dH = 1;
Ntrials = 10;
j=0
rng(4151941)
for h = Hmin:dH:Hmax
j = j+1;
if h == 0
net = narxnet(ID,FD, [] );
Nw = ( NID*I+NFD*O + 1)*O;
else
net = narxnet(ID, FD, h );
Nw = ( NID*I+NFD*O + 1)*h + ( h + 1)*O;
end
Ndof = Ntrneq-Nw;
[ Xs Xi Ai Ts ] = preparets(net,X,{},T);
ts = cell2mat(Ts);
MSE00s = mean(var(ts',1));
MSE00as = mean(var(ts'));
MSEgoal = 0.01*Ndof*MSE00as/Ntrneq;
MinGrad = MSEgoal/10;
net.trainParam.goal = MSEgoal;
net.trainParam.min_grad = MinGrad;
net.divideFcn = 'dividetrain';
for i = 1:Ntrials
s(i,j)=rng ;% save state of rng for duplication
net = configure(net,Xs,Ts);
[ net tr Ys] = train(net,Xs,Ts,Xi,Ai);
ys = cell2mat(Ys);
stopcrit{i,j} = tr.stop;
bestepoch(i,j) = tr.best_epoch;
MSE = mse(ts-ys);
MSEa = Ntrneq*MSE/Ndof;
R2(i,j) = 1-MSE/MSE00s;
R2a(i,j) = 1-MSEa/MSE00as;
end
end
stopcrit = stopcrit
bestepoch = bestepoch
R2 = R2
R2a = R2a
Totaltime = toc
It would be nice if anyone can help me to select input and out delay, and try to help me to improve the narxnet.
Although I read lots of post but still it’s not clear to me what does that delays means. when I created advanced script from the application what is going on the close loop network and remove delay and then again one step ahead prediction. Because in the feed forward loop we have already predicted one step ahead prediction. So why we are doing again close loop and then remove delay.
Any type of advice is highly appreciated
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Modeling and Prediction with NARX and Time-Delay Networks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!