Advertisement
Guest User

Untitled

a guest
Jun 7th, 2013
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package org.encog.examples.neural.xor;
  2.  
  3. import org.encog.Encog;
  4. import org.encog.engine.network.activation.ActivationSigmoid;
  5. import org.encog.ml.data.MLData;
  6. import org.encog.ml.data.MLDataPair;
  7. import org.encog.ml.data.MLDataSet;
  8. import org.encog.ml.data.basic.BasicMLDataSet;
  9. import org.encog.neural.networks.BasicNetwork;
  10. import org.encog.neural.networks.layers.BasicLayer;
  11. import org.encog.neural.networks.training.propagation.resilient.ResilientPropagation;
  12.  
  13. /**
  14.  * XOR: This example is essentially the "Hello World" of neural network
  15.  * programming.  This example shows how to construct an Encog neural
  16.  * network to predict the output from the XOR operator.  This example
  17.  * uses backpropagation to train the neural network.
  18.  *
  19.  * This example attempts to use a minimum of Encog features to create and
  20.  * train the neural network.  This allows you to see exactly what is going
  21.  * on.  For a more advanced example, that uses Encog factories, refer to
  22.  * the XORFactory example.
  23.  *
  24.  */
  25. public class XORHelloWorld {
  26.  
  27.     /**
  28.      * The input necessary for XOR.
  29.      */
  30.     public static double XOR_INPUT[][] = { { 0.0, 0.0 }, { 1.0, 0.0 },
  31.             { 0.0, 1.0 }, { 1.0, 1.0 } };
  32.  
  33.     /**
  34.      * The ideal data necessary for XOR.
  35.      */
  36.     public static double XOR_IDEAL[][] = { { 0.0 }, { 1.0 }, { 1.0 }, { 0.0 } };
  37.    
  38.     /**
  39.      * The main method.
  40.      * @param args No arguments are used.
  41.      */
  42.     public static void main(final String args[]) {
  43.        
  44.         // create a neural network, without using a factory
  45.         BasicNetwork network = new BasicNetwork();
  46.         network.addLayer(new BasicLayer(null,true,2));
  47.         network.addLayer(new BasicLayer(new ActivationSigmoid(),true,3));
  48.         network.addLayer(new BasicLayer(new ActivationSigmoid(),false,1));
  49.         network.getStructure().finalizeStructure();
  50.         network.reset();
  51.  
  52.         // create training data
  53.         MLDataSet trainingSet = new BasicMLDataSet(XOR_INPUT, XOR_IDEAL);
  54.        
  55.         // train the neural network
  56.         final ResilientPropagation train = new ResilientPropagation(network, trainingSet);
  57.  
  58.         int epoch = 1;
  59.  
  60.         do {
  61.             train.iteration();
  62.             System.out.println("Epoch #" + epoch + " Error:" + train.getError());
  63.             epoch++;
  64.         } while(train.getError() > 0.01);
  65.  
  66.         // test the neural network
  67.         System.out.println("Neural Network Results:");
  68.         for(MLDataPair pair: trainingSet ) {
  69.             final MLData output = network.compute(pair.getInput());
  70.             System.out.println(pair.getInput().getData(0) + "," + pair.getInput().getData(1)
  71.                     + ", actual=" + output.getData(0) + ",ideal=" + pair.getIdeal().getData(0));
  72.         }
  73.        
  74.         Encog.getInstance().shutdown();
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement