Example #1
0
  void develop(
      List<Instance> trn, List<Instance> tst, OnlineOptimizer op, int miniBatch, boolean sparse) {
    DoubleIntPair best = new DoubleIntPair(0d, 0);
    int yhat, count;
    float[] scores;
    String label;
    double acc;

    for (int epoch = 0; epoch < 50; epoch++) {
      // training
      count = 0;

      for (Instance instance : trn) {
        op.train(instance);

        if (++count == miniBatch) {
          op.updateMiniBatch();
          count = 0;
        }
      }

      if (count > 0) op.updateMiniBatch();

      // evaluating
      count = 0;

      for (Instance instance : tst) {
        scores = op.scores(instance.getFeatureVector());
        yhat = MLUtils.argmax(scores);
        label = op.getLabel(yhat);
        if (instance.isStringLabel(label)) count++;
      }

      acc = MathUtils.accuracy(count, tst.size());
      System.out.printf("%4d: %5.2f\n", epoch, acc);
      if (best.d < acc) best.set(acc, epoch);
    }

    System.out.printf("best: %5.2f at %d epoch\n", best.d, best.i);
  }