Gensim most similar word to vector

Viewed 1459

I am using the pretrained word vectors from Wikipedia, "glove-wiki-gigaword-100", in Gensim. As this example documentation shows, you can query the most similar words for a given word or set of words using

model_gigaword.wv.most_similar(positive=['dirty','grimy'],topn=10)

However, I would like to query the most similar words to a given vector, specified as an array (of the same format as a word-vector from the pretrained model). For example, the result from adding or subtracting two word-vectors in the pretrained model, like

vec = model_gigaword['king']-model_gigaword['man']

Output: (for vec)

array([-0.696     , -1.26119   , -0.49109   ,  0.91179   ,  0.23077281,
       -0.18835002, -0.65568995, -0.29686698, -0.60074997, -1.35762   ,
       -0.11816999,  0.01779997, -0.74096   ,  0.21192   , -0.407071  ,
       -1.04871   , -0.480674  , -0.95541   , -0.06046999,  0.20678002,
       -1.1516    , -0.98955095,  0.44508   ,  0.32682198, -0.03306001,
       -0.31138003,  0.87721   ,  0.34279   ,  0.78621   , -0.297459  ,
        0.529243  , -0.07398   ,  0.551844  ,  0.54218   , -0.39394   ,
        0.96368   ,  0.22518003,  0.05197001, -0.912573  , -0.718755  ,
        0.08056   ,  0.421177  , -0.34256   , -0.71294   , -0.25391   ,
       -0.65362   , -0.31369498,  0.216278  ,  0.41873002, -0.21784998,
        0.21340999,  0.480393  ,  0.47077006, -1.00272   ,  0.16624999,
       -0.07340002,  0.09219003, -0.02021003, -0.58403   , -0.47306   ,
        0.05066001, -0.64416003,  0.80061007,  0.224344  , -0.20483994,
       -0.33785298, -1.24589   ,  0.08900005, -0.08385998, -0.195515  ,
        0.08500999, -0.55749   ,  0.19473001, -0.0751    , -0.61184   ,
       -0.08018   , -0.34303   ,  1.03759   , -0.36085004,  0.93508005,
       -0.00997001, -0.57282   ,  0.33101702,  0.271261  ,  0.47389007,
        1.1219599 , -0.00199997, -1.609     ,  0.57377803, -0.17023998,
       -0.22913098, -0.33818996, -0.367797  ,  0.367965  , -1.08955   ,
       -0.664806  ,  0.05213001,  0.40829998,  0.125692  , -0.44967002],
      dtype=float32)

How do I get the most similar words to vec?

1 Answers

You can directly use this with model_gigaword.wv.most_similar

your_word_vector = np.array([-0.696, -1.26119, -0.49109, 0.91179, 0.23077281,
       -0.18835002, -0.65568995, -0.29686698, -0.60074997, -1.35762   ,
       -0.11816999,  0.01779997, -0.74096   ,  0.21192   , -0.407071  ,
       -1.04871   , -0.480674  , -0.95541   , -0.06046999,  0.20678002,
       -1.1516    , -0.98955095,  0.44508   ,  0.32682198, -0.03306001,
       -0.31138003,  0.87721   ,  0.34279   ,  0.78621   , -0.297459  ,
        0.529243  , -0.07398   ,  0.551844  ,  0.54218   , -0.39394   ,
        0.96368   ,  0.22518003,  0.05197001, -0.912573  , -0.718755  ,
        0.08056   ,  0.421177  , -0.34256   , -0.71294   , -0.25391   ,
       -0.65362   , -0.31369498,  0.216278  ,  0.41873002, -0.21784998,
        0.21340999,  0.480393  ,  0.47077006, -1.00272   ,  0.16624999,
       -0.07340002,  0.09219003, -0.02021003, -0.58403   , -0.47306   ,
        0.05066001, -0.64416003,  0.80061007,  0.224344  , -0.20483994,
       -0.33785298, -1.24589   ,  0.08900005, -0.08385998, -0.195515  ,
        0.08500999, -0.55749   ,  0.19473001, -0.0751    , -0.61184   ,
       -0.08018   , -0.34303   ,  1.03759   , -0.36085004,  0.93508005,
       -0.00997001, -0.57282   ,  0.33101702,  0.271261  ,  0.47389007,
        1.1219599 , -0.00199997, -1.609     ,  0.57377803, -0.17023998,
       -0.22913098, -0.33818996, -0.367797  ,  0.367965  , -1.08955   ,
       -0.664806  ,  0.05213001,  0.40829998,  0.125692  , -0.44967002])

model_gigaword.wv.most_similar(positive=[your_word_vector], topn=10)
[('vajiravudh', 0.7130449414253235),
 ('prajadhipok', 0.6764554381370544),
 ('andrianampoinimerina', 0.6474215984344482),
 ('jeongjo', 0.6449092626571655),
 ('taejong', 0.6352322697639465),
 ('rehoboam', 0.6319528818130493),
 ('injo', 0.6317901611328125),
 ('gojong', 0.6302404999732971),
 ('seonjo', 0.6272163391113281),
 ('elessar', 0.6250109672546387)]

These results will be almost garbage, as expected. Read the reason below.


One important point though. I see you are trying to find the words that are similar to the difference vector in the euclidean space of the word vectors. The difference between king and man results in a vector that is similar to the difference between queen and woman means that the length and direction of the difference vector encode the contextual difference between the 2 respective pairs of words.

The literal position of that vector maybe garbage because by checking it in the euclidean space, you will anchor it on the origin. Both the difference vectors (King->Man and Queen->Woman) above are anchored on 'King' and 'Queen' respectively.

The intuition you should have is that A->B and C->D may have similar vectors connecting them even though A, B and C, D may line in completely separate parts of the euclidean space, IF they have a similar contextual difference between them. This is what the vector space in a properly trained word2vec is encoding.

enter image description here

Related