Domanda

I have a little problem using Theano. It seems that a division by 0 results in inf not as using e.g. Numpy this results in 0 (at least the inverse function do behave like that). Take a look:

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)

Results:

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]

This is a problem for some of my calculations I want to perform in my GPU and I don't want to get back the data from it to replace inf by 0 to continue my calculations of course since this would slow down the process considerably.

È stato utile?

Soluzione

theano.tensor calculates the elementwise inverse

np.linalg.inv calculates the inverse matrix

These are not the same thing mathematically


You're probably looking for the experimental theano.sandbox.linalg.ops.MatrixInverse

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top