Java source code)

Source: Internet
Author: User
  1. Package com. sweetop. test;
  2. Import java. Io. serializable;
  3. Import java. util. arrays;
  4. Import java. util. vector;
  5. Import org. joone. Engine. fullsynapse;
  6. Import org. joone. Engine. linearlayer;
  7. Import org. joone. Engine. Monitor;
  8. Import org. joone. Engine. neuralnetevent;
  9. Import org. joone. Engine. neuralnetlistener;
  10. Import org. joone. Engine. pattern;
  11. Import org. joone. Engine. sigmoidlayer;
  12. Import org. joone. Engine. Learning. teachingsynapse;
  13. Import org. joone. Io. fileinputsynapse;
  14. Import org. joone. Io. fileoutputsynapse;
  15. Import org. joone. Io. memoryinputsynapse;
  16. Import org. joone. Io. memoryoutputsynapse;
  17. Import org.joone.net. Neuralnet;
  18. Public class test implements neuralnetlistener, serializable {
  19. /**
  20. *
  21. */
  22. Private Static final long serialversionuid = 61750666816215273l;
  23. Private Neuralnet nNet = NULL;
  24. Private memoryinputsynapse inputsynapse, desiredoutputsynapse;
  25. Private memoryoutputsynapse outputsynapse;
  26. // XOR Input
  27. Private double [] [] inputarray = new double [] [] {1.0, 1.0 },
  28. {1.0, 2.0 },{ 2.0, 2.0 },{ 2.0, 3.0 },{ 1.0, 3.0 }};
  29. // XOR desired output
  30. Private double [] [] desiredoutputarray = new double [] [] {{ 2.0/10 },
  31. {3.0/10}, {4.0/10}, {5.0/10}, {4.0/10 }};
  32. Protected void initneuralnet (){
  33. // First create the Three Layers
  34. Linearlayer input = new linearlayer ();
  35. Sigmoidlayer hidden = new sigmoidlayer ();
  36. Sigmoidlayer output = new sigmoidlayer ();
  37. Input. setlayername ("input ");
  38. Hidden. setlayername ("hidden ");
  39. Output. setlayername ("output ");
  40. // Set the dimensions of the layers
  41. Input. setrows (2 );
  42. Hidden. setrows (3 );
  43. Output. setrows (1 );
  44. // Now create the two synapses
  45. Fullsynapse synapse_ih = new fullsynapse ();/* input-> hidden conn .*/
  46. Fullsynapse synapse_ho = new fullsynapse ();/* hidden-> Output Conn .*/
  47. // Connect the input layer whit the hidden layer
  48. Input. addoutputsynapse (synapse_ih );
  49. Hidden. addinputsynapse (synapse_ih );
  50. // Connect the hidden layer whit the output layer
  51. Hidden. addoutputsynapse (synapse_ho );
  52. Output. addinputsynapse (synapse_ho );
  53. // The input to the neural net
  54. Inputsynapse = new memoryinputsynapse ();
  55. Input. addinputsynapse (inputsynapse );
  56. // The output of the neural net
  57. Outputsynapse = new memoryoutputsynapse ();
  58. Output. addoutputsynapse (outputsynapse );
  59. // The trainer and its desired output
  60. Desiredoutputsynapse = new memoryinputsynapse ();
  61. Teachingsynapse trainer = new teachingsynapse ();
  62. Trainer. setdesired (desiredoutputsynapse );
  63. NNet = new Neuralnet ();
  64. NNet. addlayer (input, Neuralnet. input_layer );
  65. NNet. addlayer (hidden, Neuralnet. hidden_layer );
  66. NNet. addlayer (output, Neuralnet. output_layer );
  67. NNet. setteacher (trainer );
  68. Output. addoutputsynapse (trainer );
  69. Train ();
  70. // File
  71. }
  72. Public void test (double [] [] d ){
  73. Inputsynapse. setinputarray (d );
  74. Inputsynapse. setadvancedcolumnselector ("1, 2 ");
  75. NNet. getmonitor (). settotcicles (1 );
  76. NNet. Start ();
  77. NNet. getmonitor (). Go ();
  78. For (Object O: outputsynapse. getallpatterns ()){
  79. Pattern P = (pattern) O;
  80. System. Out. println (10 * P. getarray () [0]);
  81. }
  82. }
  83. Public void train (){
  84. // Set the inputs
  85. Inputsynapse. setinputarray (inputarray );
  86. Inputsynapse. setadvancedcolumnselector ("1, 2 ");
  87. // Set the desired outputs
  88. Desiredoutputsynapse. setinputarray (desiredoutputarray );
  89. Desiredoutputsynapse. setadvancedcolumnselector ("1 ");
  90. // Get the monitor object to train or feed forward
  91. Monitor = nNet. getmonitor ();
  92. // Set the monitor parameters
  93. Monitor. setlearningrate (0.8 );
  94. Monitor. setmomentum (0.3 );
  95. Monitor. settrainingpatterns (inputarray. Length );
  96. Monitor. settotcicles (100000 );
  97. Monitor. setlearning (true );
  98. NNet. addneuralnetlistener (this );
  99. NNet. Start ();
  100. NNet. getmonitor (). Go ();
  101. For (Object O: outputsynapse. getallpatterns ()){
  102. Pattern P = (pattern) O;
  103. System. Out. println (10 * P. getarray () [0]);
  104. }
  105. }
  106. Public void cicleterminated (neuralnetevent e ){
  107. }
  108. Public void errorchanged (neuralnetevent e ){
  109. }
  110. Public void netstarted (neuralnetevent e ){
  111. // Todo auto-generated method stub
  112. }
  113. Public void netstopped (neuralnetevent e ){
  114. // Todo auto-generated method stub
  115. }
  116. Public void netstoppederror (neuralnetevent E, string error ){
  117. }
  118. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.