Python 如何强制Theano执行softmax列操作?
我有一个2D数组,我想按列应用softmax函数。它可以尝试以下操作:Python 如何强制Theano执行softmax列操作?,python,numpy,matrix,theano,softmax,Python,Numpy,Matrix,Theano,Softmax,我有一个2D数组,我想按列应用softmax函数。它可以尝试以下操作: value = numpy.array([[1.0,2.0], [3.0,9.0], [7.0,1.0]], dtype=theano.config.floatX) m = theano.shared(value, name='m', borrow=True) y = theano.tensor.nnet.softmax(m) print y.eval() 因此,我得到以下输出: [[ 0.26894142 0.7310
value = numpy.array([[1.0,2.0], [3.0,9.0], [7.0,1.0]], dtype=theano.config.floatX)
m = theano.shared(value, name='m', borrow=True)
y = theano.tensor.nnet.softmax(m)
print y.eval()
因此,我得到以下输出:
[[ 0.26894142 0.73105858]
[ 0.00247262 0.99752738]
[ 0.99752738 0.00247262]]
这意味着该操作已按行应用。有没有办法强制Theano执行softmax列操作
这意味着该操作已按行应用
这就是它的设计目的:
转置数组将得到预期的结果。不知道Theano这有点像猜测:
y = theano.tensor.nnet.T.softmax(m)
或:
参考:
这意味着该操作已按行应用
这就是它的设计目的:
转置数组将得到预期的结果。不知道Theano这有点像猜测:
y = theano.tensor.nnet.T.softmax(m)
或:
参考:简单解决方案:转置
>>> import theano as th
>>> T = th.tensor
>>> x = th.tensor.matrix()
>>> y = T.nnet.softmax(x.T).T
>>> fn = th.function([x],y)
>>> fn([[1.,2.],[1.,3.]])
array([[ 0.5 , 0.26894143],
[ 0.5 , 0.7310586 ]], dtype=float32)
对于秩>=3的张量,转置需要指定轴
#this perform softmax on 3rd dimension
x = tensor4()
T.nnet.softmax(x.transpose(0,1,3,2)).transpose(0,1,3,2)
简单的解决方案:转置
>>> import theano as th
>>> T = th.tensor
>>> x = th.tensor.matrix()
>>> y = T.nnet.softmax(x.T).T
>>> fn = th.function([x],y)
>>> fn([[1.,2.],[1.,3.]])
array([[ 0.5 , 0.26894143],
[ 0.5 , 0.7310586 ]], dtype=float32)
对于秩>=3的张量,转置需要指定轴
#this perform softmax on 3rd dimension
x = tensor4()
T.nnet.softmax(x.transpose(0,1,3,2)).transpose(0,1,3,2)
有帮助吗?实际语法是什么?有帮助吗?实际语法是什么?