BP神经网络实现(Java代码)_第1页
BP神经网络实现(Java代码)_第2页
BP神经网络实现(Java代码)_第3页
BP神经网络实现(Java代码)_第4页
BP神经网络实现(Java代码)_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 BP神经网络实现(Java代码)神经网络的原理虽然理解起来不难,但是要是想实现它,还是需要做一些工作的,并且有很多细节性的东西需要注意。通过参阅各种相关资料,以及参考网络上已有的资源,自己写了一个含有一个隐含层,且只能有一个输出单元的简单的BP网络,经过测试,达到了预期的效果。需要说明的是,神经网络的每个输入都在0,1中,输出也在0,1中,在使用神经网络解决实际问题的时候,还需要对实际问题的输入输出进行归一化处理。另外,尽量不要使得神经网络的输入或输出接近于0或1,这样会影响拟合效果。我用正弦函数进行了一次测试,效果如图所示:以下是相关的代码:1.神经网络代码java v

2、iew plaincopy1. package pkg1;  2.   3. import java.util.Scanner;  4.   5. /* 6.  *  7.  */  8. public class TestNeuro   9.   10.     private int INPUT

3、_DIM=1;  11.     private int HIDDEN_DIM=20;  12.     private double LEARNING_RATE=0.05;  13.     double  input_hidden_weights=new doubleINPUT_DIMHIDDEN_DIM;  14. 

4、60;   double  hidden_output_weights=new doubleHIDDEN_DIM;  15.     double hidden_thresholds=new doubleHIDDEN_DIM;  16.     double output_threshold;  17.      &#

5、160;18.     public static void main(Stringargs)  19.         Scanner in=new Scanner(System.in);  20.         TestNeuro neuro=new TestNeuro(1,5);&

6、#160; 21.         neuro.initialize();  22.         for(int i=0;i<10000;i+)  23.             double input=new double1;

7、  24.             input0=Math.random();  25.             double expectedOutput=input0*input0;  26.        

