{"record":{"id":"2fb8ef84e244bc49","repo":"stanfordnlp/CoreNLP","slug":"after-blockinitialize-param-index-not-equal-t","errorCode":null,"errorMessage":"after blockInitialize, param Index ( ) not equal to beforeOutputWeights ( )","messagePattern":"after blockInitialize, param Index \\( \\) not equal to beforeOutputWeights \\( \\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/crf/CRFNonLinearLogConditionalObjectiveFunction.java","lineNumber":204,"sourceCode":"        double twoFanIn = 2.0 * fanIn;\n        int interval = numNodeFeatures / numHiddenUnits;\n        for (int i = 0; i < numHiddenUnits; i++) {\n          int lower = i * interval;\n          int upper = (i + 1) * interval;\n          if (i == numHiddenUnits - 1)\n            upper = numNodeFeatures;\n          for (int j = 0; j < outputLayerSize; j++) {\n            for (int k = 0; k < numNodeFeatures; k++) {\n              val = 0;\n              if (k >= lower && k < upper) {\n                val = random.nextDouble() * twoFanIn - fanIn;\n              }\n              initial[count++] = val;\n            }\n          }\n        }\n        if (count != beforeOutputWeights) {\n          throw new RuntimeException(\"after blockInitialize, param Index (\" + count + \") not equal to beforeOutputWeights (\" + beforeOutputWeights + \")\");\n        }\n      } else {\n        double fanIn = 1 / Math.sqrt(numNodeFeatures+0.0);\n        double twoFanIn = 2.0 * fanIn;\n        for (int i = edgeParamCount; i < beforeOutputWeights; i++) {\n          val = random.nextDouble() * twoFanIn - fanIn;\n          initial[count++] = val;\n        }\n      }\n\n      // init output layer weights\n      if (flags.sparseOutputLayer) {\n        for (int i = 0; i < outputLayerSize; i++) {\n          double total = 1;\n          for (int j = 0; j < numHiddenUnits-1; j++) {\n            val = random.nextDouble() * total;\n            initial[count++] = val;\n            total -= val;","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/crf/CRFNonLinearLogConditionalObjectiveFunction.java#L186-L222","documentation":"During parameter initialization, the non-linear CRF objective pre-fills parameters up to beforeOutputWeights via blockInitialize. It then asserts the count of filled slots equals beforeOutputWeights; if not, an internal bookkeeping bug (wrong block sizes, mismatched layer dimensions) is reported with this RuntimeException showing both counts.","triggerScenarios":"Calling initial() with a flag/dimension combination (inputLayerSize, numClasses, useOutputLayer, sparse/tie modes) whose blockInitialize loop fills fewer or more entries than beforeOutputWeights — i.e. the block initializer and the computed offsets disagree.","commonSituations":"Non-standard combinations of useOutputLayer, sparseOutputLayer, tieOutputLayer, and inputLayerSize that the block initializer does not handle; custom patches or version mismatches in the objective function code.","solutions":["Check the RuntimeException numbers: if count < beforeOutputWeights, some block-initialize branch was skipped — inspect which flags gated it.","Use a standard flag combination (e.g. default inputLayerSize = numClasses with output layer disabled, or softmax+sparse) known to initialize correctly.","Verify inputLayerSize/hidden layer sizes are set consistently with numClasses and the feature map.","If you modified the source, fix blockInitialize so its inner loop counts match the beforeOutputWeights offset computation.","Report the exact flag combination to the Stanford NLP maintainers if stock flags reproduce it."],"exampleFix":"// before\nflags.softmaxOutputLayer = true;\nflags.sparseOutputLayer = false;\nflags.tieOutputLayer = false; // inconsistent block layout\n// after\nflags.softmaxOutputLayer = true;\nflags.sparseOutputLayer = true; // consistent block-initialize path","handlingStrategy":"validation","validationCode":"if (flags.useOutputLayer && !(flags.sparseOutputLayer || flags.tieOutputLayer || flags.softmaxOutputLayer))\n  throw new IllegalArgumentException(\"unsupported output-layer flag combination for blockInitialize\");","typeGuard":"static boolean standardOutputFlags(SeqClassifierFlags f) { return !f.useOutputLayer || f.sparseOutputLayer || f.tieOutputLayer; }","tryCatchPattern":"try {\n  double[] x = crf.initial();\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"after blockInitialize\")) {\n    log.error(\"blockInitialize invariant broken; check layer dims/flags\");\n    flags.inputLayerSize = flags.numClasses; // fall back to standard layout\n  } else throw e;\n}","preventionTips":["Stick to documented flag combinations for non-linear CRF training.","Keep inputLayerSize consistent with numClasses and the feature map.","Unit-test initial() dimensions for your exact flag set before training.","Avoid mixing patched objective-function code with stock flags."],"tags":["crf","initialization","invariant","nonlinear"],"backgroundTag":"internal-invariant-violation","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"}