如何用MATLAB的神经网络工具箱实现三层BP网络
展开全部
这是一个来自<神经网络之家>nnetinfo的例子,在matlab2012b运行后的确可以,因为百度知道的文本宽度不够,注释挤到第二行了,有些乱,楼主注意区分哪些是代码哪些是注释,
x1 =
[-3,-2.7,-2.4,-2.1,-1.8,-1.5,-1.2,-0.9,-0.6,-0.3,0,0.3,0.6,0.9,1.2,1.5,1.8]; %x1:x1 = -3:0.3:2;
x2 =
[-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,-2.2204,0.2,0.4,0.6,0.8,1,1.2];%x2:x2 = -2:0.2:1.2;
y = [0.6589,0.2206,-0.1635,-0.4712,-0.6858,-0.7975,-0.8040,...
-0.7113,-0.5326,-0.2875
,0,0.3035,0.5966,0.8553,1.0600,1.1975,1.2618]; %y:
y = sin(x1)+0.2*x2.*x2;
inputData = [x1;x2]; %将x1,x2作为输入数据
outputData = y; %将y作为输出数据
%使用用输入输出数据(inputData、outputData)建立网络,
%隐节点个数设为3.其中隐层、输出层的传递函数分别为tansig和purelin,使用trainlm方法训练。
net = newff(inputData,outputData,3,{'tansig','purelin'},'trainlm');
%设置一些常用参数
net.trainparam.goal = 0.0001;
%训练目标:均方误差低于0.0001
net.trainparam.show = 400; %每训练400次展示一次结果
net.trainparam.epochs = 15000;
%最大训练次数:15000.
[net,tr] = train(net,inputData,outputData);%调用matlab神经网络工具箱自带的train函数训练网络
simout = sim(net,inputData);
%调用matlab神经网络工具箱自带的sim函数得到网络的预测值
figure; %新建画图窗口窗口
t=1:length(simout);
plot(t,y,t,simout,'r')%画图,对比原来的y和网络预测的y
x1 =
[-3,-2.7,-2.4,-2.1,-1.8,-1.5,-1.2,-0.9,-0.6,-0.3,0,0.3,0.6,0.9,1.2,1.5,1.8]; %x1:x1 = -3:0.3:2;
x2 =
[-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,-2.2204,0.2,0.4,0.6,0.8,1,1.2];%x2:x2 = -2:0.2:1.2;
y = [0.6589,0.2206,-0.1635,-0.4712,-0.6858,-0.7975,-0.8040,...
-0.7113,-0.5326,-0.2875
,0,0.3035,0.5966,0.8553,1.0600,1.1975,1.2618]; %y:
y = sin(x1)+0.2*x2.*x2;
inputData = [x1;x2]; %将x1,x2作为输入数据
outputData = y; %将y作为输出数据
%使用用输入输出数据(inputData、outputData)建立网络,
%隐节点个数设为3.其中隐层、输出层的传递函数分别为tansig和purelin,使用trainlm方法训练。
net = newff(inputData,outputData,3,{'tansig','purelin'},'trainlm');
%设置一些常用参数
net.trainparam.goal = 0.0001;
%训练目标:均方误差低于0.0001
net.trainparam.show = 400; %每训练400次展示一次结果
net.trainparam.epochs = 15000;
%最大训练次数:15000.
[net,tr] = train(net,inputData,outputData);%调用matlab神经网络工具箱自带的train函数训练网络
simout = sim(net,inputData);
%调用matlab神经网络工具箱自带的sim函数得到网络的预测值
figure; %新建画图窗口窗口
t=1:length(simout);
plot(t,y,t,simout,'r')%画图,对比原来的y和网络预测的y
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询