{"record":{"id":"40c949b3cf678f62","repo":"stanfordnlp/CoreNLP","slug":"got-nan-for-prob-in-crflogconditionalobjectivefunc-40c949","errorCode":null,"errorMessage":"Got NaN for prob in CRFLogConditionalObjectiveFunctionForLOP.calculate()","messagePattern":"Got NaN for prob in CRFLogConditionalObjectiveFunctionForLOP\\.calculate\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/crf/CRFLogConditionalObjectiveFunctionForLOP.java","lineNumber":414,"sourceCode":"              eScales[lopIter] += (p * expected);\n\n              double[][] eOfIter = E[lopIter];\n              if (backpropTraining) {\n                for (int k = 0; k < docData[i][j].length; k++) { // k iterates over features\n                  int featureIdx = docData[i][j][k];\n                  if (indicesSet.contains(featureIdx)) {\n                    eOfIter[featureIdx][l] += p;\n                  }\n                }\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 CRFLogConditionalObjectiveFunctionForLOP.calculate()\");\n    }\n\n    value = -prob;\n    if(VERBOSE){\n      log.info(\"value is \" + value);\n    }\n    // compute the partial derivative for each feature by comparing expected counts to empirical counts\n    for (int lopIter = 0; lopIter < numLopExpert; lopIter++) {\n      double scale = scales[lopIter];\n      double observed = sumOfObservedLogPotential[lopIter];\n      for (int j = 0; j < numLopExpert; j++) {\n        observed -= scales[j] * sumOfObservedLogPotential[j];\n      }\n      observed *= scale;\n      double expected = eScales[lopIter];\n\n      derivative[lopIter] = (expected - observed);\n      if (VERBOSE) {","sourceCodeStart":396,"sourceCodeEnd":432,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/crf/CRFLogConditionalObjectiveFunctionForLOP.java#L396-L432","documentation":"CRFLogConditionalObjectiveFunctionForLOP computes a Log-Linear-Output-Pile (LOP) CRF objective; after accumulating prob across documents it throws a RuntimeException if the result is NaN. This indicates the LOP-style computation produced a non-finite log-probability, so the optimizer is stopped rather than continuing on corrupt values.","triggerScenarios":"Calling calculate() on the LOP objective when the accumulated prob is NaN — typically from underflow in mixture/ensemble weight computations, extreme parameters, or long input sequences in the LOP training data.","commonSituations":"LOP-CRF training (useNA / feature mixture setups) with badly scaled mixture weights or very long documents; also seen when a prior optimization step yields non-finite weights.","solutions":["Re-normalize the LOP mixture/feature weights so the per-document computations stay finite.","Split long training documents into shorter sequences to avoid log underflow.","Validate the parameter vector for NaN/Inf before each optimizer step.","Print intermediate per-document probs (enable VERBOSE) to locate the failing document.","Lower the learning rate and retrain from a known-good initialization."],"exampleFix":"// before\nlopCRF.calculate(x, batch, E); // throws on NaN\n// after\nboolean bad = false;\nfor (double w : x) if (Double.isNaN(w) || Double.isInfinite(w)) bad = true;\nif (!bad) lopCRF.calculate(x, batch, E); else x = lastGoodCheckpoint;","handlingStrategy":"try-catch","validationCode":"if (!isFinite(x)) x = lastGoodCheckpoint; // ensure params finite before LOP calculate\nboolean isFinite(double[] v) { for (double d : v) if (!Double.isFinite(d)) return false; return true; }","typeGuard":"static boolean isFinite(double[] v) { for (double d : v) if (Double.isNaN(d) || Double.isInfinite(d)) return false; return true; }","tryCatchPattern":"try {\n  lopCrf.calculate(x, batch, E);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"NaN for prob\")) {\n    normalizeMixtureWeights();\n    x = lastGoodCheckpoint;\n  } else throw e;\n}","preventionTips":["Keep LOP mixture weights normalized so ensemble log-probs stay finite.","Split long documents before LOP training.","Validate parameters each iteration for NaN/Inf.","Start from a well-scaled initialization."],"tags":["crf","lop","nan","training"],"backgroundTag":"value-out-of-range","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"}