2017-09-19 1 views
3

Ma sortie est très mauvaise. Réel n'est pas égal idéal. Quels codes ont tort?Comment utiliser la méthode d'apprentissage Java Encog with GA pour résoudre XOR?

Ma sortie:

Epoch #129 Error:8.755514431853456E-6 

Neural Network Results: 
0.0,0.0, actual=0.57600,ideal=0.0 
1.0,0.0, actual=0.58016,ideal=1.0 
0.0,1.0, actual=0.58886,ideal=1.0 
1.0,1.0, actual=0.59317,ideal=0.0 

Voici mon code:

public class XOR { 

    public static double XOR_INPUT[][] = { { 0.0, 0.0 }, { 1.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 1.0 } }; 

    /** 
    * The ideal data necessary for XOR. 
    */ 
    public static double XOR_IDEAL[][] = { { 0.0 }, { 1.0 }, { 1.0 }, { 0.0 } }; 

    //public static BasicNetwork network = new BasicNetwork(); 
    public static BasicNetwork createNetwork() 
    { 
     // create a neural network, without using a factory 
     BasicNetwork network = new BasicNetwork(); 
     network.addLayer(new BasicLayer(null,true,2)); 
     network.addLayer(new BasicLayer(new ActivationSigmoid(),true,3)); 
     network.addLayer(new BasicLayer(new ActivationSigmoid(),false,1)); 
     network.getStructure().finalizeStructure(); 
     network.reset(); 
     return network; 
    } 

    /** 
    * The main method. 
    * @param args No arguments are used. 
    */ 
    public static void main(final String args[]) { 
     BasicNetwork network = createNetwork(); 
     MLDataSet trainingSet = new BasicMLDataSet(XOR_INPUT, XOR_IDEAL); 

     // train the neural network 
     CalculateScore score = new TrainingSetScore(trainingSet); 
     MLTrain train = new MLMethodGeneticAlgorithm(new MethodFactory(){ 
      @Override 
      public MLMethod factor() { 
       final BasicNetwork result = createNetwork();     
       ((MLResettable)result).reset(); 
       return result; 
      }}, score, 500);   

     int epoch = 1; 

     do { 
      train.iteration();;   
      System.out 
        .println("Epoch #" + epoch + " Error:" + train.getError()); 
      epoch++;    
     } while(train.getError() > 0.01); 

     // test the neural network 
     System.out.println("Neural Network Results:"); 
     for(MLDataPair pair: trainingSet) { 
      final MLData output = network.compute(pair.getInput()); 
      System.out.println(pair.getInput().getData(0) + "," + pair.getInput().getData(1) 
        + ", actual=" + output.getData(0) + ",ideal=" + pair.getIdeal().getData(0)); 
     } 
    } 
} 

Répondre

1

Le formateur de l'algorithme génétique est un peu différent que d'autres formateurs dans Encog. Il construit une population de réseaux de neurones. Le réseau de neurones que vous avez traversé est juste un modèle du nombre de couches cachées que vous avez et de ce à quoi ressemblent les couches d'entrée/sortie. Ce réseau de modèles n'est pas réellement modifié par la formation, seulement la population. Une fois que vous avez terminé la formation, vous devez obtenir le meilleur réseau de neurones de la population. Il y a plusieurs façons de le faire, mais le plus simple est simplement d'appeler train.getMethod(). Ajoutez la ligne suivante à votre code et cela fonctionnera:

} while(train.getError() > 0.01); 

    network = (BasicNetwork)train.getMethod(); // Add this 
    // test the neural network 
+0

Je travaille très bien. Merci beaucoup. – icashwave