如果樣本比較多的話可以用 [trainV,valV,testV] = dividevec(p,t,valPercent,testPercent),然後運用 net = train(net,trainV.P,trainV.T,[],[],valV,testV);方式來訓練網絡這就是來防止過拟合的。你可以試試。具體的例子:
%bp神經網絡進行交通預測的Matlab源代碼
% BP 神經網絡用于預測
% 使用平台 - Matlab7.0
% 資料為1986年到2000年的交通量 ,網絡為3輸入,1輸出
% 15組資料,其中9組為正常訓練資料,3組為變量資料,3組為測試資料
%by akjuan
%all rights preserved by www.matlabsky.cn
%2008.11
clc
clear
All_error=[];%所有誤差存儲
%---------------------------------------------------
%原始資料
%---------------------------------------------------
year=1986:2000;%資料是從1986到2000年的
p=[493 372 445;372 445 176;445 176 235;176 235 378;235 378 429;...
378 429 561;429 561 651;561 651 467;651 467 527;467 527 668;...
527 668 841; 668 841 526;841 526 480;526 480 567;480 567 685]';%輸入資料,共15組,每組3個輸入
t=[176 235 378 429 561 651 467 527 668 841 526 480 567 685 507];%輸出資料,共15組,每組1個輸出
%---------------------------------------------------
%資料歸一化處理
%mapminmax函數預設将資料歸一化到[-1,1],調用形式如下
%[y,ps] =%mapminmax(x,ymin,ymax)
%x需歸化的資料輸入
%ymin,ymax為需歸化到的範圍,不填預設為歸化到[-1,1]
%y歸一化後的樣本資料
%ps處理設定,ps主要在結果反歸一化中需要調用,或者使用同樣的settings歸一化另外一組資料
%---------------------------------------------------
[normInput,ps] = mapminmax(p);
[normTarget,ts] = mapminmax(t);
%---------------------------------------------------
%資料亂序,及分類處理
%将輸入的15組資料的20%,即3組,用來作為測試資料;
% 樣本的20%,即3組,用來作為變化資料;
%另外9組用來正常輸入,用來訓練;
%dividevec()用來重新随機抽取上述三種分類的資料,原來的順序被打亂
%函數調用的文法
%[trainV,valV,testV] = dividevec(p,t,valPercent,testPercent)
%輸入p為輸入資料,t為輸出資料
%valPercent為訓練用的變化資料在總輸入中的百分比
%testPercent為訓練用的測試資料在總輸入中的百分比
%輸出trainV,valV,testV分别為按亂序及相應百分比,抽取得到的資料
%另外,打亂後的資料,p和t都是對應的,請放心使用
%---------------------------------------------------
testPercent = 0.20; % Adjust as desired
validatePercent = 0.20; % Adust as desired
[trainSamples,validateSamples,testSamples] = dividevec(normInput,normTarget,validatePercent,testPercent);
for j=1:200
%---------------------------------------------------
% 設定網絡參數
%---------------------------------------------------
NodeNum1 = 20; % 隐層第一層節點數
NodeNum2=40; % 隐層第二層節點數
TypeNum = 1; % 輸出維數
TF1 = 'tansig';TF2 = 'tansig'; TF3 = 'tansig';%各層傳輸函數,TF3為輸出層傳輸函數
%如果訓練結果不理想,可以嘗試更改傳輸函數,以下這些是各類傳輸函數
%TF1 = 'tansig';TF2 = 'logsig';
%TF1 = 'logsig';TF2 = 'purelin';
%TF1 = 'tansig';TF2 = 'tansig';
%TF1 = 'logsig';TF2 = 'logsig';
%TF1 = 'purelin';TF2 = 'purelin';
net=newff(minmax(normInput),[NodeNum1,NodeNum2,TypeNum],{TF1 TF2 TF3},'traingdx');%網絡建立
%---------------------------------------------------
% 設定訓練參數
%---------------------------------------------------
net.trainParam.epochs=10000;%訓練次數設定
net.trainParam.goal=1e-6;%訓練目标設定
net.trainParam.lr=0.01;%學習率設定,應設定為較少值,太大雖然會在開始加快收斂速度,但臨近最佳點時,會産生動蕩,而緻使無法收斂
%---------------------------------------------------
% 指定訓練參數
%---------------------------------------------------
% net.trainFcn = 'traingd'; % 梯度下降算法
% net.trainFcn = 'traingdm'; % 動量梯度下降算法
%
% net.trainFcn = 'traingda'; % 變學習率梯度下降算法
% net.trainFcn = 'traingdx'; % 變學習率動量梯度下降算法
%
% (大型網絡的首選算法)
% net.trainFcn = 'trainrp'; % RPROP(彈性BP)算法,記憶體需求最小
%
% (共轭梯度算法)
% net.trainFcn = 'traincgf'; % Fletcher-Reeves修正算法
% net.trainFcn = 'traincgp'; % Polak-Ribiere修正算法,記憶體需求比Fletcher-Reeves修正算法略大
% net.trainFcn = 'traincgb'; % Powell-Beal複位算法,記憶體需求比Polak-Ribiere修正算法略大
%
% (大型網絡的首選算法)
%net.trainFcn = 'trainscg'; % Scaled Conjugate Gradient算法,記憶體需求與Fletcher-Reeves修正算法相同,計算量比上面三種算法都小很多
% net.trainFcn = 'trainbfg'; % Quasi-Newton Algorithms - BFGS Algorithm,計算量和記憶體需求均比共轭梯度算法大,但收斂比較快
% net.trainFcn = 'trainoss'; % One Step Secant Algorithm,計算量和記憶體需求均比BFGS算法小,比共轭梯度算法略大
%
% (中型網絡的首選算法)
%net.trainFcn = 'trainlm'; % Levenberg-Marquardt算法,記憶體需求最大,收斂速度最快
% net.trainFcn = 'trainbr'; % 貝葉斯正則化算法
%
% 有代表性的五種算法為:'traingdx','trainrp','trainscg','trainoss', 'trainlm'
net.trainfcn='traingdm';
[net,tr] = train(net,trainSamples.P,trainSamples.T,[],[],validateSamples,testSamples);
%---------------------------------------------------
% 訓練完成後,就可以調用sim()函數,進行仿真了
%---------------------------------------------------
[normTrainOutput,Pf,Af,E,trainPerf] = sim(net,trainSamples.P,[],[],trainSamples.T);%正常輸入的9組p資料,BP得到的結果t
[normValidateOutput,Pf,Af,E,validatePerf] = sim(net,validateSamples.P,[],[],validateSamples.T);%用作變量3的資料p,BP得到的結果t
[normTestOutput,Pf,Af,E,testPerf] = sim(net,testSamples.P,[],[],testSamples.T);%用作測試的3組資料p,BP得到的結果t
%---------------------------------------------------
% 仿真後結果資料反歸一化,如果需要預測,隻需将預測的資料P填入
% 将獲得預測結果t
%---------------------------------------------------
trainOutput = mapminmax('reverse',normTrainOutput,ts);%正常輸入的9組p資料,BP得到的歸一化後的結果t
trainInsect = mapminmax('reverse',trainSamples.T,ts);%正常輸入的9組資料t
validateOutput = mapminmax('reverse',normValidateOutput,ts);%用作變量3的資料p,BP得到的歸一化的結果t
validateInsect = mapminmax('reverse',validateSamples.T,ts);%用作變量3的資料t
testOutput = mapminmax('reverse',normTestOutput,ts);%用作變量3組資料p,BP得到的歸一化的結果t
testInsect = mapminmax('reverse',testSamples.T,ts);%用作變量3組資料t
%絕對誤差計算
absTrainError = trainOutput-trainInsect;
absTestError = testOutput-testInsect;
error_sum=sqrt(absTestError(1).^2+absTestError(2).^2+absTestError(3).^2);
All_error=[All_error error_sum];
eps=90;%其為3組測試資料的标準差,或者每個資料偏差在一定範圍内而判别
if ((abs(absTestError(1))<=30 )&(abs(absTestError(2))<=30)&(abs(absTestError(3))<=30)|(error_sum<=eps))
save mynetdata net
break
end
j
end
j
Min_error_sqrt=min(All_error)
testOutput
testInsect
%---------------------------------------------------
% 資料分析和繪圖
%---------------------------------------------------
figure
plot(1:12,[trainOutput validateOutput],'b-',1:12,[trainInsect validateInsect],'g--',13:15,testOutput,'m*',13:15,testInsect,'ro');
title('o為真實值,*為預測值')
xlabel('年份');
ylabel('交通量(輛次/晝夜)');
figure
xx=1:length(All_error);
plot(xx,All_error)
title('誤差變化圖')