MCPcopy Index your code
hub / github.com/lazyprogrammer/machine_learning_examples / find_analogies

Function find_analogies

nlp_class2/util.py:47–70  ·  view source on GitHub ↗
(w1, w2, w3, We, word2idx, idx2word)

Source from the content-addressed store, hash-verified

45
46# fast version
47def find_analogies(w1, w2, w3, We, word2idx, idx2word):
48 V, D = We.shape
49
50 king = We[word2idx[w1]]
51 man = We[word2idx[w2]]
52 woman = We[word2idx[w3]]
53 v0 = king - man + woman
54
55 for dist in ('euclidean', 'cosine'):
56 distances = pairwise_distances(v0.reshape(1, D), We, metric=dist).reshape(V)
57 # idx = distances.argmin()
58 # best_word = idx2word[idx]
59 idx = distances.argsort()[:4]
60 best_idx = -1
61 keep_out = [word2idx[w] for w in (w1, w2, w3)]
62 for i in idx:
63 if i not in keep_out:
64 best_idx = i
65 break
66 best_word = idx2word[best_idx]
67
68
69 print("closest match by", dist, "distance:", best_word)
70 print(w1, "-", w2, "=", best_word, "-", w3)
71
72
73class Tree:

Callers 5

glove_theano.pyFile · 0.90
glove_svd.pyFile · 0.90
glove_tf.pyFile · 0.90
mainFunction · 0.90
glove.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected