{"record":{"id":"17a915ae7ff60b0d","repo":"stanfordnlp/CoreNLP","slug":"cannot-retrain-before-you-train","errorCode":null,"errorMessage":"Cannot retrain before you train!","messagePattern":"Cannot retrain before you train!","errorType":"exception","errorClass":"java.lang.UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/ner/CMMClassifier.java","lineNumber":543,"sourceCode":"    lc.setWeights(weights);\n    /*\n    int delme = 0;\n    if (true) {\n      for (double[] dd : weights) {\n        delme++;\n        for (double d : dd) {\n        }\n      }\n    }\n    log.info(weights[delme-1][0]);\n    log.info(\"size of weights: \"+delme);\n    */\n  }\n\n\n  public void retrain(ObjectBank<List<IN>> doc) {\n    if (classifier == null) {\n      throw new UnsupportedOperationException(\"Cannot retrain before you train!\");\n    }\n    Index<String> findex = ((LinearClassifier<String, String>)classifier).featureIndex();\n    Index<String> lindex = ((LinearClassifier<String, String>)classifier).labelIndex();\n    log.info(\"Starting retrain:\\t# of original features\"+findex.size()+\", # of original labels\"+lindex.size());\n    retrain(doc, findex, lindex);\n  }\n\n\n  @Override\n  public void train(Collection<List<IN>> wordInfos,\n                    DocumentReaderAndWriter<IN> readerAndWriter) {\n    Dataset<String, String> train = getDataset(wordInfos);\n    //train.summaryStatistics();\n    //train.printSVMLightFormat();\n    // 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?)\n    train(train);\n\n    for (int i = 0; i < flags.numTimesPruneFeatures; i++) {","sourceCodeStart":525,"sourceCodeEnd":561,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/ner/CMMClassifier.java#L525-L561","documentation":"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.","triggerScenarios":"Calling retrain() on a freshly constructed CMMClassifier before any call to train(ObjectBank) or a classifier load; deserialization path that skipped classifier initialization.","commonSituations":"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.","solutions":["Call train(doc) (or load a serialized classifier) once before invoking retrain","Load a previously serialized CMMClassifier so the classifier field is populated","Check classifier != null in your pipeline orchestration before retraining","Restructure the workflow to treat retrain as strictly a second-pass operation"],"exampleFix":"// before\ncmm.retrain(docs); // classifier == null -> UnsupportedOperationException\n// after\ncmm.train(docs);          // initial training\ncmm.retrain(moreDocs);    // now safe","handlingStrategy":"try-catch","validationCode":"if (cmm.getClassifierField() == null) { cmm.train(initialDocs); } // or load serialized model first","typeGuard":null,"tryCatchPattern":"try { cmm.retrain(docs); } catch (UnsupportedOperationException e) { cmm.train(docs); }","preventionTips":["Never call retrain on a freshly constructed CMMClassifier","Serialize/load the classifier before any retrain pass","Enforce pipeline ordering: train -> (optional) retrain"],"tags":["java","ner","lifecycle"],"backgroundTag":"unsupported-operation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}