JOONE(Java Object-Oriented Network Engine)使用初探

  1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
  2 InBlock.gif * JOONE - Java Object Oriented Neural Engine
  3 InBlock.gif *  http://joone.sourceforge.net
  4 InBlock.gif *
  5 InBlock.gif * XOR_using_NeuralNet.java
  6 InBlock.gif *
  7 ExpandedBlockEnd.gif  */

  8 None.gif package  study;
  9 None.gif
 10 None.gif import  org.joone.engine. * ;
 11 None.gif import  org.joone.engine.learning. * ;
 12 None.gif import  org.joone.io. * ;
 13 None.gif import  org.joone.net. * ;
 14 None.gif import  java.util.Vector;
 15 None.gif
 16 ExpandedBlockStart.gifContractedBlock.gif public   class  XOR_using_NeuralNet  implements  NeuralNetListener  dot.gif {
 17 InBlock.gif     private  NeuralNet            nnet  =   null ;
 18 InBlock.gif     private  MemoryInputSynapse  inputSynapse, desiredOutputSynapse;
 19 InBlock.gif     private  MemoryOutputSynapse outputSynapse;
 20 InBlock.gif    LinearLayer    input;
 21 InBlock.gif    SigmoidLayer hidden, output;
 22 InBlock.gif     boolean  singleThreadMode  =   true ;
 23 InBlock.gif    
 24 InBlock.gif     //  XOR input
 25 ExpandedSubBlockStart.gifContractedSubBlock.gif      private   double [][]            inputArray  =   new   double [][]  dot.gif {
 26 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif { 0.0 0.0 } ,
 27 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif { 0.0 1.0 } ,
 28 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif { 1.0 0.0 } ,
 29 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif { 1.0 1.0 }
 30 ExpandedSubBlockEnd.gif    }
;
 31 InBlock.gif    
 32 InBlock.gif     //  XOR desired output
 33 ExpandedSubBlockStart.gifContractedSubBlock.gif      private   double [][]            desiredOutputArray  =   new   double [][]  dot.gif {
 34 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif { 0.0 } ,
 35 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif { 1.0 } ,
 36 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif { 1.0 } ,
 37 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif { 0.0 }
 38 ExpandedSubBlockEnd.gif    }
;
 39 InBlock.gif    
 40 ExpandedSubBlockStart.gifContractedSubBlock.gif     /** */ /**
 41 InBlock.gif     *  @param  args the command line arguments
 42 ExpandedSubBlockEnd.gif      */

 43 ExpandedSubBlockStart.gifContractedSubBlock.gif     public   static   void  main(String args[])  dot.gif {
 44 InBlock.gif        XOR_using_NeuralNet xor  =   new  XOR_using_NeuralNet();
 45 InBlock.gif        
 46 InBlock.gif        xor.initNeuralNet();
 47 InBlock.gif        xor.train();
 48 InBlock.gif        xor.interrogate();
 49 ExpandedSubBlockEnd.gif    }

 50 InBlock.gif    
 51 ExpandedSubBlockStart.gifContractedSubBlock.gif     /** */ /**
 52 InBlock.gif     * Method declaration
 53 ExpandedSubBlockEnd.gif      */

 54 ExpandedSubBlockStart.gifContractedSubBlock.gif     public   void  train()  dot.gif {
 55 InBlock.gif        
 56 InBlock.gif         //  set the inputs
 57 InBlock.gif         inputSynapse.setInputArray(inputArray);
 58 InBlock.gif        inputSynapse.setAdvancedColumnSelector( " 1,2 " );
 59 InBlock.gif         //  set the desired outputs
 60 InBlock.gif         desiredOutputSynapse.setInputArray(desiredOutputArray);
 61 InBlock.gif        desiredOutputSynapse.setAdvancedColumnSelector( " 1 " );
 62 InBlock.gif        
 63 InBlock.gif         //  get the monitor object to train or feed forward
 64 InBlock.gif         Monitor monitor  =  nnet.getMonitor();
 65 InBlock.gif        
 66 InBlock.gif         //  set the monitor parameters
 67 InBlock.gif         monitor.setLearningRate( 0.8 );
 68 InBlock.gif        monitor.setMomentum( 0.3 );
 69 InBlock.gif        monitor.setTrainingPatterns(inputArray.length);
 70 InBlock.gif        monitor.setTotCicles( 5000 );
 71 InBlock.gif        monitor.setLearning( true );
 72 InBlock.gif        
 73 InBlock.gif         long  initms  =  System.currentTimeMillis();
 74 InBlock.gif         //  Run the network in single-thread, synchronized mode
 75 InBlock.gif         nnet.getMonitor().setSingleThreadMode(singleThreadMode);
 76 InBlock.gif        nnet.go( true );
 77 InBlock.gif        System.out.println( " Total time=  " + (System.currentTimeMillis()  -  initms) + "  ms " );
 78 ExpandedSubBlockEnd.gif    }

 79 InBlock.gif    
 80 ExpandedSubBlockStart.gifContractedSubBlock.gif     private   void  interrogate()  dot.gif {
 81 InBlock.gif         //  set the inputs
 82 InBlock.gif         inputSynapse.setInputArray(inputArray);
 83 InBlock.gif        inputSynapse.setAdvancedColumnSelector( " 1,2 " );
 84 InBlock.gif        Monitor monitor = nnet.getMonitor();
 85 InBlock.gif        monitor.setTrainingPatterns( 4 );
 86 InBlock.gif        monitor.setTotCicles( 1 );
 87 InBlock.gif        monitor.setLearning( false );
 88 InBlock.gif        MemoryOutputSynapse memOut  =   new  MemoryOutputSynapse();
 89 InBlock.gif         //  set the output synapse to write the output of the net
 90 InBlock.gif         
 91 ExpandedSubBlockStart.gifContractedSubBlock.gif         if (nnet != null dot.gif {
 92 InBlock.gif            nnet.addOutputSynapse(memOut);
 93 InBlock.gif            System.out.println(nnet.check());
 94 InBlock.gif            nnet.getMonitor().setSingleThreadMode(singleThreadMode);
 95 InBlock.gif            nnet.go();
 96 InBlock.gif            
 97 ExpandedSubBlockStart.gifContractedSubBlock.gif             for ( int  i = 0 ; i < 4 ; i ++ dot.gif {
 98 InBlock.gif                 double [] pattern  =  memOut.getNextPattern();
 99 InBlock.gif                System.out.println( " Output pattern # "   +  (i + 1 +   " = "   +  pattern[ 0 ]);
100 ExpandedSubBlockEnd.gif            }

101 InBlock.gif            System.out.println( " Interrogating Finished " );
102 ExpandedSubBlockEnd.gif        }

103 ExpandedSubBlockEnd.gif    }

104 InBlock.gif    
105 ExpandedSubBlockStart.gifContractedSubBlock.gif     /** */ /**
106 InBlock.gif     * Method declaration
107 ExpandedSubBlockEnd.gif      */

108 ExpandedSubBlockStart.gifContractedSubBlock.gif     protected   void  initNeuralNet()  dot.gif {
109 InBlock.gif        
110 InBlock.gif         //  First create the three layers
111 InBlock.gif         input  =   new  LinearLayer();
112 InBlock.gif        hidden  =   new  SigmoidLayer();
113 InBlock.gif        output  =   new  SigmoidLayer();
114 InBlock.gif        
115 InBlock.gif         //  set the dimensions of the layers
116 InBlock.gif         input.setRows( 2 );
117 InBlock.gif        hidden.setRows( 3 );
118 InBlock.gif        output.setRows( 1 );
119 InBlock.gif        
120 InBlock.gif        input.setLayerName( " L.input " );
121 InBlock.gif        hidden.setLayerName(

你可能感兴趣的:(技术学习)