我可以配置Theano的x除以0吗

2024-09-27 23:27:37 发布

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

我使用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中执行的一些计算来说是个问题,我不想从GPU中取回数据,用inf替换0来继续我的计算,当然,因为这样会大大减慢计算过程。你知道吗


Tags: fromimportgpunpdifffunctiontheanoinf

热门问题