Extract Diagonal Matrix

106 views
Skip to first unread message

arabidop...@gmail.com

unread,
Mar 7, 2016, 11:38:30 PM3/7/16
to lasagne-users
Hello,

I am trying to extract a diagonal matrix from a Theano tensor. When I do the equivalent operation in numpy:

arrayVals = np.zeros((5, 5, 56, 6, 74))

diagonalVals = np.diagonal(arrayVals) #shape (56,6,74,5)

This is exactly what I want. When I do the equivalent thing in Theano, I get the issue:

arrayValsTheano = T.zeros((5, 5, 56, 6, 74))

diagonalValsTheano = T.diagonal(arrayValsTheano) #ERROR

    raise TypeError('ExtractDiag only works on matrices', _x)
TypeError: ('ExtractDiag only works on matrices', Reshape{5}.0)

The Theano documentation seems to portray that my "arrayValsTheano" has more than 2 dimensions (is not a matrix), thus why it complains. Any insight?

Thanks!

Frédéric Bastien

unread,
Mar 8, 2016, 10:21:23 AM3/8/16
to lasagne-users
We updated the doc of that operation and some related operation in this PR:

https://github.com/Theano/Theano/pull/4169

It could help you.

--
You received this message because you are subscribed to the Google Groups "lasagne-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lasagne-user...@googlegroups.com.
To post to this group, send email to lasagn...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lasagne-users/987c1e7e-113b-4a6a-8a0d-22b37a66bbba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

arabidop...@gmail.com

unread,
Mar 8, 2016, 10:58:55 AM3/8/16
to lasagne-users
Thanks for getting back to me. When I use T.diag(arrayValsTheano), I get the error:

ValueError: Input must be 1- or 2-d.

And it sounds like from the Github pull request that ExtractDiag and and AllocDiag are depreciated. Does this mean diagonals can only be taken from 1D or 2D matrices?

Thanks

Frédéric Bastien

unread,
Mar 8, 2016, 11:03:26 AM3/8/16
to lasagne-users
Can you try the deprecated version? Maybe they cover more cases then the suggested one. The PR isn't yet merged, so wasn't fully reviewed.

Fred

arabidop...@gmail.com

unread,
Mar 8, 2016, 11:14:39 AM3/8/16
to lasagne-users
The theano.tensor.nlinalg.ExtractDiag also requires a 1 or 2 dimensional matrix:

if x.type.ndim != 2:
raise TypeError('ExtractDiag only works on matrices', _x)

I also tried theano.tensor.basic.Diagonal, but the grad hasn't been implemented:


def grad(self, inputs, gout):
(x,) = inputs
(gz,) = gout
return [grad_not_implemented(self, 0, x)]

arabidop...@gmail.com

unread,
Mar 8, 2016, 12:13:29 PM3/8/16
to lasagne-users, arabidop...@gmail.com
Is there any way I can do this by indexing?

Frédéric Bastien

unread,
Mar 9, 2016, 12:53:17 PM3/9/16
to lasagne-users, Arabidopsis Taco
You could do it with advanced indexing. Flatten the original tensor and manually compute the index that you need.

The other option is to update the op to support it.

Reply all
Reply to author
Forward
0 new messages