stanfordnlp/CoreNLP · error · java.lang.RuntimeException

Attempting to remove features based on weight from a…

Error message

Attempting to remove features based on weight from a non-linear classifier

What it means

getFeaturesAboveThreshold removes dataset features by inspecting classifier weights, which only exist for LinearClassifier. It throws RuntimeException if the loaded classifier is not a LinearClassifier, since non-linear classifiers have no per-feature weights to threshold.

Solutions

  1. Verify the serialized model being loaded is a CRF/linear classifier before enabling weight-based feature removal
  2. Disable or bypass the threshold-pruning step when using a non-linear classifier
  3. Add an instanceof check with a friendlier error/log before entering the pruning path
  4. Re-train and serialize the expected linear classifier so the correct type is loaded

Example fix

// before
removeFeatures(dataset, 0.1); // classifier is non-linear -> RuntimeException
// after
if (classifier instanceof LinearClassifier) { removeFeatures(dataset, 0.1); } else { log.warning("Skipping feature removal: non-linear classifier"); }
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(classifier instanceof LinearClassifier)) { skipPruning(); }

Type guard

boolean isLinear(Object c) { return c instanceof LinearClassifier; }

Try / catch

try { pruneFeatures(dataset, thresh); } catch (RuntimeException e) { log.warning("Pruning requires a linear classifier: " + e.getMessage()); }

Prevention

When it happens

Trigger: Feature-removal/threshold pruning invoked after the classifier field was assigned a non-linear classifier (e.g. a different model type loaded from a serialized file).

Common situations: Loading a wrong or older model file that deserializes to a non-linear classifier; mixing classifier implementations in a training pipeline; configuration pointing at an incompatible serialized model.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

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

    }

    if (flags.doAdaptation && flags.adaptFile != null) {
      adapt(flags.adaptFile,train,readerAndWriter);
    }

    log.info("Built this classifier: ");
    if (classifier instanceof LinearClassifier) {
      String classString = ((LinearClassifier<String, String>)classifier).toString(flags.printClassifier, flags.printClassifierParam);
      log.info(classString);
    } else {
      String classString = classifier.toString();
      log.info(classString);
    }
  }

  private Index<String> getFeaturesAboveThreshold(Dataset<String, String> dataset, double thresh) {
    if (!(classifier instanceof LinearClassifier)) {
      throw new RuntimeException("Attempting to remove features based on weight from a non-linear classifier");
    }
    Index<String> featureIndex = dataset.featureIndex;
    Index<String> labelIndex = dataset.labelIndex;

    Index<String> features = new HashIndex<>();
    Iterator<String> featureIt = featureIndex.iterator();
    LinearClassifier<String, String> lc = (LinearClassifier<String, String>)classifier;
    LOOP:
    while (featureIt.hasNext()) {
      String f = featureIt.next();
      double smallest = Double.POSITIVE_INFINITY;
      double biggest = Double.NEGATIVE_INFINITY;
      for (String l : labelIndex) {
        double weight = lc.weight(f, l);
        if (weight < smallest) {
          smallest = weight;
        }
        if (weight > biggest) {

View on GitHub (pinned to 1b7edd19c4)