stanfordnlp/CoreNLP · error · UnsupportedOperationException

If you want to ask for the probability, you must train a…

Error message

If you want to ask for the probability, you must train a Platt model!

What it means

SVMLightClassifier only produces scores, not probabilities, unless a Platt scaling model was trained on top (SVMLightClassifierFactory with setPlattScaling(true)). logProbabilityOf(Datum) requires that platt model; when it is null it throws UnsupportedOperationException telling you to train a Platt model. Without Platt scaling the SVM cannot give calibrated log-probabilities.

Solutions

  1. Train with Platt scaling: call factory.setPlattScaling(true) (or enablePlattScaling) before trainClassifier and retrain
  2. Use classifier.scoresOf(datum) or classOf(datum) instead of logProbabilityOf when you only need scores/labels
  3. Compute your own calibration (e.g. sigmoid on scores) if retraining with Platt is not feasible
  4. Check classifier.platt != null before calling probability methods in generic code

Example fix

// before
SVMLightClassifierFactory<String,String> f = new SVMLightClassifierFactory<>();
SVMLightClassifier<String,String> c = f.trainClassifier(train);
c.logProbabilityOf(datum); // throws
// after
f.setPlattScaling(true);
SVMLightClassifier<String,String> c = f.trainClassifier(train); // now has platt model
c.logProbabilityOf(datum); // works
Defensive patterns

Strategy: try-catch

Validate before calling

// enable at training time
SVMLightClassifierFactory<L,F> factory = new SVMLightClassifierFactory<>();
factory.setPlattScaling(true); // required before trainClassifier for probabilities

Try / catch

try {
  Counter<L> logProbs = clf.logProbabilityOf(datum);
} catch (UnsupportedOperationException e) {
  Counter<L> scores = clf.scoresOf(datum); // fallback: use raw scores
  L predicted = Counters.argmax(scores);
}

Prevention

When it happens

Trigger: Calling classifier.logProbabilityOf(datum) on a classifier obtained from SVMLightClassifierFactory without enablePlattScaling/setPlattScaling(true), i.e. any default-trained SVMLightClassifier.

Common situations: Switching from LogisticRegressionClassifier (which has logProbabilityOf) to SVMLight and reusing the same evaluation code that asks for log-probabilities; probability-based metrics in cross-validation loops.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/894468cae263e486. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/classify/SVMLightClassifier.java:48

  public SVMLightClassifier(ClassicCounter<Pair<F, L>> weightCounter, ClassicCounter<L> thresholds, LinearClassifier<L, L> platt) {
    super(weightCounter, thresholds);
    this.platt = platt;
  }

  public void setPlatt(LinearClassifier<L, L> platt) {
    this.platt = platt;
  }

  /**
   * Returns a counter for the log probability of each of the classes
   * looking at the the sum of e^v for each count v, should be 1
   * Note: Uses SloppyMath.logSum which isn't exact but isn't as
   * offensively slow as doing a series of exponentials
   */
  @Override
  public Counter<L> logProbabilityOf(Datum<L, F> example) {
    if (platt == null) {
      throw new UnsupportedOperationException("If you want to ask for the probability, you must train a Platt model!");
    }
    Counter<L> scores = scoresOf(example);
    scores.incrementCount(null);
    Counter<L> probs = platt.logProbabilityOf(new RVFDatum<>(scores));
    //System.out.println(scores+" "+probs);
    return probs;
  }

  /**
   * Returns a counter for the log probability of each of the classes
   * looking at the the sum of e^v for each count v, should be 1
   * Note: Uses SloppyMath.logSum which isn't exact but isn't as
   * offensively slow as doing a series of exponentials
   */
  @Override
  public Counter<L> logProbabilityOf(RVFDatum<L, F> example) {
    if (platt == null) {
      throw new UnsupportedOperationException("If you want to ask for the probability, you must train a Platt model!");

View on GitHub (pinned to 1b7edd19c4)