{"record":{"id":"cfdcdbee2050ae28","repo":"stanfordnlp/CoreNLP","slug":"after-param-initialization-param-index-not-eq-cfdcdb","errorCode":null,"errorMessage":"after param initialization, param Index ( ) not equal to domainDimension ( )","messagePattern":"after param initialization, param Index \\( \\) not equal to domainDimension \\( \\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/crf/CRFNonLinearSecondOrderLogConditionalObjectiveFunction.java","lineNumber":254,"sourceCode":"          total -= val;\n        }\n        initial[count++] = total;\n        total = 1;\n        sum = 0;\n        for (int j = 0; j < numHiddenUnits-1; j++) {\n          val = random.nextDouble() * total;\n          initial[count++] = val;\n          total -= val;\n        }\n        initial[count++] = total;\n      } else {\n        for (int i = beforeOutputWeights; i < domainDimension(); i++) {\n          val = random.nextDouble() * twoEpsilon - epsilon;\n          initial[count++] = val;\n        }\n      }\n      if (count != domainDimension()) {\n        throw new RuntimeException(\"after param initialization, param Index (\" + count + \") not equal to domainDimension (\" + domainDimension() + \")\");\n      }\n    }\n    return initial;\n  }\n\n  private double[][] emptyU4Edge() {\n    int innerSize = inputLayerSize4Edge;\n    if (flags.sparseOutputLayer || flags.tieOutputLayer) {\n      innerSize = numHiddenUnits;\n    }\n    int outerSize = outputLayerSize4Edge;\n    if (flags.tieOutputLayer) {\n      outerSize = 1;\n    }\n\n    double[][] temp = new double[outerSize][innerSize];\n    for (int i = 0; i < outerSize; i++) {\n      temp[i] = new double[innerSize];","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/crf/CRFNonLinearSecondOrderLogConditionalObjectiveFunction.java#L236-L272","documentation":"At the end of initial(), the total number of initialized parameters (count) must equal domainDimension(). A mismatch means the initialization routine produced an initial vector of the wrong length relative to the function's declared parameter-space size, so the optimizer would receive an incompatible starting point.","triggerScenarios":"initial() called when the sum of the initialized blocks (edge params, W, optional U/U4Edge, remaining random weights) differs from domainDimension() -- typically after constructor flags or dimensions changed without updating the corresponding loops.","commonSituations":"Experimenting with useOutputLayer on/off or sparseOutputLayer in the second-order non-linear CRF; patched initialization code; training NER models with modified hidden-layer or edge-feature dimensions.","solutions":["Ensure every branch of initial() fills exactly domainDimension() entries (compare count increments with domainDimension()).","Keep useOutputLayer and related flags consistent between construction and initialization expectations.","Log count, beforeOutputWeights, and domainDimension() to find which block under-/over-filled.","Use an unmodified CoreNLP build to rule out local edits."],"exampleFix":"// before (tail loop skipped when useOutputLayer true)\nif (!useOutputLayer) { for (int i = beforeOutputWeights; i < domainDimension(); i++) initial[count++] = ...; }\n// after\nfor (int i = count; i < domainDimension(); i++) initial[count++] = random.nextDouble() * twoEpsilon - epsilon;","handlingStrategy":"validation","validationCode":"// after obtaining initial weights\ndouble[] x0 = fn.initial();\nif (x0.length != fn.domainDimension())\n  throw new IllegalStateException(\"initial length \" + x0.length + \" != domainDimension \" + fn.domainDimension());","typeGuard":null,"tryCatchPattern":"try {\n  minimizer.minimize(fn, tol, fn.initial());\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"not equal to domainDimension\")) {\n    // fall back to a freshly constructed function with default flags\n  } else throw e;\n}","preventionTips":["Never resize hidden layers by editing arrays directly; use flags so domainDimension() stays authoritative","Compare count vs domainDimension() in a debug build when touching initialization code","Keep constructor arguments derived from the same data used for training"],"tags":["crf","initialization","invariant","dimension"],"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-16T04:17:20.429Z"}