開始使用免費開始

比較 logistic 與 hinge 損失

在這個練習中,你會用已提供的數學式,繪製 logistic 與 hinge 損失的圖形。

影片中的損失函式示意圖顯示在右側。

本練習屬於課程

Python 中的線性分類器

檢視課程

練習說明

  • 在「網格點」上評估 log_loss()hinge_loss(),讓它們可以被繪製出來。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Mathematical functions for logistic and hinge losses
def log_loss(raw_model_output):
   return np.log(1+np.exp(-raw_model_output))
def hinge_loss(raw_model_output):
   return np.maximum(0,1-raw_model_output)

# Create a grid of values and plot
grid = np.linspace(-2,2,1000)
plt.plot(grid, ____, label='logistic')
plt.plot(grid, ____, label='hinge')
plt.legend()
plt.show()
編輯並執行程式碼