Softmax回歸函數是用于將分類結果歸一化。但它不同于一般的按照比例歸一化的方法,它通過對數變換來進行歸一化,這樣實現了較大的值在歸一化過程中收益更多的情況。
Softmax公式

Softmax實現方法1
import numpy as npdef softmax(x): """Compute softmax values for each sets of scores in x.""" pass # TODO: Compute and return softmax(x) x = np.array(x) x = np.exp(x) x.astype('float32') if x.ndim == 1: sumcol = sum(x) for i in range(x.size): x[i] = x[i]/float(sumcol) if x.ndim > 1: sumcol = x.sum(axis = 0) for row in x: for i in range(row.size): row[i] = row[i]/float(sumcol[i]) return x#測試結果scores = [3.0,1.0, 0.2]print softmax(scores)其計算結果如下:
[ 0.8360188 0.11314284 0.05083836]
Softmax實現方法2
import numpy as npdef softmax(x): return np.exp(x)/np.sum(np.exp(x),axis=0)#測試結果scores = [3.0,1.0, 0.2]print softmax(scores)
以上這篇Python下的Softmax回歸函數的實現方法(推薦)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選