Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Rating NarX and obtaining network multistep ahead

1 view (last 30 days)
Regarding your previous questions Greg
I have several scripts to evaluate autocorrelation, crosscorrelation, hidden layers .... I'll put the script creating the network, checking in closeloop, and attempted several iterations several steps ahead with the outputs predicted:
I have two files:
p (are the inputs), consisting of 8 columns and 2200 rows
t (are the targets) consisting of 1 column and 2200 rows
if true % code p1=p(1:8,1:2100);
p2=p(1:8,2101:end);
t1=t(1,1:2100);
t2=t(1,2101:end);
%normalizacion de los datos
inputSeries = tonndata(p1,true,false);
targetSeries = tonndata(t1,true,false);
%calculo del MSE00 y MSE00a
MSE00=mean(var(t1,1)); % MSE00=0.0095
MSE00a=mean(var(t1,0)); MSE00a=0.0095
%numero de capas y retrasos
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize = 9;
%calculo de otros parámetros
[I N]=size(p); % I=8 ; N=2200
[O N]=size(t); % O=1 ; N=2200
Neq=N*O; % Neq=2200
Nw=(I+1)*hiddenLayerSize+(hiddenLayerSize+1)*O; % Nw=91
Ntrneq=0.7*Neq; % Ntrneq=1540
Ndof=Ntrneq-Nw; % Ndof=1449
%creacion de la red
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize);
%preparacion de los datos
[inputs,inputStates,layerStates,targets] = preparets(net,inputSeries,{},targetSeries);
%division de los datos en entrenamiento, validacion y prueba
net.divideFcn='divideblock';
net.divideParam.trainRatio=0.70;
net.divideParam.valRatio=0.15;
net.divideParam.testRatio=0.15;
net.trainParam.goal=0.01*MSE00;
%entrenamiento de la red
[net,tr] = train(net,inputs,targets,inputStates,layerStates);
%salidas y errores
outputs = net(inputs,inputStates,layerStates);
errors = gsubtract(targets,outputs);
MSE = perform(net,targets,outputs); % MSE= 7.7982e-5
MSEa=Neq*MSE/(Neq-Nw); % MSEa= 8.1346e-5
R2=1-MSE/MSE00; % R2=0.9918
R2a=1-MSEa/MSE00a; % R2a=0.9915
MSEtrn=tr.perf(end); %MSEtrn=7.1240e-5
MSEval=tr.vperf(end); %MSEval=9.4383e-5
MSEtst=tr.tperf(end); %MSEtst=9.3139e-5
R2trn=1-MSEtrn/MSE00; %R2trn=0.9925
R2val=1-MSEval/MSE00; %R2val=0.9901
R2tst=1-MSEtst/MSE00; %R2tst=0.9902
precisiones=[MSE MSEa R2 R2a R2trn R2val R2tst];
% we close the loop and check with 50 values of p2
%cerramos bucle
netc = closeloop(net);
netc.name = [net.name ' - Closed Loop'];
view(netc)
%cogemos numero de predicciones
NumberOfPredictions = 50;
% create new inputs and new targets
s=cell2mat(inputSeries);
t4=cell2mat(targetSeries);
a=s(1:8,2098:2100);
b=p2(1:8,1:50);
newInputSeries=[a b];
c=t4(1,2099:2100);
d=nan(1,51);
newTargetSet=[c d];
%normalizamos los nuevos inputs y los nuevos targets
newInputSeries=tonndata(newInputSeries,true,false);
newTargetSet=tonndata(newTargetSet,true,false);
%preparamos los datos
[xc,xic,aic,tc] = preparets(netc,newInputSeries,{},newTargetSet);
%obtenemos las salidas predichas
yPredicted = sim(netc,xc,xic,aic);
w=cell2mat(yPredicted);
%errores
t3=t2(1,1:50);
MSE00C=mean(var(t3,1))
errores = gsubtract(t3,yPredicted);
t3=tonndata(t3,true,false);
MSEclosed = perform(netc,t3,yPredicted) % MSEclosed=2.456e-5
R2=1-MSEclosed/MSE00C
% I think this assessment is well closeloop errors
%representacion grafica de las salidas predichas
plot(cell2mat(yPredicted),'DisplayName','cell2mat(yPredicted)','YdataSource','cell2mat(yPredicted)');figure(gcf)
plot(t2,'r','DisplayName','targetsComprobacion')
hold on
plot(w,'b','DisplayName','salidasIteradas')
title({'ITERACCIONES'})
legend('show')
hold off
% Now intend to use this network to perform several steps ahead with today's data to predict the next 3-4 days. (This the part I do not understand). % first lame today's data, which is a file of one row by 8 columns and perform a 1 iteration as follows %p7 son los datos de hoy (1 fila por 8 columnas):
%cogemos numero de predicciones
NumberOfPredictions = 1;
s=cell2mat(inputSeries);
t4=cell2mat(targetSeries);
a1=s(1:8,2098:2100);
b1=p7;
newInputSeries1=[a1 b1];
c1=t4(1,2099:2100);
d1=nan(1,2);
newTargetSet1=[c1 d1];
%normalizamos los nuevos inputs y los nuevos targets
newInputSeries1=tonndata(newInputSeries1,true,false);
newTargetSet1=tonndata(newTargetSet1,true,false);
%preparamos los datos
[xc,xic,aic,tc] = preparets(netc,newInputSeries1,{},newTargetSet1);
%obtenemos las salidas predichas
yPredicted1 = sim(netc,xc,xic,aic);
w1=cell2mat(yPredicted1);
% Now this yPredicted1 should enter in the closed loop step back to get ahead, but do not know how. I tried as follows but I get
ttt=[c1 w1];
ttt=tonndata(ttt,true,false);
[xc2,xic2,aic2,tc2] = preparets(netc,a1,{},ttt);
yPredicted2 = sim(netc,xc2,xic2,aic2);
w2=cell2mat(yPredicted2);
%---------------------------2ºITERACION PREDICHA--------------------
ttt2=[c1 w2];
ttt2=tonndata(ttt2,true,false);
[xc3,xic3,aic3,tc3] = preparets(netc,a1,{},ttt2);
yPredicted3 = sim(netc,xc3,xic3,aic3);
w3=cell2mat(yPredicted3);
% This is the part I do not understand. How to get several steps ahead with the outputs predicted? . future values of inputs I have not thus forces me to use the outputs obtained prior to continue iterating % I hope you find me understand this last step because I stuck completely
end Many thanks Greg
  2 Comments
Walter Roberson
Walter Roberson on 7 Mar 2013
Edited: Walter Roberson on 7 Mar 2013
If this is a response to Greg, it should probably have been put into the place that Greg asked the question.
FRANCISCO
FRANCISCO on 7 Mar 2013
Sorry, I've noticed and I've posted twice. if you can help me also admit others tips. thank you very much.

Answers (0)

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!