matlab BP神经网络的训练算法中训练函数(traingdm 、trainlm、trainbr)的实现过程及相应的VC源代码 MATLAB中BP神经网络的训练算法具体是怎么样的

matlab\u7684BP\u795e\u7ecf\u7f51\u7edc\u4e2d\uff0c\u5404\u8bad\u7ec3\u51fd\u6570\u90fd\u6709\u4ec0\u4e48\u7279\u70b9\uff1ftrainlm,trainbr,traingdm

\u4e00\u822c\u7528trainlm\uff0c\u8d85\u5927\u89c4\u6a21\u7684\u7528trainscg\u3001traingdm\u3002
trainbr\u7528\u7684\u5c11\u3002
\u5177\u4f53\u6570\u5b66\u539f\u7406\u770b\u4e66\u53bb\u5427\u3002

\u5148\u7528newff\u51fd\u6570\u5efa\u7acb\u7f51\u7edc\uff0c\u518d\u7528train\u51fd\u6570\u8bad\u7ec3\u5373\u53ef\u3002
1\uff09\u6b63\u5411\u4f20\u64ad\uff1a\u8f93\u5165\u6837\u672c\uff0d>\u8f93\u5165\u5c42\uff0d>\u5404\u9690\u5c42\uff08\u5904\u7406\uff09\uff0d>\u8f93\u51fa\u5c42
\u6ce81\uff1a\u82e5\u8f93\u51fa\u5c42\u5b9e\u9645\u8f93\u51fa\u4e0e\u671f\u671b\u8f93\u51fa\uff08\u6559\u5e08\u4fe1\u53f7\uff09\u4e0d\u7b26\uff0c\u5219\u8f6c\u51652\uff09\uff08\u8bef\u5dee\u53cd\u5411\u4f20\u64ad\u8fc7\u7a0b\uff09
2\uff09\u8bef\u5dee\u53cd\u5411\u4f20\u64ad\uff1a\u8f93\u51fa\u8bef\u5dee\uff08\u67d0\u79cd\u5f62\u5f0f\uff09\uff0d>\u9690\u5c42\uff08\u9010\u5c42\uff09\uff0d>\u8f93\u5165\u5c42
\u5176\u4e3b\u8981\u76ee\u7684\u662f\u901a\u8fc7\u5c06\u8f93\u51fa\u8bef\u5dee\u53cd\u4f20\uff0c\u5c06\u8bef\u5dee\u5206\u644a\u7ed9\u5404\u5c42\u6240\u6709\u5355\u5143\uff0c\u4ece\u800c\u83b7\u5f97\u5404\u5c42\u5355\u5143\u7684\u8bef\u5dee\u4fe1\u53f7\uff0c\u8fdb\u800c\u4fee\u6b63\u5404\u5355\u5143\u7684\u6743\u503c\uff08\u5176\u8fc7\u7a0b\uff0c\u662f\u4e00\u4e2a\u6743\u503c\u8c03\u6574\u7684\u8fc7\u7a0b\uff09\u3002

BP\u7b97\u6cd5\u5b9e\u73b0\u6b65\u9aa4\uff08\u8f6f\u4ef6\uff09\uff1a
1\uff09\u521d\u59cb\u5316
2\uff09\u8f93\u5165\u8bad\u7ec3\u6837\u672c\u5bf9\uff0c\u8ba1\u7b97\u5404\u5c42\u8f93\u51fa
3\uff09\u8ba1\u7b97\u7f51\u7edc\u8f93\u51fa\u8bef\u5dee
4\uff09\u8ba1\u7b97\u5404\u5c42\u8bef\u5dee\u4fe1\u53f7
5\uff09\u8c03\u6574\u5404\u5c42\u6743\u503c
6\uff09\u68c0\u67e5\u7f51\u7edc\u603b\u8bef\u5dee\u662f\u5426\u8fbe\u5230\u7cbe\u5ea6\u8981\u6c42
\u6ee1\u8db3\uff0c\u5219\u8bad\u7ec3\u7ed3\u675f\uff1b\u4e0d\u6ee1\u8db3\uff0c\u5219\u8fd4\u56de\u6b65\u9aa42\uff09
\u6ce8\uff1a\u6539\u8fdb\u7b97\u6cd5\u2014\u589e\u52a0\u52a8\u91cf\u9879\u3001\u81ea\u9002\u5e94\u8c03\u6574\u5b66\u4e60\u901f\u7387\uff08\u8fd9\u4e2a\u4f3c\u4e4e\u4e0d\u9519)\u53ca\u5f15\u5165\u9661\u5ea6\u56e0\u5b50\u3002

