public void getBatchValueGradient(double[] buffer, int batchIndex, int[] batchAssignments) { assert(batchIndex < this.numBatches) : "Incorrect batch index: " + batchIndex + ", range(0, " + this.numBatches + ")"; assert(batchAssignments.length == 2 && batchAssignments[0] <= batchAssignments[1]) : "Invalid batch assignments: " + Arrays.toString(batchAssignments); CRF.Factors batchExpectations = expectations.get(batchIndex); if (batchIndex == numBatches-1) { // crf parameters' check has to be done only once, infinite values are allowed crf.parameters.assertNotNaN(); // factor the constraints and the prior into the expectations of last batch // Gradient = (constraints - expectations + prior) = -(expectations - constraints - prior) // The minus sign is factored in combineGradients method after all gradients are computed batchExpectations.plusEquals(constraints, -1.0); if (usingHyperbolicPrior) batchExpectations.plusEqualsHyperbolicPriorGradient(crf.parameters, -hyperbolicPriorSlope, hyperbolicPriorSharpness); else batchExpectations.plusEqualsGaussianPriorGradient(crf.parameters, -gaussianPriorVariance); batchExpectations.assertNotNaNOrInfinite(); } double[] gradient = cachedGradient.get(batchIndex); // set the cached gradient batchExpectations.getParameters(gradient); System.arraycopy(gradient, 0, buffer, 0, gradient.length); }
public void getBatchValueGradient(double[] buffer, int batchIndex, int[] batchAssignments) { assert(batchIndex < this.numBatches) : "Incorrect batch index: " + batchIndex + ", range(0, " + this.numBatches + ")"; assert(batchAssignments.length == 2 && batchAssignments[0] <= batchAssignments[1]) : "Invalid batch assignments: " + Arrays.toString(batchAssignments); CRF.Factors batchExpectations = expectations.get(batchIndex); if (batchIndex == numBatches-1) { // crf parameters' check has to be done only once, infinite values are allowed crf.parameters.assertNotNaN(); // factor the constraints and the prior into the expectations of last batch // Gradient = (constraints - expectations + prior) = -(expectations - constraints - prior) // The minus sign is factored in combineGradients method after all gradients are computed batchExpectations.plusEquals(constraints, -1.0); if (usingHyperbolicPrior) batchExpectations.plusEqualsHyperbolicPriorGradient(crf.parameters, -hyperbolicPriorSlope, hyperbolicPriorSharpness); else batchExpectations.plusEqualsGaussianPriorGradient(crf.parameters, -gaussianPriorVariance); batchExpectations.assertNotNaNOrInfinite(); } double[] gradient = cachedGradient.get(batchIndex); // set the cached gradient batchExpectations.getParameters(gradient); System.arraycopy(gradient, 0, buffer, 0, gradient.length); }
public void getBatchValueGradient(double[] buffer, int batchIndex, int[] batchAssignments) { assert(batchIndex < this.numBatches) : "Incorrect batch index: " + batchIndex + ", range(0, " + this.numBatches + ")"; assert(batchAssignments.length == 2 && batchAssignments[0] <= batchAssignments[1]) : "Invalid batch assignments: " + Arrays.toString(batchAssignments); CRF.Factors batchExpectations = expectations.get(batchIndex); if (batchIndex == numBatches-1) { // crf parameters' check has to be done only once, infinite values are allowed crf.parameters.assertNotNaN(); // factor the constraints and the prior into the expectations of last batch // Gradient = (constraints - expectations + prior) = -(expectations - constraints - prior) // The minus sign is factored in combineGradients method after all gradients are computed batchExpectations.plusEquals(constraints, -1.0); if (usingHyperbolicPrior) batchExpectations.plusEqualsHyperbolicPriorGradient(crf.parameters, -hyperbolicPriorSlope, hyperbolicPriorSharpness); else batchExpectations.plusEqualsGaussianPriorGradient(crf.parameters, -gaussianPriorVariance); batchExpectations.assertNotNaNOrInfinite(); } double[] gradient = cachedGradient.get(batchIndex); // set the cached gradient batchExpectations.getParameters(gradient); System.arraycopy(gradient, 0, buffer, 0, gradient.length); }
batchExpectations.assertNotNaNOrInfinite();
batchExpectations.assertNotNaNOrInfinite();
else expectations.plusEqualsGaussianPriorGradient(crf.parameters, -gaussianPriorVariance); expectations.assertNotNaNOrInfinite(); expectations.getParameters(cachedGradient); MatrixOps.timesEquals (cachedGradient, -1.0); // This implements the -(...) in the above comment
else expectations.plusEqualsGaussianPriorGradient(crf.parameters, -gaussianPriorVariance); expectations.assertNotNaNOrInfinite(); expectations.getParameters(cachedGradient); MatrixOps.timesEquals (cachedGradient, -1.0); // This implements the -(...) in the above comment
else expectations.plusEqualsGaussianPriorGradient(crf.parameters, -gaussianPriorVariance); expectations.assertNotNaNOrInfinite(); expectations.getParameters(cachedGradient); MatrixOps.timesEquals (cachedGradient, -1.0); // This implements the -(...) in the above comment
/** * Set the constraints by running forward-backward with the <i>output label * sequence provided</i>, thus restricting it to only those paths that agree with * the label sequence. */ protected void gatherConstraints(InstanceList ilist) { logger.info("Gathering constraints..."); assert (constraints.structureMatches(crf.parameters)); constraints.zero(); for (Instance instance : ilist) { FeatureVectorSequence input = (FeatureVectorSequence) instance.getData(); FeatureSequence output = (FeatureSequence) instance.getTarget(); double instanceWeight = ilist.getInstanceWeight(instance); Transducer.Incrementor incrementor = instanceWeight == 1.0 ? constraints.new Incrementor() : constraints.new WeightedIncrementor(instanceWeight); new SumLatticeDefault (this.crf, input, output, incrementor); } constraints.assertNotNaNOrInfinite(); }
/** * Set the constraints by running forward-backward with the <i>output label * sequence provided</i>, thus restricting it to only those paths that agree with * the label sequence. */ protected void gatherConstraints(InstanceList ilist) { logger.info("Gathering constraints..."); assert (constraints.structureMatches(crf.parameters)); constraints.zero(); for (Instance instance : ilist) { FeatureVectorSequence input = (FeatureVectorSequence) instance.getData(); FeatureSequence output = (FeatureSequence) instance.getTarget(); double instanceWeight = ilist.getInstanceWeight(instance); Transducer.Incrementor incrementor = instanceWeight == 1.0 ? constraints.new Incrementor() : constraints.new WeightedIncrementor(instanceWeight); new SumLatticeDefault (this.crf, input, output, incrementor); } constraints.assertNotNaNOrInfinite(); }
/** * Set the constraints by running forward-backward with the <i>output label * sequence provided</i>, thus restricting it to only those paths that agree with * the label sequence. */ protected void gatherConstraints(InstanceList ilist) { logger.info("Gathering constraints..."); assert (constraints.structureMatches(crf.parameters)); constraints.zero(); for (Instance instance : ilist) { FeatureVectorSequence input = (FeatureVectorSequence) instance.getData(); FeatureSequence output = (FeatureSequence) instance.getTarget(); double instanceWeight = ilist.getInstanceWeight(instance); Transducer.Incrementor incrementor = instanceWeight == 1.0 ? constraints.new Incrementor() : constraints.new WeightedIncrementor(instanceWeight); new SumLatticeDefault (this.crf, input, output, incrementor); } constraints.assertNotNaNOrInfinite(); }
private void assertNotNaNOrInfinite () { // crf.parameters are allowed to have infinite values crf.parameters.assertNotNaN(); expectations.assertNotNaNOrInfinite(); constraints.assertNotNaNOrInfinite(); }
private void assertNotNaNOrInfinite () { // crf.parameters are allowed to have infinite values crf.parameters.assertNotNaN(); expectations.assertNotNaNOrInfinite(); constraints.assertNotNaNOrInfinite(); }
private void assertNotNaNOrInfinite () { // crf.parameters are allowed to have infinite values crf.parameters.assertNotNaN(); expectations.assertNotNaNOrInfinite(); constraints.assertNotNaNOrInfinite(); }
public void getValueGradient(double[] buffer) { if (cachedGradientWeightsStamp != crf.getWeightsValueChangeStamp()) { cachedGradientWeightsStamp = crf.getWeightsValueChangeStamp(); getValue(); expectations.plusEquals(constraints, -1.0); expectations.plusEqualsGaussianPriorGradient(crf.getParameters(), -gaussianPriorVariance); expectations.assertNotNaNOrInfinite(); expectations.getParameters(cachedGradient); MatrixOps.timesEquals(cachedGradient, -weight); } System.arraycopy(cachedGradient, 0, buffer, 0, cachedGradient.length); }
public void getValueGradient(double[] buffer) { if (cachedGradientWeightsStamp != crf.getWeightsValueChangeStamp()) { cachedGradientWeightsStamp = crf.getWeightsValueChangeStamp(); // cachedGradient will soon no longer be stale getValue(); // if this fails then look in computeExpectations expectations.assertNotNaNOrInfinite(); // fill up gradient expectations.getParameters(cachedGradient); } System.arraycopy(cachedGradient, 0, buffer, 0, cachedGradient.length); }
public void getValueGradient(double[] buffer) { if (cachedGradientWeightsStamp != crf.getWeightsValueChangeStamp()) { cachedGradientWeightsStamp = crf.getWeightsValueChangeStamp(); getValue(); expectations.plusEquals(constraints, -1.0); expectations.plusEqualsGaussianPriorGradient(crf.getParameters(), -gaussianPriorVariance); expectations.assertNotNaNOrInfinite(); expectations.getParameters(cachedGradient); MatrixOps.timesEquals(cachedGradient, -weight); } System.arraycopy(cachedGradient, 0, buffer, 0, cachedGradient.length); }
public void getValueGradient(double[] buffer) { if (cachedGradientWeightsStamp != crf.getWeightsValueChangeStamp()) { cachedGradientWeightsStamp = crf.getWeightsValueChangeStamp(); // cachedGradient will soon no longer be stale getValue(); // if this fails then look in computeExpectations expectations.assertNotNaNOrInfinite(); // fill up gradient expectations.getParameters(cachedGradient); } System.arraycopy(cachedGradient, 0, buffer, 0, cachedGradient.length); }
public void getValueGradient(double[] buffer) { if (cachedGradientWeightsStamp != crf.getWeightsValueChangeStamp()) { cachedGradientWeightsStamp = crf.getWeightsValueChangeStamp(); getValue(); expectations.plusEquals(constraints, -1.0); expectations.plusEqualsGaussianPriorGradient(crf.getParameters(), -gaussianPriorVariance); expectations.assertNotNaNOrInfinite(); expectations.getParameters(cachedGradient); MatrixOps.timesEquals(cachedGradient, -weight); } System.arraycopy(cachedGradient, 0, buffer, 0, cachedGradient.length); }
public void getValueGradient(double[] buffer) { if (cachedGradientWeightsStamp != crf.getWeightsValueChangeStamp()) { cachedGradientWeightsStamp = crf.getWeightsValueChangeStamp(); // cachedGradient will soon no longer be stale getValue(); // if this fails then look in computeExpectations expectations.assertNotNaNOrInfinite(); // fill up gradient expectations.getParameters(cachedGradient); } System.arraycopy(cachedGradient, 0, buffer, 0, cachedGradient.length); }