In statistics, the Huber loss is a loss function used in robust regression, that is less sensitive to outliers in data than the squared error loss. A variant for classification is also sometimes used.
def huber_fn(y_true, y_pred):
error = y_true - y_pred
is_small_error = tf.abs(error) < 1
squared_loss = tf.square(error) / 2
linear_loss = tf.abs(error) - 0.5
return tf.where(is_small_error, squared_loss, linear_loss)
注意,自定义损失函数的返回值是一个向量而不是损失平均值,每个元素对应一个实例。这样的好处是Keras可以通过class_weight
或sample_weight
调整权重。
huber_fn(y_valid, y_pred)
<tf.Tensor: id=4894, shape=(3870, 1), dtype=float64, numpy=
array([[0.10571115],
[0.03953311],
[0.02417886],
...,
[0.00039475],
[0.00245003],
[0.12238744]])>