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); }
cachedGradient.plusEquals(((GELatticeTask)task).getGradient(), 1);
cachedGradient.plusEquals(((GELatticeTask)task).getGradient(), 1);
this.expectations.plusEquals(((ExpectationTask)task).getExpectationsCopy(), 1);
this.expectations.plusEquals(((ExpectationTask)task).getExpectationsCopy(), 1);
this.expectations.plusEquals(((ExpectationTask)task).getExpectationsCopy(), 1);
expectations.plusEquals(constraints, -1.0); if (usingHyperbolicPrior) expectations.plusEqualsHyperbolicPriorGradient(crf.parameters, -hyperbolicPriorSlope, hyperbolicPriorSharpness);
expectations.plusEquals(constraints, -1.0); if (usingHyperbolicPrior) expectations.plusEqualsHyperbolicPriorGradient(crf.parameters, -hyperbolicPriorSlope, hyperbolicPriorSharpness);
expectations.plusEquals(constraints, -1.0); if (usingHyperbolicPrior) expectations.plusEqualsHyperbolicPriorGradient(crf.parameters, -hyperbolicPriorSlope, hyperbolicPriorSharpness);
/** * Adjust the parameters by learning rate according to the gradient of this * single Instance, and return the true label sequence likelihood. */ public double trainIncrementalLikelihood(Instance trainingInstance, double rate) { double singleLoglik; constraints.zero(); expectations.zero(); FeatureVectorSequence fvs = (FeatureVectorSequence) trainingInstance .getData(); Sequence labelSequence = (Sequence) trainingInstance.getTarget(); singleLoglik = new SumLatticeDefault(crf, fvs, labelSequence, constraints.new Incrementor()).getTotalWeight(); singleLoglik -= new SumLatticeDefault(crf, fvs, null, expectations.new Incrementor()).getTotalWeight(); // Calculate parameter gradient given these instances: (constraints - // expectations) constraints.plusEquals(expectations, -1); // Change the parameters a little by this difference, obeying // weightsFrozen crf.parameters.plusEquals(constraints, rate, true); return singleLoglik; } }
/** * Adjust the parameters by learning rate according to the gradient of this * single Instance, and return the true label sequence likelihood. */ public double trainIncrementalLikelihood(Instance trainingInstance, double rate) { double singleLoglik; constraints.zero(); expectations.zero(); FeatureVectorSequence fvs = (FeatureVectorSequence) trainingInstance .getData(); Sequence labelSequence = (Sequence) trainingInstance.getTarget(); singleLoglik = new SumLatticeDefault(crf, fvs, labelSequence, constraints.new Incrementor()).getTotalWeight(); singleLoglik -= new SumLatticeDefault(crf, fvs, null, expectations.new Incrementor()).getTotalWeight(); // Calculate parameter gradient given these instances: (constraints - // expectations) constraints.plusEquals(expectations, -1); // Change the parameters a little by this difference, obeying // weightsFrozen crf.parameters.plusEquals(constraints, rate, true); return singleLoglik; } }
/** * Adjust the parameters by learning rate according to the gradient of this * single Instance, and return the true label sequence likelihood. */ public double trainIncrementalLikelihood(Instance trainingInstance, double rate) { double singleLoglik; constraints.zero(); expectations.zero(); FeatureVectorSequence fvs = (FeatureVectorSequence) trainingInstance .getData(); Sequence labelSequence = (Sequence) trainingInstance.getTarget(); singleLoglik = new SumLatticeDefault(crf, fvs, labelSequence, constraints.new Incrementor()).getTotalWeight(); singleLoglik -= new SumLatticeDefault(crf, fvs, null, expectations.new Incrementor()).getTotalWeight(); // Calculate parameter gradient given these instances: (constraints - // expectations) constraints.plusEquals(expectations, -1); // Change the parameters a little by this difference, obeying // weightsFrozen crf.parameters.plusEquals(constraints, rate, true); return singleLoglik; } }
public void plusEquals (Factors other, double factor) { plusEquals(other, factor, false); }
public void plusEquals (Factors other, double factor) { plusEquals(other, factor, false); }
public void plusEquals (Factors other, double factor) { plusEquals(other, factor, false); }
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(); 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(); 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); }