新版Matlab中神经网络训练函数Newff的使用方法

一、   介绍新版newff

Syntax

· net = newff(P,T,[S1 S2...S(N-l)],{TF1 TF2...TFNl}, BTF,BLF,PF,IPF,OPF,DDF)

Description

 

newff(P,T,[S1 S2...S(N-l)],{TF1 TF2...TFNl}, BTF,BLF,PF,IPF,OPF,DDF) takes several arguments
 

P

R x Q1 matrix of Q1 sample R-element input vectors

T

SN x Q2 matrix of Q2 sample SN-element target vectors

Si

Size of ith layer, for N-1 layers, default = [ ].
(Output layer size SN is determined from T.)

TFi

Transfer function of ith layer. (Default = 'tansig' for
hidden layers and 'purelin' for output layer.)

BTF

Backpropagation network training function (default = 'trainlm')

BLF

Backpropagation weight/bias learning function (default = 'learngdm')

IPF

Row cell array of input processing functions. (Default = {'fixunknowns','removeconstantrows','mapminmax'})

OPF

Row cell array of output processing functions. (Default = {'removeconstantrows','mapminmax'})

DDF

Data divison function (default = 'dividerand')

 

Examples

Here 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(P,T,5);

The network is simulated and its output plotted against the targets.

· Y = sim(net,P);plot(P,T,P,Y,'o')

The network is trained for 50 epochs. Again the network's output is plotted.

· net.trainParam.epochs = 50;net = train(net,P,T);Y = sim(net,P);plot(P,T,P,Y,'o')

二、   新版newff与旧版newff调用语法对比

Example1

比如输入input6*1000),输出output为(4*1000),那么

 

旧版定义:net=newff(minmax(input),[14,4],{'tansig','purelin'},'trainlm');

新版定义:net=newff(input,output,14,{'tansig','purelin'},'trainlm');

Example2

比如输入input6*1000),输出output为(4*1000),那么

 

旧版定义:net=newff(minmax(input),[49,14,4],{'tansig','tansig','tansig'},'traingdx');

新版定义:net=newff(input,output, [49,14], {'tansig','tansig','tansig'},'traingdx');

  

三、   旧版newff使用方法在新版本中使用

提示:旧版本定义的newff虽也能在新版本中使用,但会有警告,警告如下:

 

Warning: NEWFF used in an obsolete way.
> In obs_use at 18
  In newff>create_network at 127
  In newff at 102
          See help for NEWFF to update calls to the new argument list.

四、   新版newff与旧版newff使用的训练效果对比

     旧版本:旧用法训练次数多,但精度高  

新版本:新用法训练次数少,但精度可能达不到要求

造成上述原因是:

程序里面的权值、阈值的初始值是随机赋值的,所以每次运行的结果都会不一样,有好有坏。
你可以把预测效果不错的网络的权值和阈值作为初始值。
具体可以查看net.iw{1,1}、net.lw{2,1}、net.b{1}、net.b{2}的值。

现在给一个完整的例子

 %% 清空环境变量

clc

clear

%% 训练数据预测数据

data=importdata('test.txt');

%1768间随机排序

k=rand(1,768);

[m,n]=sort(k);

 

%输入输出数据

input=data(:,1:8);

output =data(:,9); 

%随机提取500个样本为训练样本,268个样本为预测样本

input_train=input(n(1:500),:)';

output_train=output(n(1:500),:)';

input_test=input(n(501:768),:)';

output_test=output(n(501:768),:)'; 

%输入数据归一化

[inputn,inputps]=mapminmax(input_train); 

%% BP网络训练

% %初始化网络结构

net=newff(inputn,output_train,10); 

net.trainParam.epochs=1000;

net.trainParam.lr=0.1;

net.trainParam.goal=0.0000004;

%% 网络训练

net=train(net,inputn,output_train); 

%% BP网络预测

%预测数据归一化

inputn_test=mapminmax('apply',input_test,inputps); 

%网络预测输出

BPoutput=sim(net,inputn_test); 

%% 结果分析

%根据网络输出找出数据属于哪类

BPoutput(find(BPoutput<0.5))=0;

BPoutput(find(BPoutput>=0.5))=1; 

%% 结果分析

%画出预测种类和实际种类的分类图

figure(1)

plot(BPoutput,'og')

hold on

plot(output_test,'r*');

legend('预测类别','输出类别')

title('BP网络预测分类与实际类别比对','fontsize',12)

ylabel('类别标签','fontsize',12)

xlabel('样本数目','fontsize',12)

