Skip to content

Instantly share code, notes, and snippets.

@kostyaev
Created November 11, 2020 13:25
Show Gist options
  • Save kostyaev/9bf3bdd88c4560ed06d2e76c3aa06faa to your computer and use it in GitHub Desktop.
Save kostyaev/9bf3bdd88c4560ed06d2e76c3aa06faa to your computer and use it in GitHub Desktop.
Losses and metrics
def euler_loss(y_true, y_pred):
dist1 = tf.abs(y_true - y_pred)
dist2 = tf.abs(2*np.pi + y_true - y_pred)
dist3 = tf.abs(-2*np.pi + y_true - y_pred)
loss = tf.where(dist1<dist2, dist1, dist2)
loss = tf.where(loss<dist3, loss, dist3)
return tf.reduce_mean(loss)
def quaternion_loss(y_true, y_pred):
dist1 = tf.reduce_mean(tf.abs(y_true-y_pred), axis=-1)
dist2 = tf.reduce_mean(tf.abs(y_true+y_pred), axis=-1)
loss = tf.where(dist1<dist2, dist1, dist2)
return tf.reduce_mean(loss)
def mean_angle_btw_vectors(v1, v2, eps = 1e-8):
dot_product = tf.reduce_sum(v1*v2, axis=-1)
cos_a = dot_product / (tf.norm(v1, axis=-1) * tf.norm(v2, axis=-1))
cos_a = tf.clip_by_value(cos_a, -1 + eps, 1 - eps)
angle_dist = tf.math.acos(cos_a) / np.pi * 180.0
return tf.reduce_mean(angle_dist)
@funmonty
Copy link

In Euler loss, are the inputs in radians or in degrees? If radians, is it in the range [-pi,pi] or [0, 2pi]?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment