我可以配置Theano的x除以0行为

时间:2014-04-05 15:54:55

标签: python numpy theano

使用Theano我有点问题。似乎division by 0导致inf没有使用例如Numpy这导致0(至少反函数表现得像那样)。看看:

from theano import function, sandbox, Out, shared
import theano.tensor as T
import numpy as np

reservoirSize   = 7
_eye            = np.eye(reservoirSize)

gpu_I = shared( np.asarray(_eye, np.float32 ) )

simply_inverse = function(
[],
Out(sandbox.cuda.basic_ops.gpu_from_host(
    T.inv( gpu_I )
    ),
    borrow=True
    )
)

gpu_wOut = simply_inverse()
Wout     = np.linalg.inv(_eye)

print "gpu_wOut:\n"
print np.asarray(gpu_wOut)

print "\nWout:\n"
print np.asarray(Wout)
diff_wOut = np.asarray(gpu_wOut) - Wout
diff_wOut = [ diff_wOut[0][i] if diff_wOut[0][i] > epsilon else 0  for i in range(reservoirSize)]
print "\n\nDifference of output weights: (only first row)\n"
print np.asarray(diff_wOut)

结果:

gpu_wOut:

[[  1.  inf  inf  inf  inf  inf  inf]
 [ inf   1.  inf  inf  inf  inf  inf]
 [ inf  inf   1.  inf  inf  inf  inf]
 [ inf  inf  inf   1.  inf  inf  inf]
 [ inf  inf  inf  inf   1.  inf  inf]
 [ inf  inf  inf  inf  inf   1.  inf]
 [ inf  inf  inf  inf  inf  inf   1.]]

Wout:

[[ 1.  0.  0.  0.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.  0.  0.]
 [ 0.  0.  1.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  1.  0.  0.]
 [ 0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.]]


Difference of output weights (only first row):

[  0.  inf  inf  inf  inf  inf  inf]

这是我想要在我的GPU中执行的一些计算的问题,我不想从中取回数据,以inf替换0以继续我的当然是计算,因为这会大大减慢这个过程。

1 个答案:

答案 0 :(得分:1)

theano.tensor计算元素

np.linalg.inv计算逆矩阵

这些在数学上是不一样的


您可能正在寻找实验性 theano.sandbox.linalg.ops.MatrixInverse