Ejemplo n.º 1
0
  public void testFeedforwardPersist() throws Throwable {
    NeuralDataSet trainingData = new BasicNeuralDataSet(XOR.XOR_INPUT, XOR.XOR_IDEAL);

    BasicNetwork network = createNetwork();
    Train train = new Backpropagation(network, trainingData, 0.7, 0.9);

    for (int i = 0; i < 5000; i++) {
      train.iteration();
      network = (BasicNetwork) train.getNetwork();
    }

    TestCase.assertTrue("Error too high for backpropagation", train.getError() < 0.1);
    TestCase.assertTrue("XOR outputs not correct", XOR.verifyXOR(network, 0.1));

    EncogPersistedCollection encog = new EncogPersistedCollection();
    encog.add(network);
    encog.save("encogtest.xml");

    EncogPersistedCollection encog2 = new EncogPersistedCollection();
    encog2.load("encogtest.xml");
    new File("encogtest.xml").delete();

    BasicNetwork n = (BasicNetwork) encog2.getList().get(0);
    TestCase.assertTrue("Error too high for load", n.calculateError(trainingData) < 0.1);
  }
  private BasicNetwork trainNetwork(
      BasicNetwork network,
      NeuralDataSet trainingSet,
      double learningRate,
      double momentum,
      double maxError,
      int maxIteration) {
    final Train train = new Backpropagation(network, trainingSet, learningRate, momentum);

    int epoch = 1;

    do {
      train.iteration();
      epoch++;
    } while ((epoch < maxIteration) && (train.getError() > maxError));

    return (BasicNetwork) train.getNetwork();
  }