Skip to content

Instantly share code, notes, and snippets.

@psycharo-zz
Last active October 23, 2018 05:43
Show Gist options
  • Select an option

  • Save psycharo-zz/ca7633f50b0aef8de0096a61d868fbf0 to your computer and use it in GitHub Desktop.

Select an option

Save psycharo-zz/ca7633f50b0aef8de0096a61d868fbf0 to your computer and use it in GitHub Desktop.
pairwise functions for TF
def pdist(X):
"""
Computes pairwise distance between each pair of points
Args:
X - [N,D] matrix representing N D-dimensional vectors
Returns:
[N,N] matrix of (squared) Euclidean distances
"""
x2 = tf.reduce_sum(X * X, 1, True)
return x2 - 2 * tf.matmul(X, tf.transpose(X)) + tf.transpose(x2)
def cdist(X, Y):
"""
Computes pairwise distance between each pair of points in two sets
Args:
X - [N,D] matrix representing N D-dimensional vectors
Y - [M,D] matrix representing N D-dimensional vectors
Returns:
[N,M] matrix of (squared) Euclidean distances
"""
return (tf.reduce_sum(X * X, 1, True) -
2 * tf.matmul(X, tf.transpose(Y)) +
tf.transpose(tf.reduce_sum(Y * Y, 1, True)))
def contrastive_loss(D, labels, margin):
"""
computes contrastive loss:
L = 0.5 * (1 - labels) * D + 0.5 * labels * {max(0, margin - D^0.5)}^2
where D is a matrix of (squared) euclidean distances
Args:
D - [N,N] tensor, euclidean distances
labels - [N,N] tensor, labels {0,1}
margin - scalar, contrastive margin
Returns:
scalar, the average loss
"""
# TODO: we definitely need some kind of weight/mask for the samples
loss = (tf.mul((1.0 - labels), D) +
tf.mul(labels, tf.square(tf.maximum(0.0, margin - tf.sqrt(D)))))
loss = 0.5 * tf.reduce_mean(loss)
return loss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment