@Test public void testGradientAtNonInitialPoint() throws IOException { RealValueFileEventStream rvfes1 = new RealValueFileEventStream( "src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt", StandardCharsets.UTF_8.name()); testDataIndexer.index(rvfes1); NegLogLikelihood objectFunction = new NegLogLikelihood(testDataIndexer); double[] nonInitialPoint = new double[] { 0.2, 0.5, 0.2, 0.5, 0.2, 0.5, 0.2, 0.5, 0.2, 0.5 }; double[] gradientAtNonInitialPoint = objectFunction.gradientAt(dealignDoubleArrayForTestData(nonInitialPoint, testDataIndexer.getPredLabels(), testDataIndexer.getOutcomeLabels())); double[] expectedGradient = new double[] { -12.755042847945553, -21.227127506102434, -72.57790706276435, 38.03525795198456, 15.348650889354925, 12.755042847945557, 21.22712750610244, 72.57790706276438, -38.03525795198456, -15.348650889354925 }; Assert.assertTrue(compareDoubleArray(expectedGradient, gradientAtNonInitialPoint, testDataIndexer, TOLERANCE01)); }
public double[] gradientAt(double[] x) { if (x.length != dimension) throw new IllegalArgumentException( "x is invalid, its dimension is not equal to the function."); int ci, oi, ai, vectorIndex; double predValue, logSumOfExps; int empirical; Arrays.fill(gradient, 0); for (ci = 0; ci < numContexts; ci++) { for (oi = 0; oi < numOutcomes; oi++) { expectation[oi] = 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; expectation[oi] += predValue * x[vectorIndex]; } } logSumOfExps = ArrayMath.logSumOfExps(expectation); for (oi = 0; oi < numOutcomes; oi++) { expectation[oi] = StrictMath.exp(expectation[oi] - logSumOfExps); } for (oi = 0; oi < numOutcomes; oi++) { empirical = outcomeList[ci] == oi ? 1 : 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; gradient[vectorIndex] += predValue * (expectation[oi] - empirical) * numTimesEventsSeen[ci]; } } } return gradient; }
NegLogLikelihood implements Function { public double[] gradientAt(double[] x) { if (x.length != dimension) throw new IllegalArgumentException( "x is invalid, its dimension is not equal to the function."); int ci, oi, ai, vectorIndex; double predValue, logSumOfExps; int empirical; Arrays.fill(gradient, 0); for (ci = 0; ci < numContexts; ci++) { for (oi = 0; oi < numOutcomes; oi++) { expectation[oi] = 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; expectation[oi] += predValue * x[vectorIndex]; } } logSumOfExps = ArrayMath.logSumOfExps(expectation); for (oi = 0; oi < numOutcomes; oi++) { expectation[oi] = StrictMath.exp(expectation[oi] - logSumOfExps); } for (oi = 0; oi < numOutcomes; oi++) { empirical = outcomeList[ci] == oi ? 1 : 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; gradient[vectorIndex] += predValue * (expectation[oi] - empirical) * numTimesEventsSeen[ci]; } } } return gradient; } }
NegLogLikelihood implements Function { public double[] gradientAt(double[] x) { if (x.length != dimension) throw new IllegalArgumentException( "x is invalid, its dimension is not equal to the function."); int ci, oi, ai, vectorIndex; double predValue, logSumOfExps; int empirical; Arrays.fill(gradient, 0); for (ci = 0; ci < numContexts; ci++) { for (oi = 0; oi < numOutcomes; oi++) { expectation[oi] = 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; expectation[oi] += predValue * x[vectorIndex]; } } logSumOfExps = ArrayMath.logSumOfExps(expectation); for (oi = 0; oi < numOutcomes; oi++) { expectation[oi] = StrictMath.exp(expectation[oi] - logSumOfExps); } for (oi = 0; oi < numOutcomes; oi++) { empirical = outcomeList[ci] == oi ? 1 : 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; gradient[vectorIndex] += predValue * (expectation[oi] - empirical) * numTimesEventsSeen[ci]; } } } return gradient; } NegLogLikelihood(DataIndexer indexer); }
NegLogLikelihood implements Function { public double[] gradientAt(double[] x) { if (x.length != dimension) throw new IllegalArgumentException( "x is invalid, its dimension is not equal to the function."); int ci, oi, ai, vectorIndex; double predValue, logSumOfExps; int empirical; Arrays.fill(gradient, 0); for (ci = 0; ci < numContexts; ci++) { for (oi = 0; oi < numOutcomes; oi++) { expectation[oi] = 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; expectation[oi] += predValue * x[vectorIndex]; } } logSumOfExps = ArrayMath.logSumOfExps(expectation); for (oi = 0; oi < numOutcomes; oi++) { expectation[oi] = StrictMath.exp(expectation[oi] - logSumOfExps); } for (oi = 0; oi < numOutcomes; oi++) { empirical = outcomeList[ci] == oi ? 1 : 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; gradient[vectorIndex] += predValue * (expectation[oi] - empirical) * numTimesEventsSeen[ci]; } } } return gradient; } NegLogLikelihood(DataIndexer indexer); int getDimension(); double[] getInitialPoint(); double valueAt(double[] x); double[] gradientAt(double[] x); }
NegLogLikelihood implements Function { public double[] gradientAt(double[] x) { if (x.length != dimension) throw new IllegalArgumentException( "x is invalid, its dimension is not equal to the function."); int ci, oi, ai, vectorIndex; double predValue, logSumOfExps; int empirical; Arrays.fill(gradient, 0); for (ci = 0; ci < numContexts; ci++) { for (oi = 0; oi < numOutcomes; oi++) { expectation[oi] = 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; expectation[oi] += predValue * x[vectorIndex]; } } logSumOfExps = ArrayMath.logSumOfExps(expectation); for (oi = 0; oi < numOutcomes; oi++) { expectation[oi] = StrictMath.exp(expectation[oi] - logSumOfExps); } for (oi = 0; oi < numOutcomes; oi++) { empirical = outcomeList[ci] == oi ? 1 : 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; gradient[vectorIndex] += predValue * (expectation[oi] - empirical) * numTimesEventsSeen[ci]; } } } return gradient; } NegLogLikelihood(DataIndexer indexer); int getDimension(); double[] getInitialPoint(); double valueAt(double[] x); double[] gradientAt(double[] x);  }