机器学习常用loss:L1 loss、L2 loss、smothL1 loss、huber loss
常用loss:
L1:
- 公式:
L
1
=
∑
i
=
1
n
∣
y
i
−
f
(
x
i
)
∣
L1=\sum_{i=1}^{n}\left|y_{i}-f\left(x_{i}\right)\right|
- 导数:
d
L
1
(
x
)
d
x
=
{
1
if
x
≥
0
−
1
otherwise
\frac{\mathrm{d} L_{1}(x)}{\mathrm{d} x}=\left\{\begin{array}{ll}{1} & {\text { if } x \geq 0} \\ {-1} & {\text { otherwise }}\end{array}\right.
L2:
- 公式:
L
2
=
∑
i
=
1
n
(
y
i
−
f
(
x
i
)
)
2
L2=\sum_{i=1}^{n}\left(y_{i}-f\left(x_{i}\right)\right)^{2}
- 导数:
d
L
2
(
x
)
d
x
=
2
x
\frac{\mathrm{d} L_{2}(x)}{\mathrm{d} x}=2 x
- 特性:对离群点比较敏感,需要自习调整学习率,防止出现梯度爆炸的情况(因为两端值很大);
smooth L1:
- 公式:
smooth
L
1
(
x
)
=
{
0.5
x
2
if
∣
x
∣
<
1
∣
x
∣
−
0.5
otherwise
\operatorname{smooth}_{L_{1}}(x)=\left\{\begin{array}{ll}{0.5 x^{2}} & {\text { if }|x|<1} \\ {|x|-0.5} & {\text { otherwise }}\end{array}\right.
- 导数:
d
smooth
L
1
d
x
=
{
x
if
∣
x
∣
<
1
±
1
otherwise
\frac{\mathrm{d} \operatorname{smooth}_{L_{1}}}{\mathrm{d} x}=\left\{\begin{array}{ll}{x} & {\text { if }|x|<1} \\ { \pm 1} & {\text { otherwise }}\end{array}\right.
Huber loss:
- 公式:
L
δ
(
y
,
f
(
x
)
)
=
{
1
2
(
y
−
f
(
x
)
)
2
,
for
∣
y
−
f
(
x
)
∣
≤
δ
δ
⋅
(
∣
y
−
f
(
x
)
∣
−
1
2
δ
)
,
otherwise
L_{\delta}(y, f(x))=\left\{\begin{array}{ll}{\frac{1}{2}(y-f(x))^{2},} & {\text { for }|y-f(x)| \leq \delta} \\ {\delta \cdot\left(|y-f(x)|-\frac{1}{2} \delta\right),} & {\text { otherwise }}\end{array}\right.
- 优点是能增强平方误差损失函数(MSE)对离群点的鲁棒性,用于回归问题。δ是一个可以自己设置的参数。
当预测偏差小于 δ 时,它采用平方误差,
当预测偏差大于 δ 时,采用的线性误差。
[外链图片转存失败(img-TExue3Iw-1568215766580)(./1567858938658.png)]
如上图绿色部分为huber loss。
[外链图片转存失败(img-kAJeSxmv-1568215766580)(./1567846985700.png)]
说明
L1loss在零点不平滑,学习慢;
L2loss学习快,因为是平方增长,但是当预测值太大的时候,会在loss中占据主导位置(如真实值为1,预测多次,有一次预测值为100,其余预测为2);
Smooth L1 Loss 相比L1修改零点不平滑问题,而且在x较大的时候不像L2对异常值敏感,是一个缓慢变化的loss;
Huber loss增强L2对离群点的鲁棒性,因为偏差大的时候变成线性增长;
上图绿色部分为huber loss,紫色部分为L2loss;
上图为smooth L1 loss和L2 loss的导数对比,在两端smooth L1 导数恒定为1,而L2 loss会一直上升;
另外L1和L2范数常用于正则化项:
L1正则会制造稀疏的特征,大部分无用特征的权重会被置为0,有特征选择作用;
L2正则会让特征的权重不过大,使得特征的权重比较平均。
上图为1范数和2范数的图像,可以看出L1正则倾向于选择坐标轴上的参数(即出现0为稀疏解),L2正则倾向于选择均匀参数;