stanfordnlp/CoreNLP · error · InvalidElementException
vectorName + " element " + i + " is " + vector[i]
Error message
vectorName + " element " + i + " is " + vector[i]
What it means
ArrayMath.assertFinite(double[] vector, String vectorName) verifies every element is a normal finite double. If any element is NaN or +/-Infinity it throws InvalidElementException (a RuntimeException subclass) naming the vector, index, and offending value, so numerical corruption is caught at the boundary instead of propagating silently.
Solutions
- Find the source of the NaN/Infinity using the index and value in the message, and fix the upstream arithmetic
- Pre-sanitize the vector: replace non-finite values or clamp them before assertFinite
- Skip assertFinite for intentionally non-finite data and validate only where finiteness is required
Example fix
// before
ArrayMath.assertFinite(vector, "weights"); // throws: weights element 7 is NaN
// after
for (int i = 0; i < vector.length; i++) {
if (!Double.isFinite(vector[i])) vector[i] = 0.0; // or log & fix upstream
}
ArrayMath.assertFinite(vector, "weights"); Defensive patterns
Strategy: try-catch
Validate before calling
boolean finite = true;
for (double v : vector) { if (Double.isNaN(v) || Double.isInfinite(v)) { finite = false; break; } } Type guard
static boolean isFiniteVector(double[] v) {
for (double d : v) if (Double.isNaN(d) || Double.isInfinite(d)) return false;
return true;
} Try / catch
try {
ArrayMath.assertFinite(vector, "weights");
} catch (ArrayMath.InvalidElementException e) {
log.error("non-finite vector: " + e.getMessage(), e);
// quarantine/recompute the vector
} Prevention
- Check norms and denominators for zero before dividing
- Sanitize model outputs (replace NaN/Inf) after exp/log operations
- Run assertFinite at training checkpoints so corruption is localized early
When it happens
Trigger: Calling assertFinite on a vector produced by division by zero (Infinity), 0/0 (NaN), or overflow from exp/log, e.g. validating model weights or gradient arrays before use.
Common situations: Degenerate training data, learning rates too large causing overflow, uninitialized arrays filled with NaN, or failed normalization producing division by zero.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- Counters.dotProduct infinite or NaN value for key:
- datum
- Got NaN for prob in…
- Got NaN for prob in…
- Cosine is not between -1 and 1: " + cosValue
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/1e39c1a4508bbd9e.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/math/ArrayMath.java:2134
public static void multiplyInto(double[] a, double[] b, double c) {
for (int i=0; i<a.length; i++) {
a[i] = b[i] * c;
}
}
public static double entropy(double[] probs) {
double e = 0.0;
for (double p : probs) {
if (p != 0.0)
e -= p * Math.log(p);
}
return e;
}
public static void assertFinite(double[] vector, String vectorName) throws InvalidElementException {
for(int i=0; i<vector.length; i++){
if (Double.isNaN(vector[i]) || Double.isInfinite(vector[i])) {
throw new InvalidElementException(vectorName + " element " + i + " is " + vector[i]);
}
}
}
public static class InvalidElementException extends RuntimeException {
private static final long serialVersionUID = 1647150702529757545L;
public InvalidElementException(String s) {
super(s);
}
}
}
View on GitHub (pinned to 1b7edd19c4)