Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

損失関数

回帰 Regression

二乗損失

誤差を二乗するので、絶対誤差に比べて大きく予測を外した値が強調されるのが特徴。

12\frac{1}{2}を乗じているのは微分したときに簡潔に示すため。

二乗誤差の勾配
LSquarede=e=yy^\frac{\partial L_{\text{Squared}}}{\partial e} = e = y - \hat{y}

絶対損失

二乗しないため外れ値に強い(外れ値の予測誤差を比較的小さく評価する)。

勾配

e=0e = 0 では微分が定義されないため、劣勾配(subgradient) として区間 [1,1][-1, 1] を取る

LAbsolutee={+1(e>0)1(e<0)[1,1](e=0)\frac{\partial L_{\text{Absolute}}}{\partial e} = \begin{cases} +1 & (e > 0) \\ -1 & (e < 0) \\ [-1,\,1] & (e = 0) \end{cases}

実装上は多くの場合、Le=sign(e)\frac{\partial L}{\partial e} = \mathrm{sign}(e) とし、e=0e = 0 の場合は 0 を返すことが多い

Source
import matplotlib.pyplot as plt
import numpy as np

e = np.linspace(-4, 4, 100)

plt.figure(figsize=[4, 3])
plt.plot(e, (1/2) * e**2, label=r"Squared Loss $\frac{1}{2}e^2$")
plt.plot(e, abs(e), label=r"Absolute Loss $|e|$")

plt.ylabel("Loss")
plt.xlabel(r"Residual $e$")
plt.legend()
plt.show()
<Figure size 400x300 with 1 Axes>

Huber Loss

絶対損失がゼロ付近で微分不可能である問題に対処し、ある閾値δ\delta未満の誤差( eδ|e| \le \delta )のときだけ二乗損失にしたもの。

勾配

LHubere={e(eδ)δsign(e)(e>δ)\frac{\partial L_{\text{Huber}}}{\partial e} = \begin{cases} e & (|e| \le \delta) \\\\ \delta \cdot \mathrm{sign}(e) & (|e| > \delta) \end{cases}

Fair loss

Fair loss は、残差が大きくなるにつれて影響を連続的になだらかに抑制するロバスト損失関数である。
Huber loss のような明確な折れ点を持たず、全域で滑らかな形状を持つ。

勾配

LFaire=e1+e/c\frac{\partial L_{\text{Fair}}}{\partial e} = \frac{e}{1 + |e|/c}

特徴

  • 小さな残差では二乗誤差に近い挙動

  • 残差が大きくなるにつれて勾配が連続的に減衰

  • 全域で滑らか(高階微分も連続)

  • 非常に大きな外れ値に対しても安定

利用場面

  • 外れ値が多い、または裾の重い分布を持つデータ

  • 勾配ベース最適化で数値安定性を重視する場合

  • Huber loss よりも強いロバスト性が必要なケース

Source
import matplotlib.pyplot as plt
import numpy as np

e = np.linspace(-5, 5, 1000)
# parameters
delta = 1.0
c = 1.0
# Huber loss
huber = np.where(
    np.abs(e) <= delta,
    0.5 * e**2,
    delta * (np.abs(e) - 0.5 * delta)
)
# Fair loss
fair = c**2 * (np.abs(e) / c - np.log(1 + np.abs(e) / c))

plt.figure(figsize=[4,3])

plt.plot(e, abs(e), label=r"Absolute loss")
plt.plot(e, huber, label="Huber loss")
plt.plot(e, fair, label="Fair loss")
plt.xlabel("Residual $e$")
plt.ylabel("Loss")
plt.legend()
plt.show()
<Figure size 400x300 with 1 Axes>
Source
import matplotlib.pyplot as plt
import numpy as np

e = np.linspace(-5, 5, 1000)

# parameters
delta = 1.0
c = 1.0

# gradients

# Absolute loss gradient (subgradient, 0 at e=0)
grad_abs = np.sign(e)

# Huber loss gradient
grad_huber = np.where(
    np.abs(e) <= delta,
    e,
    delta * np.sign(e)
)

# Fair loss gradient
grad_fair = e / (1 + np.abs(e) / c)

# plot
plt.figure(figsize=(4, 3))
plt.title(r"Gradient of Loss $\frac{\partial L}{\partial e}$")
plt.plot(e, grad_abs, label=r"Absolute")
plt.plot(e, grad_huber, label="Huber")
plt.plot(e, grad_fair, label="Fair")
plt.xlabel("Residual $e$")
plt.ylabel("Gradient")
plt.legend()
plt.show()
<Figure size 400x300 with 1 Axes>

確率予測

Log Loss(Cross Entropy Loss)

予測確率の信頼度を含めた誤差指標。確率出力モデルに適する。

LogLoss=1ni=1n[yilogp^i+(1yi)log(1p^i)]\text{LogLoss} = -\frac{1}{n}\sum_{i=1}^n [y_i\log \hat{p}_i + (1-y_i)\log(1-\hat{p}_i)]