{"record":{"id":"f5aacb59b564c567","repo":"stanfordnlp/CoreNLP","slug":"cannot-create-random-word-vectors-for-an-unknown-n","errorCode":null,"errorMessage":"Cannot create random word vectors for an unknown numHid","messagePattern":"Cannot create random word vectors for an unknown numHid","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/sentiment/SentimentModel.java","lineNumber":482,"sourceCode":"    SimpleMatrix score = new SimpleMatrix(numClasses, numHid + 1);\n    double range = 1.0 / (Math.sqrt((double) numHid));\n    score.insertIntoThis(0, 0, SimpleMatrix.random_DDRM(numClasses, numHid, -range, range, rand));\n    // bias column goes from 0 to 1 initially\n    score.insertIntoThis(0, numHid, SimpleMatrix.random_DDRM(numClasses, 1, 0.0, 1.0, rand));\n    return score.scale(op.trainOptions.scalingForInit);\n  }\n\n  SimpleMatrix randomWordVector() {\n    return randomWordVector(op.numHid, rand);\n  }\n\n  static SimpleMatrix randomWordVector(int size, Random rand) {\n    return NeuralUtils.randomGaussian(size, 1, rand).scale(0.1);\n  }\n\n  void initRandomWordVectors(List<Tree> trainingTrees) {\n    if (op.numHid == 0) {\n      throw new RuntimeException(\"Cannot create random word vectors for an unknown numHid\");\n    }\n    Set<String> words = Generics.newHashSet();\n    words.add(UNKNOWN_WORD);\n    for (Tree tree : trainingTrees) {\n      List<Tree> leaves = tree.getLeaves();\n      for (Tree leaf : leaves) {\n        String word = leaf.label().value();\n        if (op.lowercaseWordVectors) {\n          word = word.toLowerCase();\n        }\n        words.add(word);\n      }\n    }\n    this.wordVectors = Generics.newTreeMap();\n    for (String word : words) {\n      SimpleMatrix vector = randomWordVector();\n      wordVectors.put(word, vector);\n    }","sourceCodeStart":464,"sourceCodeEnd":500,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/sentiment/SentimentModel.java#L464-L500","documentation":"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.","triggerScenarios":"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.","commonSituations":"Building a model from scratch without specifying numHid; forgetting to set numHid in RNNOptions when not loading word vectors; numHid reset by default options.","solutions":["Set op.numHid to the desired hidden dimension (must match your word vectors, e.g. 25/50/100/300) before constructing the model","Load a word vector file so the model can derive the vector size instead of relying on random init"],"exampleFix":"// before\nRNNOptions op = new RNNOptions(); // numHid defaults to 0\nSentimentModel model = new SentimentModel(op, trainingTrees);\n// after\nRNNOptions op = new RNNOptions();\nop.numHid = 25;\nSentimentModel model = new SentimentModel(op, trainingTrees);","handlingStrategy":"validation","validationCode":"if (op.numHid <= 0) {\n  throw new IllegalArgumentException(\"Set op.numHid (e.g. 25/50/100/300) before building a SentimentModel with random word vectors\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  model = new SentimentModel(op, trainingTrees);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"unknown numHid\")) {\n    op.numHid = 25;\n    model = new SentimentModel(op, trainingTrees);\n  } else { throw e; }\n}","preventionTips":["Always set numHid explicitly in RNNOptions","Keep numHid consistent with any embedding files used later","Fail fast on numHid==0 at application configuration load time"],"tags":["java","stanford-nlp","sentiment","configuration"],"backgroundTag":"missing-required-config-field","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"}