基于Matlab實現(xiàn)人工神經(jīng)網(wǎng)絡(ANN)回歸的示例詳解
在之前的文章MATLAB實現(xiàn)隨機森林(RF)回歸與自變量影響程度分析中,我們對基于MATLAB的隨機森林(RF)回歸與變量影響程度(重要性)排序的代碼加以詳細講解與實踐。本次我們繼續(xù)基于MATLAB,對另一種常用的機器學習方法——神經(jīng)網(wǎng)絡方法加以代碼實戰(zhàn)。
首先需要注明的是,在MATLAB中,我們可以直接基于“APP”中的“Neural Net Fitting”工具箱實現(xiàn)在無需代碼的情況下,對神經(jīng)網(wǎng)絡算法加以運行。

基于工具箱的神經(jīng)網(wǎng)絡方法雖然方便,但是一些參數(shù)不能調整;同時也不利于我們對算法、代碼的理解。因此,本文不利用“Neural Net Fitting”工具箱,而是直接通過代碼將神經(jīng)網(wǎng)絡方法加以運行——但是,本文的代碼其實也是通過上述工具箱運行后生成的;而這種生成神經(jīng)網(wǎng)絡代碼的方法也是MATLAB官方推薦的方式。
另外,需要注意的是,本文直接進行神經(jīng)網(wǎng)絡算法的執(zhí)行,省略了前期數(shù)據(jù)處理、訓練集與測試集劃分、精度衡量指標選取等。因此建議大家先將文章MATLAB實現(xiàn)隨機森林(RF)回歸與自變量影響程度分析閱讀后,再閱讀本文。
本文分為兩部分,首先是將代碼分段、詳細講解,方便大家理解;隨后是完整代碼,方便大家自行嘗試。
1 分解代碼
1.1 循環(huán)準備
由于機器學習往往需要多次執(zhí)行,我們就在此先定義循環(huán)。
%% ANN Cycle Preparation ANNRMSE=9999; ANNRunNum=0; ANNRMSEMatrix=[]; ANNrAllMatrix=[]; while ANNRMSE>400
其中,ANNRMSE是初始的RMSE;ANNRunNum是神經(jīng)網(wǎng)絡算法當前運行的次數(shù);ANNRMSEMatrix用來存儲每一次神經(jīng)網(wǎng)絡運行后所得到的RMSE結果;ANNrAllMatrix用來存儲每一次神經(jīng)網(wǎng)絡運行后所得到的皮爾遜相關系數(shù)結果;最后一句表示當所得到的模型RMSE>400時,則停止循環(huán)。
1.2 神經(jīng)網(wǎng)絡構建
接下來,我們對神經(jīng)網(wǎng)絡的整體結構加以定義。
%% ANN x=TrainVARI'; t=TrainYield'; trainFcn = 'trainlm'; hiddenLayerSize = [10 10 10]; ANNnet = fitnet(hiddenLayerSize,trainFcn);
其中,TrainVARI、TrainYield分別是我這里訓練數(shù)據(jù)的自變量(特征)與因變量(標簽);trainFcn為神經(jīng)網(wǎng)絡所選用的訓練函數(shù)方法名稱,其名稱與對應的方法對照如下表:

hiddenLayerSize為神經(jīng)網(wǎng)絡所用隱層與各層神經(jīng)元個數(shù),[10 10 10]代表共有三層隱層,各層神經(jīng)元個數(shù)分別為10,10與10。
1.3 數(shù)據(jù)處理
接下來,對輸入神經(jīng)網(wǎng)絡模型的數(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表示劃分數(shù)據(jù)訓練集、驗證集與測試集的方法,'dividerand'表示依據(jù)所給定的比例隨機劃分;divideMode表示對數(shù)據(jù)劃分的維度,我們這里選擇'sample',也就是對樣本進行劃分;divideParam表示訓練集、驗證集與測試集所占比例,那么在這里,因為是直接用了先前隨機森林方法(可以看這篇博客)中的數(shù)據(jù)劃分方式,那么為了保證訓練集、測試集的固定,我們就將divideParam.testRatio設置為0.0,然后將訓練集與驗證集比例劃分為0.6與0.4。
1.4 模型訓練參數(shù)配置
接下來對模型運行過程中的主要參數(shù)加以配置。
ANNnet.performFcn = 'mse'; ANNnet.trainParam.epochs=5000; ANNnet.trainParam.goal=0.01;
其中,performFcn為模型誤差衡量函數(shù),'mse'表示均方誤差;trainParam.epochs表示訓練時Epoch次數(shù),trainParam.goal表示模型所要達到的精度要求(即模型運行到trainParam.epochs次時或誤差小于trainParam.goal時將會停止運行)。
1.5 神經(jīng)網(wǎng)絡實現(xiàn)
這一部分代碼大多數(shù)與繪圖、代碼與GUI生成等相關,因此就不再一一解釋了,大家可以直接運行。需要注意的是,train是模型訓練函數(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為預測結果;ANNRMSE、ANNrMatrix分別為模型精度衡量指標RMSE與皮爾遜相關系數(shù)。結合本文1.1部分可知,我這里設置為當所得神經(jīng)網(wǎng)絡模型RMSE在400以內時,將會停止循環(huán);否則繼續(xù)開始執(zhí)行本文1.2部分至1.6部分的代碼。
1.7 保存模型
這一部分就不再贅述了,大家可以參考文章MATLAB實現(xiàn)隨機森林(RF)回歸與自變量影響程度分析。
%% 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');以上就是基于Matlab實現(xiàn)人工神經(jīng)網(wǎng)絡(ANN)回歸的示例詳解的詳細內容,更多關于Matlab人工神經(jīng)網(wǎng)絡ANN回歸的資料請關注腳本之家其它相關文章!
相關文章
詳解C語言中typedef和#define的用法與區(qū)別
這篇文章主要給大家介紹了關于C語言中typedef和#define的的用法、區(qū)別,以及陷阱。文中通過示例進行了詳細講解,感興趣的小伙伴可以了解一下2022-07-07
Qt中正則表達式的常見用法(QRegularExpression類)
正則表達式即一個文本匹配字符串的一種模式,Qt中使用QRegExp類進行模式匹配,下面這篇文章主要給大家介紹了關于Qt中正則表達式的常見用法,文中介紹的是QRegularExpression類的相關資料,需要的朋友可以參考下2024-05-05
OpenCV霍夫變換(Hough Transform)直線檢測詳解
這篇文章主要為大家詳細介紹了OpenCV霍夫變換直線檢測的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12

