Python 我可以配置Theano';x除以0

Python 我可以配置Theano';x除以0,python,numpy,theano,Python,Numpy,Theano,我使用Theano有点问题。似乎除以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

我使用Theano有点问题。似乎
除以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中执行的一些计算来说,这是一个问题,我不想从GPU中获取数据,用
0
替换
inf
,以继续我的计算,当然,因为这会大大减慢计算过程。

无张量
计算元素逆

np.linalg.inv
计算逆矩阵

从数学上讲,这不是一回事



您可能正在寻找实验性的

在代码中没有
numpy
除以0…-你的矩阵不是奇异的,所以行列式是非零的,将所有元素倒置意味着计算(x_ij)^(-1),这意味着元素等于0 1/0。但无论出于什么原因,这种情况正在发生,我都需要一个解决方案,来应对这种情况。可能只是因为我不再研究张量数学(如果我曾经研究过的话),但是有没有理由假设矩阵的张量逆等于矩阵的逆?@StefanR.Falk:你知道theano张量倒数和numpy矩阵逆是完全不同的数学运算,不是吗?你可以阅读文档:,