1.輸入nftool;點擊next
2.輸入特征X 和目標值Y如下:【注意按行/按列】
3.設置訓練集/驗證集/測試機比例:【一般默認為0.7:0.15:0.15】
4.設置隱藏層個數:【需要調的參數之一】
5.選擇優化算法:默認如圖;點擊train進行訓練
6.生成圖像:【如圖plots】
6.1 performance
橫坐標:訓練結束時的epochs數【神經網絡一次前向傳播+一次反向傳播=一個epoch】
縱坐標:均方誤差
從圖中可以得到:在epochs=5時,驗證集valiadation和測試集test達到最小均方誤差。
6.2 training state
橫坐標:epoch
縱坐標:梯度gradient;mu?;val fail?;
梯度:若梯度為0,則為圖像最低點,即最優位置
mu:
val fail:
【validation check=6:若連續六次訓練,訓練誤差沒有變小,則假定繼續訓練下去效果不會變好,停止訓練。】
6.3 error histogram【誤差直方圖】
橫坐標:誤差區間的中位數;
縱坐標:位於該誤差區間的樣本個數
可以得到:神經網絡的輸出值與樣本原目標值的誤差;
6.4 regression【檢驗預測值和目標值的線性化程度?】
橫坐標:樣本原目標值;
縱坐標:神經網絡輸出預測值;
可以得到:原目標值和預測值的相關度;用系數R表示,若R越接近1,則表示線性化程度越高,結果越好。
7 另外添加更多的測試集
8.生成代&保存訓練結果和網絡
點擊xx script,生成所需要的代碼(m文件);
點擊save results,將數據結果和網絡輸出到workspace;
9.生成代碼如圖所示:
% Solve an Input-Output Fitting problem with a Neural Network % Script generated by Neural Fitting app % Created 15-May-2020 10:45:36 % % This script assumes these variables are defined: % % XXnum - input data. % YYnum - target data. x = XXnum'; t = YYnum'; % Choose a Training Function % For a list of all training functions type: help nntrain % 'trainlm' is usually fastest. % 'trainbr' takes longer but may be better for challenging problems. % 'trainscg' uses less memory. Suitable in low memory situations. trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation. % Create a Fitting Network hiddenLayerSize = 10; net = fitnet(hiddenLayerSize,trainFcn); % Setup Division of Data for Training, Validation, Testing net.divideParam.trainRatio = 70/100; net.divideParam.valRatio = 15/100; net.divideParam.testRatio = 15/100; % Train the Network [net,tr] = train(net,x,t); % Test the Network y = net(x); e = gsubtract(t,y); performance = perform(net,t,y) % View the Network view(net) % Plots % Uncomment these lines to enable various plots. %figure, plotperform(tr) %figure, plottrainstate(tr) %figure, ploterrhist(e) %figure, plotregression(t,y) %figure, plotfit(net,x,t)
參考資料:
1.https://blog.csdn.net/ljyljyok/article/details/81362465 Lindsay.Lu,如何利用matlab做BP神經網絡分析,
2. https://blog.csdn.net/weixin_44486547/article/details/93394970 晴松,初探MATLAB神經網絡