how does spacy generate word vector even that is not a word?

Viewed 349

Plz take a look at my code:

import spacy
nlp = spacy.load('en_core_web_sm')
a=nlp('fjoeij foeijfo foejf')
a[0].vector
array([ 4.4017673 ,  2.2732968 ,  1.8201342 ,  0.285315  ,  4.301816  ,
       -1.6101733 , -1.9763635 ,  0.7946968 , -0.49857655,  2.4312825 ,
       -0.5321884 ,  1.8810408 , -2.4434211 , -0.1565853 , -0.3007983 ,
       -0.5251692 , -0.47107434,  2.392819  , -2.1653428 ......

You can see that they are not words, I just randomly typed in. But spacy can still generate the vectors. So, I have two questions:

  1. How spacy generate vectors?
  2. All the vectors are 96 dimensions. Can I change the number of dimensions?
1 Answers

The sm models don't have static word vectors, so token.vector returns context-sensitive tensors from the tok2vec model as a backoff. The dimensions setting comes from the tok2vec model parameters and can't be changed after the model is initialized and trained.

These tensors are useful for the tagger/parser/etc. components in the pipeline, but probably aren't that useful otherwise, e.g. for similarity comparisons, where you'd be better off using a md or lg model with static word vectors.

See: https://spacy.io/usage/linguistic-features#vectors-similarity

Related