stanfordnlp/CoreNLP · error · RuntimeException

Incompatible CRFClassifier: weight length mismatch for…

Error message

Incompatible CRFClassifier: weight length mismatch for feature " + newIndex + ": " + featureIndex.get(newIndex) + " (also feature " + i + ": " + crf.featureIndex.get(i) + ") " + ", len1=" + weights[newIndex].length + ", len2=" + crf.weights[i].length

What it means

combineWeights merges weight arrays from another CRF into this one, aligned through the merged featureIndex. For each feature, this classifier's weight row must be at least as long as the other classifier's row (weights[newIndex].length >= crf.weights[i].length). A shorter row means the two models were built with inconsistent label structures for the same feature, so merging is impossible.

Solutions

  1. Ensure both classifiers were trained with the identical label set and windowSize so labelIndices have equal lengths.
  2. Combine models in the order where the classifier with the larger weight rows is 'this' — or merge into a fresh classifier with the superset labels.
  3. Verify both models come from the same CoreNLP version; combine() is fragile across versions.

Example fix

// before
smallerModel.combine(biggerModel);
// after
biggerModel.combine(smallerModel); // 'this' has rows >= other's
Defensive patterns

Strategy: validation

Validate before calling

for (int i = 0; i < other.weights.length; i++) {
  int idx = this.featureIndex.indexOf(other.featureIndex.get(i));
  if (idx >= 0 && this.weights[idx].length < other.weights[i].length)
    throw new IllegalStateException("Combine order would fail for feature " + other.featureIndex.get(i));
}

Prevention

When it happens

Trigger: Calling classifierA.combine(classifierB) where both classifiers contain the same feature name but B's weight matrix row for it is longer than A's — i.e. B has more label combinations (longer labelIndices entries) for that feature type.

Common situations: Combining models trained on label sets of different sizes (e.g. different tag inventories), or models from different CRF versions / window sizes whose labelIndices differ despite passing earlier checks.

Understand the failure class

Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ie/crf/CRFClassifier.java:330

    for (int i = 0; i < numFeatures; i++) {
      int length = labelIndices.get(map[i]).size();
      newWeights[i] = new float[length];
      if (i < oldNumFeatures) {
        assert (length >= weights[i].length);
        System.arraycopy(weights[i], 0, newWeights[i], 0, weights[i].length);
      }
    }
    weights = newWeights;

    // Get original weight indices from other crf and weight them in
    // depending on the type of the feature, different number of weights is
    // associated with it
    for (int i = 0; i < crf.weights.length; i++) {
      String feature = crf.featureIndex.get(i);
      int newIndex = featureIndex.indexOf(feature);
      // Check weights are okay dimension
      if (weights[newIndex].length < crf.weights[i].length) {
        throw new RuntimeException("Incompatible CRFClassifier: weight length mismatch for feature " + newIndex + ": "
            + featureIndex.get(newIndex) + " (also feature " + i + ": " + crf.featureIndex.get(i) + ") " + ", len1="
            + weights[newIndex].length + ", len2=" + crf.weights[i].length);
      }
      int featureTypeIndex = map[newIndex];
      for (int j = 0; j < crf.weights[i].length; j++) {
        CRFLabel labels = crf.labelIndices.get(featureTypeIndex).get(j);
        CRFLabel newLabels = crfLabelMap.get(labels);
        int k = this.labelIndices.get(featureTypeIndex).indexOf(newLabels);
        weights[newIndex][k] += crf.weights[i][j] * weight;
      }
    }
  }

  /**
   * Combines weighted crf with this crf.
   *
   * @param crf Other CRF whose weights to combine into this CRF
   * @param weight Amount to scale the other CRF's weights by

View on GitHub (pinned to 1b7edd19c4)