基于Matlab實(shí)現(xiàn)人工神經(jīng)網(wǎng)絡(luò)(ANN)回歸的示例詳解
在之前的文章MATLAB實(shí)現(xiàn)隨機(jī)森林(RF)回歸與自變量影響程度分析中,我們對(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)選取等。因此建議大家先將文章MATLAB實(shí)現(xiàn)隨機(jī)森林(RF)回歸與自變量影響程度分析閱讀后,再閱讀本文。
本文分為兩部分,首先是將代碼分段、詳細(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 保存模型
這一部分就不再贅述了,大家可以參考文章MATLAB實(shí)現(xiàn)隨機(jī)森林(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實(shí)現(xiàn)人工神經(jīng)網(wǎng)絡(luò)(ANN)回歸的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Matlab人工神經(jīng)網(wǎng)絡(luò)ANN回歸的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言開(kāi)源庫(kù)iniparser解析ini文件的方法
INI(Initialization?File)文件是一種簡(jiǎn)單直觀的數(shù)據(jù)存儲(chǔ)格式,常用于配置應(yīng)用程序的初始化設(shè)置,使用?iniparser?庫(kù)的應(yīng)用程序可以很方便地讀取和解析INI文件中的配置信息,大大簡(jiǎn)化了對(duì)配置文件的處理工作,降低了程序的開(kāi)發(fā)復(fù)雜度,感興趣的的朋友跟隨小編一起看看吧2024-04-04利用C語(yǔ)言來(lái)求最大連續(xù)子序列乘積的方法
這篇文章主要介紹了利用C語(yǔ)言來(lái)求最大連續(xù)子序列乘積的方法,基本的思路以外文中還附有相關(guān)ACM題目,需要的朋友可以參考下2015-08-08C語(yǔ)言中的字符串?dāng)?shù)據(jù)在C中的存儲(chǔ)方式
這篇文章主要介紹了C語(yǔ)言中的字符串?dāng)?shù)據(jù)在C中的存儲(chǔ)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07C++核心編程之占位參數(shù)和默認(rèn)參數(shù)
這篇文章主要介紹了C++核心編程之占位參數(shù)和默認(rèn)參數(shù),c++中函數(shù)的形參列表中的形參是可以有默認(rèn)值的,函數(shù)的形參列表里可以有占位參數(shù),用來(lái)占位,調(diào)用函數(shù)時(shí)必須填補(bǔ)位置。下面更多相關(guān)內(nèi)容的詳細(xì)介紹,需要的小伙伴可以參考一下2022-03-03