2.1 案例背景
在工程應用中經常會遇到一些復雜的非線性系統,這些系統狀態方程復雜,難以用數學方法准確建模。在這種情況下,可以建立BP神經網絡表達這些非線性系統。該方法把未知系統看成是一個黑箱,首先用系統輸入輸出數據訓練BP神經網絡,使網絡能夠表達該未知函數,然后用訓練好的BP神經網絡預測系統輸出。
本章擬合的非線性函數為\[y = {x_1}^2 + {x_2}^2\]該函數的圖形如下圖所示。
t=-5:0.1:5; [x1,x2] =meshgrid(t); y=x1.^2+x2.^2; surfc(x1,x2,y); shading interp xlabel('x1'); ylabel('x2'); zlabel('y'); title('非線性函數');
2.2 模型建立
神經網絡結構:2-5-1
從非線性函數中隨機得到2000組輸入輸出數據,從中隨機選擇1900 組作為訓練數據,用於網絡訓練,100組作為測試數據,用於測試網絡的擬合性能。
2.3 MATLAB實現
2.3.1 BP神經網絡工具箱函數
newff
BP神經網絡參數設置函數。
net=newff(P, T, S, TF, BTF, BLF, PF, IPF, OPF, DDF)
- P:輸入數據矩陣;
- T:輸出數據矩陣;
- S:隱含層節點數;
- TF:結點傳遞函數。包括硬限幅傳遞函數hardlim、對稱硬限幅傳遞函數hardlims、線性傳遞函數purelin、正切 型傳遞函數tansig、對數型傳遞函數logsig;
x=-5:0.1:5; subplot(2,6,[2,3]); y=hardlim(x); plot(x,y,'LineWidth',1.5); title('hardlim'); subplot(2,6,[4,5]); y=hardlims(x); plot(x,y,'LineWidth',1.5); title('hardlims'); subplot(2,6,[7,8]); y=purelin(x); plot(x,y,'LineWidth',1.5); title('purelin'); subplot(2,6,[9,10]); y=tansig(x); plot(x,y,'LineWidth',1.5); title('tansig'); subplot(2,6,[11,12]); y=logsig(x); plot(x,y,'LineWidth',1.5); title('logsig');
- BTF:訓練函數。包括梯度下降BP算法訓練函數traingd、動量反傳的梯度下降BP算法訓練函數traingdm、動態自適應學習率的梯度下降BP算法訓練函數traingda、動量反傳和動態自適應學習率的梯度下降BP算法訓練函數traingdx、Levenberg_Marquardt的BP算法訓練函數trainlm;
- BLF:網絡學習函數。包括BP學習規則learngd、帶動量項的BP學習規則learngdm;
- PF:性能分析函數,包括均值絕對誤差性能分析函數mae、均方差性能分析函數mse;
- IPF:輸入處理函數;
- OPF:輸出處理函數;
- DDF:驗證數據划分函數。
一般在使用過程中設置前面6個參數,后面4個參數采用系統默認參數。
train
用訓練數據訓練BP神經網絡。
[net, tr]=train(NET, X, T, Pi, Ai)
- NET:待訓練網絡;
- X:輸入數據矩陣;
- T輸出數據矩陣;
- Pi:初始化輸入層條件;
- Ai:初始化輸出層條件;
- net:訓練好的網絡;
- 訓練過程記錄。
sim
BP神經網絡預測函數。
y=sim(net, x)
- net:訓練好的網絡;
- x:輸入數據;
- y:網絡預測數據。
2.3.2 數據選擇和歸一化
%% 基於BP神經網絡的預測算法 %% 清空環境變量 clc clear %% 訓練數據預測數據提取及歸一化 input=10*randn(2,2000); output=sum(input.*input); %從1到2000間隨機排序 k=rand(1,2000); [m,n]=sort(k); %找出訓練數據和預測數據 input_train=input(:,n(1:1900)); output_train=output(n(1:1900)); input_test=input(:,n(1901:2000)); output_test=output(n(1901:2000)); %選連樣本輸入輸出數據歸一化 [inputn,inputps]=mapminmax(input_train); [outputn,outputps]=mapminmax(output_train);
2.3.3 BP神經網絡訓練
%% BP網絡訓練 % %初始化網絡結構 net=newff(inputn,outputn,5); % 配置網絡參數(迭代次數,學習率,目標) net.trainParam.epochs=100; net.trainParam.lr=0.1; net.trainParam.goal=0.00004; %網絡訓練 net=train(net,inputn,outputn);
2.3.4 BP神經網絡預測
%% BP網絡預測 %預測數據歸一化 inputn_test=mapminmax('apply',input_test,inputps); %網絡預測輸出 an=sim(net,inputn_test); %網絡輸出反歸一化 BPoutput=mapminmax('reverse',an,outputps);
2.3.5 結果分析
%% 結果分析 figure(1) plot(BPoutput,':og') hold on plot(output_test,'-*'); legend('預測輸出','期望輸出') title('BP網絡預測輸出','fontsize',12) ylabel('函數輸出','fontsize',12) xlabel('樣本','fontsize',12)
%預測誤差 error=BPoutput-output_test; figure(2) plot(error,'-*') title('BP網絡預測誤差','fontsize',12) ylabel('誤差','fontsize',12) xlabel('樣本','fontsize',12)
figure(3) plot((BPoutput-output_test)./output_test,'-*'); title('神經網絡預測誤差百分比')
% 計算平均絕對百分比誤差(mean absolute percentage error) MAPE=mean(abs(BPoutput-output_test)./output_test)
2.4 擴展
2.4.1 多隱含層BP神經網絡
net=newff(P, T, S, TF, BTF, BLF, PF, IPF, OPF, DDF)
S可為向量,如[5,5]表示該BP神經網絡為雙層,每個隱含層的節點數都是5。
- Neural Network:神經網絡結構,輸入、中間層、輸出維度。
- Data Division:數據划分算法;
- Training:訓練算法;
- Performance:誤差算法;
- Calculations:編譯算法。
- Epoch:迭代次數;
- Time:訓練時間;
- Performance:誤差;
- Gradient:梯度;
- Mu:阻尼因子;
- Validation Checks:泛化能力檢查。
- Performance:誤差圖示;
- Training State:訓練狀況圖示;
- Regression:回歸曲線圖示;
- Plot Interval:繪圖刻度。
2.4.2 隱含層節點數
對復雜問題來說,網絡預測誤差隨節點數增加一般呈現先減少后增加的趨勢。
2.4.3 訓練數據對預測精度的影響
缺乏訓練數據可能使BP神經網絡得不到充分訓練,預測值和期望值之間誤差較大。
2.4.4 節點轉移函數
一般隱含層選擇logsig或tansig,輸出層選擇tansig或purelin。
2.4.5 網絡擬合的局限性
BP神經網絡雖然具有較好的擬合能力,但其擬合能力不是絕對的,對於一些復雜系統,BP 神經網絡預測結果會存在較大誤差。
2.5 MATLAB doc
Feedforward networks consist of a series of layers. The first layer has a connection from the network input. Each subsequent layer has a connection from the previous layer. The final layer produces the network’s output.
Feedforward networks can be used for any kind of input to output mapping. A feedforward network with one hidden layer and enough neurons in the hidden layers, can fit any finite input-output mapping problem.
- feedforwardnet(hiddenSizes,trainFcn)
This following example loads a dataset that maps anatomical measurements x
to body fat percentages t
. A feedforward network with 10 neurons is created and trained on that data, then simulated.
[x,t] = bodyfat_dataset; net = feedforwardnet([10,10]); view(net); net = train(net,x,t); y = net(x); plot((y-t))