如何强制Theano按列执行softmax?

2024-03-29 10:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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()

因此,我得到以下输出:

^{pr2}$

这意味着操作已按行应用。有没有办法强迫Theano按列顺序执行softmax?在


Tags: 方法函数namenumpyconfigtruevaluetheano
2条回答

Which means that the operation has been applied row wise

它的设计目的是: http://deeplearning.net/software/theano/library/tensor/nnet/nnet.html#theano.tensor.nnet.nnet.softmax

换位数组将得到预期的结果。不知道西亚诺这只是一个猜测:

y = theano.tensor.nnet.T.softmax(m)

或者:

^{pr2}$

参考号:http://deeplearning.net/software/theano/library/tensor/basic.html#theano.tensor._tensor_py_operators.T

简单的解决方案:转置

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

对于张量rank>;=3,转置需要指定轴。在

^{pr2}$

相关问题 更多 >