2017-04-16 79 views
1

我正在嘗試通過assignment 1爲斯坦福大學cs244n類。問題1b強烈建議對Softmax函數進行優化。我設法得到了N維矢量的Softmax。我還得到了MxN維矩陣的Softmax,但在列中使用了for循環。我有以下代碼:如何矢量化多維矩陣的Softmax概率

​​

我可以實施更優化的Matrix實施嗎?

回答

3

就有關ufuncs使用NumPy broadcasting和覆蓋方面的通用號碼的ndarrays -

exp_max = np.exp(x - np.max(x,axis=-1,keepdims=True)) 
out = exp_max/np.sum(exp_max,axis=-1,keepdims=True) 
+0

感謝您的回答。我將你的回答標記爲答案,因爲這項任務建議看看廣播。 –

1

您可以嘗試使用np.apply_along_axis,您必須指定執行代碼的軸(在您的情況下爲axis=1)。 這裏有一個工作示例:

In [1]: import numpy as np 

In [2]: def softmax(x): 
    ...:  orig_shape = x.shape 
    ...: 
    ...:  # Matrix 
    ...:  if len(x.shape) > 1: 
    ...:   softmax = np.zeros(orig_shape) 
    ...:   for i,col in enumerate(x): 
    ...:    softmax[i] = np.exp(col - np.max(col))/np.sum(np.exp(col - np.max(col))) 
    ...:  # Vector 
    ...:  else: 
    ...:   softmax = np.exp(x - np.max(x))/np.sum(np.exp(x - np.max(x))) 
    ...:  return softmax 
    ...: 

In [3]: def softmax_vectorize(x): 
    ...:  return np.exp(x - np.max(x))/np.sum(np.exp(x - np.max(x))) 
    ...: 

In [4]: X = np.array([[1, 0, 0, 4, 5, 0, 7], 
    ...:   [1, 0, 0, 4, 5, 0, 7], 
    ...:   [1, 0, 0, 4, 5, 0, 7]]) 

In [5]: print softmax(X) 
[[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02 
    1.13694955e-01 7.66070581e-04 8.40098401e-01] 
[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02 
    1.13694955e-01 7.66070581e-04 8.40098401e-01] 
[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02 
    1.13694955e-01 7.66070581e-04 8.40098401e-01]] 

In [6]: print np.apply_along_axis(softmax_vecorize, axis=1, arr=X) 
[[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02 
    1.13694955e-01 7.66070581e-04 8.40098401e-01] 
[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02 
    1.13694955e-01 7.66070581e-04 8.40098401e-01] 
[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02 
    1.13694955e-01 7.66070581e-04 8.40098401e-01]] 
+0

哇,這是做它的另一種有趣的方式。謝謝。 –