stanfordnlp/CoreNLP · error · IllegalStateException

Testing of stochastic objective function failed.

Error message

Testing of stochastic objective function failed.

What it means

With flags.testObjFunction=true, training runs StochasticDiffFunctionTester.testSumOfBatches on the CRF objective with tolerance 1e-4 before minimization. If the test fails (per-batch sums of the stochastic objective are inconsistent with the full objective), an IllegalStateException is thrown, indicating the stochastic objective setup is unreliable and training will not proceed.

Solutions

  1. Fix the objective/gradient implementation the tester flagged before training.
  2. Adjust stochastic settings (batch size, gain) that make batch sums inconsistent.
  3. Run the test on a small, clean dataset to localize the mismatch.
  4. Remove testObjFunction once validation passes — it is a debug-only gate.

Example fix

// before
props.setProperty("testObjFunction", "true"); // blocks training on failure
// after (once the objective is verified)
props.remove("testObjFunction");
Defensive patterns

Strategy: try-catch

Validate before calling

// enable the objective test only on small, clean datasets
boolean clean = features.stream().noneMatch(v -> Double.isNaN(v) || Double.isInfinite(v));
if ("true".equals(props.getProperty("testObjFunction")) && !clean) {
  throw new IllegalArgumentException("NaN/Inf features will make the objective test fail");
}

Try / catch

try {
  classifier.train(files);
} catch (IllegalStateException e) {
  if ("Testing of stochastic objective function failed.".equals(e.getMessage())) {
    log.warn("Stochastic objective test failed; fix batch/objective settings before training");
    props.remove("testObjFunction"); // after investigation
  } else throw e;
}

Prevention

When it happens

Trigger: Setting testObjFunction=true and tester.testSumOfBatches returns false — typically misconfigured stochastic batch settings or a custom objective whose batch decomposition doesn't sum correctly.

Common situations: Debugging custom CRF objectives or feature transforms; verifying refactored objective implementations; testing with inappropriate batch sizes or data scaling.

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


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/4c70f99179f1cbb4. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ie/crf/CRFClassifier.java:1867

    if (flags.initialWeights == null) {
      initialWeights = func.initial();
    } else {
      try {
        log.info("Reading initial weights from file " + flags.initialWeights);
        DataInputStream dis = IOUtils.getDataInputStream(flags.initialWeights);
        initialWeights = ConvertByteArray.readDoubleArr(dis);
      } catch (IOException e) {
        throw new RuntimeException("Could not read from double initial weight file " + flags.initialWeights);
      }
    }
    log.info("numWeights: " + initialWeights.length);

    if (flags.testObjFunction) {
      StochasticDiffFunctionTester tester = new StochasticDiffFunctionTester(func);
      if (tester.testSumOfBatches(initialWeights, 1e-4)) {
        log.info("Successfully tested stochastic objective function.");
      } else {
        throw new IllegalStateException("Testing of stochastic objective function failed.");
      }

    }
    //check gradient
    if (flags.checkGradient) {
      if (func.gradientCheck()) {
        log.info("gradient check passed");
      } else {
        throw new RuntimeException("gradient check failed");
      }
    }
    return minimizer.minimize(func, flags.tolerance, initialWeights);
  }

  public Minimizer<DiffFunction> getMinimizer() {
    return getMinimizer(0, null);
  }

View on GitHub (pinned to 1b7edd19c4)