用MATLAB实现神经网络
- 一 BP神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)不使用MATLAB神經(jīng)網(wǎng)絡(luò)工具箱
- 問題
- 分析
- MATLAB實(shí)現(xiàn)代碼
- 運(yùn)行結(jié)果
- 繪制的圖像
- 二 使用MATLAB的神經(jīng)網(wǎng)絡(luò)工具箱簡易實(shí)現(xiàn)BP網(wǎng)絡(luò)
- 問題
- 分析
- 工具箱中的相關(guān)函數(shù)一些參考了MATLAB自帶的英文手冊
- mapminmax函數(shù)
- newff函數(shù)新版本
- 關(guān)于nettrainParam的常用屬性
- train函數(shù)
- sim函數(shù)
- MATLAB實(shí)現(xiàn)代碼
- 運(yùn)行結(jié)果
- 繪制的圖像
-
一. BP神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)(不使用MATLAB神經(jīng)網(wǎng)絡(luò)工具箱)
1. 問題
公路運(yùn)量主要包括公路客運(yùn)量和公路貨運(yùn)量兩方面。某個地區(qū)的公路運(yùn)量主要與該地區(qū)的人數(shù)、機(jī)動車數(shù)量和公路面積有關(guān),已知該地區(qū)20年(1990-2009)的公路運(yùn)量相關(guān)數(shù)據(jù)如下:
人數(shù)/萬人
20.55 22.44 25.37 27.13 29.45 30.10 30.96 34.06 36.42 38.09 39.13 39.99 41.93 44.59 47.30 52.89 55.73 56.76 59.17 60.63
機(jī)動車數(shù)量/萬輛
0.6 0.75 0.85 0.9 1.05 1.35 1.45 1.6 1.7 1.85 2.15 2.2 2.25 2.35 2.5 2.6 2.7 2.85 2.95 3.1
公路面積/單位:萬平方公里
0.09 0.11 0.11 0.14 0.20 0.23 0.23 0.32 0.32 0.34 0.36 0.36 0.38 0.49 0.56 0.59 0.59 0.67 0.69 0.79
公路客運(yùn)量/萬人
5126 6217 7730 9145 10460 11387 12353 15750 18304 19836 21024 19490 20433 22598 25107 33442 36836 40548 42927 43462
公路貨運(yùn)量/萬噸
1237 1379 1385 1399 1663 1714 1834 4322 8132 8936 11099 11203 10524 11115 13320 16762 18673 20724 20803 21804
2. 分析
樣本數(shù)據(jù)較多,且已知影響數(shù)據(jù)的因素(三大因素:該地區(qū)的人數(shù)、機(jī)動車數(shù)量和公路面積),可考慮將其作為BP神經(jīng)網(wǎng)絡(luò)的訓(xùn)練集,對該神經(jīng)網(wǎng)絡(luò)進(jìn)行訓(xùn)練,然后對訓(xùn)練好的神經(jīng)網(wǎng)絡(luò)進(jìn)行測試,最后使用測試合格的神經(jīng)網(wǎng)絡(luò)進(jìn)行預(yù)測工作。
3. MATLAB實(shí)現(xiàn)代碼
BP_road.m
numberOfSample = 20; %輸入樣本數(shù)量 %取測試樣本數(shù)量等于輸入(訓(xùn)練集)樣本數(shù)量,因?yàn)檩斎霕颖?#xff08;訓(xùn)練集)容量較少,否則一般必須用新鮮數(shù)據(jù)進(jìn)行測試 numberOfTestSample = 20; numberOfForcastSample = 2; numberOfHiddenNeure = 8; inputDimension = 3; outputDimension = 2;%準(zhǔn)備好訓(xùn)練集%人數(shù)(單位:萬人) numberOfPeople=[20.55 22.44 25.37 27.13 29.45 30.10 30.96 34.06 36.42 38.09 39.13 39.99 41.93 44.59 47.30 52.89 55.73 56.76 59.17 60.63]; %機(jī)動車數(shù)(單位:萬輛) numberOfAutomobile=[0.6 0.75 0.85 0.9 1.05 1.35 1.45 1.6 1.7 1.85 2.15 2.2 2.25 2.35 2.5 2.6 2.7 2.85 2.95 3.1]; %公路面積(單位:萬平方公里) roadArea=[0.09 0.11 0.11 0.14 0.20 0.23 0.23 0.32 0.32 0.34 0.36 0.36 0.38 0.49 0.56 0.59 0.59 0.67 0.69 0.79]; %公路客運(yùn)量(單位:萬人) passengerVolume = [5126 6217 7730 9145 10460 11387 12353 15750 18304 19836 21024 19490 20433 22598 25107 33442 36836 40548 42927 43462]; %公路貨運(yùn)量(單位:萬噸) freightVolume = [1237 1379 1385 1399 1663 1714 1834 4322 8132 8936 11099 11203 10524 11115 13320 16762 18673 20724 20803 21804];%由系統(tǒng)時鐘種子產(chǎn)生隨機(jī)數(shù) rand('state', sum(100*clock));%輸入數(shù)據(jù)矩陣 input = [numberOfPeople; numberOfAutomobile; roadArea]; %目標(biāo)(輸出)數(shù)據(jù)矩陣 output = [passengerVolume; freightVolume];%對訓(xùn)練集中的輸入數(shù)據(jù)矩陣和目標(biāo)數(shù)據(jù)矩陣進(jìn)行歸一化處理 [sampleInput, minp, maxp, tmp, mint, maxt] = premnmx(input, output);%噪聲強(qiáng)度 noiseIntensity = 0.01; %利用正態(tài)分布產(chǎn)生噪聲 noise = noiseIntensity * randn(outputDimension, numberOfSample); %給樣本輸出矩陣tmp添加噪聲,防止網(wǎng)絡(luò)過度擬合 sampleOutput = tmp + noise;%取測試樣本輸入(輸出)與輸入樣本相同,因?yàn)檩斎霕颖?#xff08;訓(xùn)練集)容量較少,否則一般必須用新鮮數(shù)據(jù)進(jìn)行測試 testSampleInput = sampleInput; testSampleOutput = sampleOutput;%最大訓(xùn)練次數(shù) maxEpochs = 50000;%網(wǎng)絡(luò)的學(xué)習(xí)速率 learningRate = 0.035;%訓(xùn)練網(wǎng)絡(luò)所要達(dá)到的目標(biāo)誤差 error0 = 0.65*10^(-3);%初始化輸入層與隱含層之間的權(quán)值 W1 = 0.5 * rand(numberOfHiddenNeure, inputDimension) - 0.1; %初始化輸入層與隱含層之間的閾值 B1 = 0.5 * rand(numberOfHiddenNeure, 1) - 0.1; %初始化輸出層與隱含層之間的權(quán)值 W2 = 0.5 * rand(outputDimension, numberOfHiddenNeure) - 0.1; %初始化輸出層與隱含層之間的閾值 B2 = 0.5 * rand(outputDimension, 1) - 0.1;%保存能量函數(shù)(誤差平方和)的歷史記錄 errorHistory = [];for i = 1:maxEpochs%隱含層輸出hiddenOutput = logsig(W1 * sampleInput + repmat(B1, 1, numberOfSample));%輸出層輸出networkOutput = W2 * hiddenOutput + repmat(B2, 1, numberOfSample);%實(shí)際輸出與網(wǎng)絡(luò)輸出之差error = sampleOutput - networkOutput;%計算能量函數(shù)(誤差平方和)E = sumsqr(error);errorHistory = [errorHistory E];if E < error0break;end%以下依據(jù)能量函數(shù)的負(fù)梯度下降原理對權(quán)值和閾值進(jìn)行調(diào)整delta2 = error;delta1 = W2' * delta2.*hiddenOutput.*(1 - hiddenOutput);dW2 = delta2 * hiddenOutput';dB2 = delta2 * ones(numberOfSample, 1);dW1 = delta1 * sampleInput';dB1 = delta1 * ones(numberOfSample, 1);W2 = W2 + learningRate * dW2;B2 = B2 + learningRate * dB2;W1 = W1 + learningRate * dW1;B1 = B1 + learningRate * dB1; end%下面對已經(jīng)訓(xùn)練好的網(wǎng)絡(luò)進(jìn)行(仿真)測試%對測試樣本進(jìn)行處理 testHiddenOutput = logsig(W1 * testSampleInput + repmat(B1, 1, numberOfTestSample)); testNetworkOutput = W2 * testHiddenOutput + repmat(B2, 1, numberOfTestSample); %還原網(wǎng)絡(luò)輸出層的結(jié)果(反歸一化) a = postmnmx(testNetworkOutput, mint, maxt);%繪制測試樣本神經(jīng)網(wǎng)絡(luò)輸出和實(shí)際樣本輸出的對比圖(figure(1))-------------------------------------- t = 1990:2009;%測試樣本網(wǎng)絡(luò)輸出客運(yùn)量 a1 = a(1,:); %測試樣本網(wǎng)絡(luò)輸出貨運(yùn)量 a2 = a(2,:);figure(1); subplot(2, 1, 1); plot(t, a1, 'ro', t, passengerVolume, 'b+'); legend('網(wǎng)絡(luò)輸出客運(yùn)量', '實(shí)際客運(yùn)量'); xlabel('年份'); ylabel('客運(yùn)量/萬人'); title('神經(jīng)網(wǎng)絡(luò)客運(yùn)量學(xué)習(xí)與測試對比圖'); grid on;subplot(2, 1, 2); plot(t, a2, 'ro', t, freightVolume, 'b+'); legend('網(wǎng)絡(luò)輸出貨運(yùn)量', '實(shí)際貨運(yùn)量'); xlabel('年份'); ylabel('貨運(yùn)量/萬噸'); title('神經(jīng)網(wǎng)絡(luò)貨運(yùn)量學(xué)習(xí)與測試對比圖'); grid on;%使用訓(xùn)練好的神經(jīng)網(wǎng)絡(luò)對新輸入數(shù)據(jù)進(jìn)行預(yù)測%新輸入數(shù)據(jù)(2010年和2011年的相關(guān)數(shù)據(jù)) newInput = [73.39 75.55; 3.9635 4.0975; 0.9880 1.0268]; %利用原始輸入數(shù)據(jù)(訓(xùn)練集的輸入數(shù)據(jù))的歸一化參數(shù)對新輸入數(shù)據(jù)進(jìn)行歸一化 newInput = tramnmx(newInput, minp, maxp);newHiddenOutput = logsig(W1 * newInput + repmat(B1, 1, numberOfForcastSample)); newOutput = W2 * newHiddenOutput + repmat(B2, 1, numberOfForcastSample); newOutput = postmnmx(newOutput, mint, maxt);disp('預(yù)測2010和2011年的公路客運(yùn)量分別為(單位:萬人):'); newOutput(1,:) disp('預(yù)測2010和2011年的公路貨運(yùn)量分別為(單位:萬噸):'); newOutput(2,:)%在figure(1)的基礎(chǔ)上繪制2010和2011年的預(yù)測情況------------------------------------------------- figure(2); t1 = 1990:2011;subplot(2, 1, 1); plot(t1, [a1 newOutput(1,:)], 'ro', t, passengerVolume, 'b+'); legend('網(wǎng)絡(luò)輸出客運(yùn)量', '實(shí)際客運(yùn)量'); xlabel('年份'); ylabel('客運(yùn)量/萬人'); title('神經(jīng)網(wǎng)絡(luò)客運(yùn)量學(xué)習(xí)與測試對比圖(添加了預(yù)測數(shù)據(jù))'); grid on;subplot(2, 1, 2); plot(t1, [a2 newOutput(2,:)], 'ro', t, freightVolume, 'b+'); legend('網(wǎng)絡(luò)輸出貨運(yùn)量', '實(shí)際貨運(yùn)量'); xlabel('年份'); ylabel('貨運(yùn)量/萬噸'); title('神經(jīng)網(wǎng)絡(luò)貨運(yùn)量學(xué)習(xí)與測試對比圖(添加了預(yù)測數(shù)據(jù))'); grid on;%觀察能量函數(shù)(誤差平方和)在訓(xùn)練神經(jīng)網(wǎng)絡(luò)過程中的變化情況------------------------------------------ figure(3);n = length(errorHistory); t3 = 1:n; plot(t3, errorHistory, 'r-');%為了更加清楚地觀察出能量函數(shù)值的變化情況,這里我只繪制前100次的訓(xùn)練情況 xlim([1 100]); xlabel('訓(xùn)練過程'); ylabel('能量函數(shù)值'); title('能量函數(shù)(誤差平方和)在訓(xùn)練神經(jīng)網(wǎng)絡(luò)過程中的變化圖'); grid on;- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
4. 運(yùn)行結(jié)果
預(yù)測2010和2011年的公路客運(yùn)量分別為(單位:萬人):
ans =
1.0e+04 *
4.6188 4.6601
預(yù)測2010和2011年的公路貨運(yùn)量分別為(單位:萬噸):
ans =
1.0e+04 *
2.1521 2.1519
5. 繪制的圖像
- figure(1)
- 功能:通過將原矩陣每行的最小值和最大值映射到[YMIN,YMAX]來得到規(guī)范化的矩陣。
- 算法:y = ( ymax - ymin ) * ( x - xmin ) / ( xmax - xmin ) + ymin
- 默認(rèn)算法:
[Y,PS] = mapminmax(X)
- X:原矩陣
- Y:對矩陣X進(jìn)行規(guī)范化得到的矩陣
- PS:存放關(guān)于原矩陣規(guī)范化過程中的相關(guān)映射數(shù)據(jù)的結(jié)構(gòu)體
[Y,PS] = mapminmax(X,FP)
X:原矩陣
FP:含有字段FP.ymin和FP.ymax的結(jié)構(gòu)體
Y:對矩陣X進(jìn)行規(guī)范化得到的矩陣(使用在FP的ymin和ymax規(guī)定下的算法)
PS:存放關(guān)于原矩陣規(guī)范化過程中的相關(guān)映射數(shù)據(jù)的結(jié)構(gòu)體
Y = mapminmax(‘a(chǎn)pply’,X,PS)
- ’apply’:必寫
- X:原矩陣
- PS:存放關(guān)于某個矩陣規(guī)范化過程中的相關(guān)映射數(shù)據(jù)的結(jié)構(gòu)體
- Y:對矩陣X進(jìn)行規(guī)范化得到的矩陣(使用PS中的規(guī)范化方式)
X = mapminmax(‘reverse’,Y,PS)
- ’reverse’:必寫
- Y:某個矩陣
- PS:存放關(guān)于某個矩陣規(guī)范化過程中的相關(guān)映射數(shù)據(jù)的結(jié)構(gòu)體
- X:將矩陣Y反規(guī)范化得到的矩陣(使用PS中的規(guī)范化方式,這里指將矩陣X轉(zhuǎn)換為矩陣Y的規(guī)范化方式)
newff函數(shù)(新版本)
a. 功能:建立一個前饋反向傳播(BP)網(wǎng)絡(luò)。
b. net=newff(P,T,S)
- P: 輸入數(shù)據(jù)矩陣。(RxQ1),其中Q1代表R元的輸入向量。其數(shù)據(jù)意義是矩陣P有Q1列,每一列都是一個樣本,而每個樣本有R個屬性(特征)。一般矩陣P需要事先歸一化好,即P的每一行都?xì)w一化到[0 1]或者[-1 1]。
- T:目標(biāo)數(shù)據(jù)矩陣。(SNxQ2),其中Q2代表SN元的目標(biāo)向量。數(shù)據(jù)意義參考上面,矩陣T也是事先歸一化好的。
- S:第i層的神經(jīng)元個數(shù)。(新版本中可以省略輸出層的神經(jīng)元個數(shù)不寫,因?yàn)檩敵鰧拥纳窠?jīng)元個數(shù)已經(jīng)取決于T)
?c. net = newff(P,T,S,TF,BTF,BLF,PF,IPF,OPF,DDF)(提供了可選擇的參數(shù))
- TF:相關(guān)層的傳遞函數(shù),默認(rèn)隱含層使用tansig函數(shù),輸出層使用purelin函數(shù)。
- BTF:BP神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)訓(xùn)練函數(shù),默認(rèn)值為trainlm函數(shù)。
- BLF:權(quán)重學(xué)習(xí)函數(shù),默認(rèn)值為learngdm。
- PF:性能函數(shù),默認(rèn)值為mse。
- PF,OPF,DDF均為默認(rèn)值即可。
d. 常用的傳遞函數(shù):
- purelin:線性傳遞函數(shù)
- tansig:正切 S 型傳遞函數(shù)
- logsig: 對數(shù) S 型傳遞函數(shù)
(注意:隱含層和輸出層函數(shù)的選擇對BP神經(jīng)網(wǎng)絡(luò)預(yù)測精度有較大影響,一般隱含層節(jié)點(diǎn)傳遞函數(shù)選用tansig函數(shù)或logsig函數(shù),輸出層節(jié)點(diǎn)轉(zhuǎn)移函數(shù)選用tansig函數(shù)或purelin函數(shù)。)
關(guān)于net.trainParam的常用屬性
(假定已經(jīng)定義了一個BP網(wǎng)絡(luò)net)
* net.trainParam.show: 兩次顯示之間的訓(xùn)練次數(shù)
* net.trainParam.epochs: 最大訓(xùn)練次數(shù)
* net.trainParam.lr: 網(wǎng)絡(luò)的學(xué)習(xí)速率
* net.trainParam.goal: 訓(xùn)練網(wǎng)絡(luò)所要達(dá)到的目標(biāo)誤差
* net.trainParam.time: 最長訓(xùn)練時間(秒)train函數(shù)
功能:訓(xùn)練一個神經(jīng)網(wǎng)絡(luò)
[NET2,TR] = train(NET1,X,T)(也可[NET2] = train(NET1,X,T) )
- NET1:待訓(xùn)練的網(wǎng)絡(luò)
- X: 輸入數(shù)據(jù)矩陣(已歸一化)
- T:目標(biāo)數(shù)據(jù)矩陣(已歸一化)
- NET2:訓(xùn)練得到的網(wǎng)絡(luò)TR:存放有關(guān)訓(xùn)練過程的數(shù)據(jù)的結(jié)構(gòu)體
sim函數(shù)
功能:模擬Simulink模型
SimOut = sim(‘MODEL’, PARAMETERS)
- (見名知意,不必再解釋)
4. MATLAB實(shí)現(xiàn)代碼
BP_toolbox_road.m
%準(zhǔn)備好訓(xùn)練集%人數(shù)(單位:萬人) numberOfPeople=[20.55 22.44 25.37 27.13 29.45 30.10 30.96 34.06 36.42 38.09 39.13 39.99 41.93 44.59 47.30 52.89 55.73 56.76 59.17 60.63]; %機(jī)動車數(shù)(單位:萬輛) numberOfAutomobile=[0.6 0.75 0.85 0.9 1.05 1.35 1.45 1.6 1.7 1.85 2.15 2.2 2.25 2.35 2.5 2.6 2.7 2.85 2.95 3.1]; %公路面積(單位:萬平方公里) roadArea=[0.09 0.11 0.11 0.14 0.20 0.23 0.23 0.32 0.32 0.34 0.36 0.36 0.38 0.49 0.56 0.59 0.59 0.67 0.69 0.79]; %公路客運(yùn)量(單位:萬人) passengerVolume = [5126 6217 7730 9145 10460 11387 12353 15750 18304 19836 21024 19490 20433 22598 25107 33442 36836 40548 42927 43462]; %公路貨運(yùn)量(單位:萬噸) freightVolume = [1237 1379 1385 1399 1663 1714 1834 4322 8132 8936 11099 11203 10524 11115 13320 16762 18673 20724 20803 21804];%輸入數(shù)據(jù)矩陣 p = [numberOfPeople; numberOfAutomobile; roadArea]; %目標(biāo)(輸出)數(shù)據(jù)矩陣 t = [passengerVolume; freightVolume];%對訓(xùn)練集中的輸入數(shù)據(jù)矩陣和目標(biāo)數(shù)據(jù)矩陣進(jìn)行歸一化處理 [pn, inputStr] = mapminmax(p); [tn, outputStr] = mapminmax(t);%建立BP神經(jīng)網(wǎng)絡(luò) net = newff(pn, tn, [3 7 2], {'purelin', 'logsig', 'purelin'});%每10輪回顯示一次結(jié)果 net.trainParam.show = 10;%最大訓(xùn)練次數(shù) net.trainParam.epochs = 5000;%網(wǎng)絡(luò)的學(xué)習(xí)速率 net.trainParam.lr = 0.05;%訓(xùn)練網(wǎng)絡(luò)所要達(dá)到的目標(biāo)誤差 net.trainParam.goal = 0.65 * 10^(-3);%網(wǎng)絡(luò)誤差如果連續(xù)6次迭代都沒變化,則matlab會默認(rèn)終止訓(xùn)練。為了讓程序繼續(xù)運(yùn)行,用以下命令取消這條設(shè)置 net.divideFcn = '';%開始訓(xùn)練網(wǎng)絡(luò) net = train(net, pn, tn);%使用訓(xùn)練好的網(wǎng)絡(luò),基于訓(xùn)練集的數(shù)據(jù)對BP網(wǎng)絡(luò)進(jìn)行仿真得到網(wǎng)絡(luò)輸出結(jié)果 %(因?yàn)檩斎霕颖?#xff08;訓(xùn)練集)容量較少,否則一般必須用新鮮數(shù)據(jù)進(jìn)行仿真測試) answer = sim(net, pn);%反歸一化 answer1 = mapminmax('reverse', answer, outputStr);%繪制測試樣本神經(jīng)網(wǎng)絡(luò)輸出和實(shí)際樣本輸出的對比圖(figure(1))------------------------------------------- t = 1990:2009;%測試樣本網(wǎng)絡(luò)輸出客運(yùn)量 a1 = answer1(1,:); %測試樣本網(wǎng)絡(luò)輸出貨運(yùn)量 a2 = answer1(2,:);figure(1); subplot(2, 1, 1); plot(t, a1, 'ro', t, passengerVolume, 'b+'); legend('網(wǎng)絡(luò)輸出客運(yùn)量', '實(shí)際客運(yùn)量'); xlabel('年份'); ylabel('客運(yùn)量/萬人'); title('神經(jīng)網(wǎng)絡(luò)客運(yùn)量學(xué)習(xí)與測試對比圖'); grid on;subplot(2, 1, 2); plot(t, a2, 'ro', t, freightVolume, 'b+'); legend('網(wǎng)絡(luò)輸出貨運(yùn)量', '實(shí)際貨運(yùn)量'); xlabel('年份'); ylabel('貨運(yùn)量/萬噸'); title('神經(jīng)網(wǎng)絡(luò)貨運(yùn)量學(xué)習(xí)與測試對比圖'); grid on;%使用訓(xùn)練好的神經(jīng)網(wǎng)絡(luò)對新輸入數(shù)據(jù)進(jìn)行預(yù)測%新輸入數(shù)據(jù)(2010年和2011年的相關(guān)數(shù)據(jù)) newInput = [73.39 75.55; 3.9635 4.0975; 0.9880 1.0268]; %利用原始輸入數(shù)據(jù)(訓(xùn)練集的輸入數(shù)據(jù))的歸一化參數(shù)對新輸入數(shù)據(jù)進(jìn)行歸一化 newInput = mapminmax('apply', newInput, inputStr);%進(jìn)行仿真 newOutput = sim(net, newInput);%反歸一化 newOutput = mapminmax('reverse',newOutput, outputStr);disp('預(yù)測2010和2011年的公路客運(yùn)量分別為(單位:萬人):'); newOutput(1,:) disp('預(yù)測2010和2011年的公路貨運(yùn)量分別為(單位:萬噸):'); newOutput(2,:)%在figure(1)的基礎(chǔ)上繪制2010和2011年的預(yù)測情況------------------------------------------------------- figure(2); t1 = 1990:2011;subplot(2, 1, 1); plot(t1, [a1 newOutput(1,:)], 'ro', t, passengerVolume, 'b+'); legend('網(wǎng)絡(luò)輸出客運(yùn)量', '實(shí)際客運(yùn)量'); xlabel('年份'); ylabel('客運(yùn)量/萬人'); title('神經(jīng)網(wǎng)絡(luò)客運(yùn)量學(xué)習(xí)與測試對比圖(添加了預(yù)測數(shù)據(jù))'); grid on;subplot(2, 1, 2); plot(t1, [a2 newOutput(2,:)], 'ro', t, freightVolume, 'b+'); legend('網(wǎng)絡(luò)輸出貨運(yùn)量', '實(shí)際貨運(yùn)量'); xlabel('年份'); ylabel('貨運(yùn)量/萬噸'); title('神經(jīng)網(wǎng)絡(luò)貨運(yùn)量學(xué)習(xí)與測試對比圖(添加了預(yù)測數(shù)據(jù))'); grid on;- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
5. 運(yùn)行結(jié)果
預(yù)測2010和2011年的公路客運(yùn)量分別為(單位:萬人):
ans =
1.0e+04 *
4.4384 4.4656
預(yù)測2010和2011年的公路貨運(yùn)量分別為(單位:萬噸):
ans =
1.0e+04 *
2.1042 2.1139
6. 繪制的圖像
- figure(1)
- figure(2)
2. figure(2)
3. figure(3)
可以看出,使用BP網(wǎng)絡(luò)中的負(fù)梯度下降原理之后,效果顯著。
二. 使用MATLAB的神經(jīng)網(wǎng)絡(luò)工具箱簡易實(shí)現(xiàn)BP網(wǎng)絡(luò)
1. 問題
同<一>
2. 分析
同<一>
3. 工具箱中的相關(guān)函數(shù)(一些參考了MATLAB自帶的英文手冊)
mapminmax函數(shù)
(注:當(dāng)xmax與xmin相等時,則對原矩陣使用mapminmax函數(shù)仍得到原矩陣)
(默認(rèn)情況下,mapminmax函數(shù)的YMIN和YMAX分別是-1和1)
y = 2 * ( x - xmin ) / ( xmax - xmin ) - 1
總結(jié)
以上是生活随笔為你收集整理的用MATLAB实现神经网络的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用JQuery插件CleverTabs
- 下一篇: 用FileZilla Server搭建f