示例#1
0
  public static void main(String[] args) throws IOException, Exception {
    BasicConfigurator.configure();

    if (args.length < 6) {
      System.err.println(SYNOPSIS);
      System.exit(1);
    }

    String inf = null;
    String ouf = null;
    String lif = null;
    String inDir = null;

    int ufv = 0;

    boolean savePartialEstimates = false;

    // number of iterations
    int n = 10;

    // number of cores
    int c = Runtime.getRuntime().availableProcessors();

    Density.Flags flags = Flags.fAllParams;

    for (int i = 0; i < args.length; ++i) {
      if (args[i].equals("-i")) inf = args[++i];
      else if (args[i].equals("-o")) ouf = args[++i];
      else if (args[i].equals("-p")) {
        int tc = Integer.parseInt(args[++i]);
        //				if (tc > c)
        //					throw new RuntimeException("too many cores requested!");
        if (tc > 0) c = tc;
      } else if (args[i].equals("-s")) c = -1;
      else if (args[i].equals("-n")) n = Integer.parseInt(args[++i]);
      else if (args[i].equals("-l")) lif = args[++i];
      else if (args[i].equals("-d")) inDir = args[++i];
      else if (args[i].equals("--update")) {
        String arg = args[++i].toLowerCase();
        flags = new Density.Flags(arg.contains("w"), arg.contains("m"), arg.contains("v"));
      } else if (args[i].equals("--save-partial-estimates")) {
        savePartialEstimates = true;
      } else if (args[i].equals("--ufv")) {
        ufv = Integer.parseInt(args[++i]);

      } else {
        System.err.println("Unknown argument: " + args[i]);
        System.exit(-1);
      }
    }

    if (inf == null) {
      System.err.println("no input file specified");
      System.exit(1);
    }

    if (ouf == null) {
      System.err.println("no output file specified");
      System.exit(1);
    }

    if (lif == null) {
      System.err.println("no list file specified");
      System.exit(1);
    }

    logger.info("Reading from " + inf + "...");
    Mixture initial, estimate;
    initial = Mixture.readFromFile(new File(inf));

    if (savePartialEstimates) initial.writeToFile(new File(ouf + ".0"));

    if (c == -1) {
      logger.info("Caching feature data...");
      LinkedList<Sample> data = new LinkedList<Sample>();
      ChunkedDataSet set = new ChunkedDataSet(new File(lif), inDir, ufv);
      ChunkedDataSet.Chunk chunk;
      while ((chunk = set.nextChunk()) != null) {
        FrameInputStream r = chunk.getFrameReader();
        double[] buf = new double[r.getFrameSize()];
        while (r.read(buf)) data.add(new Sample((short) 0, buf));
      }
      logger.info(data.size() + " samples cached");

      logger.info("Starting " + n + " EM iterations: single-core, cached data");

      estimate = initial;
      for (int i = 0; i < n; ++i) {
        estimate = Trainer.em(estimate, data);
        if (savePartialEstimates) estimate.writeToFile(new File(ouf + "." + (i + 1)));
      }
    } else {
      logger.info("Starting " + n + " EM iterations on " + c + " cores");

      ParallelEM pem =
          new ParallelEM(initial, new ChunkedDataSet(new File(lif), inDir, ufv), c, flags);

      for (int i = 0; i < n; ++i) {
        pem.iterate();
        if (savePartialEstimates) pem.current.writeToFile(new File(ouf + "." + (i + 1)));
      }
      estimate = pem.current;
    }

    logger.info("Saving new estimate...");
    estimate.writeToFile(new File(ouf));
  }