Package cc.mallet.fst

Examples of cc.mallet.fst.CRF


    Pipe p2 = new TestCRF2String();

    InstanceList instances = new InstanceList(p);
    instances.addThruPipe(new ArrayIterator(data));
    InstanceList[] lists = instances.split(new double[] { .5, .5 });
    CRF crf = new CRF(p, p2);
    crf.addFullyConnectedStatesForLabels();
    crf.setWeightsDimensionAsIn(lists[0], false);
    CRFTrainerByStochasticGradient crft = new CRFTrainerByStochasticGradient(
        crf, 0.0001);
    System.out.println("Training Accuracy before training = "
        + crf.averageTokenAccuracy(lists[0]));
    System.out.println("Testing  Accuracy before training = "
        + crf.averageTokenAccuracy(lists[1]));
    System.out.println("Training...");
    // either fixed learning rate or selected on a sample
    crft.setLearningRateByLikelihood(lists[0]);
    // crft.setLearningRate(0.01);
    crft.train(lists[0], 100);
    crf.print();
    System.out.println("Training Accuracy after training = "
        + crf.averageTokenAccuracy(lists[0]));
    System.out.println("Testing  Accuracy after training = "
        + crf.averageTokenAccuracy(lists[1]));
  }
View Full Code Here


    // first do normal training for getting weights
    InstanceList instances = new InstanceList(p);
    instances.addThruPipe(new ArrayIterator(data));
    InstanceList[] lists = instances.split(new double[] { .5, .5 });
    CRF crf = new CRF(p, p2);
    crf.addFullyConnectedStatesForLabels();
    crf.setWeightsDimensionAsIn(lists[0], false);
    CRFTrainerByStochasticGradient crft = new CRFTrainerByStochasticGradient(
        crf, 0.0001);
    System.out.println("Training Accuracy before training = "
        + crf.averageTokenAccuracy(lists[0]));
    System.out.println("Testing  Accuracy before training = "
        + crf.averageTokenAccuracy(lists[1]));
    System.out.println("Training...");
    // either fixed learning rate or selected on a sample
    crft.setLearningRateByLikelihood(lists[0]);
    // crft.setLearningRate(0.01);
    crft.train(lists[0], 100);
    crf.print();
    System.out.println("Training Accuracy after training = "
        + crf.averageTokenAccuracy(lists[0]));
    System.out.println("Testing  Accuracy after training = "
        + crf.averageTokenAccuracy(lists[1]));

    // now check the speeds of SumLatticeDefault vs SumLatticeScaling
    long totalTimeDefault = 0, totalTimeScaling = 0;
    for (int iter = 0; iter < 10000; iter++) {
      for (int ii = 0; ii < lists[1].size(); ii++) {
View Full Code Here

    InstanceList instances = new InstanceList(p);
    instances.addThruPipe(new ArrayIterator(data));
    InstanceList[] lists = instances.split(new Random(777), new double[] {
        .5, .5 });

    CRF crf = new CRF(p.getDataAlphabet(), p.getTargetAlphabet());
    crf.addFullyConnectedStatesForLabels();
    CRFTrainerByLabelLikelihood crft = new CRFTrainerByLabelLikelihood(crf);
    crft.setUseSparseWeights(true);

    crft.trainIncremental(lists[0]);
View Full Code Here

    InstanceList training = new InstanceList (pipe);
    training.addThruPipe (new ArrayIterator (data0));
    InstanceList testing = new InstanceList (pipe);
    testing.addThruPipe (new ArrayIterator (data1));

    CRF crf = new CRF (pipe, null);
    crf.addFullyConnectedStatesForLabels ();
    CRFTrainerByLabelLikelihood crft = new CRFTrainerByLabelLikelihood (crf);
    crft.trainIncremental (training);

    CRFExtractor extor = hackCrfExtor (crf);
    Extraction extration = extor.extract (new ArrayIterator (data1));
View Full Code Here

        new TokenSequence2FeatureVectorSequence(),
        new PrintInputAndTarget(), });
    InstanceList one = new InstanceList(p);
    String[] data = new String[] { "ABCDE", };
    one.addThruPipe(new ArrayIterator(data));
    CRF crf = new CRF(p, null);
    crf.addFullyConnectedStatesForThreeQuarterLabels(one);
    CRFTrainerByLabelLikelihood crft = new CRFTrainerByLabelLikelihood(crf);
    crf.setWeightsDimensionAsIn(one, false);
    Optimizable mcrf = crft.getOptimizableCRF(one);
    double[] params = new double[mcrf.getNumParameters()];
    for (int i = 0; i < params.length; i++) {
      params[i] = i;
    }
    mcrf.setParameters(params);
    crf.print();
  }