ylim([-0.5 1.5]) 

%预测正确率

rightnumber=0;

for i=1:size(output_test,2)

    if BPoutput(i)==output_test(i)

        rightnumber=rightnumber+1;

    end

end

rightratio=rightnumber/size(output_test,2)*100;

 

sprintf('测试准确率=%0.2f',rightratio)

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 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: 在 MATLAB ,我们可以使用 newff 函数来创建一个前馈神经网络,并设置激活函数。激活函数神经网络非常重要的一部分,它通常用于模拟神经元的工作方式,以及计算网络输出和误差。 newff 函数默认激活函数为 Sigmoid 激活函数。如果需要设置其他激活函数,可以使用 setwb 函数。例如,如果需要设置 Tanh 激活函数,则可以按照以下步骤进行操作: 1. 创建前馈神经网络 net = newff(inputs, targets, hidden_layers, {'tansig', 'purelin'}, 'trainlm'); 其,inputs 表示输入层节点数;targets 表示输出层节点数;hidden_layers 是一个数组,表示每个隐藏层的节点数;{'tansig', 'purelin'} 表示每个层的激活函数,第一个元素表示隐藏层的激活函数,第二个元素表示输出层的激活函数;'trainlm' 表示使用 Levenberg-Marquardt 优化算法进行训练。 2. 设置 Tanh 激活函数 net.layers{1}.transferFcn = 'tansig'; 其,'tansig' 表示 Tanh 激活函数,{1} 表示第一个隐藏层。 3. 训练网络并预测 训练和预测的具体方法可以根据实际需求选取,例如使用 trainlm 函数进行训练使用 sim 函数进行预测。 综上所述,我们通过创建前馈神经网络和设置激活函数,可以使用 MATLAB 实现神经网络训练和预测,并根据实际需求选择合适的激活函数。 ### 回答2: 在MATLAB使用神经网络的时候,需要设置激活函数。激活函数决定了神经元之间的信号传递。通常来说,MATLAB使用的是sigmoid激活函数,但是可以通过设置newff函数的参数来选择其他不同的激活函数newff函数的语法如下: net = newff(P,T,S,TF,BTF,PF,IPF,OPF) 其,TF参数表示的是网络非输出神经元的激活函数类型,而OPF参数则表示了输出神经元的激活函数类型。 常见的激活函数有sigmoid函数、ReLU函数和tanh函数。 sigmoid函数的数学表达式为: sigmoid(x) = 1 / (1 + exp(-x)) ReLU函数的数学表达式为: ReLU(x) = max(0,x) tanh函数的数学表达式为: tanh(x) = ( exp(x) - exp(-x) ) / ( exp(x) + exp(-x) ) 其,x表示输入的神经元输入值。 这些不同的激活函数具有不同的特点和适用场景,因此需要根据具体应用场景来选择适合的激活函数。在选择激活函数的过程,需要考虑到应用的需求,在网络的训练和调试的过程,可以根据效果的反馈来不断地进行调整和优化。 总的来说,matlabnewff函数的激活函数设置比较灵活,可以根据不同的需求选择适合的激活函数,以提高神经网络的性能和效果。 ### 回答3: 在MATLABnewff函数是构建神经网络的工具。在神经网络,激活函数是非常重要的一部分。在newff函数,可以设置不同的激活函数来实现不同的神经网络模型。下面是关于newff函数激活函数设置的详细说明。 在newff函数,可以通过指定net.trainFcn来设置激活函数。在MATLAB,常见的激活函数包括sigmoid(sigmoid函数)、tansig(双曲正切函数)、purelin(线性函数)和logsig(逻辑函数)等。不同的激活函数有不同的性质,可以实现不同的神经网络模型。 例如,如果要创建一个使用sigmoid激活函数神经网络模型,可以设置如下: ```matlab net = newff(minmax(P),[10 1],{'sigmoid','purelin'},'traingd'); ``` 其,'sigmoid'指定了隐藏层的激活函数,'purelin'指定了输出层的激活函数。如果要创建一个使用双曲正切函数(tansig)作为激活函数神经网络模型,可以设置如下: ```matlab net = newff(minmax(P),[10 1],{'tansig','purelin'},'traingd'); ``` 除了以上所列举的激活函数外,还有一些其他的激活函数可供选择。例如,可以使用softmax函数来进行多分类问题的神经网络建模,还可以使用radbas函数来构建基于径向基函数神经网络模型等。 总之,在使用newff函数构建神经网络时,选择适当的激活函数非常重要,可以直接影响神经网络模型的性能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值