Gensim: Adding Word-to-Context Prediction in Word2Vec (inverse of `predict_output_word()`)

Created on 9 Aug 2018  路  2Comments  路  Source: RaRe-Technologies/gensim

In issue #863 there is the suggestion to predict a word given its contexts.

Another nice feature would be the opposite: given a word, output the probability distribution over contexts (of some window length).

difficulty easy feature

Most helpful comment

Hello @ellliottt,

863 implemented in #1209, you want to have "inverse" method, am I right?

All 2 comments

Hello @ellliottt,

863 implemented in #1209, you want to have "inverse" method, am I right?

I want predict_output_context function too, but I'm not sure how to implement it.

from numpy import exp, dot
from gensim import matutils

def predict_output_context(model, center_word, topn=10):
    word = model.wv.vocab[center_word]
    vec = model.wv.vectors[word.index]
    prob_values = exp(dot(vec, model.trainables.syn1neg.T))
    prob_values /= sum(prob_values)
    top_indices = matutils.argsort(prob_values, topn=topn, reverse=True)
    return [(model.wv.index2word[index1], prob_values[index1]) for index1 in top_indices]

Could you tell me whether this is correct implementation or not?
If not, could you write the correct one?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shubhvachher picture shubhvachher  路  4Comments

k0nserv picture k0nserv  路  3Comments

jeradf picture jeradf  路  4Comments

johann-petrak picture johann-petrak  路  3Comments

bgokden picture bgokden  路  3Comments