近期在准備美賽,因為比賽需要故重新安裝了matlab,在里面想嘗試一下神將網絡工具箱。就找了一個看起來還挺賞心悅目的函數例子練練手:
y=1+sin(1+pi*x/4)
針對這個函數,我們首先畫出其在[-1,8]上的函數圖像,這里間隔為0.05.代碼為:
p=[-1:0.05:8] t=1+sin(1+pi*p/4) plot(p,t,'-') title("要逼近的線性函數") xlabel("x")
畫出的圖像如下:
然后我們通過建立神經網絡,並且訓練,設置訓練時間為50.這里采用了四層神經網絡。
net=newff(minmax(p),[4,1],{'tansig''purelin'},'tranlm') net=newff(minmax(p),[4,1],{'tansig','purelin'},'tranlm') net=newff(minmax(p),[4,1],{'tansig','purelin'},'trainlm') y1=sim(net,p); figure; plot(p,t,'-',p,y1,':') title('未訓練網絡的輸出結果'); xlabel('時間'); ylabel('仿真輸出--原函數-'); net.trainParam.epochs=50 net.trainParam.goal=0.01; net=train(net,p,t); y2=sim(net,p);figure;plot(p,t,'-',p,y1,':',p,y2, '--') title('訓練后網絡的輸出結果');xlabel('時間'); ylabel('仿真輸出'); y2=sim(net,p); figure; plot(p,t,' - ',p,y1,':',p,y2, '--')* title('訓練后網絡的輸出結果'); xlabel('時間'); ylabel('仿真輸出');
最后訓練的結果如下,還是可以接受的