Search in sources :

Example 1 with DiffFunction

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);
}
Also used : DiffFunction(edu.stanford.nlp.optimization.DiffFunction) QNMinimizer(edu.stanford.nlp.optimization.QNMinimizer)

Example 2 with DiffFunction

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);
}
Also used : DiffFunction(edu.stanford.nlp.optimization.DiffFunction) QNMinimizer(edu.stanford.nlp.optimization.QNMinimizer)

Example 3 with DiffFunction

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);
}
Also used : DiffFunction(edu.stanford.nlp.optimization.DiffFunction) QNMinimizer(edu.stanford.nlp.optimization.QNMinimizer)

Example 4 with DiffFunction

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;
}
Also used : DiffFunction(edu.stanford.nlp.optimization.DiffFunction) QNMinimizer(edu.stanford.nlp.optimization.QNMinimizer)

Example 5 with DiffFunction

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);
}
Also used : DiffFunction(edu.stanford.nlp.optimization.DiffFunction) QNMinimizer(edu.stanford.nlp.optimization.QNMinimizer)

Aggregations

DiffFunction (edu.stanford.nlp.optimization.DiffFunction)5 QNMinimizer (edu.stanford.nlp.optimization.QNMinimizer)5