use of edu.stanford.nlp.optimization.DiffFunction 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.DiffFunction 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.DiffFunction 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.DiffFunction 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.DiffFunction in project CoreNLP by stanfordnlp.
the class LogisticClassifierFactory method trainWeightedData.
public LogisticClassifier<L, F> trainWeightedData(GeneralDataset<L, F> data, float[] dataWeights) {
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;
LogisticObjectiveFunction lof = null;
if (data instanceof Dataset<?, ?>)
lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getLabelsArray(), new LogPrior(LogPrior.LogPriorType.QUADRATIC), dataWeights);
else if (data instanceof RVFDataset<?, ?>)
lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getValuesArray(), data.getLabelsArray(), new LogPrior(LogPrior.LogPriorType.QUADRATIC), 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);
return new LogisticClassifier<>(weights, featureIndex, classes);
}
Aggregations