Could some one please explain me this MATLAB code for Neural Network
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Data_Inputs = xlsread('1.xlsx');
Testing_Data = xlsread('2.xlsx');
%The training data sample are randmonized by using the function'randperm'
% Shuffling_Inputs=Data_Inputs(randperm(end),1:2); % integers (training sample)
Shuffling_Inputs = Data_Inputs(randperm(3649),1:4);
Training_Set=Data_Inputs(1:3649,1:3);%specific training set
Target_Set=Data_Inputs(1:3649,4); %specific target set
Input=Training_Set'; %Convert to row
Target=Target_Set'; %Convert to row
X = con2seq(Input); %Convert to cell
T = con2seq(Target); %Convert to cell
%%2. Data preparation
N = 365; % Multi-step ahead prediction
% Input and target series are divided in two groups of data:
% 1st group: used to train the network
% inputSeries = X(1:end-N);
% targetSeries = T(1:end-N);
% inputSeriesVal = X(end-N+1:end);
% targetSeriesVal = T(end-N+1:end);
inputSeries = X(1:3649-N);
targetSeries = T(1:3649-N);
inputSeriesVal = X(3649-N+1:3649);
targetSeriesVal = T(3649-N+1:3649);
% Create a Nonlinear Autoregressive Network with External Input
delay = 4;
inputDelays = 1:4;
feedbackDelays = 1:4;
hiddenLayerSize = 50;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize);
net.trainFcn = 'trainlm';
net.trainParam.epochs=750;
net.trainParam.max_fail = 10;
net.trainparam.min_grad = 0.00000001;
net.trainParam.lr = 0.4;
% Prepare the Data for Training and Simulation
% The function PREPARETS prepares timeseries data for a particular network,
% shifting time by the minimum amount to fill input states and layer states.
% Using PREPARETS allows you to keep your original time series data unchanged, while
% easily customizing it for networks with differing numbers of delays, with
% open loop or closed loop feedback modes.
[inputs,inputStates,layerStates,targets] = preparets(net,inputSeries,{},targetSeries);
% Setup Division of Data for Training, Validation, Testing
%net.divideParam.trainRatio = 70/100;
%net.divideParam.valRatio = 15/100;
%net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,inputs,targets,inputStates,layerStates);
%net.trainParam.epochs = 1000;
% Test the Network
outputs = net(inputs,inputStates,layerStates);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs);
% View the Network
view(net)
% Plots
% Uncomment these lines to enable various plots.
figure, plotwb(net)
figure, plotperform(tr)
figure, plottrainstate(tr)
figure, plotregression(targets,outputs)
figure, plotresponse(targets,outputs)
figure, ploterrcorr(errors)
figure, plotinerrcorr(inputs,errors)
% Closed Loop Network
% Use this network to do multi-step prediction.
% The function CLOSELOOP replaces the feedback input with a direct
% connection from the outout layer.
netc = closeloop(net);
netc.name = [net.name ' - Closed Loop'];
view(netc)
[xc,xic,aic,tc] = preparets(netc,inputSeries,{},targetSeries);
yc = netc(xc,xic,aic);
closedLoopPerformance = perform(netc,tc,yc);
% Early Prediction Network
% For some applications it helps to get the prediction a timestep early.
% The original network returns predicted y(t+1) at the same time it is given y(t+1).
% For some applications such as decision making, it would help to have predicted
% y(t+1) once y(t) is available, but before the actual y(t+1) occurs.
% The network can be made to return its output a timestep early by removing one delay
% so that its minimal tap delay is now 0 instead of 1. The new network returns the
% same outputs as the original network, but outputs are shifted left one timestep.
nets = removedelay(net);
nets.name = [net.name ' - Predict One Step Ahead'];
view(nets)
[xs,xis,ais,ts] = preparets(nets,inputSeries,{},targetSeries);
ys = nets(xs,xis,ais);
earlyPredictPerformance = perform(nets,ts,ys);
%%5. Multi-step ahead prediction
inputSeriesPred = [inputSeries(end-delay+1:end),inputSeriesVal];
targetSeriesPred = [targetSeries(end-delay+1:end), con2seq(nan(1,N))];
[Xs,Xi,Ai,Ts] = preparets(netc,inputSeriesPred,{},targetSeriesPred);
yPred = netc(Xs,Xi,Ai);
perf = perform(net,yPred,targetSeriesVal);
figure;
plot([cell2mat(targetSeries),nan(1,N);
nan(1,length(targetSeries)),cell2mat(yPred);
nan(1,length(targetSeries)),cell2mat(targetSeriesVal)]')
legend('Original Targets','Network Predictions','Expected Outputs');
outputs = net(inputs);
trOut = outputs(tr.trainInd);
vOut = outputs(tr.valInd);
tsOut = outputs(tr.testInd);
trTarg = targets(tr.trainInd);
vTarg = targets(tr.valInd);
tsTarg = targets(tr.testInd);
plotregression(trTarg,trOut,'Train',vTarg,vOut,'Validation',...
tsTarg,tsOut,'Testing');
2 comentarios
José-Luis
el 14 de Abr. de 2014
It would be easier if you had a more specific question? What is it you don't understand?
Respuestas (0)
Ver también
Categorías
Más información sobre Modeling and Prediction with NARX and Time-Delay Networks en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!