MATLAB神經網路程式設計(七)——BP神經網路的實現
《MATLAB神經網路程式設計》 化學工業出版社 讀書筆記
第四章 前向型神經網路 4.3 BP傳播網路
本文是《MATLAB神經網路程式設計》書籍的閱讀筆記,其中涉及的原始碼、公式、原理都來自此書,若有不理解之處請參閱原書
本文講述BP網路常用的兩個例子:函式逼近與噪聲消除
【例4-34】利用一個單隱層的BP網路來逼近一個函式。
通過對函式進行取樣得到了網路的輸入變數P和目標變數T,在M檔案中輸入以下命令:
P=-1:0.1:1;
T=[-0.9602 -0.5770 -0.0729 0.3771 0.6405 0.6600 0.4600 0.1336 -0.2013 -0.4344 -0.5000...
-0.3930 -0.1647 0.0988 0.3072 0.3960 0.3449 0.1816 -0.0312 -0.2189 -0.3201];
每組向量都有21組資料,可以將輸入向量和目標向量繪製在一起,如下圖:
該網路的輸入層與輸出層的神經元個數均為1,根據以上的隱含層設計經驗公式(時間久了,不記得這個公式在哪裡了),以及考慮本例的實際情況,解決該問題的網路的隱層神經元個數應該在3~8之間。因此,下面設計一個隱含層神經元數目可變的BP網路,通過誤差比,確定最佳的隱含層神經元個數,並檢驗隱含層神經元個數對網路效能的影響。
在M檔案中輸入命令:
P=-1:0.1:1;
T=[-0.9602 -0.5770 -0.0729 0.3771 0.6405 0.6600 0.4600 0.1336 -0.2013 -0.4344 -0.5000...
-0.3930 -0.1647 0.0988 0.3072 0.3960 0.3449 0.1816 -0.0312 -0.2189 -0.3201];
s=3:8;
res=1:6
for i=1:6
net=newff(minmax(P),[s(i),1],{'tansig','logsig'},'traingdx');
net.trainParam.epochs=2000;
net.trainParam.goal=0.001;
net=train(net,P,T)
y=sim(net,P);
error=y-T ;
res(i)=norm(error)
end;
程式輸出:
net =
Neural Network object:
architecture:
numInputs: 1
numLayers: 2
biasConnect: [1; 1]
inputConnect: [1; 0]
layerConnect: [0 0; 1 0]
outputConnect: [0 1]
numOutputs: 1 (read-only)
numInputDelays: 0 (read-only)
numLayerDelays: 0 (read-only)
subobject structures:
inputs: {1x1 cell} of inputs
layers: {2x1 cell} of layers
outputs: {1x2 cell} containing 1 output
biases: {2x1 cell} containing 2 biases
inputWeights: {2x1 cell} containing 1 input weight
layerWeights: {2x2 cell} containing 1 layer weight
functions:
adaptFcn: 'trains'
divideFcn: (none)
gradientFcn: 'calcgrad'
initFcn: 'initlay'
performFcn: 'mse'
plotFcns: {'plotperform','plottrainstate','plotregression'}
trainFcn: 'traingdx'
parameters:
adaptParam: .passes
divideParam: (none)
gradientParam: (none)
initParam: (none)
performParam: (none)
trainParam: .show, .showWindow, .showCommandLine, .epochs,
.time, .goal, .max_fail, .lr,
.lr_inc, .lr_dec, .max_perf_inc, .mc,
.min_grad
weight and bias values:
IW: {2x1 cell} containing 1 input weight matrix
LW: {2x2 cell} containing 1 layer weight matrix
b: {2x1 cell} containing 2 bias vectors
other:
name: ''
userdata: (user information)
res =
1.5775 1.8000 1.9073 1.5776 1.4413 1.4408
由此可見,網路的隱含層神經元的傳遞函式為tansig,輸出層神經元的傳遞函式是logsig,因為目標向量的元素都位於區間[-1,1]中,正好滿足函式tansig的輸出要求。
以上結果表明,在經過2000次訓練後(訓練函式traingdx),隱含層神經元為8的BP網路對函式的逼近效果最好,因為他的誤差最小,而且網路經過27次訓練就達到了目標誤差。隱含層為6和9的網路誤差也比較小,但是他們需要的訓練時間比較長。考慮到網路效能的訓練速度,這裡把網路隱含層的神經元數目設定為8。
當隱含層神經元數目是8時,網路的逼近誤差是1.4407.網路的訓練過程記錄如下:
通過對訓練好的網路進行模擬,可以得到網路對函式的逼近情況,輸入下面命令:
y=sim(net,P);
plot(P,T,'rp');
hold on
plot(P,y,'.');
legend('原始網路','訓練後的網路');
plot(1:21,y-T);
得到函式的逼近結果與誤差曲線:
【例4-35】利用BP神經網路去除噪聲問題。
在MATLAB神經網路工具箱中,提供了26個大寫字母的資料矩陣,利用BP神經網路,可以進行字元識別處理。
原始碼:
%訓練樣本資料點
[AR,TS]=prprob;
A=size(AR,1);
B=size(AR,2);
C2=size(TS,1);
%測試樣本資料點
CM=AR( : ,13)
noisyCharM=AR(:,13)+rand(A,1)*0.3
figure
plotchar(noisyCharM)
%建立BP網路,並使用資料點訓練網路
P=AR;
T=TS;
%輸入層包含10個神經元,輸出層為C2(26)個神經元,輸入輸出層分別使用logsig傳遞函式
net=newff(minmax(P),[10,C2],{'logsig','logsig'},'traingdx');
net.trainParam.show=50;
net.trainParam.lr=0.1;
net.trainParam.lr_inc=1.05;
net.trainParam.epochs=3000;
net.trainParam.goal=0.01;
[net,tr]=train(net,P,T);
%迴帶檢驗
A=sim(net,CM)
%測試樣本檢驗
a=sim(net,noisyCharM)
%找到字母所在的位置
pos=find(compet(a)==1)
figure
%繪製去除噪聲後的字母
plotchar(AR(:,pos))
訓練過程:
實驗的結果:
可見BP網路去除了字母M上的隨機噪聲。