简体   繁体   中英

How to use the Java Encog with GA training method to solve XOR?

My output is very bad. Actual is not equal ideal. Which codes are wrong?

My output:

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

Here is my 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));
        }
    }
}

The genetic algorithm trainer is a little different than other trainers in Encog. It is building a population of neural networks. The neural network that you passed in is just a template of how many hidden layers you have and what the input/output layers look like. This template network is not actually modified by training, just the population. Once you are done training you need to obtain the top neural network in the population. There are several ways to do this, but the easiest is simply to call train.getMethod(). Add the following line to your code and it will work:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM