stanfordnlp/CoreNLP · error · RuntimeException
Counters.dotProduct infinite or NaN value for key:
Error message
Counters.dotProduct infinite or NaN value for key:
What it means
Counters.dotProduct validates every nonzero-or-checked count of the smaller counter before multiplying: if c1's count for a key is NaN or infinite, it throws RuntimeException naming the key and both counters' values. This guards the dot product from silently producing NaN/Infinity.
Solutions
- Find how the offending key got a NaN/Infinity count (the message names the key and both values)
- Sanitize counts before the dot product: replace non-finite values with 0 or clamp
- Fix upstream math: guard divisions (total>0) and use log(0+epsilon) instead of log(0)
Example fix
// before
double dot = Counters.dotProduct(logCounterA, logCounterB); // logCounterA has -Infinity for "unk"
// after
counterA.setCount("unk", 0.0); // or use Math.log(1e-10) upstream
double dot = Counters.dotProduct(counterA, counterB); Defensive patterns
Strategy: validation
Validate before calling
boolean isFinite(Counter<?> c) {
for (Object k : c.keySet()) {
double v = c.getCount(k);
if (Double.isNaN(v) || Double.isInfinite(v)) return false;
}
return true;
} Try / catch
try {
dot = Counters.dotProduct(c1, c2);
} catch (RuntimeException e) {
log.warn("non-finite counts: {}", e.getMessage());
sanitize(c1); sanitize(c2);
dot = Counters.dotProduct(c1, c2);
} Prevention
- Sanitize counters with a removeNonFiniteCounts-style pass before math
- Guard divisions by zero totals; use log(x + epsilon)
- Unit-test pipelines against NaN/Infinity propagation
When it happens
Trigger: Calling Counters.dotProduct(c1, c2) when either counter contains a count set to Double.NaN or +/-Infinity — usually from dividing by zero, log(0), or downstream arithmetic overflow.
Common situations: Building counters from log-probabilities where log(0) produced -Infinity; smoothing/normalization code dividing by a zero total; accidental division by zero in feature extraction.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- vectorName + " element " + i + " is " + vector[i]
- datum
- Got NaN for prob in…
- Got NaN for prob in…
- We need at least 2 extractors for ExtractorMerger to make…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/2289f12243fed544.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/stats/Counters.java:1210
return result;
}
/**
* Returns the product of c1 and c2.
*
* @return The product of c1 and c2.
*/
public static <E> double dotProduct(Counter<E> c1, Counter<E> c2) {
double dotProd = 0.0;
if (c1.size() > c2.size()) {
Counter<E> tmpCnt = c1;
c1 = c2;
c2 = tmpCnt;
}
for (E key : c1.keySet()) {
double count1 = c1.getCount(key);
if (Double.isNaN(count1) || Double.isInfinite(count1)) {
throw new RuntimeException("Counters.dotProduct infinite or NaN value for key: " + key + '\t' + c1.getCount(key) + '\t' + c2.getCount(key));
}
if (count1 != 0.0) {
double count2 = c2.getCount(key);
if (Double.isNaN(count2) || Double.isInfinite(count2)) {
throw new RuntimeException("Counters.dotProduct infinite or NaN value for key: " + key + '\t' + c1.getCount(key) + '\t' + c2.getCount(key));
}
if (count2 != 0.0) {
// this is the inner product
dotProd += (count1 * count2);
}
}
}
return dotProd;
}
/**
* Returns the product of Counter c and double[] a, using Index idx to map
* entries in C onto a.View on GitHub (pinned to 1b7edd19c4)