Example #1
0
  public void train(int[] instanceLengths, String trainfile, File train_forest) throws IOException {

    // System.out.print("About to train. ");
    // System.out.print("Num Feats: " + pipe.dataAlphabet.size());

    int i = 0;
    for (i = 0; i < options.numIters; i++) {

      System.out.print(" Iteration " + i);
      // System.out.println("========================");
      // System.out.println("Iteration: " + i);
      // System.out.println("========================");
      System.out.print("[");

      long start = System.currentTimeMillis();

      trainingIter(instanceLengths, trainfile, train_forest, i + 1);

      long end = System.currentTimeMillis();
      // System.out.println("Training iter took: " + (end-start));
      System.out.println("|Time:" + (end - start) + "]");
    }

    params.averageParams(i * instanceLengths.length);
  }
Example #2
0
 public void loadModel(String file) throws Exception {
   ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
   params.parameters = (double[]) in.readObject();
   pipe.dataAlphabet = (Alphabet) in.readObject();
   pipe.typeAlphabet = (Alphabet) in.readObject();
   in.close();
   pipe.closeAlphabets();
 }
 public void loadModel(InputStream inputStream) throws IOException {
   try {
     ObjectInputStream is = new ObjectInputStream(inputStream);
     params.parameters = (double[]) is.readObject();
     pipe.dataAlphabet = (Alphabet) is.readObject();
     pipe.typeAlphabet = (Alphabet) is.readObject();
     pipe.closeAlphabets();
   } catch (ClassNotFoundException e) {
     IOException e2 = new IOException("Unable to load model: " + e.getMessage());
     e2.initCause(e);
     throw e2;
   }
 }
Example #4
0
  private void trainingIter(int[] instanceLengths, String trainfile, File train_forest, int iter)
      throws IOException {

    int numUpd = 0;
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(train_forest));
    boolean evaluateI = true;

    int numInstances = instanceLengths.length;

    for (int i = 0; i < numInstances; i++) {
      if ((i + 1) % 500 == 0) {
        System.out.print((i + 1) + ",");
        // System.out.println("  "+(i+1)+" instances");
      }

      int length = instanceLengths[i];

      // Get production crap.
      FeatureVector[][][] fvs = new FeatureVector[length][length][2];
      double[][][] probs = new double[length][length][2];
      FeatureVector[][][][] nt_fvs = new FeatureVector[length][pipe.types.length][2][2];
      double[][][][] nt_probs = new double[length][pipe.types.length][2][2];
      FeatureVector[][][] fvs_trips = new FeatureVector[length][length][length];
      double[][][] probs_trips = new double[length][length][length];
      FeatureVector[][][] fvs_sibs = new FeatureVector[length][length][2];
      double[][][] probs_sibs = new double[length][length][2];

      DependencyInstance inst;

      if (options.secondOrder) {
        inst =
            ((DependencyPipe2O) pipe)
                .readInstance(
                    in,
                    length,
                    fvs,
                    probs,
                    fvs_trips,
                    probs_trips,
                    fvs_sibs,
                    probs_sibs,
                    nt_fvs,
                    nt_probs,
                    params);
      } else inst = pipe.readInstance(in, length, fvs, probs, nt_fvs, nt_probs, params);

      double upd =
          (double) (options.numIters * numInstances - (numInstances * (iter - 1) + (i + 1)) + 1);
      int K = options.trainK;
      Object[][] d = null;
      if (options.decodeType.equals("proj")) {
        if (options.secondOrder)
          d =
              ((DependencyDecoder2O) decoder)
                  .decodeProjective(
                      inst,
                      fvs,
                      probs,
                      fvs_trips,
                      probs_trips,
                      fvs_sibs,
                      probs_sibs,
                      nt_fvs,
                      nt_probs,
                      K);
        else d = decoder.decodeProjective(inst, fvs, probs, nt_fvs, nt_probs, K);
      }
      if (options.decodeType.equals("non-proj")) {
        if (options.secondOrder)
          d =
              ((DependencyDecoder2O) decoder)
                  .decodeNonProjective(
                      inst,
                      fvs,
                      probs,
                      fvs_trips,
                      probs_trips,
                      fvs_sibs,
                      probs_sibs,
                      nt_fvs,
                      nt_probs,
                      K);
        else d = decoder.decodeNonProjective(inst, fvs, probs, nt_fvs, nt_probs, K);
      }
      params.updateParamsMIRA(inst, d, upd);
    }

    // System.out.println("");
    // System.out.println("  "+numInstances+" instances");

    System.out.print(numInstances);

    in.close();
  }