这样:
clear;
%输入数据矩阵
p1=zeros(1,1000);
p2=zeros(1,1000);
%填充数据
for i=1:1000
p1(i)=rand;
p2(i)=rand;
end
%输入层有两个,样本数为1000
p=[p1;p2];
%目标(输出)数据矩阵,待拟合的关系为简单的三角函数
t = cos(pi*p1)+sin(pi*p2);
%对训练集中的输入数据矩阵和目标数据矩阵进行归一化处理
[pn, inputStr] = mapminmax(p);
[tn, outputStr] = mapminmax(t);
%建立BP神经网络
net = newff(pn, tn, [200,10]);
%每10轮回显示一次结果
net.trainParam.show = 10;
%最大训练次数
net.trainParam.epochs = 5000;
%网络的学习速率
net.trainParam.lr = 0.05;
%训练网络所要达到的目标误差
net.trainParam.goal = 10^(-8);
%网络误差如果连续6次迭代都没变化,则matlab会默认终止训练。为了让程序继续运行,用以下命令取消这条设置
net.divideFcn = '''';
%开始训练网络
net = train(net, pn, tn);
%训练完网络后要求网络的权值w和阈值b
%获取网络权值、阈值
netiw = net.iw;
netlw = net.lw;
netb = net.b;
w1 = net.iw{1,1}; %输入层到隐层1的权值
b1 = net.b{1} ; %输入层到隐层1的阈值
w2 = net.lw{2,1}; %隐层1到隐层2的权值
b2 = net.b{2} ; %隐层1到隐层2的阈值
w3 = net.lw{3,2}; %隐层2到输出层的权值
b3 = net.b{3} ;%隐层2到输出层的阈值
%在默认的训练函数下,拟合公式为,y=w3*tansig(w2*tansig(w1*in+b1)+b2)+b3;
%用公式计算测试数据[x1;x2]的输出,输入要归一化,输出反归一化
in = mapminmax(''apply'',[x1;x2],inputStr);
y=w3*tansig(w2*tansig(w1*in+b1)+b2)+b3;
y1=mapminmax(''reverse'',y,outputStr);
%用bp神经网络验证计算结果
out = sim(net,in);
out1=mapminmax(''reverse'',out,outputStr);
扩展资料:
注意事项
一、训练函数
1、traingd
Name:Gradient descent backpropagation (梯度下降反向传播算法 )
Description:triangd is a network training function that updates weight and bias values according to gradient descent.
2、traingda
Name:Gradient descent with adaptive learning rate backpropagation(自适应学习率的t梯度下降反向传播算法)
Description:triangd is a network training function that updates weight and bias values according to gradient descent with adaptive learning rate. it will return a trained net (net) and the trianing record (tr).
3、traingdx (newelm函数默认的训练函数)
name:Gradient descent with momentum and adaptive learning rate backpropagation(带动量的梯度下降的自适应学习率的反向传播算法)
Description:triangdx is a network training function that updates weight and bias values according to gradient descent momentum and an adaptive learning rate.it will return a trained net (net) and the trianing record (tr).
4、trainlm
Name:Levenberg-Marquardt backpropagation (L-M反向传播算法)
Description:triangd is a network training function that updates weight and bias values according toLevenberg-Marquardt optimization. it will return a trained net (net) and the trianing record (tr).
注:更多的训练算法请用matlab的help命令查看。
二、学习函数
1、learngd
Name:Gradient descent weight and bias learning function (梯度下降的权值和阈值学习函数)
Description:learngd is the gradient descent weight and bias learning function, it will return the weight change dW and a new learning state.
2、learngdm
Name:Gradient descent with momentum weight and bias learning function (带动量的梯度下降的权值和阈值学习函数)
Description:learngd is the gradient descent with momentum weight and bias learning function, it will return the weight change dW and a new learning state.
注:更多的学习函数用matlab的help命令查看。
三、训练函数与学习函数的区别
函数的输出是权值和阈值的增量,训练函数的输出是训练好的网络和训练记录,在训练过程中训练函数不断调用学习函数修正权值和阈值,通过检测设定的训练步数或性能函数计算出的误差小于设定误差,来结束训练。
或者这么说:训练函数是全局调整权值和阈值,考虑的是整体误差的最小。学习函数是局部调整权值和阈值,考虑的是单个神经元误差的最小。
它的基本思想是学习过程由信号的正向传播与误差的反向传播两个过程组成。
正向传播时,输入样本从输入层传入,经各隐层逐层处理后,传向输出层。若输出层的实际输出与期望的输出(教师信号)不符,则转入误差的反向传播阶段。
反向传播时,将输出以某种形式通过隐层向输入层逐层反传,并将误差分摊给各层的所有单元,从而获得各层单元的误差信号,此误差信号即作为修正各单元权值的依据。