Skip to content

Instantly share code, notes, and snippets.

@VXU1230
Last active March 19, 2019 22:58
Show Gist options
  • Save VXU1230/c0a68009fed196fcdb7c3e0e919d7e2e to your computer and use it in GitHub Desktop.
Save VXU1230/c0a68009fed196fcdb7c3e0e919d7e2e to your computer and use it in GitHub Desktop.
Calculate Word Similarity
NUM_SIM = 5
def get_similarity(sim_examples, embed_weights):
norm = tf.sqrt(tf.reduce_sum(tf.square(embed_weights), 1, keepdims=True))
norm_embed_matrix = embed_weights / norm
valid_embed = tf.nn.embedding_lookup(norm_embed_matrix, sim_examples)
sim_matrix = tf.matmul(valid_embed, tf.transpose(norm_embed_matrix))
return sim_matrix
def print_eval(valid_examples, sim_matrix, reverse_dic):
top_k = 3
for i in range(NUM_SIM):
valid_word = reverse_dic[valid_examples[i]]
nearest = np.argsort(-sim_matrix[i, :])[1:top_k + 1]
log = '\nNearest to %s:' % valid_word
for k in range(top_k):
close_word = reverse_dic[nearest[k]]
log = '%s %s,' % (log, close_word)
print(log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment