{"record":{"id":"392537b2468505e9","repo":"stanfordnlp/CoreNLP","slug":"after-w-derivative-index-beforeoutputweights-392537","errorCode":null,"errorMessage":"after W derivative, index() != beforeOutputWeights()","messagePattern":"after W derivative, index\\(\\) != beforeOutputWeights\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/crf/CRFNonLinearSecondOrderLogConditionalObjectiveFunction.java","lineNumber":751,"sourceCode":"      for (int j = 0; j < eW4Edge[i].length; j++) {\n        derivative[index++] = (eW4Edge[i][j] - What4Edge[i][j]);\n        if (VERBOSE) {\n          log.info(\"inputLayerWeights4Edge deriv(\" + i + \",\" + j + \") = \" + eW4Edge[i][j] + \" - \" + What4Edge[i][j] + \" = \" + derivative[index - 1]);\n        }\n      }\n    }\n\n    for (int i = 0; i < eW.length; i++) {\n      for (int j = 0; j < eW[i].length; j++) {\n        derivative[index++] = (eW[i][j] - What[i][j]);\n        if (VERBOSE) {\n          log.info(\"inputLayerWeights deriv(\" + i + \",\" + j + \") = \" + eW[i][j] + \" - \" + What[i][j] + \" = \" + derivative[index - 1]);\n        }\n      }\n    }\n\n    if (index != beforeOutputWeights)\n      throw new RuntimeException(\"after W derivative, index(\"+index+\") != beforeOutputWeights(\"+beforeOutputWeights+\")\");\n\n    if (useOutputLayer) {\n      for (int i = 0; i < eU4Edge.length; i++) {\n        for (int j = 0; j < eU4Edge[i].length; j++) {\n          derivative[index++] = (eU4Edge[i][j] - Uhat4Edge[i][j]);\n          if (VERBOSE) {\n            log.info(\"outputLayerWeights4Edge deriv(\" + i + \",\" + j + \") = \" + eU4Edge[i][j] + \" - \" + Uhat4Edge[i][j] + \" = \" + derivative[index - 1]);\n          }\n        }\n      }\n      for (int i = 0; i < eU.length; i++) {\n        for (int j = 0; j < eU[i].length; j++) {\n          derivative[index++] = (eU[i][j] - Uhat[i][j]);\n          if (VERBOSE) {\n            log.info(\"outputLayerWeights deriv(\" + i + \",\" + j + \") = \" + eU[i][j] + \" - \" + Uhat[i][j] + \" = \" + derivative[index - 1]);\n          }\n        }\n      }","sourceCodeStart":733,"sourceCodeEnd":769,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/crf/CRFNonLinearSecondOrderLogConditionalObjectiveFunction.java#L733-L769","documentation":"In CRFNonLinearSecondOrderLogConditionalObjectiveFunction.calculate(), after writing the input-layer W derivative block the write index must equal beforeOutputWeights (the start of output-layer parameters). A mismatch means the W-derivative loops wrote entries inconsistent with the declared layer layout, corrupting the gradient/parameter alignment.","triggerScenarios":"calculate() invoked when eW/What dimensions (derived from inputLayerSize and numClasses) disagree with beforeOutputWeights -- e.g. non-linear second-order CRF training with useOutputLayer configurations or custom hidden-layer sizes that break the offset computation.","commonSituations":"Patched or extended second-order CRF training code; inconsistent constructor parameters (numNodeFeatures, numEdgeFeatures, window) relative to weight matrix sizes during NER model experiments.","solutions":["Ensure inputLayerSize and numClasses produce eW exactly of size inputLayerSize x numClasses and that beforeOutputWeights is computed from the same values.","Verify the W-derivative loop covers the full eW matrix exactly once.","Align output-layer flags (softmaxOutputLayer/sparseOutputLayer/tieOutputLayer) with the code path so offsets are computed identically.","Rebuild from official CoreNLP sources if local patches touched the layout constants."],"exampleFix":"// before (hidden layer resized without updating offsets)\neW = new double[newHiddenSize][numClasses];\n// after\ninputLayerSize = newHiddenSize; // recomputes beforeOutputWeights consistently\neW = new double[inputLayerSize][numClasses];","handlingStrategy":"validation","validationCode":"// verify sizes line up before training\nif (inputLayerSize * numClasses + edgeParamCount != beforeOutputWeights)\n  throw new IllegalStateException(\"layer offsets inconsistent with weight dimensions\");","typeGuard":null,"tryCatchPattern":"try {\n  minimizer.minimize(fn, tol, x);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"after W derivative\")) {\n    // re-derive layer sizes from flags and rebuild the objective function\n  } else throw e;\n}","preventionTips":["Derive all layer offsets from a single source (flags/constructor args), never hand-computed constants","Verify eW dimensions match inputLayerSize x numClasses before training","Smoke-test calculate() once on a tiny dataset before long optimization runs"],"tags":["crf","gradient","invariant","indexing"],"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"}