/** * Negative log-likelihood */ @Override public double valueAt(double[] x) { if (x.length != dimension) throw new IllegalArgumentException( "x is invalid, its dimension is not equal to domain dimension."); // Compute partial value of negative log-likelihood in each thread computeInParallel(x, NegLLComputeTask.class); double negLogLikelihood = 0; for (int t = 0; t < threads; t++) { negLogLikelihood += negLogLikelihoodThread[t]; } return negLogLikelihood; }
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]; 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; gradientThread[threadIndex][vectorIndex] +=
} else { System.out.println("Computing model parameters in " + threads + " threads ..."); objectiveFunction = new ParallelNegLogLikelihood(indexer, threads);
} else { System.out.println("Computing model parameters in " + threads + " threads ..."); objectiveFunction = new ParallelNegLogLikelihood(indexer, threads);
/** * Compute gradient */ @Override public double[] gradientAt(double[] x) { if (x.length != dimension) throw new IllegalArgumentException( "x is invalid, its dimension is not equal to the function."); // Compute partial gradient in each thread computeInParallel(x, GradientComputeTask.class); // Accumulate gradient for (int i = 0; i < dimension; i++) { gradient[i] = 0; for (int t = 0; t < threads; t++) { gradient[i] += gradientThread[t][i]; } } return gradient; }
@Override public NegLLComputeTask call() { int ci, oi, ai, vectorIndex, outcome; double predValue, logSumOfExps; negLogLikelihoodThread[threadIndex] = 0; for (ci = startIndex; ci < startIndex + length; ci++) { for (oi = 0; oi < numOutcomes; oi++) { tempSums[oi] = 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; tempSums[oi] += predValue * x[vectorIndex]; } } logSumOfExps = ArrayMath.logSumOfExps(tempSums); outcome = outcomeList[ci]; negLogLikelihoodThread[threadIndex] -= (tempSums[outcome] - logSumOfExps) * numTimesEventsSeen[ci]; } return this; } }
} else { System.out.println("Computing model parameters in " + threads + " threads ..."); objectiveFunction = new ParallelNegLogLikelihood(indexer, threads);
/** * Negative log-likelihood */ @Override public double valueAt(double[] x) { if (x.length != dimension) throw new IllegalArgumentException( "x is invalid, its dimension is not equal to domain dimension."); // Compute partial value of negative log-likelihood in each thread computeInParallel(x, NegLLComputeTask.class); double negLogLikelihood = 0; for (int t = 0; t < threads; t++) { negLogLikelihood += negLogLikelihoodThread[t]; } return negLogLikelihood; }
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]; 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; gradientThread[threadIndex][vectorIndex] +=
/** * Negative log-likelihood */ @Override public double valueAt(double[] x) { if (x.length != dimension) throw new IllegalArgumentException( "x is invalid, its dimension is not equal to domain dimension."); // Compute partial value of negative log-likelihood in each thread computeInParallel(x, NegLLComputeTask.class); double negLogLikelihood = 0; for (int t = 0; t < threads; t++) { negLogLikelihood += negLogLikelihoodThread[t]; } return negLogLikelihood; }
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]; 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; gradientThread[threadIndex][vectorIndex] +=
/** * Compute gradient */ @Override public double[] gradientAt(double[] x) { if (x.length != dimension) throw new IllegalArgumentException( "x is invalid, its dimension is not equal to the function."); // Compute partial gradient in each thread computeInParallel(x, GradientComputeTask.class); // Accumulate gradient for (int i = 0; i < dimension; i++) { gradient[i] = 0; for (int t = 0; t < threads; t++) { gradient[i] += gradientThread[t][i]; } } return gradient; }
@Override public NegLLComputeTask call() { int ci, oi, ai, vectorIndex, outcome; double predValue, logSumOfExps; negLogLikelihoodThread[threadIndex] = 0; for (ci = startIndex; ci < startIndex + length; ci++) { for (oi = 0; oi < numOutcomes; oi++) { tempSums[oi] = 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; tempSums[oi] += predValue * x[vectorIndex]; } } logSumOfExps = ArrayMath.logSumOfExps(tempSums); outcome = outcomeList[ci]; negLogLikelihoodThread[threadIndex] -= (tempSums[outcome] - logSumOfExps) * numTimesEventsSeen[ci]; } return this; } }
/** * Compute gradient */ @Override public double[] gradientAt(double[] x) { if (x.length != dimension) throw new IllegalArgumentException( "x is invalid, its dimension is not equal to the function."); // Compute partial gradient in each thread computeInParallel(x, GradientComputeTask.class); // Accumulate gradient for (int i = 0; i < dimension; i++) { gradient[i] = 0; for (int t = 0; t < threads; t++) { gradient[i] += gradientThread[t][i]; } } return gradient; }
@Override public NegLLComputeTask call() { int ci, oi, ai, vectorIndex, outcome; double predValue, logSumOfExps; negLogLikelihoodThread[threadIndex] = 0; for (ci = startIndex; ci < startIndex + length; ci++) { for (oi = 0; oi < numOutcomes; oi++) { tempSums[oi] = 0; for (ai = 0; ai < contexts[ci].length; ai++) { vectorIndex = indexOf(oi, contexts[ci][ai]); predValue = values != null ? values[ci][ai] : 1.0; tempSums[oi] += predValue * x[vectorIndex]; } } logSumOfExps = ArrayMath.logSumOfExps(tempSums); outcome = outcomeList[ci]; negLogLikelihoodThread[threadIndex] -= (tempSums[outcome] - logSumOfExps) * numTimesEventsSeen[ci]; } return this; } }