Skip to content

Instantly share code, notes, and snippets.

@NISH1001
Created November 15, 2018 07:55
Show Gist options
  • Save NISH1001/2164f985f1d17d2920fe930404ec7436 to your computer and use it in GitHub Desktop.
Save NISH1001/2164f985f1d17d2920fe930404ec7436 to your computer and use it in GitHub Desktop.
Compute Distances between Keras tensors
def euclidean_distance(vects):
x, y = vects
sum_square = K.sum(K.square(x - y), axis=1, keepdims=True)
return K.sqrt(K.maximum(sum_square, K.epsilon()))
def eucl_dist_output_shape(shapes):
shape1, shape2 = shapes
return (shape1[0], 1)
def cosine_distance(vests):
x, y = vests
x = K.l2_normalize(x, axis=-1)
y = K.l2_normalize(y, axis=-1)
return -K.mean(x * y, axis=-1, keepdims=True)
def cos_dist_output_shape(shapes):
shape1, shape2 = shapes
return (shape1[0],1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment