Example #1
0
  public static NeuralDataSet generateTraining() {
    NeuralDataSet result = new BasicNeuralDataSet();
    for (int i = 0; i < DIGITS.length; i++) {
      BasicNeuralData ideal = new BasicNeuralData(DIGITS.length);

      // setup input
      NeuralData input = image2data(DIGITS[i]);

      // setup ideal
      for (int j = 0; j < DIGITS.length; j++) {
        if (j == i) ideal.setData(j, 1);
        else ideal.setData(j, -1);
      }

      // add training element
      result.add(input, ideal);
    }
    return result;
  }
Example #2
0
  /** Run method for the background training thread. */
  public void run() {
    try {
      final int inputNeuron = OCR.DOWNSAMPLE_HEIGHT * OCR.DOWNSAMPLE_WIDTH;
      final int outputNeuron = this.letterListModel.size();

      final NeuralDataSet trainingSet = new BasicNeuralDataSet();
      for (int t = 0; t < this.letterListModel.size(); t++) {
        final NeuralData item = new BasicNeuralData(inputNeuron);
        int idx = 0;
        final SampleData ds = (SampleData) this.letterListModel.getElementAt(t);
        for (int y = 0; y < ds.getHeight(); y++) {
          for (int x = 0; x < ds.getWidth(); x++) {
            item.setData(idx++, ds.getData(x, y) ? .5 : -.5);
          }
        }

        trainingSet.add(new BasicNeuralDataPair(item, null));
      }

      this.net = new BasicNetwork();
      this.net.addLayer(new BasicLayer(new ActivationLinear(), false, inputNeuron));
      this.net.addLayer(new BasicLayer(new ActivationLinear(), false, outputNeuron));
      this.net.getStructure().finalizeStructure();
      this.net.reset();

      final CompetitiveTraining train =
          new CompetitiveTraining(
              this.net, 0.25, trainingSet, new NeighborhoodGaussian(new GaussianFunction(0, 1, 2)));

      int tries = 1;
      while (!this.halt) {
        train.iteration();
        update(tries++, train.getError());
      }

      markStopped();
      this.halt = false;

    } catch (final Exception e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(this, "Error: " + e, "Training", JOptionPane.ERROR_MESSAGE);
    }
  }