Neural network 如何使用javaencog和GA训练方法来解决XOR?

Neural network 如何使用javaencog和GA训练方法来解决XOR?,neural-network,encog,Neural Network,Encog,我的输出很差。现实并不等于理想。哪些代码是错误的 我的输出: 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 这是我的密码: public clas

我的输出很差。现实并不等于理想。哪些代码是错误的

我的输出:

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
这是我的密码:

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));
        }
    }
}

在Encog中,遗传算法训练器与其他训练器略有不同。它正在建立一个神经网络群体。您传入的神经网络只是一个模板,其中包含有多少隐藏层以及输入/输出层的外观。这个模板网络实际上并不是通过训练来修改的,只是通过人口。一旦你完成了训练,你需要在人群中获得顶级的神经网络。有几种方法可以做到这一点,但最简单的方法是调用train.getMethod()。将以下行添加到代码中,它将起作用:

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

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