Skip to content
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

Taking derivatives with respect to only some elements of an array #1283

Open
raphaelchinchilla opened this issue Aug 5, 2022 · 3 comments
Open
Labels
enhancement New feature or request

Comments

@raphaelchinchilla
Copy link

This is a feature suggestion. I do not believe it currently exists.

Suppose one has access to a function f(x,p) that, and this is the important part, cannot be rewritten or modified. In my particular example, I am solving a differential equation, but another situation could be:

using Flux, Optimisers
model=Dense(2,2)
p,re=destructure(model)
function f(x,p)
  m = re(p)
  return m(x)
end

For simplicity, suppose p is a vector and let us split it into p=[p1;p2]. I would like to take the gradient of a cost function of f(x,p) (let us say, sum) with respect to only p1. I think that the best way to do it currently is something like

gradient(p1) do p1
  p=[p1;p2]
  sum(f(x,p))
end

However, this allocates a new vector p each time which is inefficient, specially if the number of elements in p1 is much smaller than the one in p2 (another possibility is to take the gradient with respect to both p1 and p2 and ignore the p2 component but this is probably even more inefficient).

For context, I am needing to take the derivative with respect to only some parameters because I am updating the others with another method (that does not require the gradient).

I am not sure how, but it would be nice if there were a way to tell Zygote to only compute the gradient with respect to some indexes of a vector (and also the convert function, to not compute the gradient with respect to some indexes)

@ToucheSir ToucheSir added the enhancement New feature or request label Aug 5, 2022
@ToucheSir
Copy link
Member

#981 is one thing which would help this, but it is unlikely to be addressed holistically because Zygote's analysis is just not that smart (the exact opposite, really). In fact, it does not even know at "compile time" whether certain values are arrays or not (hence why #981 is purely a "runtime" solution).

@raphaelchinchilla
Copy link
Author

Oh, what a shame.

Should I keep the issue open or just go ahead and close it?

@ToucheSir
Copy link
Member

We can leave it open for now and see if your use case is addressed by that PR if/when it's merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants