关于spaCy生成非词汇词向量的原理及向量维度调整的技术咨询
Great questions! Let's unpack each one clearly:
1. How spaCy generates vectors for non-word strings
When you feed random gibberish like fjoeij into spaCy, the way it produces a vector depends on the model you're using. For the en_core_web_sm model you're working with:
- It uses a hash embedding system. Every token you input gets a hash calculated, and that hash is used to look up a position in a pre-trained embedding table.
- For out-of-vocabulary (OOV) tokens (like your random strings), the hash maps to some entry in that table, which gives you the 96-dimensional vector you see. No fancy subword breakdown here—just a deterministic hash-to-vector mapping built into the small model.
- If you switched to a larger model like
en_core_web_lgoren_core_web_trf, things change. Those models use subword segmentation (think Byte Pair Encoding) to split OOV tokens into smaller, known subword pieces. The final vector is then a combination of the vectors for those subwords.
2. Can you modify the dimension of word vectors?
This depends on whether you're using a pre-built spaCy model or rolling your own:
- Pre-trained spaCy models: No, you can't change the vector dimension. Each model's vector size is fixed during training—
en_core_web_smuses 96 dimensions,en_core_web_lguses 300, and transformer-based models likeen_core_web_trfmatch their underlying Transformer's output (e.g., 768 for BERT-base). These are hardcoded into the model's architecture. - Custom models/embeddings: Yes, you absolutely can. Here's how:
- Use the
spacy init vectorscommand to create a custom vector table from your own corpus or external word vectors, specifying your desired dimension (like 128, 256, etc.). You can then train a spaCy model using this custom vector set. - When building a custom spaCy model from scratch, you can set the
vectors_dimparameter in your config file to define the embedding dimension, then train the embedding layer alongside the rest of the model.
- Use the
内容的提问来源于stack exchange,提问作者Feng Chen
相关产品推荐
相关产品推荐

