stanfordnlp/CoreNLP · error · RuntimeException

Word vectors file has dimension too small for requested numH

Error message

Word vectors file has dimension too small for requested numHid of 

What it means

Embedding.loadWordVectors parses a word-vector text file and compares each row's numeric width (dimOfWords) to the requested embedding size (embeddingSize / numHid). If the file's vectors are narrower than the requested dimension, it cannot pad them, so it throws this RuntimeException. It is thrown only when the file dimension is strictly smaller; larger dimensions are truncated with a warning instead.

Solutions

  1. Regenerate/download a word-vector file whose dimension equals (or exceeds) the requested numHid
  2. Lower the numHid/embeddingSize parameter to match the vector file's dimension
  3. Check the vectors file header (first line is often 'vocab dim') and set numHid accordingly

Example fix

// before
Embedding embedding = new Embedding("vectors-50d.txt", 100);
// after
Embedding embedding = new Embedding("vectors-50d.txt", 50); // match file dimension
Defensive patterns

Strategy: validation

Validate before calling

int fileDim = getVectorDimension(vectorsFile); // parse header or first line
if (fileDim < numHid) throw new IllegalArgumentException("vectors dim " + fileDim + " < numHid " + numHid);

Try / catch

try { new Embedding(file, numHid); } catch (RuntimeException e) { if (e.getMessage().startsWith("Word vectors file has dimension too small")) { /* fix numHid or file */ } else throw e; }

Prevention

When it happens

Trigger: Calling an Embedding constructor (or loadWordVectors) with a numHid/embeddingSize larger than the number of numeric columns in the supplied word-vector file, e.g. word2vec vectors of dim 50 with numHid=100.

Common situations: Mismatch between a models/config file that says numHid=100 and a pre-trained vectors file of dimension 50 or 300; downloading vectors of the wrong dimension for the task.

Understand the failure class

Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/neural/Embedding.java:157

      if(word.equals("</s>")){
        word = END_WORD;
      }

      int dimOfWords = lineSplit.length - 1;
      if (embeddingSize <= 0) {
        embeddingSize = dimOfWords;
        log.info("  detected embedding size = " + dimOfWords);
      }
      // the first entry is the word itself
      // the other entries will all be entries in the word vector
      if (dimOfWords > embeddingSize) {
        if (!warned) {
          warned = true;
          log.info("WARNING: Dimensionality of numHid parameter and word vectors do not match, deleting word vector dimensions to fit!");
        }
        dimOfWords = embeddingSize;
      } else if (dimOfWords < embeddingSize) {
        throw new RuntimeException("Word vectors file has dimension too small for requested numHid of " + embeddingSize);
      }
      double[][] vec = new double[dimOfWords][1];
      for (int i = 1; i <= dimOfWords; i++) {
        vec[i-1][0] = Double.parseDouble(lineSplit[i]);
      }
      SimpleMatrix vector = new SimpleMatrix(vec);
      wordVectors.put(word, vector);

      numWords++;
    }
    log.info("  num words = " + numWords);
  }

  /**
   * This method takes as input two files: wordFile (one word per line) and a raw word vector file
   * with a given expected size, and returns a map of word to vector.
   * <p>
   * The word vector file should be in the format <br>

View on GitHub (pinned to 1b7edd19c4)