8、0;    /System.out.println("input : "+input0+"ttexpectedOutput : "+expectedOutput);  27.             /System.out.println("predict before training :

9、0;"+neuro.predict(input);  28.             neuro.trainOnce(input, expectedOutput);  29.             /System.out.println("predict after 

10、training : "+neuro.predict(input);  30.             /in.next();  31.           32.         while(true)  3

11、3.             /neuro.printLinks();  34.             double input=new double1;  35.            

12、 input0=in.nextDouble();  36.             double expectedOutput=in.nextDouble();  37.             System.out.println("predict before tr

13、aining : "+neuro.predict(input);  38.             neuro.trainOnce(input, expectedOutput);  39.             System.out.println("predict&

14、#160;after training : "+neuro.predict(input);  40.               41.           42.       43.     

15、60; 44.     public TestNeuro(int input_dimension,int hidden_dimension)  45.         this.INPUT_DIM=input_dimension;  46.         this.HIDDEN_DIM=hidden_dimensio

16、n;  47.         this.initialize();  48.       49.       50.       51.     /* 52.      * 打印出本神经元

17、网络各层之间的连接权重,以及各个神经元上的阈值的信息。 53.      */  54.     void print()  55.         System.out.println("隐含层阈值:");  56.         for(int

18、 i=0;i<HIDDEN_DIM;i+)  57.             System.out.print(hidden_thresholdsi+" ");  58.         System.out.println();  59.     &#

19、160;   System.out.println("输出层阈值:");  60.         System.out.println(output_threshold);  61.           62.         System.out

20、.println("连接权重:*");  63.         System.out.println("输入层与隐含层的连接");  64.         for(int i=0;i<INPUT_DIM;i+)  65.         &

21、#160;   for(int j=0;j<HIDDEN_DIM;j+)  66.                 System.out.print(input_hidden_weightsij+" ");  67.          

22、;   System.out.println();  68.           69.         System.out.println("隐含层到输出层的连接");  70.         for(int i=0;i<HID

23、DEN_DIM;i+)  71.             System.out.print(hidden_output_weightsi+" ");  72.         System.out.println();  73.       

24、0; System.out.println("*");  74.       75.       76.     /* 77.      * 初始化,对所有的权值产生一个(0,1)之间的随机double型值 78.      */  79.

25、     void initialize()  80.           81.         /输入层到隐含层的连接权重  82.         for(int i=0;i<INPUT_DIM;i+)  8

26、3.             for(int j=0;j<HIDDEN_DIM;j+)  84.                 input_hidden_weightsij=Math.random();  85.     

27、          86.           87.         /隐含层到输出层的连接权重  88.         for(int i=0;i<HIDDEN_DIM;i+)  

28、;89.             hidden_output_weightsi=Math.random();  90.           91.         /隐含层的阈值  92.       

29、;  for(int i=0;i<HIDDEN_DIM;i+)  93.             hidden_thresholdsi=Math.random();  94.           95.         /输

30、出层的阈值  96.         output_threshold=Math.random();  97.       98.       99.     /* 100.      * 激励函数 101.   

31、60;  * param x 102.      * return 103.      */  104.     double function(double x)  105.         return 1/(1+Math.pow(Ma

32、th.E, -x);  106.       107.       108.     /* 109.      * 给定一个输入,进行预测 110.      * param input 111.     &#

33、160;* return 112.      */  113.     double predict(doubleinput)  114.         double hiddenValues=new doubleHIDDEN_DIM;  115.      

34、0;  for(int i=0;i<hiddenValues.length;i+)  116.             double sum=0;  117.             for(int j=0;j<input.length;j+) &

35、#160;118.                 sum+=inputj*input_hidden_weightsji;  119.               120.          

36、;   sum+=hidden_thresholdsi;/再加上本神经元的阈值  121.             hiddenValuesi=function(sum);  122.           123.         

37、;  124.           125.         double sum=0;  126.         for(int i=0;i<HIDDEN_DIM;i+)  127.      

38、60;      sum+=hiddenValuesi*hidden_output_weightsi;  128.           129.         sum+=output_threshold;/输出层神经元的阈值  130.       

39、0; return function(sum);  131.       132.       133.     /* 134.      * 进行一次训练 135.      * param input 136.   

40、   * param expectedOutput 137.      */  138.     void trainOnce(double input, double expectedOutput)  139.         double hiddenValues=new&#

41、160;doubleHIDDEN_DIM;  140.         double hiddenParams=new doubleHIDDEN_DIM;  141.           142.         for(int i=0;i<hiddenValue

42、s.length;i+)  143.             double sum=0;  144.             for(int j=0;j<input.length;j+)  145.       &

43、#160;         sum+=inputj*input_hidden_weightsji;  146.               147.             sum+=hidden_thresholdsi;/

44、60; 148.             hiddenValuesi=function(sum);  149.             hiddenParamsi=sum;  150.           15

45、1.           152.         double sum=0;  153.         for(int i=0;i<HIDDEN_DIM;i+)  154.         

46、    sum+=hiddenValuesi*hidden_output_weightsi;  155.           156.         sum+=output_threshold;/  157.         double outp

47、utValue=function(sum);  158.         double outputParam=sum;  159.         /System.out.println("实际输出");  160.           161.

48、        /* 162.          * 调整权值和阈值 163.          */  164.           165.    

49、0;    for(int i=0;i<input.length;i+)  166.             double factor=(expectedOutput-outputValue)*outputValue*(1-outputValue)*LEARNING_RATE*inputi;  167.      

50、60;      for(int j=0;j<HIDDEN_DIM;j+)  168.                 double delta=factor*hidden_output_weightsj*hiddenValuesj*(1-hiddenValuesj);  169.   

51、60;             /System.out.println("输入层到隐含层连接的权重调整:delta = "+delta+"tt weight = "+input_hidden_weightsij);  170.            

52、     input_hidden_weightsij+=delta;  171.               172.           173.         double factor=(e

53、xpectedOutput-outputValue)*outputValue*(1-outputValue)*LEARNING_RATE;  174.         for(int i=0;i<hidden_thresholds.length;i+)  175.             double delta=factor*hi

54、dden_output_weightsi*hiddenValuesi*(1-hiddenValuesi);  176.             hidden_thresholdsi+=delta;  177.           178.         &#

55、160; 179.         /System.out.println("hidden_output_weights : "+hidden_output_weights.length);  180.         for(int i=0;i<hidden_output_weights.length;i+)  181. 

56、0;           /w+=(exp-act)*df/dw  182.             /df/dw=x(1-x)*hiddenj  183.             double de

57、lta=factor*hiddenValuesi;  184.             /System.out.println("隐含层到输出层连接的权值调整:delta = "+delta+"tt weight = "+hidden_output_weightsi);  185.      

58、60;      hidden_output_weightsi+=delta;  186.               187.           188.         double 

59、delta=(expectedOutput-outputValue)*outputValue*(1-outputValue)*LEARNING_RATE;  189.         output_threshold+=delta;  190.         if(Math.abs(outputValue-expectedOutput)>0.1)  191.  

60、;           /System.out.println(input0+"tt"+outputValue+"tt"+expectedOutput);  192.           193.           194. &

61、#160; 195.           196.       197.       198.   2.测试代码java view plaincopy1. package pkg1;  2.   3. import java.awt.Graphics;  4.

62、 import java.util.Scanner;  5.   6. import javax.swing.JFrame;  7.   8. public class DisplayNeuro extends javax.swing.JPanel  9.   10.     public static final int SIDE_LE

63、NGTH=200;  11.       12.     TestNeuro neuro;/=new TestNeuro();  13.     /* 14.      * param args 15.      */  16.  

64、;   public static void main(String args)   17.         / TODO Auto-generated method stub  18.         DisplayNeuro dn=new DisplayN

65、euro();  19.         JFrame jFrame=new JFrame();  20.         jFrame.setBounds(100, 100, 300, 300);  21.         jFrame.setDe

66、faultCloseOperation(JFrame.EXIT_ON_CLOSE);  22.         jFrame.add(dn);  23.         jFrame.setVisible(true);  24.           25.   

67、;      TestNeuro neuro=new TestNeuro(1,20);  26.         dn.neuro=neuro;  27.         Scanner in=new Scanner(System.in);  28.   &#

68、160;     dn.repaint();  29.         for(int i=0;i<100000000;i+)  30.             double input=new double1;  31.   

69、60;         input0=Math.random()/2+0.25;  32.             double expectedOutput=(Math.sin(3.14*(input0-0.25)*2*4)+1)/8*3+0.125;  33.       &

70、#160;     /System.out.println("input : "+input0+"ttexpectedOutput : "+expectedOutput);  34.             /System.out.println("predict before training&#

71、160;: "+neuro.predict(input);  35.             neuro.trainOnce(input, expectedOutput);  36.             /System.out.println("predict after training : "+neuro.predict(input);  37.               38.      &#

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论