{"record":{"id":"f6441a37fbdb4906","repo":"stanfordnlp/CoreNLP","slug":"two-models-must-have-the-same-number-of-classes","errorCode":null,"errorMessage":"Two models must have the same number of classes","messagePattern":"Two models must have the same number of classes","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/sequences/FactoredSequenceModel.java","lineNumber":111,"sourceCode":"    return model1.getPossibleValues(position);\n  }\n\n  /**\n   * using this constructor results in a weighted addition of the two models' scores.\n   * @param model1\n   * @param model2\n   * @param wt1 weight of model1\n   * @param wt2 weight of model2\n   */\n  public FactoredSequenceModel(SequenceModel model1, SequenceModel model2, double wt1, double wt2){\n    this(model1,model2);\n    this.model1Wt = wt1;\n    this.model2Wt = wt2;\n  }\n\n  public FactoredSequenceModel(SequenceModel model1, SequenceModel model2) {\n    //if (model1.leftWindow() != model2.leftWindow()) throw new RuntimeException(\"Two models must have same window size\");\n    if (model1.getPossibleValues(0).length != model2.getPossibleValues(0).length) throw new RuntimeException(\"Two models must have the same number of classes\");\n    if (model1.length() != model2.length()) throw new RuntimeException(\"Two models must have the same sequence length\");\n    this.model1 = model1;\n    this.model2 = model2;\n  }\n\n  public FactoredSequenceModel(SequenceModel[] models, double[] weights){\n    this.models = models;\n    this.wts = weights;\n    /*\n  for(int i = 1; i < models.length; i++){\n    if (models[0].getPossibleValues(0).length != models[i].getPossibleValues(0).length) throw new RuntimeException(\"All models must have the same number of classes\");\n    if(models[0].length() != models[i].length())\n      throw new RuntimeException(\"All models must have the same sequence length\");\n\n    }\n    */\n  }\n","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/sequences/FactoredSequenceModel.java#L93-L129","documentation":"The two-model FactoredSequenceModel constructor combines two SequenceModels that score the same label space, so it verifies both models report the same number of possible values at position 0. If the class counts differ it throws RuntimeException('Two models must have the same number of classes').","triggerScenarios":"new FactoredSequenceModel(model1, model2) where model1.getPossibleValues(0).length != model2.getPossibleValues(0).length — e.g. combining a tagger model with a different tag set against an NER model, at FactoredSequenceModel.java:111.","commonSituations":"Mixing models trained on different tag inventories or different feature pipelines (e.g. a 3-class entity model with a 5-class one); combining a coarse tagger with a fine NER model in factored decoding.","solutions":["Combine only models trained/defined over the same label space; retrain one model with matching class counts.","Map or pad the smaller model's possible values so counts align before construction.","Use the SequenceModel[] + weights constructor if the intended semantics allow different label spaces (verify it fits your use case)."],"exampleFix":"// before\nSequenceModel m1 = taggerModel;   // 45 tags\nSequenceModel m2 = nerModel;      // 5 classes\nFactoredSequenceModel f = new FactoredSequenceModel(m1, m2);\n// after\nSequenceModel m2 = retrained5classCompatModel; // same class count as m1\nFactoredSequenceModel f = new FactoredSequenceModel(m1, m2);","handlingStrategy":"validation","validationCode":"// verify equal class counts before composing\nif (m1.getPossibleValues(0).length != m2.getPossibleValues(0).length)\n  throw new IllegalArgumentException(\"class-count mismatch: \" + m1.getPossibleValues(0).length + \" vs \" + m2.getPossibleValues(0).length);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Train combined models on the same label inventory.","Log possibleValues(0).length for each model at composition time.","Keep model metadata (tag set) alongside serialized models to check compatibility."],"tags":["java","stanford-corenlp","sequence-model","model-composition"],"backgroundTag":"incompatible-source-type","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"}