{"record":{"id":"1e39c1a4508bbd9e","repo":"stanfordnlp/CoreNLP","slug":"vectorname-element-i-is-vector-i","errorCode":null,"errorMessage":"vectorName + \" element \" + i + \" is \" + vector[i]","messagePattern":"vectorName \\+ \" element \" \\+ i \\+ \" is \" \\+ vector\\[i\\]","errorType":"exception","errorClass":"InvalidElementException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/ArrayMath.java","lineNumber":2134,"sourceCode":"  public static void multiplyInto(double[] a, double[] b, double c) {\n    for (int i=0; i<a.length; i++) {\n      a[i] = b[i] * c;\n    }\n  }\n\n  public static double entropy(double[] probs) {\n    double e = 0.0;\n    for (double p : probs) {\n      if (p != 0.0)\n        e -= p * Math.log(p);\n    }\n    return e;\n  }\n\n  public static void assertFinite(double[] vector, String vectorName) throws InvalidElementException {\n    for(int i=0; i<vector.length; i++){\n      if (Double.isNaN(vector[i]) || Double.isInfinite(vector[i])) {\n        throw new InvalidElementException(vectorName + \" element \" + i + \" is \" + vector[i]);\n      }\n    }\n  }\n\n  public static class InvalidElementException extends RuntimeException {\n\n    private static final long serialVersionUID = 1647150702529757545L;\n\n    public InvalidElementException(String s) {\n      super(s);\n    }\n  }\n\n}\n","sourceCodeStart":2116,"sourceCodeEnd":2149,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/ArrayMath.java#L2116-L2149","documentation":"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.","triggerScenarios":"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.","commonSituations":"Degenerate training data, learning rates too large causing overflow, uninitialized arrays filled with NaN, or failed normalization producing division by zero.","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"],"exampleFix":"// before\nArrayMath.assertFinite(vector, \"weights\"); // throws: weights element 7 is NaN\n// after\nfor (int i = 0; i < vector.length; i++) {\n  if (!Double.isFinite(vector[i])) vector[i] = 0.0; // or log & fix upstream\n}\nArrayMath.assertFinite(vector, \"weights\");","handlingStrategy":"try-catch","validationCode":"boolean finite = true;\nfor (double v : vector) { if (Double.isNaN(v) || Double.isInfinite(v)) { finite = false; break; } }","typeGuard":"static boolean isFiniteVector(double[] v) {\n  for (double d : v) if (Double.isNaN(d) || Double.isInfinite(d)) return false;\n  return true;\n}","tryCatchPattern":"try {\n  ArrayMath.assertFinite(vector, \"weights\");\n} catch (ArrayMath.InvalidElementException e) {\n  log.error(\"non-finite vector: \" + e.getMessage(), e);\n  // quarantine/recompute the vector\n}","preventionTips":["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"],"tags":["java","numerical","nan","assertion"],"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"}