use of edu.stanford.nlp.optimization.GoldenSectionLineSearch in project CoreNLP by stanfordnlp.
the class NBLinearClassifierFactory method tuneSigma.
private void tuneSigma(final int[][] data, final int[] labels) {
Function<Double, Double> CVSigmaToPerplexity = trialSigma -> {
double score = 0.0;
double sumScore = 0.0;
int foldSize, nbCV;
logger.info("Trying sigma = " + trialSigma);
if (data.length >= folds) {
foldSize = data.length / folds;
nbCV = folds;
} else {
foldSize = 1;
nbCV = data.length;
}
for (int j = 0; j < nbCV; j++) {
int testMin = j * foldSize;
int testMax = testMin + foldSize;
LinearClassifier<L, F> c = new LinearClassifier<>(weights(data, labels, testMin, testMax, trialSigma, foldSize), featureIndex, labelIndex);
for (int i = testMin; i < testMax; i++) {
score -= c.logProbabilityOf(new BasicDatum<>(featureIndex.objects(data[i]))).getCount(labelIndex.get(labels[i]));
}
sumScore += score;
}
System.err.printf(": %8g%n", sumScore);
return sumScore;
};
GoldenSectionLineSearch gsls = new GoldenSectionLineSearch(true);
sigma = gsls.minimize(CVSigmaToPerplexity, 0.01, 0.0001, 2.0);
System.out.println("Sigma used: " + sigma);
}
use of edu.stanford.nlp.optimization.GoldenSectionLineSearch in project CoreNLP by stanfordnlp.
the class CRFBiasedClassifier method adjustBias.
/**
* Adjust the bias parameter to optimize some objective function.
* Note that this function only tunes the bias parameter of one class
* (class of index 0), and is thus only useful for binary classification
* problems.
*/
public void adjustBias(List<List<IN>> develData, Function<Double, Double> evalFunction, double low, double high) {
LineSearcher ls = new GoldenSectionLineSearch(true, 1e-2, low, high);
CRFBiasedClassifierOptimizer optimizer = new CRFBiasedClassifierOptimizer(this, evalFunction);
double optVal = ls.minimize(optimizer);
int bi = featureIndex.indexOf(BIAS);
log.info("Class bias of " + weights[bi][0] + " reaches optimal value " + optVal);
}
Aggregations