Natural Language Processing with PyTorch by Delip Rao

Natural Language Processing with PyTorch by Delip Rao

Author:Delip Rao
Language: eng
Format: epub
Publisher: O'Reilly Media
Published: 2019-01-23T16:00:00+00:00


Input[0]

nn_indices = self.index.get_nns_by_vector(vector, n) return [self.index_to_word[neighbor] for neighbor in nn_indices] def compute_and_print_analogy(self, word1, word2, word3): """Prints the solutions to analogies using word embeddings Analogies are word1 is to word2 as word3 is to __ This method will print: word1 : word2 :: word3 : word4 Args: word1 (str) word2 (str) word3 (str) """ vec1 = self.get_embedding(word1) vec2 = self.get_embedding(word2) vec3 = self.get_embedding(word3) # Simple hypothesis: Analogy is a spatial relationship spatial_relationship = vec2 - vec1 vec4 = vec3 + spatial_relationship closest_words = self.get_closest_to_vector(vec4, n=4) existing_words = set([word1, word2, word3]) closest_words = [word for word in closest_words if word not in existing_words] if len(closest_words) == 0: print("Could not find nearest neighbors for the vector!") return for word4 in closest_words: print("{} : {} :: {} : {}".format(word1, word2, word3, word4))



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.