stanfordnlp/CoreNLP · error · RuntimeException
Got NaN for prob in…
Error message
Got NaN for prob in CRFLogConditionalObjectiveFunctionWithDropout.calculate() - this may well indicate numeric underflow due to overly long documents.
What it means
CRFLogConditionalObjectiveFunctionWithDropout adds dropout regularization to the CRF objective; after computing prob (aggregated with combine2DArr over partial gradients) it throws a RuntimeException if prob is NaN, explicitly warning that overly long documents can cause numeric underflow. Training is aborted because a NaN objective cannot be optimized.
Solutions
- Reduce document length by splitting into sentences/segments before training.
- Lower the dropout rate or adjust dropout schedule parameters (dropoutScale, multiTouchGrad) in SeqClassifierFlags.
- Validate weights x for NaN/Inf before the optimizer step and reduce the learning rate if needed.
- Check feature values for zeros that cause log(0) in the objective.
- Catch the exception and restart from a checkpoint with smaller learning rate.
Example fix
// before flags.dropoutRate = 0.5; train(fullLongDocs); // after flags.dropoutRate = 0.2; List<List<CoreLabel>> chunks = splitBySentence(fullLongDocs); train(chunks);
Defensive patterns
Strategy: validation
Validate before calling
if (flags.dropoutRate > 0.5) throw new IllegalArgumentException("dropoutRate too high; underflow risk");
for (List<CoreLabel> doc : trainingData) if (doc.size() > MAX_LEN) splitDocument(doc); Type guard
static boolean validDropoutFlags(SeqClassifierFlags f) { return f.dropoutRate >= 0 && f.dropoutRate <= 0.5; } Try / catch
try {
dropoutCrf.calculate(x, batch, E);
} catch (RuntimeException e) {
if (e.getMessage().contains("NaN for prob")) {
flags.dropoutRate *= 0.5;
restartTraining(lastGoodCheckpoint);
} else throw e;
} Prevention
- Use moderate dropout rates; very high dropout pushes sequence probabilities to zero.
- Chunk long documents into sentences before dropout training.
- Tune dropoutScale and schedule parameters per SeqClassifierFlags docs.
- Monitor the objective value per iteration and stop on the first non-finite value.
When it happens
Trigger: Calling calculate() on the dropout CRF objective when the accumulated prob is NaN — dropout training on long sequences underflows, or parameters/feature values are non-finite.
Common situations: Dropout-regularized CRF training (SeqClassifierFlags.dropoutRate > 0) on long documents or with aggressive dropout schedules that push probabilities to zero.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Got NaN for prob in…
- Got NaN for prob in…
- Got NaN for prob in…
- Got NaN for prob in…
- Got NaN for prob in…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/cd4f5250363b53f0.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ie/crf/CRFLogConditionalObjectiveFunctionWithDropout.java:799
Map<Integer, double[]> partialDropout = result.fourth();
if (partialDropout != null) {
if (isUnsup) {
combine2DArr(dropoutPriorGradTotal, partialDropout, unsupDropoutScale);
} else {
combine2DArr(dropoutPriorGradTotal, partialDropout);
}
}
if (!isUnsup) {
Map<Integer, double[]> partialE = result.third();
if (partialE != null)
combine2DArr(E, partialE);
}
}
if (Double.isNaN(prob)) { // shouldn't be the case
throw new RuntimeException("Got NaN for prob in CRFLogConditionalObjectiveFunctionWithDropout.calculate()" +
" - this may well indicate numeric underflow due to overly long documents.");
}
// because we minimize -L(\theta)
value = -prob;
if (VERBOSE) {
log.info("value is " + Math.exp(-value));
}
// compute the partial derivative for each feature by comparing expected counts to empirical counts
int index = 0;
for (int i = 0; i < E.length; i++) {
for (int j = 0; j < E[i].length; j++) {
// because we minimize -L(\theta)
derivative[index] = (E[i][j] - Ehat[i][j]);
derivative[index] += dropoutScale * dropoutPriorGradTotal[i][j];
if (VERBOSE) {
log.info("deriv(" + i + ',' + j + ") = " + E[i][j] + " - " + Ehat[i][j] + " = " + derivative[index]);View on GitHub (pinned to 1b7edd19c4)