stanfordnlp/CoreNLP · error · java.lang.UnsupportedOperationException

Cannot retrain before you train!

Error message

Cannot retrain before you train!

What it means

CMMClassifier.retrain(ObjectBank) reloads training with the existing classifier's feature and label indexes. It throws UnsupportedOperationException if the internal classifier field is still null, i.e. train()/loadClassifier was never run, because there are no indexes to reuse.

Solutions

  1. Call train(doc) (or load a serialized classifier) once before invoking retrain
  2. Load a previously serialized CMMClassifier so the classifier field is populated
  3. Check classifier != null in your pipeline orchestration before retraining
  4. Restructure the workflow to treat retrain as strictly a second-pass operation

Example fix

// before
cmm.retrain(docs); // classifier == null -> UnsupportedOperationException
// after
cmm.train(docs);          // initial training
cmm.retrain(moreDocs);    // now safe
Defensive patterns

Strategy: try-catch

Validate before calling

if (cmm.getClassifierField() == null) { cmm.train(initialDocs); } // or load serialized model first

Try / catch

try { cmm.retrain(docs); } catch (UnsupportedOperationException e) { cmm.train(docs); }

Prevention

When it happens

Trigger: Calling retrain() on a freshly constructed CMMClassifier before any call to train(ObjectBank) or a classifier load; deserialization path that skipped classifier initialization.

Common situations: Script that reuses a classifier object across datasets but forgot the initial training/serialization load; wiring retrain as the first step in a pipeline; reloading a serialized model into the wrong class.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ie/ner/CMMClassifier.java:543

    lc.setWeights(weights);
    /*
    int delme = 0;
    if (true) {
      for (double[] dd : weights) {
        delme++;
        for (double d : dd) {
        }
      }
    }
    log.info(weights[delme-1][0]);
    log.info("size of weights: "+delme);
    */
  }


  public void retrain(ObjectBank<List<IN>> doc) {
    if (classifier == null) {
      throw new UnsupportedOperationException("Cannot retrain before you train!");
    }
    Index<String> findex = ((LinearClassifier<String, String>)classifier).featureIndex();
    Index<String> lindex = ((LinearClassifier<String, String>)classifier).labelIndex();
    log.info("Starting retrain:\t# of original features"+findex.size()+", # of original labels"+lindex.size());
    retrain(doc, findex, lindex);
  }


  @Override
  public void train(Collection<List<IN>> wordInfos,
                    DocumentReaderAndWriter<IN> readerAndWriter) {
    Dataset<String, String> train = getDataset(wordInfos);
    //train.summaryStatistics();
    //train.printSVMLightFormat();
    // wordInfos = null;  // cdm: I think this does no good as ptr exists in caller (could empty the list or better refactor so conversion done earlier?)
    train(train);

    for (int i = 0; i < flags.numTimesPruneFeatures; i++) {

View on GitHub (pinned to 1b7edd19c4)