VC源代码?你很搞笑嘛。。
给你trainlm的m码

function [out1,out2] = trainlm(varargin)
%TRAINLM Levenberg-Marquardt backpropagation.
%
% <a href="matlab:doc trainlm">trainlm</a> is a network training function that updates weight and
% bias states according to Levenberg-Marquardt optimization.
%
% <a href="matlab:doc trainlm">trainlm</a> is often the fastest backpropagation algorithm in the toolbox,
% and is highly recommended as a first choice supervised algorithm,
% although it does require more memory than other algorithms.
%
% [NET,TR] = <a href="matlab:doc trainlm">trainlm</a>(NET,X,T) takes a network NET, input data X
% and target data T and returns the network after training it, and a
% a training record TR.
%
% [NET,TR] = <a href="matlab:doc trainlm">trainlm</a>(NET,X,T,Xi,Ai,EW) takes additional optional
% arguments suitable for training dynamic networks and training with
% error weights. Xi and Ai are the initial input and layer delays states
% respectively and EW defines error weights used to indicate
% the relative importance of each target value.
%
% Training occurs according to training parameters, with default values.
% Any or all of these can be overridden with parameter name/value argument
% pairs appended to the input argument list, or by appending a structure
% argument with fields having one or more of these names.
% show 25 Epochs between displays
% showCommandLine 0 generate command line output
% showWindow 1 show training GUI
% epochs 100 Maximum number of epochs to train
% goal 0 Performance goal
% max_fail 5 Maximum validation failures
% min_grad 1e-10 Minimum performance gradient
% mu 0.001 Initial Mu
% mu_dec 0.1 Mu decrease factor
% mu_inc 10 Mu increase factor
% mu_max 1e10 Maximum Mu
% time inf Maximum time to train in seconds
%
% To make this the default training function for a network, and view
% and/or change parameter settings, use these two properties:
%
% net.<a href="matlab:doc nnproperty.net_trainFcn">trainFcn</a> = 'trainlm';
% net.<a href="matlab:doc nnproperty.net_trainParam">trainParam</a>
%
% See also trainscg, feedforwardnet, narxnet.

% Mark Beale, 11-31-97, ODJ 11/20/98
% Updated by Orlando De Jes鷖, Martin Hagan, Dynamic Training 7-20-05
% Copyright 1992-2010 The MathWorks, Inc.
% $Revision: 1.1.6.11.2.2 $ $Date: 2010/07/23 15:40:16 $

%% =======================================================
% BOILERPLATE_START
% This code is the same for all Training Functions.

