Huber Loss in TensorFlow

PHOTO EMBED

Saved by @baristageek #python

x = tf.random_normal([300], mean = 5, stddev = 1)
y = tf.random_normal([300], mean = 5, stddev = 1)
avg = tf.reduce_mean(x - y)
cond = tf.less(avg, 0)
left_op = tf.reduce_mean(tf.square(x-y))
right_op = tf.reduce_mean(tf.abs(x-y))
out = tf.where(cond, left_op, right_op) #tf.select() has been fucking deprecated
content_copyCOPY

Gets the mean squared error of (x - y) if the average of all elements in (x - y) is negative, or the sum of the absolute value of all elements in the tensor (x - y) otherwise. See Huber loss: https://en.wikipedia.org/wiki/Huber_loss