{"record":{"id":"52372572f3d06176","repo":"stanfordnlp/CoreNLP","slug":"got-nan-for-prob-in-crfnonlinearsecondorderlogcond","errorCode":null,"errorMessage":"Got NaN for prob in CRFNonLinearSecondOrderLogConditionalObjectiveFunction.calculate()","messagePattern":"Got NaN for prob in CRFNonLinearSecondOrderLogConditionalObjectiveFunction\\.calculate\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/crf/CRFNonLinearSecondOrderLogConditionalObjectiveFunction.java","lineNumber":722,"sourceCode":"              if (useHiddenLayer)\n                deltaK *= fDeriv[k];\n              double[] eWK = null;\n              if (j == 0) {\n                eWK = eW[k];\n              } else {\n                eWK = eW4Edge[k];\n              }\n              for (int cliqueFeature : cliqueFeatures) {\n                eWK[cliqueFeature] += deltaK * p;\n              }\n            }\n          }\n        }\n      }\n    }\n\n    if (Double.isNaN(prob)) { // shouldn't be the case\n      throw new RuntimeException(\"Got NaN for prob in CRFNonLinearSecondOrderLogConditionalObjectiveFunction.calculate()\");\n    }\n\n    value = -prob;\n    if(VERBOSE){\n      log.info(\"value is \" + value);\n    }\n\n    // compute the partial derivative for each feature by comparing expected counts to empirical counts\n    int index = 0;\n    for (int i = 0; i < eW4Edge.length; i++) {\n      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","sourceCodeStart":704,"sourceCodeEnd":740,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/crf/CRFNonLinearSecondOrderLogConditionalObjectiveFunction.java#L704-L740","documentation":"CRFNonLinearSecondOrderLogConditionalObjectiveFunction.calculate() computes the sequence log-probability; if it comes out NaN, the function throws rather than feeding a broken value to the optimizer. NaN arises from numerical instability in the second-order (higher-order) CRF computations: overflow/underflow in forward-backward, zero softmax denominators, or non-finite weights.","triggerScenarios":"Calling calculate() during training of a second-order non-linear CRF (useNonLinearCRF=true with second-order window) when x contains NaN/Inf, activations overflow, or expected-count terms diverge to Inf-Inf.","commonSituations":"Divergent optimization runs (learning rate too high, too many iterations), unscaled feature values, degenerate softmax output-layer settings, or very long sequences whose probabilities underflow.","solutions":["Validate the parameter vector for NaN/Inf before each calculate() call and restart optimization from clean initial weights.","Scale input features and consider reducing hidden-layer size or adding regularization to tame activation magnitudes.","Fix output-layer flag combinations (softmaxOutputLayer with sparseOutputLayer/tieOutputLayer) to a supported configuration.","Lower the learning rate / iteration count, or verify forward-backward scaling for long sequences."],"exampleFix":"// before\nminimizer.minimize(fn, tolerance, initialWeights);\n// after\ndouble[] w = initialWeights;\nfor (double v : w) if (!(Double.isFinite(v))) throw new IllegalArgumentException(\"non-finite initial weight\");\ndouble[] result = minimizer.minimize(fn, tolerance, w);","handlingStrategy":"validation","validationCode":"// guard before each optimizer iteration is impractical; guard inputs instead\nfor (double v : initialWeights)\n  if (!Double.isFinite(v)) throw new IllegalArgumentException(\"non-finite initial weight\");\n// and bound features\nfor (double[] doc : flatFeatures)\n  for (double v : doc)\n    if (!Double.isFinite(v) || Math.abs(v) > 1e6) throw new IllegalArgumentException(\"bad feature value\");","typeGuard":null,"tryCatchPattern":"try {\n  minimizer.minimize(fn, tol, x0);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"Got NaN for prob in CRFNonLinearSecondOrder\")) {\n    // halve learning rate, rescale features, and restart from clean weights\n  } else throw e;\n}","preventionTips":["Normalize features and monitor objective values for divergence","Use modest hidden-layer sizes and regularization during second-order CRF training","Restart training from freshly initialized (finite) weights after any NaN failure","Keep long-sequence handling in mind: check scaling/normalization of forward-backward terms"],"tags":["crf","nan","numerical","training"],"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"}