{"record":{"id":"1d5284af03105d19","repo":"stanfordnlp/CoreNLP","slug":"incompatible-crfclassifier-weight-length-mismatch","errorCode":null,"errorMessage":"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","messagePattern":"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","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/crf/CRFClassifier.java","lineNumber":330,"sourceCode":"    for (int i = 0; i < numFeatures; i++) {\n      int length = labelIndices.get(map[i]).size();\n      newWeights[i] = new float[length];\n      if (i < oldNumFeatures) {\n        assert (length >= weights[i].length);\n        System.arraycopy(weights[i], 0, newWeights[i], 0, weights[i].length);\n      }\n    }\n    weights = newWeights;\n\n    // Get original weight indices from other crf and weight them in\n    // depending on the type of the feature, different number of weights is\n    // associated with it\n    for (int i = 0; i < crf.weights.length; i++) {\n      String feature = crf.featureIndex.get(i);\n      int newIndex = featureIndex.indexOf(feature);\n      // Check weights are okay dimension\n      if (weights[newIndex].length < crf.weights[i].length) {\n        throw new RuntimeException(\"Incompatible CRFClassifier: weight length mismatch for feature \" + newIndex + \": \"\n            + featureIndex.get(newIndex) + \" (also feature \" + i + \": \" + crf.featureIndex.get(i) + \") \" + \", len1=\"\n            + weights[newIndex].length + \", len2=\" + crf.weights[i].length);\n      }\n      int featureTypeIndex = map[newIndex];\n      for (int j = 0; j < crf.weights[i].length; j++) {\n        CRFLabel labels = crf.labelIndices.get(featureTypeIndex).get(j);\n        CRFLabel newLabels = crfLabelMap.get(labels);\n        int k = this.labelIndices.get(featureTypeIndex).indexOf(newLabels);\n        weights[newIndex][k] += crf.weights[i][j] * weight;\n      }\n    }\n  }\n\n  /**\n   * Combines weighted crf with this crf.\n   *\n   * @param crf Other CRF whose weights to combine into this CRF\n   * @param weight Amount to scale the other CRF's weights by","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/crf/CRFClassifier.java#L312-L348","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure both classifiers were trained with the identical label set and windowSize so labelIndices have equal lengths.","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.","Verify both models come from the same CoreNLP version; combine() is fragile across versions."],"exampleFix":"// before\nsmallerModel.combine(biggerModel);\n// after\nbiggerModel.combine(smallerModel); // 'this' has rows >= other's","handlingStrategy":"validation","validationCode":"for (int i = 0; i < other.weights.length; i++) {\n  int idx = this.featureIndex.indexOf(other.featureIndex.get(i));\n  if (idx >= 0 && this.weights[idx].length < other.weights[i].length)\n    throw new IllegalStateException(\"Combine order would fail for feature \" + other.featureIndex.get(i));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Train models with identical label inventories and windowSize.","Combine into the model with the larger weight dimensions.","Compare labelIndices sizes before calling combine()."],"tags":["java","crf","weight-mismatch","model-combine"],"backgroundTag":"tensor-shape-mismatch","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"}