public CRFOptimizableByLabelLikelihood getOptimizableCRF (InstanceList trainingSet) { if (cachedWeightsStructureStamp != crf.weightsStructureChangeStamp) { if (!useNoWeights) { if (useSparseWeights) crf.setWeightsDimensionAsIn (trainingSet, useSomeUnsupportedTrick); else crf.setWeightsDimensionDensely (); } //reallocateSufficientStatistics(); // Not necessary here because it is done in the constructor for OptimizableCRF ocrf = null; cachedWeightsStructureStamp = crf.weightsStructureChangeStamp; } if (ocrf == null || ocrf.trainingSet != trainingSet) { //ocrf = new OptimizableCRF (crf, trainingSet); ocrf = new CRFOptimizableByLabelLikelihood(crf, trainingSet); ocrf.setGaussianPriorVariance(gaussianPriorVariance); ocrf.setHyperbolicPriorSharpness(hyperbolicPriorSharpness); ocrf.setHyperbolicPriorSlope(hyperbolicPriorSlope); ocrf.setUseHyperbolicPrior(usingHyperbolicPrior); opt = null; } return ocrf; }
/** Returns the log probability of the training sequence labels and the prior over parameters. */ public double getValue () { if (crf.weightsValueChangeStamp != cachedValueWeightsStamp) { // The cached value is not up to date; it was calculated for a different set of CRF weights. cachedValueWeightsStamp = crf.weightsValueChangeStamp; // cachedValue will soon no longer be stale long startingTime = System.currentTimeMillis(); //crf.print(); // Get the value of all the all the true labels, also filling in expectations at the same time. cachedValue = getExpectationValue (); // Incorporate prior on parameters if (usingHyperbolicPrior) // Hyperbolic prior cachedValue += crf.parameters.hyberbolicPrior(hyperbolicPriorSlope, hyperbolicPriorSharpness); else // Gaussian prior cachedValue += crf.parameters.gaussianPrior(gaussianPriorVariance); // gsc: make sure the prior gives a correct value assert(!(Double.isNaN(cachedValue) || Double.isInfinite(cachedValue))) : "Label likelihood is NaN/Infinite"; logger.info ("getValue() (loglikelihood, optimizable by label likelihood) = "+cachedValue); long endingTime = System.currentTimeMillis(); logger.fine ("Inference milliseconds = "+(endingTime - startingTime)); } return cachedValue; }
new CRFOptimizableByLabelLikelihood(crf, labeled); likelihood.setGaussianPriorVariance(gaussianPriorVariance); this.bfgs = new LimitedMemoryBFGS(likelihood); logger.info ("CRF about to train with "+numIterations+" iterations"); CRFOptimizableByLabelLikelihood likelihood = new CRFOptimizableByLabelLikelihood(crf, labeled); likelihood.setGaussianPriorVariance(gaussianPriorVariance); CRFOptimizableByEntropyRegularization regularization = new CRFOptimizableByEntropyRegularization(crf, unlabeled); regularization.setScalingFactor(this.entRegScalingFactor);
public Optimizable.ByGradientValue newCRFOptimizable (CRF crf, InstanceList trainingData) { return new CRFOptimizableByLabelLikelihood (crf, trainingData); } }
public CRFOptimizableByLabelLikelihood (CRF crf, InstanceList ilist) { // Set up this.crf = crf; this.trainingSet = ilist; //cachedGradient = new DenseVector (numParameters); cachedGradient = new double[crf.parameters.getNumFactors()]; constraints = new CRF.Factors(crf.parameters); expectations = new CRF.Factors(crf.parameters); // This resets and values that may have been in expectations and constraints //reallocateSufficientStatistics(); // This is unfortunately necessary, b/c cachedValue & cachedValueStale not in same place! cachedValueWeightsStamp = -1; cachedGradientWeightsStamp = -1; gatherConstraints (ilist); }
CRFTrainerByLabelLikelihood crft2 = new CRFTrainerByLabelLikelihood( crf2); double val2 = crft2.getOptimizableCRF(one).getValue(); assertEquals(val1, val2, 1e-5);
public void testDenseFeatureSelection() { Pipe p = makeSpacePredictionPipe(); 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 int nParams2 = crft2.getOptimizableCRF(instances).getNumParameters(); assertEquals(nParams2, nParams1 + 4); }
new CRFOptimizableByLabelLikelihood(crf, labeled); likelihood.setGaussianPriorVariance(gaussianPriorVariance); this.bfgs = new LimitedMemoryBFGS(likelihood); logger.info ("CRF about to train with "+numIterations+" iterations"); CRFOptimizableByLabelLikelihood likelihood = new CRFOptimizableByLabelLikelihood(crf, labeled); likelihood.setGaussianPriorVariance(gaussianPriorVariance); CRFOptimizableByEntropyRegularization regularization = new CRFOptimizableByEntropyRegularization(crf, unlabeled); regularization.setScalingFactor(this.entRegScalingFactor);
public Optimizable.ByGradientValue newCRFOptimizable (CRF crf, InstanceList trainingData) { return new CRFOptimizableByLabelLikelihood (crf, trainingData); } }
public CRFOptimizableByLabelLikelihood (CRF crf, InstanceList ilist) { // Set up this.crf = crf; this.trainingSet = ilist; //cachedGradient = new DenseVector (numParameters); cachedGradient = new double[crf.parameters.getNumFactors()]; constraints = new CRF.Factors(crf.parameters); expectations = new CRF.Factors(crf.parameters); // This resets and values that may have been in expectations and constraints //reallocateSufficientStatistics(); // This is unfortunately necessary, b/c cachedValue & cachedValueStale not in same place! cachedValueWeightsStamp = -1; cachedGradientWeightsStamp = -1; gatherConstraints (ilist); }
CRFTrainerByLabelLikelihood crft2 = new CRFTrainerByLabelLikelihood( crf2); double val2 = crft2.getOptimizableCRF(one).getValue(); assertEquals(val1, val2, 1e-5);
public void testDenseFeatureSelection() { Pipe p = makeSpacePredictionPipe(); 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 int nParams2 = crft2.getOptimizableCRF(instances).getNumParameters(); assertEquals(nParams2, nParams1 + 4); }
public CRFOptimizableByLabelLikelihood getOptimizableCRF (InstanceList trainingSet) { if (cachedWeightsStructureStamp != crf.weightsStructureChangeStamp) { if (!useNoWeights) { if (useSparseWeights) crf.setWeightsDimensionAsIn (trainingSet, useSomeUnsupportedTrick); else crf.setWeightsDimensionDensely (); } //reallocateSufficientStatistics(); // Not necessary here because it is done in the constructor for OptimizableCRF ocrf = null; cachedWeightsStructureStamp = crf.weightsStructureChangeStamp; } if (ocrf == null || ocrf.trainingSet != trainingSet) { //ocrf = new OptimizableCRF (crf, trainingSet); ocrf = new CRFOptimizableByLabelLikelihood(crf, trainingSet); ocrf.setGaussianPriorVariance(gaussianPriorVariance); ocrf.setHyperbolicPriorSharpness(hyperbolicPriorSharpness); ocrf.setHyperbolicPriorSlope(hyperbolicPriorSlope); ocrf.setUseHyperbolicPrior(usingHyperbolicPrior); opt = null; } return ocrf; }
new CRFOptimizableByLabelLikelihood(crf, labeled); likelihood.setGaussianPriorVariance(gaussianPriorVariance); this.bfgs = new LimitedMemoryBFGS(likelihood); logger.info ("CRF about to train with "+numIterations+" iterations"); CRFOptimizableByLabelLikelihood likelihood = new CRFOptimizableByLabelLikelihood(crf, labeled); likelihood.setGaussianPriorVariance(gaussianPriorVariance); CRFOptimizableByEntropyRegularization regularization = new CRFOptimizableByEntropyRegularization(crf, unlabeled); regularization.setScalingFactor(this.entRegScalingFactor);
public Optimizable.ByGradientValue newCRFOptimizable (CRF crf, InstanceList trainingData) { return new CRFOptimizableByLabelLikelihood (crf, trainingData); } }
public CRFOptimizableByLabelLikelihood (CRF crf, InstanceList ilist) { // Set up this.crf = crf; this.trainingSet = ilist; //cachedGradient = new DenseVector (numParameters); cachedGradient = new double[crf.parameters.getNumFactors()]; constraints = new CRF.Factors(crf.parameters); expectations = new CRF.Factors(crf.parameters); // This resets and values that may have been in expectations and constraints //reallocateSufficientStatistics(); // This is unfortunately necessary, b/c cachedValue & cachedValueStale not in same place! cachedValueWeightsStamp = -1; cachedGradientWeightsStamp = -1; gatherConstraints (ilist); }
/** Returns the log probability of the training sequence labels and the prior over parameters. */ public double getValue () { if (crf.weightsValueChangeStamp != cachedValueWeightsStamp) { // The cached value is not up to date; it was calculated for a different set of CRF weights. cachedValueWeightsStamp = crf.weightsValueChangeStamp; // cachedValue will soon no longer be stale long startingTime = System.currentTimeMillis(); //crf.print(); // Get the value of all the all the true labels, also filling in expectations at the same time. cachedValue = getExpectationValue (); // Incorporate prior on parameters if (usingHyperbolicPrior) // Hyperbolic prior cachedValue += crf.parameters.hyberbolicPrior(hyperbolicPriorSlope, hyperbolicPriorSharpness); else // Gaussian prior cachedValue += crf.parameters.gaussianPrior(gaussianPriorVariance); // gsc: make sure the prior gives a correct value assert(!(Double.isNaN(cachedValue) || Double.isInfinite(cachedValue))) : "Label likelihood is NaN/Infinite"; logger.info ("getValue() (loglikelihood, optimizable by label likelihood) = "+cachedValue); long endingTime = System.currentTimeMillis(); logger.fine ("Inference milliseconds = "+(endingTime - startingTime)); } return cachedValue; }