??在之前的一篇博客(https://blog.csdn.net/zhebushibiaoshifu/article/details/114806478)中,我們對(duì)基于MATLAB的隨機(jī)森林(RF)回歸與變量影響程度(重要性)排序代碼加以詳細(xì)講解與實(shí)踐。本次我們繼續(xù)基于MATLAB,對(duì)另一種常用的機(jī)器學(xué)習(xí)方法——神經(jīng)網(wǎng)絡(luò)方法加以代碼實(shí)戰(zhàn)。
??首先需要注明的是,在MATLAB中,我們可以直接基于“APP”中的“Neural Net Fitting”工具箱實(shí)現(xiàn)在無(wú)需代碼的情況下,對(duì)神經(jīng)網(wǎng)絡(luò)算法加以運(yùn)行:
??基于工具箱的神經(jīng)網(wǎng)絡(luò)方法雖然方便,但是一些參數(shù)不能調(diào)整;同時(shí)也不利于我們對(duì)算法、代碼的理解。因此,本文不利用“Neural Net Fitting”工具箱,而是直接通過(guò)代碼將神經(jīng)網(wǎng)絡(luò)方法加以運(yùn)行——但是,本文的代碼其實(shí)也是通過(guò)上述工具箱運(yùn)行后生成的;而這種生成神經(jīng)網(wǎng)絡(luò)代碼的方法也是MATLAB官方推薦的方式。
??另外,需要注意的是,本文直接進(jìn)行神經(jīng)網(wǎng)絡(luò)算法的執(zhí)行,省略了前期數(shù)據(jù)處理、訓(xùn)練集與測(cè)試集劃分、精度衡量指標(biāo)選取等。因此建議大家先將這一篇博客(https://blog.csdn.net/zhebushibiaoshifu/article/details/114806478)閱讀后,再閱讀本文。
??本文分為兩部分,首先是將代碼分段、詳細(xì)講解,方便大家理解;隨后是完整代碼,方便大家自行嘗試。
1 分解代碼
1.1 循環(huán)準(zhǔn)備
??由于機(jī)器學(xué)習(xí)往往需要多次執(zhí)行,我們就在此先定義循環(huán)。
%% ANN Cycle Preparation
ANNRMSE=9999;
ANNRunNum=0;
ANNRMSEMatrix=[];
ANNrAllMatrix=[];
while ANNRMSE>400
??其中,ANNRMSE是初始的RMSE;ANNRunNum是神經(jīng)網(wǎng)絡(luò)算法當(dāng)前運(yùn)行的次數(shù);ANNRMSEMatrix用來(lái)存儲(chǔ)每一次神經(jīng)網(wǎng)絡(luò)運(yùn)行后所得到的RMSE結(jié)果;ANNrAllMatrix用來(lái)存儲(chǔ)每一次神經(jīng)網(wǎng)絡(luò)運(yùn)行后所得到的皮爾遜相關(guān)系數(shù)結(jié)果;最后一句表示當(dāng)所得到的模型RMSE>400時(shí),則停止循環(huán)。
1.2 神經(jīng)網(wǎng)絡(luò)構(gòu)建
??接下來(lái),我們對(duì)神經(jīng)網(wǎng)絡(luò)的整體結(jié)構(gòu)加以定義。
%% ANN
x=TrainVARI';
t=TrainYield';
trainFcn = 'trainlm';
hiddenLayerSize = [10 10 10];
ANNnet = fitnet(hiddenLayerSize,trainFcn);
??其中,TrainVARI、TrainYield分別是我這里訓(xùn)練數(shù)據(jù)的自變量(特征)與因變量(標(biāo)簽);trainFcn為神經(jīng)網(wǎng)絡(luò)所選用的訓(xùn)練函數(shù)方法名稱,其名稱與對(duì)應(yīng)的方法對(duì)照如下表:
??hiddenLayerSize為神經(jīng)網(wǎng)絡(luò)所用隱層與各層神經(jīng)元個(gè)數(shù),[10 10 10]代表共有三層隱層,各層神經(jīng)元個(gè)數(shù)分別為10,10,10。
1.3 數(shù)據(jù)處理
??接下來(lái),對(duì)輸入神經(jīng)網(wǎng)絡(luò)模型的數(shù)據(jù)加以處理。
ANNnet.input.processFcns = {'removeconstantrows','mapminmax'};
ANNnet.output.processFcns = {'removeconstantrows','mapminmax'};
ANNnet.divideFcn = 'dividerand';
ANNnet.divideMode = 'sample';
ANNnet.divideParam.trainRatio = 0.6;
ANNnet.divideParam.valRatio = 0.4;
ANNnet.divideParam.testRatio = 0.0;
??其中,ANNnet.input.processFcns與ANNnet.output.processFcns分別代表輸入模型數(shù)據(jù)的處理方法,'removeconstantrows'表示刪除在各樣本中數(shù)值始終一致的特征列,'mapminmax'表示將數(shù)據(jù)歸一化處理;divideFcn表示劃分?jǐn)?shù)據(jù)訓(xùn)練集、驗(yàn)證集與測(cè)試集的方法,'dividerand'表示依據(jù)所給定的比例隨機(jī)劃分;divideMode表示對(duì)數(shù)據(jù)劃分的維度,我們這里選擇'sample',也就是對(duì)樣本進(jìn)行劃分;divideParam表示訓(xùn)練集、驗(yàn)證集與測(cè)試集所占比例,那么在這里,因?yàn)槭侵苯佑昧讼惹半S機(jī)森林方法(可以看這篇博客)中的數(shù)據(jù)劃分方式,那么為了保證訓(xùn)練集、測(cè)試集的固定,我們就將divideParam.testRatio設(shè)置為0.0,然后將訓(xùn)練集與驗(yàn)證集比例劃分為0.6與0.4。
1.4 模型訓(xùn)練參數(shù)配置
??接下來(lái)對(duì)模型運(yùn)行過(guò)程中的主要參數(shù)加以配置。
ANNnet.performFcn = 'mse';
ANNnet.trainParam.epochs=5000;
ANNnet.trainParam.goal=0.01;
??其中,performFcn為模型誤差衡量函數(shù),'mse'表示均方誤差;trainParam.epochs表示訓(xùn)練時(shí)Epoch次數(shù),trainParam.goal表示模型所要達(dá)到的精度要求(即模型運(yùn)行到trainParam.epochs次時(shí)或誤差小于trainParam.goal時(shí)將會(huì)停止運(yùn)行。
1.5 神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)
??這一部分代碼大多數(shù)與繪圖、代碼與GUI生成等相關(guān),因此就不再一一解釋了,大家可以直接運(yùn)行。需要注意的是,train是模型訓(xùn)練函數(shù)。
% For a list of all plot functions type: help nnplot
ANNnet.plotFcns = {'plotperform','plottrainstate','ploterrhist','plotregression','plotfit'};
[ANNnet,tr] = train(ANNnet,x,t);
y = ANNnet(x);
e = gsubtract(t,y);
performance = perform(ANNnet,t,y);
% Recalculate Training, Validation and Test Performance
trainTargets = t .* tr.trainMask{1};
valTargets = t .* tr.valMask{1};
testTargets = t .* tr.testMask{1};
trainPerformance = perform(ANNnet,trainTargets,y);
valPerformance = perform(ANNnet,valTargets,y);
testPerformance = perform(ANNnet,testTargets,y);
% view(net)
% Plots
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotfit(net,x,t)
% Deployment
% See the help for each generation function for more information.
if (false)
% Generate MATLAB function for neural network for application
% deployment in MATLAB scripts or with MATLAB Compiler and Builder
% tools, or simply to examine the calculations your trained neural
% network performs.
genFunction(ANNnet,'myNeuralNetworkFunction');
y = myNeuralNetworkFunction(x);
end
if (false)
% Generate a matrix-only MATLAB function for neural network code
% generation with MATLAB Coder tools.
genFunction(ANNnet,'myNeuralNetworkFunction','MatrixOnly','yes');
y = myNeuralNetworkFunction(x);
end
if (false)
% Generate a Simulink diagram for simulation or deployment with.
% Simulink Coder tools.
gensim(ANNnet);
end
1.6 精度衡量
%% Accuracy of ANN
ANNPredictYield=sim(ANNnet,TestVARI')';
ANNRMSE=sqrt(sum(sum((ANNPredictYield-TestYield).^2))/size(TestYield,1));
ANNrMatrix=corrcoef(ANNPredictYield,TestYield);
ANNr=ANNrMatrix(1,2);
ANNRunNum=ANNRunNum+1;
ANNRMSEMatrix=[ANNRMSEMatrix,ANNRMSE];
ANNrAllMatrix=[ANNrAllMatrix,ANNr];
disp(ANNRunNum);
end
disp(ANNRMSE);
??其中,ANNPredictYield為預(yù)測(cè)結(jié)果;ANNRMSE、ANNrMatrix分別為模型精度衡量指標(biāo)RMSE與皮爾遜相關(guān)系數(shù)。結(jié)合本文1.1部分可知,我這里設(shè)置為當(dāng)所得神經(jīng)網(wǎng)絡(luò)模型RMSE在400以內(nèi)時(shí),將會(huì)停止循環(huán);否則繼續(xù)開(kāi)始執(zhí)行本文1.2部分至1.6部分的代碼。
1.7 保存模型
??這一部分就不再贅述了,大家可以參考這篇博客(https://blog.csdn.net/zhebushibiaoshifu/article/details/114806478)。
%% ANN Model Storage
ANNModelSavePath='G:\CropYield\02_CodeAndMap\00_SavedModel\';
save(sprintf('%sRF0417ANN0399.mat',ANNModelSavePath),'TestVARI','TestYield','TrainVARI','TrainYield','ANNnet','ANNPredictYield','ANNr','ANNRMSE',...
'hiddenLayerSize');
2 完整代碼
??完整代碼如下:
%% ANN Cycle Preparation
ANNRMSE=9999;
ANNRunNum=0;
ANNRMSEMatrix=[];
ANNrAllMatrix=[];
while ANNRMSE>1000
%% ANN
x=TrainVARI';
t=TrainYield';
trainFcn = 'trainlm';
hiddenLayerSize = [10 10 10];
ANNnet = fitnet(hiddenLayerSize,trainFcn);
ANNnet.input.processFcns = {'removeconstantrows','mapminmax'};
ANNnet.output.processFcns = {'removeconstantrows','mapminmax'};
ANNnet.divideFcn = 'dividerand';
ANNnet.divideMode = 'sample';
ANNnet.divideParam.trainRatio = 0.6;
ANNnet.divideParam.valRatio = 0.4;
ANNnet.divideParam.testRatio = 0.0;
ANNnet.performFcn = 'mse';
ANNnet.trainParam.epochs=5000;
ANNnet.trainParam.goal=0.01;
% For a list of all plot functions type: help nnplot
ANNnet.plotFcns = {'plotperform','plottrainstate','ploterrhist','plotregression','plotfit'};
[ANNnet,tr] = train(ANNnet,x,t);
y = ANNnet(x);
e = gsubtract(t,y);
performance = perform(ANNnet,t,y);
% Recalculate Training, Validation and Test Performance
trainTargets = t .* tr.trainMask{1};
valTargets = t .* tr.valMask{1};
testTargets = t .* tr.testMask{1};
trainPerformance = perform(ANNnet,trainTargets,y);
valPerformance = perform(ANNnet,valTargets,y);
testPerformance = perform(ANNnet,testTargets,y);
% view(net)
% Plots
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotfit(net,x,t)
% Deployment
% See the help for each generation function for more information.
if (false)
% Generate MATLAB function for neural network for application
% deployment in MATLAB scripts or with MATLAB Compiler and Builder
% tools, or simply to examine the calculations your trained neural
% network performs.
genFunction(ANNnet,'myNeuralNetworkFunction');
y = myNeuralNetworkFunction(x);
end
if (false)
% Generate a matrix-only MATLAB function for neural network code
% generation with MATLAB Coder tools.
genFunction(ANNnet,'myNeuralNetworkFunction','MatrixOnly','yes');
y = myNeuralNetworkFunction(x);
end
if (false)
% Generate a Simulink diagram for simulation or deployment with.
% Simulink Coder tools.
gensim(ANNnet);
end
%% Accuracy of ANN
ANNPredictYield=sim(ANNnet,TestVARI')';
ANNRMSE=sqrt(sum(sum((ANNPredictYield-TestYield).^2))/size(TestYield,1));
ANNrMatrix=corrcoef(ANNPredictYield,TestYield);
ANNr=ANNrMatrix(1,2);
ANNRunNum=ANNRunNum+1;
ANNRMSEMatrix=[ANNRMSEMatrix,ANNRMSE];
ANNrAllMatrix=[ANNrAllMatrix,ANNr];
disp(ANNRunNum);
end
disp(ANNRMSE);
%% ANN Model Storage
ANNModelSavePath='G:\CropYield\02_CodeAndMap\00_SavedModel\';
save(sprintf('%sRF0417ANN0399.mat',ANNModelSavePath),'AreaPercent','InputOutput','nLeaf','nTree',...
'RandomNumber','RFModel','RFPredictConfidenceInterval','RFPredictYield','RFr','RFRMSE',...
'TestVARI','TestYield','TrainVARI','TrainYield','ANNnet','ANNPredictYield','ANNr','ANNRMSE',...
'hiddenLayerSize');