Created
November 15, 2018 07:55
-
-
Save NISH1001/2164f985f1d17d2920fe930404ec7436 to your computer and use it in GitHub Desktop.
Compute Distances between Keras tensors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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