Skip to content Skip to sidebar Skip to footer

Broadcasting Linalg.pinv On A 3D Theano Tensor

in the example below, there is a 3d numpy matrix of size (4, 3, 3)+ a solution about how to calculate pinv of each of 4 of those 3*3 matrices in numpy. I also tried to use the same

Solution 1:

The error message is

Traceback (most recent call last):
  File "D:/Dropbox/source/intro_theano/pinv.py", line 32, in <module>
    apinvt = map(lambda n: T.nlinalg.pinv(n), at)
  File "d:\dropbox\source\theano\theano\tensor\var.py", line 549, in __iter__
    raise TypeError(('TensorType does not support iteration. '
TypeError: TensorType does not support iteration. Maybe you are using builtin.sum instead of theano.tensor.sum? (Maybe .max?)

This is occurring because, as the error message indicates, the symbolic variable at is not iterable.

The fundamental problem here is that you're incorrectly mixing immediately executed Python code with delayed execution Theano symbolic code.

You need to use a symbolic loop, not a Python loop. The correct solution is to use Theano's scan operator:

at=T.tensor3('a')
apinvt, _ = theano.scan(lambda n: T.nlinalg.pinv(n), at, strict=True)
f = theano.function([at], apinvt)
print np.allclose(f(a), apinv)

Post a Comment for "Broadcasting Linalg.pinv On A 3D Theano Tensor"