Efficiently Finding Closest Word In TensorFlow Embedding : <p>Recently, I've been trying to find the closest word to an embedding. The two most notable ways of doing this is by cosine distance or euclidean distance.</p>

<p>I'm trying to find how to efficiently compute the cosine distance for a tensor of shape <code>[batch_size x embedding_size]</code></p>

<p>One approach is to unpack the tensor and the compute the cosine distance</p>

<pre><code>  #embedding is shape [vocab_size x embedding size]
  array_list = tf.unpack(batch_array)
  word_class_list = tf.unpack(embedding)
  index_list_of_closest_word = []
  for eacharray in array_list:
    list_of_distances = []
    for eachwordclass in word_class_list:
      list_of_distances.append(cosine_distance(eacharray, eachwordclass))
    index_list_of_closest_word.append(tf.argmax(tf.pack(list_of_distances)))
</code></pre>

<p>However, this approach is terribly inefficient. Is there perhaps a more efficient manner to do this? I know word2vec does this pretty fast and tensorflow, with the power of a gpu, should be able to do these batch calculations in parallel. </p>

<p>Thanks!</p>
