def huber_loss(y_true, y_pred, clip_value):
# Huber loss, see https://en.wikipedia.org/wiki/Huber_loss and
# https://medium.com/@karpathy/yes-you-should-understand-backprop-e2f06eab496b
# for details.
assert clip_value > 0.
x = y_true - y_pred
if np.isinf(clip_value):
# Spacial case for infinity since Tensorflow does have problems
# if we compare `K.abs(x) < np.inf`.
return .5 * tf.square(x)
condition = tf.abs(x) < clip_value
squared_loss = .5 * tf.square(x)
linear_loss = clip_value * (tf.abs(x) - .5 * clip_value)
return tf.where(condition, squared_loss, linear_loss) # condition, true, false
评论列表
文章目录