View Full Code Here

    InstanceList training = new InstanceList (pipe);
    training.addThruPipe (new ArrayIterator (data0));
    InstanceList testing = new InstanceList (pipe);
    testing.addThruPipe (new ArrayIterator (data1));

    CRF crf = new CRF (pipe, null);
    crf.addFullyConnectedStatesForLabels ();
    CRFTrainerByLabelLikelihood crft = new CRFTrainerByLabelLikelihood (crf);
    TokenAccuracyEvaluator eval = new TokenAccuracyEvaluator (new InstanceList[] {training, testing}, new String[] {"Training", "Testing"});
    for (int i = 0; i < 5; i++) {
      crft.train (training, 1);
      eval.evaluate(crft);
View Full Code Here

        new TokenSequence2FeatureVectorSequence(),
        new PrintInputAndTarget(), });
    InstanceList one = new InstanceList(p);
    String[] data = new String[] { "ABCDE", };
    one.addThruPipe(new ArrayIterator(data));
    CRF crf = new CRF(p, null);
    crf.addFullyConnectedStatesForLabels();
    CRFTrainerByLabelLikelihood crft = new CRFTrainerByLabelLikelihood(crf);
    crf.setWeightsDimensionAsIn(one, false);
    Optimizable.ByGradientValue mcrf = crft.getOptimizableCRF(one);
    double[] params = new double[mcrf.getNumParameters()];
    for (int i = 0; i < params.length; i++) {
      params[i] = i;
    }
    mcrf.setParameters(params);

    StringWriter out = new StringWriter();
    crf.print(new PrintWriter(out, true));
    System.out.println("------------- CRF1 -------------");
    crf.print();

    // Make a copy of this CRF
    CRF crf2 = new CRF(crf);

    StringWriter out2 = new StringWriter();
    crf2.print(new PrintWriter(out2, true));
    System.out.println("------------- CRF2 -------------");
    crf2.print();

    assertEquals(out.toString(), out2.toString());

    double val1 = mcrf.getValue();
    CRFTrainerByLabelLikelihood crft2 = new CRFTrainerByLabelLikelihood(
View Full Code Here

    InstanceList data = new InstanceList(p);
    data.addThruPipe(new LineGroupIterator(new StringReader(toy), Pattern
        .compile("\n"), true));

    CRF crf = new CRF(p, null);
    crf.print();
    crf.addStatesForLabelsConnectedAsIn(data);
    crf.addStartState();
    CRFTrainerByLabelLikelihood crft = new CRFTrainerByLabelLikelihood(crf);

    Optimizable.ByGradientValue maxable = crft.getOptimizableCRF(data);
    assertEquals(-1.3862, maxable.getValue(), 1e-4);

    crf = new CRF(p, null);
    crf
        .addOrderNStates(data, new int[] { 1 }, null, "A", null, null,
            false);
    crf.print();
    crft = new CRFTrainerByLabelLikelihood(crf);

    maxable = crft.getOptimizableCRF(data);
    assertEquals(-3.09104245335831, maxable.getValue(), 1e-4);
  }
View Full Code Here

    InstanceList instances = new InstanceList(p);
    instances.addThruPipe(new ArrayIterator(data));

    // Test that dense observations wights aren't added for
    // "default-feature" edges.
    CRF crf1 = new CRF(p, null);
    crf1.addOrderNStates(instances, new int[] { 0 }, null, "start", null,
        null, true);
    CRFTrainerByLabelLikelihood crft1 = new CRFTrainerByLabelLikelihood(
        crf1);
    crft1.setUseSparseWeights(false);
    crft1.train(instances, 1); // Set weights dimension
    int nParams1 = crft1.getOptimizableCRF(instances).getNumParameters();

    CRF crf2 = new CRF(p, null);
    crf2.addOrderNStates(instances, new int[] { 0, 1 }, new boolean[] {
        false, true }, "start", null, null, true);
    CRFTrainerByLabelLikelihood crft2 = new CRFTrainerByLabelLikelihood(
        crf2);
    crft2.setUseSparseWeights(false);
    crft2.train(instances, 1); // Set weights dimension
View Full Code Here

  }
 
  public static CRF getCRF(InstanceList training, int[] orders, String defaultLabel, String forbidden, String allowed, boolean connected) {
    Pattern forbiddenPat = Pattern.compile(forbidden);
    Pattern allowedPat = Pattern.compile(allowed);
    CRF crf = new CRF(training.getPipe(), (Pipe)null);
    String startName = crf.addOrderNStates(training, orders, null,
        defaultLabel, forbiddenPat, allowedPat, connected);
    for (int i = 0; i < crf.numStates(); i++)
      crf.getState(i).setInitialWeight (Transducer.IMPOSSIBLE_WEIGHT);
    crf.getState(startName).setInitialWeight(0.0);
    crf.setWeightsDimensionDensely();
    return crf;
  }
View Full Code Here

TOP

Related Classes of cc.mallet.fst.CRF

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.