huber_loss

paddle.fluid.layers.huber_loss(input, label, delta)[source]

This operator computes the Huber loss between input and label. Huber loss is commonly used in regression tasks. Compared to square_error_cost, Huber loss is more robust and less sensitivity to outliers.

When the absolute difference between input and label is greater than delta, the linear error is calculated:

\[huber\_loss = delta * (label - input) - 0.5 * delta * delta\]

When the absolute difference between input and label is greater than delta, the square error is calculated:

\[huber\_loss = 0.5 * (label - input) * (label - input)\]
Parameters
  • input (Variable) – Predicted data, 2D-Tensor with the shape of [batch_size, 1]. The data type should be float32 or float64.

  • label (Variable) – Ground truth label, 2D-Tensor with the shape of [batch_size, 1]. The data type should be float32 or float64.

  • delta (float) – The threshold for Huber loss, which is used to control the balance between the linear error and square error. The data type should be float32.

Returns

The huber loss, a tensor with the same shape and data type as input.

Return type

Variable

Examples:

import paddle.fluid as fluid
import numpy as np

DATATYPE='float32'
input_data = np.array([[1.],[2.],[3.],[4.]]).astype(DATATYPE)
label_data = np.array([[3.],[3.],[4.],[4.]]).astype(DATATYPE)

x = fluid.data(name='input', shape=[None, 1], dtype=DATATYPE)
y = fluid.data(name='label', shape=[None, 1], dtype=DATATYPE)
loss = fluid.layers.huber_loss(input=x, label=y, delta=1.0)

place = fluid.CPUPlace()
#place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)
HuberLoss, = exe.run(feed={'input':input_data ,'label':label_data}, fetch_list=[loss.name])
print(HuberLoss)  #[[1.5], [0.5], [0.5], [0. ]], dtype=float32