MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / analogy

Function analogy

nlp_class2/pmi.py:273–304  ·  view source on GitHub ↗
(pos1, neg1, pos2, neg2)

Source from the content-addressed store, hash-verified

271
272
273def analogy(pos1, neg1, pos2, neg2):
274 # don't actually use pos2 in calculation, just print what's expected
275 print("testing: %s - %s = %s - %s" % (pos1, neg1, pos2, neg2))
276 for w in (pos1, neg1, pos2, neg2):
277 if w not in word2idx:
278 print("Sorry, %s not in word2idx" % w)
279 return
280
281 p1 = W[word2idx[pos1]]
282 n1 = W[word2idx[neg1]]
283 p2 = W[word2idx[pos2]]
284 n2 = W[word2idx[neg2]]
285
286 vec = p1 - n1 + n2
287
288 distances = pairwise_distances(vec.reshape(1, D), W, metric='cosine').reshape(V)
289 idx = distances.argsort()[:10]
290
291 # pick the best that's not p1, n1, or n2
292 best_idx = -1
293 keep_out = [word2idx[w] for w in (pos1, neg1, neg2)]
294 for i in idx:
295 if i not in keep_out:
296 best_idx = i
297 break
298
299 print("got: %s - %s = %s - %s" % (pos1, neg1, top_words[best_idx], neg2))
300 print("closest 10:")
301 for i in idx:
302 print(top_words[i], distances[i])
303
304 print("dist to %s:" % pos2, cos_dist(p2, vec))
305
306
307analogy('king', 'man', 'queen', 'woman')

Callers 1

pmi.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected