先看迭代效果图(附带sklearn的LinearRegression对比)
我们可以看到,不同的目标函数对最终的拟合曲线效果也是不一样的。以下是函数代码:
def huber_approx_obj(real, predict):
d = predict - real
h = 1 # h is delta in the graphic
scale = 1 + (d / h) ** 2
scale_sqrt = np.sqrt(scale)
grad = d / scale_sqrt
hess = 1 / scale / scale_sqrt
return grad, hess
相应损失函数图像(当预测值越偏离真实值的时候):