資源描述:
《BP神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)(Java代碼).doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、BP神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)(Java代碼)神經(jīng)網(wǎng)絡(luò)的原理雖然理解起來不難,但是要是想實(shí)現(xiàn)它,還是需要做一些工作的,并且有很多細(xì)節(jié)性的東西需要注意。通過參閱各種相關(guān)資料,以及參考網(wǎng)絡(luò)上已有的資源,自己寫了一個(gè)含有一個(gè)隱含層,且只能有一個(gè)輸出單元的簡單的BP網(wǎng)絡(luò),經(jīng)過測試,達(dá)到了預(yù)期的效果。需要說明的是,神經(jīng)網(wǎng)絡(luò)的每個(gè)輸入都在[0,1]中,輸出也在[0,1]中,在使用神經(jīng)網(wǎng)絡(luò)解決實(shí)際問題的時(shí)候,還需要對(duì)實(shí)際問題的輸入輸出進(jìn)行歸一化處理。另外,盡量不要使得神經(jīng)網(wǎng)絡(luò)的輸入或輸出接近于0或1,這樣會(huì)影響擬合效果。我用正弦函數(shù)進(jìn)行了一次測試,效果如
2、圖所示:以下是相關(guān)的代碼:1.神經(jīng)網(wǎng)絡(luò)代碼[java]?viewplaincopy1.package?pkg1;??2.??3.import?java.util.Scanner;??4.??5./*?6.?*??7.?*/??8.public?class?TestNeuro?{??1.??2.????private?int?INPUT_DIM=1;??3.????private?int?HIDDEN_DIM=20;??4.????private?double?LEARNING_RATE=0.05;??5.????double?
3、[][]?input_hidden_weights=new?double[INPUT_DIM][HIDDEN_DIM];??6.????double?[]?hidden_output_weights=new?double[HIDDEN_DIM];??7.????double[]?hidden_thresholds=new?double[HIDDEN_DIM];??8.????double?output_threshold;??9.??????10.????public?static?void?main(String[]args
4、){??11.????????Scanner?in=new?Scanner(System.in);??12.????????TestNeuro?neuro=new?TestNeuro(1,5);??13.????????neuro.initialize();??14.????????for(int?i=0;i<10000;i++){??15.????????????double[]?input=new?double[1];??16.????????????input[0]=Math.random();??17.????????
5、????double?expectedOutput=input[0]*input[0];??18.????????????//System.out.println("input?:?"+input[0]+"ttexpectedOutput?:?"+expectedOutput);??19.????????????//System.out.println("predict?before?training?:?"+neuro.predict(input));??20.????????????neuro.trainOnce(in
6、put,?expectedOutput);??21.????????????//System.out.println("predict?after?training?:?"+neuro.predict(input));??22.????????????//in.next();??23.????????}??24.????????while(true){??25.????????????//neuro.printLinks();??26.????????????double[]?input=new?double[1];??27.
7、????????????input[0]=in.nextDouble();??28.????????????double?expectedOutput=in.nextDouble();??29.????????????System.out.println("predict?before?training?:?"+neuro.predict(input));??30.????????????neuro.trainOnce(input,?expectedOutput);??31.????????????System.out.pri
8、ntln("predict?after?training?:?"+neuro.predict(input));??32.??????????????33.????????}??34.????}??35.??????36.????public?TestNeuro(int?inp