2017-05-31 16 views
0

我試圖在Python中實現鉸鏈丟失函數,並面臨一些誤導。Python中的Softmax和Hinge函數

在我曾閱讀過的一些資料中(例如,Luca Massoron的「Python中的迴歸分析」)指出,鉸鏈有時稱爲Softmax函數。

但對我來說,這是一種奇怪的,因爲鉸鏈: enter image description here

和SOFTMAX就像是剛剛指數函數:enter image description here

我在Python所做的功能(使用SoftMax)這樣說:

def softmax(x): 
    e_x = np.exp(x - np.max(x)) 
    return e_x/e_x.sum(axis=0) 

有兩個問題:

  1. 我可以像使用鉸鏈功能一樣使用softmax功能嗎?
  2. 如果不是,那麼在Python中如何實現hinge?

謝謝。

回答

1

我可以像使用鉸鏈功能一樣使用softmax功能嗎?

不 - 它們不等價。

鉸鏈函數是一個損失函數,不提供良好校準的概率,而softmax是一個映射函數(將一組分數映射到一個分佈,一個分數到一個分佈)。

如果沒有,那麼在Python中如何實現hinge?

這下面的代碼片斷捕獲的鉸鏈損失函數的本質:

import numpy as np 
import matplotlib.pyplot as plt 

xmin, xmax = -1, 2 
xx = np.linspace(xmin, xmax, 100) 
plt.plot(xx, np.where(xx < 1, 1 - xx, 0), label="Hinge loss")