persistent INFO;
if isempty(INFO), INFO = get_info; end
nnassert.minargs(nargin,1);
in1 = varargin{1};
if ischar(in1)
switch (in1)
case 'info'
out1 = INFO;
case 'check_param'
nnassert.minargs(nargin,2);
param = varargin{2};
err = nntest.param(INFO.parameters,param);
if isempty(err)
err = check_param(param);
end
if nargout > 0
out1 = err;
elseif ~isempty(err)
nnerr.throw('Type',err);
end
otherwise,
try
out1 = eval(['INFO.' in1]);
catch me, nnerr.throw(['Unrecognized first argument: ''' in1 ''''])
end
end
return
end
nnassert.minargs(nargin,2);
net = nn.hints(nntype.network('format',in1,'NET'));
oldTrainFcn = net.trainFcn;
oldTrainParam = net.trainParam;
if ~strcmp(net.trainFcn,mfilename)
net.trainFcn = mfilename;
net.trainParam = INFO.defaultParam;
end
[args,param] = nnparam.extract_param(varargin(2:end),net.trainParam);
err = nntest.param(INFO.parameters,param);
if ~isempty(err), nnerr.throw(nnerr.value(err,'NET.trainParam')); end
if INFO.isSupervised && isempty(net.performFcn) % TODO - fill in MSE
nnerr.throw('Training function is supervised but NET.performFcn is undefined.');
end
if INFO.usesGradient && isempty(net.derivFcn) % TODO - fill in
nnerr.throw('Training function uses derivatives but NET.derivFcn is undefined.');
end
if net.hint.zeroDelay, nnerr.throw('NET contains a zero-delay loop.'); end
[X,T,Xi,Ai,EW] = nnmisc.defaults(args,{},{},{},{},{1});
X = nntype.data('format',X,'Inputs X');
T = nntype.data('format',T,'Targets T');
Xi = nntype.data('format',Xi,'Input states Xi');
Ai = nntype.data('format',Ai,'Layer states Ai');
EW = nntype.nndata_pos('format',EW,'Error weights EW');
% Prepare Data
[net,data,tr,~,err] = nntraining.setup(net,mfilename,X,Xi,Ai,T,EW);
if ~isempty(err), nnerr.throw('Args',err), end
% Train
net = struct(net);
fcns = nn.subfcns(net);
[net,tr] = train_network(net,tr,data,fcns,param);
tr = nntraining.tr_clip(tr);
if isfield(tr,'perf')
tr.best_perf = tr.perf(tr.best_epoch+1);
end
if isfield(tr,'vperf')
tr.best_vperf = tr.vperf(tr.best_epoch+1);
end
if isfield(tr,'tperf')
tr.best_tperf = tr.tperf(tr.best_epoch+1);
end
net.trainFcn = oldTrainFcn;
net.trainParam = oldTrainParam;
out1 = network(net);
out2 = tr;
end

% BOILERPLATE_END
%% =======================================================

% TODO - MU => MU_START
% TODO - alternate parameter names (i.e. MU for MU_START)

function info = get_info()
info = nnfcnTraining(mfilename,'Levenberg-Marquardt',7.0,true,true,...
[ ...
nnetParamInfo('showWindow','Show Training Window Feedback','nntype.bool_scalar',true,...
'Display training window during training.'), ...
nnetParamInfo('showCommandLine','Show Command Line Feedback','nntype.bool_scalar',false,...
'Generate command line output during training.'), ...
nnetParamInfo('show','Command Line Frequency','nntype.strict_pos_int_inf_scalar',25,...
'Frequency to update command line.'), ...
...
nnetParamInfo('epochs','Maximum Epochs','nntype.pos_int_scalar',1000,...
'Maximum number of training iterations before training is stopped.'), ...
nnetParamInfo('time','Maximum Training Time','nntype.pos_inf_scalar',inf,...
'Maximum time in seconds before training is stopped.'), ...
...
nnetParamInfo('goal','Performance Goal','nntype.pos_scalar',0,...
'Performance goal.'), ...
nnetParamInfo('min_grad','Minimum Gradient','nntype.pos_scalar',1e-5,...
'Minimum performance gradient before training is stopped.'), ...
nnetParamInfo('max_fail','Maximum Validation Checks','nntype.strict_pos_int_scalar',6,...
'Maximum number of validation checks before training is stopped.'), ...
...
nnetParamInfo('mu','Mu','nntype.pos_scalar',0.001,...
'Mu.'), ...
nnetParamInfo('mu_dec','Mu Decrease Ratio','nntype.real_0_to_1',0.1,...
'Ratio to decrease mu.'), ...
nnetParamInfo('mu_inc','Mu Increase Ratio','nntype.over1',10,...
'Ratio to increase mu.'), ...
nnetParamInfo('mu_max','Maximum mu','nntype.strict_pos_scalar',1e10,...
'Maximum mu before training is stopped.'), ...
], ...
[ ...
nntraining.state_info('gradient','Gradient','continuous','log') ...
nntraining.state_info('mu','Mu','continuous','log') ...
nntraining.state_info('val_fail','Validation Checks','discrete','linear') ...
]);
end

function err = check_param(param)
err = '';
end

function [net,tr] = train_network(net,tr,data,fcns,param)

% Checks
if isempty(net.performFcn)
warning('nnet:trainlm:Performance',nnwarning.empty_performfcn_corrected);
net.performFcn = 'mse';
net.performParam = mse('defaultParam');
tr.performFcn = net.performFcn;
tr.performParam = net.performParam;
end
if isempty(strmatch(net.performFcn,{'sse','mse'},'exact'))
warning('nnet:trainlm:Performance',nnwarning.nonjacobian_performfcn_replaced);
net.performFcn = 'mse';
net.performParam = mse('defaultParam');
tr.performFcn = net.performFcn;
tr.performParam = net.performParam;
end

% Initialize
startTime = clock;
original_net = net;
[perf,vperf,tperf,je,jj,gradient] = nntraining.perfs_jejj(net,data,fcns);
[best,val_fail] = nntraining.validation_start(net,perf,vperf);
WB = getwb(net);
lengthWB = length(WB);
ii = sparse(1:lengthWB,1:lengthWB,ones(1,lengthWB));
mu = param.mu;

% Training Record
tr.best_epoch = 0;
tr.goal = param.goal;
tr.states = {'epoch','time','perf','vperf','tperf','mu','gradient','val_fail'};

% Status
status = ...
[ ...
nntraining.status('Epoch','iterations','linear','discrete',0,param.epochs,0), ...
nntraining.status('Time','seconds','linear','discrete',0,param.time,0), ...
nntraining.status('Performance','','log','continuous',perf,param.goal,perf) ...
nntraining.status('Gradient','','log','continuous',gradient,param.min_grad,gradient) ...
nntraining.status('Mu','','log','continuous',mu,param.mu_max,mu) ...
nntraining.status('Validation Checks','','linear','discrete',0,param.max_fail,0) ...
];
nn_train_feedback('start',net,status);

% Train
for epoch = 0:param.epochs

% Stopping Criteria
current_time = etime(clock,startTime);
[userStop,userCancel] = nntraintool('check');
if userStop, tr.stop = 'User stop.'; net = best.net;
elseif userCancel, tr.stop = 'User cancel.'; net = original_net;
elseif (perf <= param.goal), tr.stop = 'Performance goal met.'; net = best.net;
elseif (epoch == param.epochs), tr.stop = 'Maximum epoch reached.'; net = best.net;
elseif (current_time >= param.time), tr.stop = 'Maximum time elapsed.'; net = best.net;
elseif (gradient <= param.min_grad), tr.stop = 'Minimum gradient reached.'; net = best.net;
elseif (mu >= param.mu_max), tr.stop = 'Maximum MU reached.'; net = best.net;
elseif (val_fail >= param.max_fail), tr.stop = 'Validation stop.'; net = best.net;
end

% Feedback
tr = nntraining.tr_update(tr,[epoch current_time perf vperf tperf mu gradient val_fail]);
nn_train_feedback('update',net,status,tr,data, ...
[epoch,current_time,best.perf,gradient,mu,val_fail]);

% Stop
if ~isempty(tr.stop), break, end

% Levenberg Marquardt
while (mu <= param.mu_max)
% CHECK FOR SINGULAR MATRIX
[msgstr,msgid] = lastwarn;
lastwarn('MATLAB:nothing','MATLAB:nothing')
warnstate = warning('off','all');
dWB = -(jj+ii*mu) \ je;
[~,msgid1] = lastwarn;
flag_inv = isequal(msgid1,'MATLAB:nothing');
if flag_inv, lastwarn(msgstr,msgid); end;
warning(warnstate)
WB2 = WB + dWB;
net2 = setwb(net,WB2);
perf2 = nntraining.train_perf(net2,data,fcns);

% TODO - possible speed enhancement
% - retain intermediate variables for Memory Reduction = 1

if (perf2 < perf) && flag_inv
WB = WB2; net = net2;
mu = max(mu*param.mu_dec,1e-20);
break
end
mu = mu * param.mu_inc;
end

% Validation
[perf,vperf,tperf,je,jj,gradient] = nntraining.perfs_jejj(net,data,fcns);
[best,tr,val_fail] = nntraining.validation(best,tr,val_fail,net,perf,vperf,epoch);
end
end

具体函数在matlab\R2010b\toolbox\nnet\nnet\nntrain目录下有。
vc源码自己找。

扩展阅读:pinn神经网络搭建matlab ... matlab 滑动平均 ... matlab libsvm ... 植物神经紊乱的症状 ... matlab 官网 ... matlab bp工具箱 ... matlab bp神经网络代码 ... matlab 概率 tabulate ... matlab opts ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网