{"record":{"id":"894468cae263e486","repo":"stanfordnlp/CoreNLP","slug":"if-you-want-to-ask-for-the-probability-you-must-t","errorCode":null,"errorMessage":"If you want to ask for the probability, you must train a Platt model!","messagePattern":"If you want to ask for the probability, you must train a Platt model!","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/classify/SVMLightClassifier.java","lineNumber":48,"sourceCode":"  public SVMLightClassifier(ClassicCounter<Pair<F, L>> weightCounter, ClassicCounter<L> thresholds, LinearClassifier<L, L> platt) {\n    super(weightCounter, thresholds);\n    this.platt = platt;\n  }\n\n  public void setPlatt(LinearClassifier<L, L> platt) {\n    this.platt = platt;\n  }\n\n  /**\n   * Returns a counter for the log probability of each of the classes\n   * looking at the the sum of e^v for each count v, should be 1\n   * Note: Uses SloppyMath.logSum which isn't exact but isn't as\n   * offensively slow as doing a series of exponentials\n   */\n  @Override\n  public Counter<L> logProbabilityOf(Datum<L, F> example) {\n    if (platt == null) {\n      throw new UnsupportedOperationException(\"If you want to ask for the probability, you must train a Platt model!\");\n    }\n    Counter<L> scores = scoresOf(example);\n    scores.incrementCount(null);\n    Counter<L> probs = platt.logProbabilityOf(new RVFDatum<>(scores));\n    //System.out.println(scores+\" \"+probs);\n    return probs;\n  }\n\n  /**\n   * Returns a counter for the log probability of each of the classes\n   * looking at the the sum of e^v for each count v, should be 1\n   * Note: Uses SloppyMath.logSum which isn't exact but isn't as\n   * offensively slow as doing a series of exponentials\n   */\n  @Override\n  public Counter<L> logProbabilityOf(RVFDatum<L, F> example) {\n    if (platt == null) {\n      throw new UnsupportedOperationException(\"If you want to ask for the probability, you must train a Platt model!\");","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/classify/SVMLightClassifier.java#L30-L66","documentation":"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.","triggerScenarios":"Calling classifier.logProbabilityOf(datum) on a classifier obtained from SVMLightClassifierFactory without enablePlattScaling/setPlattScaling(true), i.e. any default-trained SVMLightClassifier.","commonSituations":"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.","solutions":["Train with Platt scaling: call factory.setPlattScaling(true) (or enablePlattScaling) before trainClassifier and retrain","Use classifier.scoresOf(datum) or classOf(datum) instead of logProbabilityOf when you only need scores/labels","Compute your own calibration (e.g. sigmoid on scores) if retraining with Platt is not feasible","Check classifier.platt != null before calling probability methods in generic code"],"exampleFix":"// before\nSVMLightClassifierFactory<String,String> f = new SVMLightClassifierFactory<>();\nSVMLightClassifier<String,String> c = f.trainClassifier(train);\nc.logProbabilityOf(datum); // throws\n// after\nf.setPlattScaling(true);\nSVMLightClassifier<String,String> c = f.trainClassifier(train); // now has platt model\nc.logProbabilityOf(datum); // works","handlingStrategy":"try-catch","validationCode":"// enable at training time\nSVMLightClassifierFactory<L,F> factory = new SVMLightClassifierFactory<>();\nfactory.setPlattScaling(true); // required before trainClassifier for probabilities","typeGuard":null,"tryCatchPattern":"try {\n  Counter<L> logProbs = clf.logProbabilityOf(datum);\n} catch (UnsupportedOperationException e) {\n  Counter<L> scores = clf.scoresOf(datum); // fallback: use raw scores\n  L predicted = Counters.argmax(scores);\n}","preventionTips":["Always set Platt scaling at factory setup if your evaluation code uses probabilities","Standardize on scoresOf/classOf for SVM evaluation unless calibration is required","Document in shared eval code that SVMLightClassifier needs Platt scaling for logProbabilityOf"],"tags":["java","stanford-nlp","svm","probability","unsupported-operation"],"backgroundTag":"feature-not-enabled","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}