4

我写了一个softmax回归函数def softmax_1(x),它本质上是一个m x n矩阵,对矩阵求幂,然后对每一列的指数求和。

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))

将其转换为 DataFrame 我必须转置

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

4

1 回答 1

4

改变

np.exp(y/np.sum(np.exp(y),axis=1))

np.exp(y)/np.sum(np.exp(y),axis=1, keepdims=True)

这将意味着np.sum将返回一个 shape 数组(80, 1)而不是(80,),它将正确广播除法。另请注意对括号关闭的更正。

于 2016-03-26T22:38:34.860 回答