{"record":{"id":"8004855438d826e3","repo":"stanfordnlp/CoreNLP","slug":"vector-of-incorrect-size-passed-to-applyinitialhes","errorCode":null,"errorMessage":"Vector of incorrect size passed to applyInitialHessian in QNInfo class","messagePattern":"Vector of incorrect size passed to applyInitialHessian in QNInfo class","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/optimization/QNMinimizer.java","lineNumber":724,"sourceCode":"      return used;\n    }\n  } // end class ScalarQNInfo\n\n  class DiagonalQNInfo extends QNInfo {\n    DiagonalQNInfo(int size) {\n      super(size);\n    }\n\n    DiagonalQNInfo(List<double[]> sList, List<double[]> yList) {\n      super(sList, yList);\n    }\n\n    double[] applyInitialHessian(double[] x, StringBuilder sb) {\n      sb.append('D');\n      if(d != null) {\n        // Check sizes\n        if(x.length != d.length)\n          throw new IllegalArgumentException(\"Vector of incorrect size passed to applyInitialHessian in QNInfo class\");\n        // Scale element-wise\n        for(int i = 0; i < x.length; i++)\n          x[i] /= d[i];\n      }\n      return x;\n    }\n\n    int update(double[] newS, double[] newY, double yy, double sy, double sg, double step) {\n      if(sy < 0) {\n        // NOTE: if applying QNMinimizer to a non convex problem, we would still\n        // like to update the matrix\n        // or we could get stuck in a series of skipped updates.\n        if(!quiet)\n          log.info(\" Negative curvature detected, update skipped \");\n        return used;\n      }\n      if(yy == 0.0) {\n        if(!quiet)","sourceCodeStart":706,"sourceCodeEnd":742,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/optimization/QNMinimizer.java#L706-L742","documentation":"QNMinimizer.QNInfo.applyInitialHessian scales the input vector element-wise by the stored diagonal d. If the passed vector's length differs from d.length, the element-wise division is impossible, so it throws this IllegalArgumentException. If d is null, the vector is returned unscaled (no error).","triggerScenarios":"Calling applyInitialHessian (directly or through the QNMinimizer pipeline) with a state vector x whose dimension differs from the dimension of the previously stored Hessian diagonal/surrogate history d.","commonSituations":"Reusing a QNInfo object across problems of different dimensionality; changing model size between optimization runs while reusing the same minimizer/QNInfo; passing a gradient subset instead of the full vector.","solutions":["Pass an x vector whose length matches the QNInfo's stored d (same model dimension)","Create a fresh QNInfo/QNMinimizer for the new problem dimension instead of reusing one","Clear/reset the stored history (set d null or construct a new QNInfo) when the dimension changes"],"exampleFix":"// before\nqnInfo.applyInitialHessian(new double[100]); // qnInfo built for dim 50\n// after\ndouble[] x = new double[50];\nqnInfo.applyInitialHessian(x); // match stored dimension","handlingStrategy":"validation","validationCode":"if (x.length != expectedDim) throw new IllegalArgumentException(\"x.length=\" + x.length + \" but QNInfo dimension=\" + expectedDim);","typeGuard":null,"tryCatchPattern":"try { qnInfo.applyInitialHessian(x); } catch (IllegalArgumentException e) { if (e.getMessage().contains(\"incorrect size passed to applyInitialHessian\")) { qnInfo = new QNMinimizer.QNInfo(x.length); qnInfo.applyInitialHessian(x); } else throw e; }","preventionTips":["Never reuse QNInfo/QNMinimizer instances across problems of different dimension","Assert vector lengths equal the model dimension before optimizer calls","Reset optimizer state whenever the model size changes"],"tags":["java","stanford-nlp","optimization","dimension-mismatch"],"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"}