用 matlab 编的神经网络程序
一、
% bp.m - Implementation of backpropagation algorithm
% (C) copyright 2001 by Yu Hen Hu
% created: 3/17/2001
% call bpconfig.m, cvgtest.m, bpdisplay.m, bptest.m
% rsample.m, randomize.m, actfun.m, actfunp.m
% partunef.m
% modified: 12/10/2003 handle case when testing file has no label
clear all,
bpconfig; % configurate the MLP network and learning parameters.
% BP iterations begins
while not_converged==1,
% start a new epoch
% Randomly select K training samples from the training set.
[train,ptr,train0]=rsample(train0,K,Kr,ptr); % train is K by M+N
z{1}=(train(:,1:M))'; % input sample matrix M by K
d=train(:,M+1:MN)'; % corresponding target value N by K
% Feed-forward phase, compute sum of square errors
for l=2:L, % the l-th layer
u{l}=w{l}*[ones(1,K);z{l-1}]; % u{l} is n(l) by K
z{l}=actfun(u{l},atype(l));
end
error=d-z{L}; % error is N by K
E(t)=sum(sum(error.*error));
% Error back-propagation phase, compute delta error
delta{L}=actfunp(u{L},atype(L)).*error; % N (=n(L)) by K
if L>2,
for l=L-1:-1:2,
delta{l}=(w{l+1}(:,2:n(l)+1))'*delta{l+1}.*actfunp(u{l},atype(l));
end
end
% update the weight matrix using gradient, momentum and random perturbation
for l=2:L,
dw{l}=alpha*delta{l}*[ones(1,K);z{l-1}]'+...
mom*dw{l}+randn(size(w{l}))*0.005;
w{l}=w{l}+dw{l};
end