-
Notifications
You must be signed in to change notification settings - Fork 912
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
Differentiation with np.select (piecewise functions) #36
Comments
I think the problem was that Reopen if still busted! |
@mattjj Are you sure you don't want to add a test? A while ago I added a systematic test for np.select but left it commented out: |
Yeah yeah okay :) That test is actually for the gradient of select, right? My fix didn't implement a gradient for select so much as it made select work as control flow (as it would have done automatically if not for the zero-dimensional array issue, afaict). |
Except it only works in the scalar case (i.e. when condlist and choicelist are lists of scalars)... reopening! |
Okay, I undid the change because making np.select actually work in general seems like a bit of a mess and I can't think of a quick fix. Any ideas? |
My quick-fix thinking was that we could just use the fact that np.select should work on object ndarrays, then just wrap its output back to an ArrayNode. I'm kind of confused at the moment as to why that's not working. |
Okay, took another quick stab at this one. The basic strategy is for autograd.numpy.select to be a non-primitive function that unboxes its arguments (from ListNode to list, or just keeps them as lists if they come in as lists), uses the underlying Maybe this is shady! But it passes both David's test and the OP's test case. Since I haven't thought too hard about how shady this might be, I'm leaving it in a branch named issue36 for now. |
The consensus is that 2ae6bed is a good fix! |
You're doing some great work here! I've run into a slight issue with computing gradients of piecewise functions using
np.select
. I've put together a minimal (trivial) example (Python 3.3):select
accepts a list of conditions and a list of choices, and the first condition to be evaluatedTrue
is returned from the corresponding choice list. The gradient of a piecewise-defined function should just be the gradient of the choice list, but I haven't yet wrapped my head around how constructing a closure fordefgrad
would work in this context.The text was updated successfully, but these errors were encountered: