Matlab中newff函数使用方法和搭建BP神经网络的方法

1. newff()函数使用方法

net = newff(data,label,[8,8],{'tansig','purelin'},'trainlm')
(1)输入参数详细介绍:
  • data:训练时网络的输入数据。newff函数会把data的一列当作一个样本,如果你的数据集是一行当作一个样本,记得将你的输入数据矩阵转置data.‘
  • label:对应输入数据的标签,也可以看作真实输出。同上,newff函数会把label的一列当作一个样本的标签,如果你的数据集是一行当作一个样本,记得将你的输入数据矩阵转置label.‘
  • [8,8]:用于确定神经网络隐藏层的层数以及每层的神经单元个数。[8,8]就表示该网络包含两个隐藏层,每个隐藏层有8个神经单元
  • {‘tansig’,‘purelin’}:表示个隐藏层所用的激活函数。{‘tansig’,‘purelin’}表示第一层用tansig激活函数,是一种S型激活函数;第二次用purelin激活函数,是一种线性型激活函数。常见的激活函数如下:
 logsig:对数S型函数
 tansig:正切S型函数
 purelin:线性型函数
  • ‘trainlm’:确定训练函数,默认为trainlm函数,该方法需要占用更大的存储空间,使用了Levenberg-Marquardt算法,对于中等规模的BP神经网络有最快的收敛速度。由于其避免了直接计算赫赛矩阵,从而减少了训练中的计算量,但需要较大内存量,随着神经元的增多,训练时间增加较大。常见的训练方法如下:
 traingd:梯度下降算法
 traingdm:带动量的梯度下降算法
 traingda:学习率变化的梯度下降算法
 traingdx:学习率变化带动量的梯度下降算法
 trainrp:RPROP算法,内存需求小,适用于大型网络
 trainoss:OneStep Secant Algorithm,计算量与内存需求较小,适用于大型网络
(2)其他参数设置:
  • net.trainParam.goal = 1e-3 :确定目标值为0.001,到此值时训练停止
  • net.trainFcn=‘trainlm’ :确定训练函数trainlm。如果newff()中设置了,后续可以用不设置
  • net.trainParam.lr = 0.1 : 确定学习率为0.1
  • net.divideFcn = ‘’ :取消newff()默认再次划分操作。newff()默认将训练集重新划分6:2:2,训练集:测试集:验证集,在训练过程中会自动在某一处停止,我认为是为了防止过拟合吧,实践中在新训练集损失和新验证集损失下降不明显,且验证损失有上升的趋势时停止。对于数据本来就少的情况,建议取消划分。
  • net.trainParam.epochs = 1000 :训练最大迭代次数
  • net.trainParam.min_grad =1e-24 : 最小梯度值,到此值时训练停止

2. 用newff()搭建一个简单的BP神经网络

% I. 清空环境变量
clear all
clc

% II. 训练集/测试集产生
p_train = rand(3,4);%按列看,4个样本,每个样本维度为3
t_train = rand(1,4);%按列看,4个样本标签,每个标签维度1维

p_test = rand(3,4);
t_test = rand(1,4);

% III. BP神经网络创建、训练及仿真测试
% 1. 创建网络,[3]意思为网络含有一个隐藏层,每层有3个单元
net = newff(p_train,t_train,[3],{'tansig','purelin'});

% 2. 设置训练参数
net.trainParam.epochs = 1000;%训练批次
net.trainParam.goal = 1e-12;%训练目标,误差小于1e-12即结束训练
net.trainParam.lr = 0.01;%设置学习率
%另外也可以设置其他训练参数,可看newff()函数介绍手册

% 3. 训练网络
net = train(net,p_train,t_train);

% 4. 仿真测试
t_sim = sim(net,p_test);%预测结果
error = t_test - t_sim;%误差

新版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
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值