-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Added function "predict_output_word" to predict the output word given the context words. Fixes issue #863. #1209
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
70642f3
added function to predict output word in CBOW from context words
chinmayapancholi13 146c5d2
Merge branch 'develop' of https://github.com/RaRe-Technologies/gensim…
chinmayapancholi13 8d91738
handling negative_sampling case
chinmayapancholi13 fa6d14f
added warnings for out-of-vocabulary and not negative sampling cases
chinmayapancholi13 867d79a
Merge branch 'develop' of https://github.com/RaRe-Technologies/gensim…
chinmayapancholi13 d0aa6dc
added unit tests for predict_output_word
chinmayapancholi13 da9bb32
updated CHANGELOG
chinmayapancholi13 13c6639
Merge branch 'develop' of https://github.com/RaRe-Technologies/gensim…
chinmayapancholi13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1203,6 +1203,27 @@ def similarity(self, w1, w2): | |
def n_similarity(self, ws1, ws2): | ||
return self.wv.n_similarity(ws1, ws2) | ||
|
||
def predict_output_word(self, context_words_list, topn=10): | ||
#verify that required parameters have not been discarded | ||
if not hasattr(self.wv, 'syn0') or not hasattr(self, 'syn1neg'): | ||
raise RuntimeError("Parameters required for predicting the output words not found.") | ||
|
||
word_vocabs = [self.wv.vocab[w] for w in context_words_list if w in self.wv.vocab] | ||
|
||
word2_indices = [] | ||
for pos, word in enumerate(word_vocabs): | ||
word2_indices.append(word.index) | ||
|
||
l1 = np_sum(self.wv.syn0[word2_indices], axis=0) | ||
if word2_indices and self.cbow_mean: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if word_vocabs is empty, then return None with a warning |
||
l1 /= len(word2_indices) | ||
|
||
if self.negative : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please raise exception
|
||
prob_values = exp(dot(l1, self.syn1.T)) # propagate hidden -> output and take softmax to get probabilities | ||
prob_values /= sum(prob_values) | ||
top_indices = matutils.argsort(prob_values, topn=topn, reverse=True) | ||
return [(self.wv.index2word[index1], prob_values[index1]) for index1 in top_indices] #returning the most probable output words with their probabilities | ||
|
||
def init_sims(self, replace=False): | ||
""" | ||
init_sims() resides in KeyedVectors because it deals with syn0 mainly, but because syn1 is not an attribute | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use list comprehension