Skip to content

Instantly share code, notes, and snippets.

@gaphex
Last active June 28, 2019 19:20
Show Gist options
  • Save gaphex/915f47fb5723808d8057202c0c21d5ef to your computer and use it in GitHub Desktop.
Save gaphex/915f47fb5723808d8057202c0c21d5ef to your computer and use it in GitHub Desktop.
Q = tf.placeholder("float", [dim])
S = tf.placeholder("float", [None, dim])
Qr = tf.reshape(Q, (1, -1))
PP = tf.keras.backend.batch_dot(S, S, axes=1)
QQ = tf.matmul(Qr, tf.transpose(Qr))
PQ = tf.matmul(S, tf.transpose(Qr))
distance = PP - 2 * PQ + QQ
distance = tf.sqrt(tf.reshape(distance, (-1,)))
top_neg_dists, top_indices = tf.math.top_k(tf.negative(distance), k=top_k)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment