4
我寫了一個softmax迴歸函數def softmax_1(x)
,它實質上包含一個m x n
矩陣,對矩陣求冪,然後求和每列的指數。np.sum行軸不能在Numpy工作
x = np.arange(-2.0, 6.0, 0.1)
scores = np.vstack([x, np.ones_like(x), 0.2 * np.ones_like(x)])
#scores shape is (3, 80)
def softmax_1(x):
"""Compute softmax values for each sets of scores in x."""
return(np.exp(x)/np.sum(np.exp(x),axis=0))
將其轉換成數據幀我不得不轉
DF_activation_1 = pd.DataFrame(softmax_1(scores).T,index=x,columns=["x","1.0","0.2"])
,所以我想嘗試做一個版本的SOFTMAX函數,它在換位版本並計算出SOFTMAX功能
scores_T = scores.T
#scores_T shape is (80,3)
def softmax_2(y):
return(np.exp(y/np.sum(np.exp(y),axis=1)))
DF_activation_2 = pd.DataFrame(softmax_2(scores_T),index=x,columns=["x","1.0","0.2"])
然後我得到這個錯誤:
Traceback (most recent call last):
File "softmax.py", line 22, in <module>
DF_activation_2 = pd.DataFrame(softmax_2(scores_T),index=x,columns=["x","1.0","0.2"])
File "softmax.py", line 18, in softmax_2
return(np.exp(y/np.sum(np.exp(y),axis=1)))
ValueError: operands could not be broadcast together with shapes (80,3) (80,)
爲什麼當我在np.sum
方法中轉置和切換軸時,它不工作?
嗨,感謝您的快速回復,爲什麼我的行在移調後不再總計爲1? 'DF_activation_1'的第一行是'0.033211 0.667060 0.299729',而'DF_activation_2'的第一行是'0.612139 1.278129 1.050304' –
我會亂它,仍然總計> 1.我會讓你知道我什麼時候想到它出。 –
將以下單獨答案中意外發布的括號的評論移到上面的答案中。 – YXD