Matlab神经网络函数newff()新旧用法差异

摘要

在Matlab R2010a版中,如果要创建一个具有两个隐含层、且神经元数分别为5、3的前向BP网络,使用旧的语法可以这样写:

    net1 = newff(minmax(P), [5 3 1]);

注意minmax()函数的使用,还有对输出层神经元数(1)的指定。

当然也可以采用新的语法,更简洁(请留意差异):

    net2 = newff(P, T, [5 3]);

不用求minmax,也不用人工指定输出层神元数了(newff会根据参数T自行推导)。

不过,为了得到与书本示例接近的结果,接下来需要清除net2.divideFcn等属性再训练,否则结果相去甚远,且远不止一个数量级。

	  net2.divideFcn = '';

    net2.inputs{1}.processFcns = {};  % 1是输入层所在网络层编号

    net2.outputs{3}.processFcns = {};    % 3 是输出层所在网络层编号

正文

最近在看朱凯的《精通Matlab神经网络》,到第10章例10-3时,发现newff()的新旧用法得到的结果相去甚远。

书中例10-3采用了旧式写法,代码如下:

% 例10-3,旧式写法

clear all
P = [-1 -1 2 2; 0 5 0 5];
T = [-1 -1 2 2];
%% 旧式语法
net1 = newff(minmax(P),[5 1],{'tansig', 'purelin'}, 'traingd'); % 隐含层有5个神经元
net1.trainParam.goal = 1e-5;
net1.trainParam.epochs = 300;
net1.trainParam.lr = 0.05;
net1.trainParam.showWindow = 1;
net1= train(net1,P,T);
Y1 = sim(net1,P);

disp(['旧式语法 mse: ' num2str(mse(T-Y1))]);

训练窗口最终如下:

程序输出如下:

Warning: NEWFF used in an obsolete way.
> In nntobsu at 18
In newff at 86
See help for NEWFF to update calls to the new argument list.
旧式语法 mse: 9.8073e-006

很明显,达到了设定的1e-5的目标。

不过我们也收到了警告,建议我们采用新的参数列表。于是查帮助,改成新的写法,代码如下:

% 例10-3,新式写法

clear all

P = [-1 -1 2 2; 0 5 0 5];

T = [-1 -1 2 2];

 

%% 新式语法

net2 = newff(P,T,5,{'tansig', 'purelin'}, 'traingd'); % 隐含层有5个神经元

 

net2.trainParam.goal = 1e-5;

net2.trainParam.epochs = 300;

net2.trainParam.lr = 0.05;

net2.trainParam.showWindow = 1;

net2 = train(net2,P,T);

 

Y2 = sim(net2,P);

disp(['新式语法 mse: ' num2str(mse(T-Y2))]);

训练窗口:

程序输出:

新式语法 mse: 10.7499

可见,远远没有达到1e-5的目标。

这是为什么呢?QQ群咨询无果,无奈之下自行研究源码。

newff.m分成三大块:主程序、新版实现子函数 new_5p1()、旧版实现子函数 new_5p0()。通过仔细比较新旧这两个子函数,发现新版设置了 net.divideFcn 属性,其值为’dividerand’。该函数把样本数据三分为训练集、验证集和测试集,默认比例是6:2:2。于是在我的程序中清除该属性再训练:

% 例10-3,新写法,改进

 

clear all

P = [-1 -1 2 2; 0 5 0 5];

T = [-1 -1 2 2];

 

%% 新式语法

net2 = newff(P,T,5,{'tansig', 'purelin'}, 'traingd'); % 隐含层有5个神经元

 

net2.trainParam.goal = 1e-5;

net2.trainParam.epochs = 300;

net2.trainParam.lr = 0.05;

net2.trainParam.showWindow = 1;

net2.divideFcn = ''; % 为和书本一致,对于样本极少的情况,不要再三分了

net2 = train(net2,P,T);

 

Y2 = sim(net2,P);

disp(['新式语法,改进 mse: ' num2str(mse(T-Y2))]);

训练窗口:

程序输出:

新式语法,改进 mse: 9.8129e-006

也达到了预期目标。

其实,新旧两次的训练窗口和Performance窗口也可以发现端倪的,此处不再细说,请各位看官自行对比。

当然,至于新版为什么要引入divideFcn必有其道理,我是初学者,有所揣测暂不表,先跟着书走。

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
新版Matlab神经网络训练函数Newff的详细讲解-新版Matlab神经网络训练函数Newff的使用方法.doc 本帖最后由 小小2008鸟 于 2013-1-15 21:42 编辑 新版Matlab神经网络训练函数Newff的详细讲解 一、   介绍新版newffSyntax·          net = newff],{TF1 TF2...TFNl}, BTF,BLF,PF,IPF,OPF,DDF) Descriptionnewff],{TF1 TF2...TFNl}, BTF,BLF,PF,IPF,OPF,DDF) takes several arguments PR x Q1 matrix of Q1 sample R-element input vectorsTSN x Q2 matrix of Q2 sample SN-element target vectorsSiSize of ith layer, for N-1 layers, default = [ ]. TFiTransfer function of ith layer. (Default = 'tansig' for hidden layers and 'purelin' for output layer.)BTFBackpropagation network training function BLFBackpropagation weight/bias learning function IPFRow cell array of input processing functions. OPFRow cell array of output processing functions. DDFData divison function ExamplesHere is a problem consisting of inputs P and targets T to be solved with a network.·          P = [0 1 2 3 4 5 6 7 8 9 10];T = [0 1 2 3 4 3 2 1 2 3 4];Here a network is created with one hidden layer of five neurons.·          net = newff;The network is simulated and its output plotted against the targets.·          Y = sim;plotThe network is trained for 50 epochs. Again the network's output is plotted.·          net.trainParam.epochs = 50;net = train;Y = sim; plot 二、   新版newff与旧版newff调用语法对比 Example1比如输入input(6*1000),输出output为(4*1000),那么旧版定义:net=newff,[14,4],{'tansig','purelin'},'trainlm');新版定义:net=newff; Example2比如输入input(6*1000),输出output为(4*1000),那么旧版定义:net=newff,[49,10,4],{'tansig','tansig','tansig'},'traingdx');新版定义:net=newff; 更详细请看word文档 新版Matlab神经网络训练函数Newff的使用方法.doc

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值