Package cc.mallet.fst

Examples of cc.mallet.fst.CRFTrainerByLabelLikelihood


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

    CRF crf1 = new CRF(p.getDataAlphabet(), p.getTargetAlphabet());
    crf1.addFullyConnectedStatesForLabels();
    CRFTrainerByLabelLikelihood crft1 = new CRFTrainerByLabelLikelihood(
        crf1);
    crft1.trainIncremental(instances);

    CRF crf2 = new CRF(p.getDataAlphabet(), p.getTargetAlphabet());
    crf2.addFullyConnectedStatesForLabels();
    // Freeze some weights, before training
    for (int i = 0; i < crf2.getWeights().length; i += 2)
      crf2.freezeWeights(i);
    CRFTrainerByLabelLikelihood crft2 = new CRFTrainerByLabelLikelihood(
        crf2);
    crft2.trainIncremental(instances);

    SparseVector[] w = crf2.getWeights();
    double[] b = crf2.getDefaultWeights();
    for (int i = 0; i < w.length; i += 2) {
      assertEquals(0.0, b[i], 1e-10);
      for (int loc = 0; loc < w[i].numLocations(); loc++) {
        assertEquals(0.0, w[i].valueAtLocation(loc), 1e-10);
      }
    }

    // Check that the frozen weights has worse likelihood
    Optimizable.ByGradientValue optable1 = crft1
        .getOptimizableCRF(instances);
    Optimizable.ByGradientValue optable2 = crft2
        .getOptimizableCRF(instances);
    double val1 = optable1.getValue();
    double val2 = optable2.getValue();
    assertTrue(
        "Error: Freezing weights does not harm log-likelihood!  Full "
View Full Code Here


    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]);

    TokenAccuracyEvaluator eval = new TokenAccuracyEvaluator(lists,
        new String[] { "Train", "Test" });
    eval.evaluateInstanceList(crft, lists[1], "Test");
View Full Code Here

    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);
View Full Code Here

    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(
        crf2);
    double val2 = crft2.getOptimizableCRF(one).getValue();
    assertEquals(val1, val2, 1e-5);
  }
View Full Code Here

    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

    // 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
    int nParams2 = crft2.getOptimizableCRF(instances).getNumParameters();

    assertEquals(nParams2, nParams1 + 4);

  }
View Full Code Here

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

    CRF crf1 = new CRF(p, null);
    crf1.addFullyConnectedStatesForLabels();
    CRFTrainerByLabelLikelihood crft1 = new CRFTrainerByLabelLikelihood(
        crf1);
    crft1.train(instances, 10); // Let's get some parameters

    Instance inst = instances.get(0);
    Sequence input = (Sequence) inst.getData();
    SumLatticeDefault lattice = new SumLatticeDefault(crf1, input,
        (Sequence) inst.getTarget(), null, true);
View Full Code Here

    // don't know why -akm
    // 12/2007

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

    // Check that the notstart state is used at test time
    Sequence input = (Sequence) training.get(0).getData();
    Sequence output = new MaxLatticeDefault(crf, input)
        .bestOutputSequence();
View Full Code Here

TOP

Related Classes of cc.mallet.fst.CRFTrainerByLabelLikelihood

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.