use of edu.stanford.nlp.optimization.QNMinimizer in project CoreNLP by stanfordnlp.
the class LogisticClassifier method trainWeightedData.
/**
* Trains on weighted dataset.
* @param dataWeights weights of the data.
*/
// Use LogisticClassifierFactory to train instead.
@Deprecated
public void trainWeightedData(GeneralDataset<L, F> data, float[] dataWeights) {
if (data.labelIndex.size() != 2) {
throw new RuntimeException("LogisticClassifier is only for binary classification!");
}
Minimizer<DiffFunction> minim;
LogisticObjectiveFunction lof = null;
if (data instanceof Dataset<?, ?>)
lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getLabelsArray(), prior, dataWeights);
else if (data instanceof RVFDataset<?, ?>)
lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getValuesArray(), data.getLabelsArray(), prior, dataWeights);
minim = new QNMinimizer(lof);
weights = minim.minimize(lof, 1e-4, new double[data.numFeatureTypes()]);
featureIndex = data.featureIndex;
classes[0] = data.labelIndex.get(0);
classes[1] = data.labelIndex.get(1);
}
use of edu.stanford.nlp.optimization.QNMinimizer in project CoreNLP by stanfordnlp.
the class LogisticClassifier method train.
// Use LogisticClassifierFactory to train instead.
@Deprecated
public void train(GeneralDataset<L, F> data, double l1reg, double tol) {
if (data.labelIndex.size() != 2) {
throw new RuntimeException("LogisticClassifier is only for binary classification!");
}
Minimizer<DiffFunction> minim;
if (!biased) {
LogisticObjectiveFunction lof = null;
if (data instanceof Dataset<?, ?>)
lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getLabelsArray(), prior);
else if (data instanceof RVFDataset<?, ?>)
lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getValuesArray(), data.getLabelsArray(), prior);
if (l1reg > 0.0) {
minim = ReflectionLoading.loadByReflection("edu.stanford.nlp.optimization.OWLQNMinimizer", l1reg);
} else {
minim = new QNMinimizer(lof);
}
weights = minim.minimize(lof, tol, new double[data.numFeatureTypes()]);
} else {
BiasedLogisticObjectiveFunction lof = new BiasedLogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getLabelsArray(), prior);
if (l1reg > 0.0) {
minim = ReflectionLoading.loadByReflection("edu.stanford.nlp.optimization.OWLQNMinimizer", l1reg);
} else {
minim = new QNMinimizer(lof);
}
weights = minim.minimize(lof, tol, new double[data.numFeatureTypes()]);
}
featureIndex = data.featureIndex;
classes[0] = data.labelIndex.get(0);
classes[1] = data.labelIndex.get(1);
}
use of edu.stanford.nlp.optimization.QNMinimizer in project CoreNLP by stanfordnlp.
the class LogisticClassifierFactory method trainClassifier.
public LogisticClassifier<L, F> trainClassifier(GeneralDataset<L, F> data, double l1reg, double tol, LogPrior prior, boolean biased) {
if (data instanceof RVFDataset)
((RVFDataset<L, F>) data).ensureRealValues();
if (data.labelIndex.size() != 2) {
throw new RuntimeException("LogisticClassifier is only for binary classification!");
}
Minimizer<DiffFunction> minim;
if (!biased) {
LogisticObjectiveFunction lof = null;
if (data instanceof Dataset<?, ?>)
lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getLabelsArray(), prior);
else if (data instanceof RVFDataset<?, ?>)
lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getValuesArray(), data.getLabelsArray(), prior);
if (l1reg > 0.0) {
minim = ReflectionLoading.loadByReflection("edu.stanford.nlp.optimization.OWLQNMinimizer", l1reg);
} else {
minim = new QNMinimizer(lof);
}
weights = minim.minimize(lof, tol, new double[data.numFeatureTypes()]);
} else {
BiasedLogisticObjectiveFunction lof = new BiasedLogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getLabelsArray(), prior);
if (l1reg > 0.0) {
minim = ReflectionLoading.loadByReflection("edu.stanford.nlp.optimization.OWLQNMinimizer", l1reg);
} else {
minim = new QNMinimizer(lof);
}
weights = minim.minimize(lof, tol, new double[data.numFeatureTypes()]);
}
featureIndex = data.featureIndex;
classes[0] = data.labelIndex.get(0);
classes[1] = data.labelIndex.get(1);
return new LogisticClassifier<>(weights, featureIndex, classes);
}
use of edu.stanford.nlp.optimization.QNMinimizer in project CoreNLP by stanfordnlp.
the class ShiftParamsLogisticClassifierFactory method trainWeights.
private double[][] trainWeights() {
QNMinimizer minimizer = new QNMinimizer(15, true);
minimizer.useOWLQN(true, lambda);
DiffFunction objective = new ShiftParamsLogisticObjectiveFunction(data, dataValues, convertLabels(labels), numClasses, numFeatures + data.length, numFeatures, prior);
double[] augmentedThetas = new double[(numClasses - 1) * (numFeatures + data.length)];
augmentedThetas = minimizer.minimize(objective, 1e-4, augmentedThetas);
// calculate number of non-zero parameters, for debugging
int count = 0;
for (int j = numFeatures; j < augmentedThetas.length; j++) {
if (augmentedThetas[j] != 0)
count++;
}
Redwood.log("NUM NONZERO PARAMETERS: " + count);
double[][] thetas = new double[numClasses - 1][numFeatures];
LogisticUtils.unflatten(augmentedThetas, thetas);
return thetas;
}
use of edu.stanford.nlp.optimization.QNMinimizer in project CoreNLP by stanfordnlp.
the class ChineseMaxentLexicon method finishTraining.
@Override
public void finishTraining() {
IntCounter<String> tagCounter = new IntCounter<>();
WeightedDataset data = new WeightedDataset(datumCounter.size());
for (TaggedWord word : datumCounter.keySet()) {
int count = datumCounter.getIntCount(word);
if (trainOnLowCount && count > trainCountThreshold) {
continue;
}
if (functionWordTags.containsKey(word.word())) {
continue;
}
tagCounter.incrementCount(word.tag());
if (trainByType) {
count = 1;
}
data.add(new BasicDatum(featExtractor.makeFeatures(word.word()), word.tag()), count);
}
datumCounter = null;
tagDist = Distribution.laplaceSmoothedDistribution(tagCounter, tagCounter.size(), 0.5);
tagCounter = null;
applyThresholds(data);
verbose("Making classifier...");
// new ResultStoringMonitor(5, "weights"));
QNMinimizer minim = new QNMinimizer();
// minim.shutUp();
LinearClassifierFactory factory = new LinearClassifierFactory(minim);
factory.setTol(tol);
factory.setSigma(sigma);
if (tuneSigma) {
factory.setTuneSigmaHeldOut();
}
scorer = factory.trainClassifier(data);
verbose("Done training.");
}
Aggregations