scipy.special.
log_softmax#
- scipy.special.log_softmax(x, axis=None)[原始碼]#
計算 softmax 函數的對數。
原則上
log_softmax(x) = log(softmax(x))
但使用更精確的實作方式。
- 參數:
- xarray_like
輸入陣列。
- axisint 或 ints 元組,選用
計算數值的軸。預設為 None,且 softmax 將會針對整個陣列 x 進行計算。
- 回傳值:
- sndarray 或 純量
與 x 具有相同形狀的陣列。結果的指數沿著指定的軸加總會是 1。如果 x 是純量,則會回傳純量。
筆記
log_softmax
比np.log(softmax(x))
在輸入使softmax
飽和時更精確(請參閱以下範例)。在 1.5.0 版本中新增。
範例
>>> import numpy as np >>> from scipy.special import log_softmax >>> from scipy.special import softmax >>> np.set_printoptions(precision=5)
>>> x = np.array([1000.0, 1.0])
>>> y = log_softmax(x) >>> y array([ 0., -999.])
>>> with np.errstate(divide='ignore'): ... y = np.log(softmax(x)) ... >>> y array([ 0., -inf])