BP神經網路預測例項
阿新 • • 發佈:2019-01-06
BP神經網路的預測,根據輸出層的是線性函式,這樣可以擬合出輸出和輸入之間的函式關係,
從而實現輸入新的值時,可以預測值,
例項:
<span style="font-size:18px;"><span style="font-size:18px;">clear; p0=[11.9850 12.1121 12.2389 12.3626 12.4810 12.5768 12.6743 12.7627 12.8453 12.9227 12.9905 13.0756]; for i =1:7 p(:,i)=[p0(i) p0(i+1) p0(i+2)]'; t(i)=p0(i+3); end p t net=newff(minmax(p),[5,1],{'logsig','purelin'},'trainlm'), net.trainParam.show = 100, net.trainParam.epoch = 2000, net.trainParam.goal= 1e-4, [net,tr]=train(net,p,t); ptest(:,1)=[p0(8) p0(9) p0(10)]'; ptest(:,2)=[p0(9) p0(10) p0(11)]'; ttest(1)=p0(11); ttest(2)=p0(12); result_test = sim(net,p) result_test1 = sim(net,ptest) delta = result_test1-ttest result = [result_test result_test1]; plot([1997:2005],p0(4:12),'-r*',[1997:2005],result,'-o'); </span></span>
第二個例項:對下一個值進行預測
3、第三個例項,股市的預測<span style="font-size:18px;">clear; p0 =[123.46, 155.54, 204.38, 290.94, 462.71, 688.19, 988.85, 1342.04, 1773.29, 2431.21, 3330.82, 4792.7, 4556.26, 5695.8, 7019.79, 9712.29, 12028.54]; a=max(p0); b=min(p0); for i=1:17 p0(i) = (p0(i)-b)/(a-b); end %進行歸一化 for i = 1:11 p(:,i)=[p0(i); p0(i+1); p0(i+2); p0(i+3)]; t(i) = p0(i+4); end %輸入輸出的賦值 net=newff(minmax(p),[8,1],{'logsig','purelin'},'trainlm'),%建立神經網路 net.trainParam.show = 100,%練顯示的間隔 net.trainParam.epoch = 2000,%最大訓練次數 net.trainParam.goal= 1e-3,%訓練的最小均方誤差 [net,tr]=train(net,p,t); %對神經網路進行訓練 ptest(:,1)=[p0(12) p0(13) p0(14) p0(15)]'; ptest(:,2)=[p0(13) p0(14) p0(15) p0(16)]'; ttest(1)=p0(16); ttest(2)=p0(17); %形成預測的資料和理想輸出 result_test = sim(net,p) result_test1 = sim(net,ptest) delta = result_test1-ttest%理想與預期的誤差 result = [result_test result_test1];</span>
clear; p0=[1258.02 1267.43 1294.54 1298.66 1334.43 1354.66 1392.62 1416.8 1542.83 ... 1635.86 1611.7 1660.15 1619.23 1548.22 1595.7 1651.22 1713.81 1708.22... 1666.55 1675.05 1597.08 1583.72 1597.37 1614.59 1650.4 1664.58 1694.19... 1733.39 1731.1]; a=max(p0); b=min(p0); for i=1:29 p0(i)=(p0(i)-b)/(a-b); end for i = 1:22 p(:,i)=[p0(i); p0(i+1); p0(i+2); p0(i+3)]; t(i) = p0(i+4); end net=newff(minmax(p),[12,1],{'logsig','purelin'},'trainlm'), net.trainParam.show = 100, net.trainParam.epoch = 2000, net.trainParam.goal= 1e-4, [net,tr]=train(net,p,t); ptest(:,1)=[p0(23) p0(24) p0(25) p0(26)]'; ptest(:,2)=[p0(24) p0(25) p0(26) p0(27)]'; ptest(:,3)=[p0(25) p0(26) p0(27) p0(28)]'; ttest(1)=p0(27); ttest(2)=p0(28); ttest(3)=p0(29); result_test = sim(net,p); result_test1 = sim(net,ptest) delta = result_test1-ttest result = [result_test result_test1]; plot([1:25],p0(5:29),'-r*',[1:25],result,'-o');