% Solve an Input-Output Fitting problem with a Neural Network
% Script generated by Neural Fitting app
% Created 29-Oct-2019 21:21:55
%
% This script assumes these variables are defined:
%
% x - input data.
% y - target data.
a1=0:.2:2*pi+.2;
rng(2);
y1=sin(a1)+rand(1,length(a1))*0.5;
a2=2*pi+.2:.2:4*pi+.2;
rng(2);
y2=sin(a2)+rand(1,length(a2))*0.5;
x = [a1,a2];
t = [y1,y2];
% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation.
%
% Create a Fitting Network
hiddenLayerSize = 10;
net = fitnet(hiddenLayerSize,trainFcn);
%net = feedforwardnet(hiddenLayerSize,trainFcn);
net.divideFcn = 'dividerand';% only for "early stopping" method, divide function:divideind,dividerand,divideblock,divideint.default function is dividerand
% Setup Division of Data for Training, Validation, Testing(only for "early stopping" method)
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 20/100;
net.divideParam.testRatio = 10/100;
%if not use early stopping methods,we can use Regularization
%net.divideFcn = '';
%net.performParam.regularization = 0.5;
% Train the Network
net.trainParam.showWindow = true;% whether to show the training window, default value is true.
net.trainParam.showCommandLine = false;% default is false
net.trainParam.min_grad=1e-5;%default is 1e-5
net.trainParam.max_fail=6;%default is 6
net.trainParam.epochs=1000;
[net,tr] = train(net,x,t);
% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y);
% View the Network
%view(net);
xx=0:.1:4*pi+.2;
yy=sin(xx);
yx=net(xx);
plot(a1,y1,'bo');
hold on;
plot(a2,y2,'b*');
hold on;
plot(xx,yy,'g-')
hold on;
plot(xx,yx,'r+')
legend('训练样本1','训练样本2','正弦曲线','实际输出');
% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotfit(net,x,t)
train two samples sets;
including almost all the train parameters and functions.
% Solve an Input-Output Fitting problem with a Neural Network% Script generated by Neural Fitting app% Created 29-Oct-2019 21:21:55%% This script assumes these variables are defined:%% x - inp...