matlab 曲线收敛,BP神经网络学习曲线收敛问题
利用手算、串行訓練方式下的BP神經網絡,其誤差學習曲線不能呈現收斂嗎?
[% script: main_seral.m
% 串行方式訓練BP網絡,實現性別識別
%% 清理
clear all
clc
%% 讀入數據
xlsfile='student.xls';
[data,label]=getdata(xlsfile);
%% 劃分數據
[traind,trainl,testd,testl]=divide(data,label);
%% 設置參數
rng('default')
rng(0)
nTrainNum = 60; % 60個訓練樣本
nSampDim = 2;? ?% 樣本是2維的
M=2000;? ?? ?? ?% 迭代次數
ita=0.1;? ?? ???% 學習率
alpha=0.2;
%% 構造網絡
HN=3;? ?? ?? ???% 隱含層層數
net.w1=rand(3,HN);
net.w2=rand(HN+1,1);
%% 歸一化數據
mm=mean(traind);
for i=1:2
traind_s(:,i)=traind(:,i)-mm(i);
end
ml(1) = std(traind_s(:,1));
ml(2) = std(traind_s(:,2));
for i=1:2
traind_s(:,i)=traind_s(:,i)/ml(i);
end
%% 訓練
for x=1:M? ?? ?? ?? ?? ?? ?? ?? ???% 迭代
ind=randi(60);? ?? ?? ?? ?? ???% 從1-60中選一個隨機數
in=[traind_s(ind,:),1];? ?? ???% 輸入層輸出
net1_in=in*net.w1;? ?? ?? ?? ? % 隱含層輸入
net1_out=logsig(net1_in);? ?? ?% 隱含層輸出
net2_int = [net1_out,1];? ?? ? % 下一次輸入
net2_in = net2_int*net.w2;? ???% 輸出層輸入
net2_out = logsig(net2_in);? ? % 輸出層輸出
err=trainl(ind)-net2_out;? ?? ?% 誤差
errt(x)=1/2*sqrt(sum(err.^2)); % 誤差平方
fprintf('第 %d 次循環, 第%d個學生, 誤差??%f\n',x,ind, errt(x));
% 調整權值
for i=1:length(net1_out)+1
for j=1:1
ipu1(j)=err(j);? ???% 局部梯度
% 輸出層與隱含層之間的調整量
delta1(i,j) = ita.*ipu1(j).*net2_int(i);
end
end
for m=1:3
for i=1:length(net1_out)
% 局部梯度
ipu2(i)=net1_out(i).*(1-net1_out(i)).*sum(ipu1.*net.w2);
% 輸入層和隱含層之間的調整量
delta2(m,i)= ita.*in(m).*ipu2(i);
end
end
% 調整權值
if x==1
net.w1 = net.w1+delta2;
net.w2 = net.w2+delta1;
else
net.w1 = net.w1+delta2*(1-alpha) + alpha*old_delta2;
net.w2 = net.w2+delta1*(1-alpha) + alpha*old_delta1;
end
old_delta1=delta1;
old_delta2=delta2;
end
%% 測試
% 測試數據歸一化
for i=1:2
testd_s(:,i)=testd(:,i)-mm(i);
end
for i=1:2
testd_s(:,i)=testd_s(:,i)/ml(i);
end
testd_s = [testd_s,ones(length(testd_s),1)];
net1_in=testd_s*net.w1;
net1_out=logsig(net1_in);
net1_out=[net1_out,ones(length(net1_out),1)];
net2_int = net1_out;
net2_in = net2_int*net.w2;
net2_out=net2_in;
% 取整
net2_out(net2_out<0.5)=0;
net2_out(net2_out>=0.5)=1;
rate=sum(net2_out==testl')/length(net2_out);
%% 顯示
fprintf('??正確率:\n? ? %f %%\n', rate*100);
figure(1);
plot(1:M,errt,'b-','LineWidth',1.5);
xlabel('迭代次數')
ylabel('誤差')
title('BP網絡串行訓練的誤差')
% axis([0,200,0,0.45])]
BP_seral.jpg
(59.39 KB, 下載次數: 1)
2016-5-5 15:34 上傳
2016-5-5 15:40 上傳
點擊文件名下載附件
882 Bytes, 下載次數: 11
被調函數
2016-5-5 15:41 上傳
點擊文件名下載附件
383 Bytes, 下載次數: 13
被調函數
2016-5-5 15:41 上傳
點擊文件名下載附件
31 KB, 下載次數: 11
數據
2016-5-5 15:41 上傳
點擊文件名下載附件
2.66 KB, 下載次數: 12
主程序
總結
以上是生活随笔為你收集整理的matlab 曲线收敛,BP神经网络学习曲线收敛问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: chrony时间同步端口
- 下一篇: 模块化多电平换流器matlab建模,模块