本文整理汇总了Java中cc.mallet.fst.CRFTrainerByLabelLikelihood.getOptimizableCRF方法的典型用法代码示例。如果您正苦于以下问题:Java CRFTrainerByLabelLikelihood.getOptimizableCRF方法的具体用法?Java CRFTrainerByLabelLikelihood.getOptimizableCRF怎么用?Java CRFTrainerByLabelLikelihood.getOptimizableCRF使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cc.mallet.fst.CRFTrainerByLabelLikelihood
的用法示例。
在下文中一共展示了CRFTrainerByLabelLikelihood.getOptimizableCRF方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetSetParameters
import cc.mallet.fst.CRFTrainerByLabelLikelihood; //导入方法依赖的package包/类
public void testGetSetParameters() {
int inputVocabSize = 100;
int numStates = 5;
Alphabet inputAlphabet = new Alphabet();
for (int i = 0; i < inputVocabSize; i++)
inputAlphabet.lookupIndex("feature" + i);
Alphabet outputAlphabet = new Alphabet();
CRF crf = new CRF(inputAlphabet, outputAlphabet);
String[] stateNames = new String[numStates];
for (int i = 0; i < numStates; i++)
stateNames[i] = "state" + i;
crf.addFullyConnectedStates(stateNames);
CRFTrainerByLabelLikelihood crft = new CRFTrainerByLabelLikelihood(crf);
Optimizable.ByGradientValue mcrf = crft
.getOptimizableCRF(new InstanceList(null));
TestOptimizable.testGetSetParameters(mcrf);
}
示例2: testPrint
import cc.mallet.fst.CRFTrainerByLabelLikelihood; //导入方法依赖的package包/类
public void testPrint() {
Pipe p = new SerialPipes(new Pipe[] {
new CharSequence2TokenSequence("."), new TokenText(),
new TestCRFTokenSequenceRemoveSpaces(),
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();
}
示例3: getLikelihood
import cc.mallet.fst.CRFTrainerByLabelLikelihood; //导入方法依赖的package包/类
double getLikelihood(CRF crf, InstanceList data) {
CRFTrainerByLabelLikelihood crft = new CRFTrainerByLabelLikelihood(crf);
Optimizable.ByGradientValue mcrf = crft.getOptimizableCRF(data);
// Do this elaborate thing so that crf.cachedValueStale is forced true
double[] params = new double[mcrf.getNumParameters()];
mcrf.getParameters(params);
mcrf.setParameters(params);
return mcrf.getValue();
}
示例4: testFrozenWeights
import cc.mallet.fst.CRFTrainerByLabelLikelihood; //导入方法依赖的package包/类
public void testFrozenWeights() {
Pipe p = makeSpacePredictionPipe();
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 "
+ val1 + ", Frozen " + val2, val1 > val2);
}
示例5: testCopyStatesAndWeights
import cc.mallet.fst.CRFTrainerByLabelLikelihood; //导入方法依赖的package包/类
public void testCopyStatesAndWeights() {
Pipe p = new SerialPipes(new Pipe[] {
new CharSequence2TokenSequence("."), new TokenText(),
new TestCRFTokenSequenceRemoveSpaces(),
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(
crf2);
double val2 = crft2.getOptimizableCRF(one).getValue();
assertEquals(val1, val2, 1e-5);
}