matlab neural network example

% 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)

在这里插入图片描述

  1. train two samples sets;
  2. including almost all the train parameters and functions.
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值