stanfordnlp/CoreNLP · error · RuntimeException

Cannot create random word vectors for an unknown numHid

Error message

Cannot create random word vectors for an unknown numHid

What it means

initRandomWordVectors fills in vectors for words not present in the pretrained embeddings using a Gaussian of dimension op.numHid. If numHid is 0 (unknown hidden-layer size), random vectors of the right dimension cannot be generated, so it throws RuntimeException.

Solutions

  1. Set op.numHid to the desired hidden dimension (must match your word vectors, e.g. 25/50/100/300) before constructing the model
  2. Load a word vector file so the model can derive the vector size instead of relying on random init

Example fix

// before
RNNOptions op = new RNNOptions(); // numHid defaults to 0
SentimentModel model = new SentimentModel(op, trainingTrees);
// after
RNNOptions op = new RNNOptions();
op.numHid = 25;
SentimentModel model = new SentimentModel(op, trainingTrees);
Defensive patterns

Strategy: validation

Validate before calling

if (op.numHid <= 0) {
  throw new IllegalArgumentException("Set op.numHid (e.g. 25/50/100/300) before building a SentimentModel with random word vectors");
}

Try / catch

try {
  model = new SentimentModel(op, trainingTrees);
} catch (RuntimeException e) {
  if (e.getMessage().contains("unknown numHid")) {
    op.numHid = 25;
    model = new SentimentModel(op, trainingTrees);
  } else { throw e; }
}

Prevention

When it happens

Trigger: Creating a SentimentModel with random word-vector initialization (no word vector file supplying all words) while op.numHid == 0, i.e. numHid was never set and there is no embedding file to infer it from.

Common situations: Building a model from scratch without specifying numHid; forgetting to set numHid in RNNOptions when not loading word vectors; numHid reset by default options.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/sentiment/SentimentModel.java:482

    SimpleMatrix score = new SimpleMatrix(numClasses, numHid + 1);
    double range = 1.0 / (Math.sqrt((double) numHid));
    score.insertIntoThis(0, 0, SimpleMatrix.random_DDRM(numClasses, numHid, -range, range, rand));
    // bias column goes from 0 to 1 initially
    score.insertIntoThis(0, numHid, SimpleMatrix.random_DDRM(numClasses, 1, 0.0, 1.0, rand));
    return score.scale(op.trainOptions.scalingForInit);
  }

  SimpleMatrix randomWordVector() {
    return randomWordVector(op.numHid, rand);
  }

  static SimpleMatrix randomWordVector(int size, Random rand) {
    return NeuralUtils.randomGaussian(size, 1, rand).scale(0.1);
  }

  void initRandomWordVectors(List<Tree> trainingTrees) {
    if (op.numHid == 0) {
      throw new RuntimeException("Cannot create random word vectors for an unknown numHid");
    }
    Set<String> words = Generics.newHashSet();
    words.add(UNKNOWN_WORD);
    for (Tree tree : trainingTrees) {
      List<Tree> leaves = tree.getLeaves();
      for (Tree leaf : leaves) {
        String word = leaf.label().value();
        if (op.lowercaseWordVectors) {
          word = word.toLowerCase();
        }
        words.add(word);
      }
    }
    this.wordVectors = Generics.newTreeMap();
    for (String word : words) {
      SimpleMatrix vector = randomWordVector();
      wordVectors.put(word, vector);
    }

View on GitHub (pinned to 1b7edd19c4)