Search in sources :

Example 1 with QNMinimizer

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

Example 2 with QNMinimizer

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

Example 3 with QNMinimizer

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

Example 4 with QNMinimizer

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

Example 5 with QNMinimizer

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.");
}
Also used : TaggedWord(edu.stanford.nlp.ling.TaggedWord) LinearClassifierFactory(edu.stanford.nlp.classify.LinearClassifierFactory) WeightedDataset(edu.stanford.nlp.classify.WeightedDataset) QNMinimizer(edu.stanford.nlp.optimization.QNMinimizer) BasicDatum(edu.stanford.nlp.ling.BasicDatum)

Aggregations

QNMinimizer (edu.stanford.nlp.optimization.QNMinimizer)10 DiffFunction (edu.stanford.nlp.optimization.DiffFunction)5 LinearClassifierFactory (edu.stanford.nlp.classify.LinearClassifierFactory)3 List (java.util.List)2 edu.stanford.nlp.classify (edu.stanford.nlp.classify)1 RVFDataset (edu.stanford.nlp.classify.RVFDataset)1 WeightedDataset (edu.stanford.nlp.classify.WeightedDataset)1 IOUtils (edu.stanford.nlp.io.IOUtils)1 RuntimeIOException (edu.stanford.nlp.io.RuntimeIOException)1 BasicDatum (edu.stanford.nlp.ling.BasicDatum)1 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)1 CoreLabel (edu.stanford.nlp.ling.CoreLabel)1 IndexedWord (edu.stanford.nlp.ling.IndexedWord)1 RVFDatum (edu.stanford.nlp.ling.RVFDatum)1 TaggedWord (edu.stanford.nlp.ling.TaggedWord)1 Annotation (edu.stanford.nlp.pipeline.Annotation)1 StanfordCoreNLP (edu.stanford.nlp.pipeline.StanfordCoreNLP)1 SceneGraphImage (edu.stanford.nlp.scenegraph.image.SceneGraphImage)1 SceneGraphImageRegion (edu.stanford.nlp.scenegraph.image.SceneGraphImageRegion)1 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)1