New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add triangle/nonzero functions #1181
Conversation
Couple notes -- numpy.nonzero() always returns a tuple of indices, one for each dimension. I could only get the Nonzero Op to return a list, not a tuple. Also, it won't return a list of length 1, returning instead the sole Apply instance. So for ndim > 1, numpy and this Op both return sequences of indices, but for ndim == 1, numpy returns a length-1 tuple and this op returns a Nonzero object. If anyone can tell me how to address either issue, of course I'll change that. |
This behavior for extracting all nonzero elements is supported by Numpy but not by Theano: a[np.nonzero(a)] Even doing the indexing explicitly in Theano only works for matrices: a[tensor.nonzero(a)[0], tensor.nonzero(a)[1]]) So I've added a helper function, tensor.nonzero_values, to get around the advanced indexing problem. Calling tensor.nonzero_values(a) or a.nonzero_values() in Theano is equivalent to calling a[np.nonzero(a)] in Numpy. This relies on another numpy function, flatnonzeros, which I've also added to Theano. |
I know! I'm eagerly waiting for it. But don't worry, I didn't waste any time -- it probably took longer to write the docstring than the function. It's basically just a convenience wrapper around a.flatten()[tensor.nonzero(a.flatten())] |
Rather than output a list of ndim different outputs for a NonZero Op, which all have the same length, I think it would be better (more Theano-like) to have only one matrix output, of shape (ndim, nnonzero). |
That makes sense, and I agree having the Op output a matrix is more Theano-like. I hesitate a little bit on the tensor.nonzero function returning a matrix because I know a lot of effort goes into keeping things as similar to Numpy as possible. But, as you say, Theano can't even use the output in the same way (hence, nonzero_values) so I'm not sure if that rule of thumb should apply. I'll change the Op now, and let me know what you prefer for the function and I'll make the change as well. |
travis-ci have a few errors. Can you check them? |
My local tests are passing, I'll see if it's something happening with 2.7.3 vs 2.5. Edit -- no, looks like older versions of numpy complain about concatenating zero-length arrays. submitting something to address that and the ensuing casting issue. |
Hopefully these changes will address the issues with older versions of numpy -- how do I get travis to run? |
The latest version of the PR seems to be in Travis's queue: https://travis-ci.org/Theano/Theano/pull_requests |
It seems to have had an error before starting |
Add triangle/nonzero functions
Triangle:
Nonzero: