You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be really nice to have a gradient operation for tf.cholesky similar to Theano's CholeskyGrad.
I'll be seeing if I can implement the gradient using tf.control_flow_ops.While following the suggestion provided here, but if someone with C++ experience can implement the gradient directly in C++, that'd be great.
I'm interested in constructing a differentiable cost function for a Gaussian process (the log marginal likelihood) and wanted to replace the matrix inversion with solving via Cholesky. It'd also be nice to replace log determinant with: logdetA = 2*sum(log(diag(chol(A))));
Thus the interest in a differentiable Cholesky :P
Reacted by Victor Melo, Meet Shah , Samuel Ainsworth, Călin Cruceru and Luyao Peng
matrix_inverse was recently changed so it will try to use Cholesky if the matrix is symmetric, in the interest of speed. However for the log determinant, I agree that a Cholesky gradient would be nice to have.
The best way would be to make a pull request. That's the best way for us to see and comment on the changes. It's ok to mark the PR as [WIP]. (see examples on our PR page).
FYI: Alex's excellent code in PR 1465 was merged today, so you should be able to use cholesky with gradients now. We still need to add support for batch_cholesky, so I'm leaving the bug open for now.
I've been working on making a CUDA version of Alex's code. I now have the following ops implemented in my fork:
get_diag (get_diag*)
tril (triangle*)
cholesky (gpu_cholesky*)
cholesky_grad (gpu_cholgrad*)
There is still a lot left to do, but the algorithms are all there and I believe they numerically match Alex's code. I will be adding python wrappers and tests over the coming days, as well as tidying up source files and trying to get it all to build with Bazel.
Meantime, I have a few questions:
streams
Is it correct to call standard CUDA kernels and operations on the op's stream by launching with context->eigen_device<Device>().stream()?
stream.ThenLaunch()
I think there is a way to launch kernels directly on a tensorflow stream - but there's not much information on TypedKernel or Kernel that I can find. Do I need to worry about this?
cusolver:
I use cusolver for the cholesky op. This isn't yet wrapped in Stream.h so I'm linking/calling it raw. This adds a dependency on linking cusolver into the framework, and maybe wrapping it as you guys have wrapped cublas.
edit: I will also work on reflecting the style/formatting recommendations made on Alex's code.
@RuiShu (and others): are you aware that grad(logdet(A)) = transpose(inv(A))? Unless it's actually faster to compute the grad of the cholesky decomp (and to the other operations), then IMO it might make sense to use the cholesky decomp in getting the value of the logdet and to use the inverse-transpose form for the gradient?
@harpone It's been a while since I've checked up on this issue. Generally, it'd be nice to not compute the inverse explicitly. Since I was primarily interested in logdets within the context of gaussian processes, there are ways of getting around it (such as something that ultimately requires something like the cholesky decomp in the backwards pass as well :P)
In any case, I was under the impression that Alex's contribution (as well as his work in GPFlow in general) has effectively solved the issue.
If the matrix is not positive definite, how to implement logdet(A) and its gradient without calculating det(A) explicit? (Because det(A) itself can overflow easily.)
@EverettYou as for the numerical value, you could now use s, u, v = tf.svd(M), where s is an array of the singular values (which are eigenvalues of M since M is square). Then log det(M) = log det(P S P^{-1}) = log det(S) = log(prod_i(s[i])) = sum_i log(s[i]), i.e. just take log of s and sum. Note that since M is not necessarily pos def, you'd actually need log abs det so just take the abs of s first. The gradient is the same, since log(-1) is a constant (imaginary number but a constant nevertheless).
I don't know about the status of the gradient... haven't thought about this in a while. You could define the gradient by RegisterGradient as grad(logdet(M)) = transpose(inv(M)), but as @RuiShu mentioned, it would be nice to avoid taking the inverse, at least if the matrix is big...
Activity
vrv commentedon Nov 28, 2015
control_flow_ops.While is not part of the public API (hence there is no documentation about it) because it is still not ready to use externally yet.
girving commentedon Dec 7, 2015
@yaroslavvb: Were you pondering linear algebra gradients?
yaroslavvb commentedon Jan 23, 2016
Just curious, what is the use-case for differentiable Cholesky?
RuiShu commentedon Jan 29, 2016
I'm interested in constructing a differentiable cost function for a Gaussian process (the log marginal likelihood) and wanted to replace the matrix inversion with solving via Cholesky. It'd also be nice to replace log determinant with:
logdetA = 2*sum(log(diag(chol(A))));Thus the interest in a differentiable Cholesky :P
rmlarsen commentedon Jan 29, 2016
matrix_inverse was recently changed so it will try to use Cholesky if the matrix is symmetric, in the interest of speed. However for the log determinant, I agree that a Cholesky gradient would be nice to have.
cesarsalgado commentedon Feb 25, 2016
This new paper may be of interest: Differentiation of the Cholesky decomposition
alexggmatthews commentedon Mar 2, 2016
Myself and James Hensman have implemented the method from the reference paper by Iain murray in TensorFlow on a fork:
https://github.com/GPflow/tensorflow/blob/master/tensorflow/core/user_ops/chol_grad.cc
@vrv we're aware it isn't yet up to TensorFlow main repo standards but would you like to discuss getting it into the main repo.
2 remaining items
martinwicke commentedon Mar 8, 2016
The best way would be to make a pull request. That's the best way for us to see and comment on the changes. It's ok to mark the PR as [WIP]. (see examples on our PR page).
rmlarsen commentedon Mar 8, 2016
Hi Alex,
Sorry for the delayed response. I'd be happy to review your code and help
getting it integrated into the TensorFlow core.
Rasmus
On Wed, Mar 2, 2016 at 5:45 AM, Alexander G. de G. Matthews <
[email protected]> wrote:
alexggmatthews commentedon Mar 8, 2016
OK thanks. I'll put in a pull request.
rmlarsen commentedon Apr 8, 2016
FYI: Alex's excellent code in PR 1465 was merged today, so you should be able to use cholesky with gradients now. We still need to add support for batch_cholesky, so I'm leaving the bug open for now.
c0g commentedon May 1, 2016
@alexggmatthews said to post here when I had something to show and to tag @rmlarsen and @yaroslavvb - hi guys!
I've been working on making a CUDA version of Alex's code. I now have the following ops implemented in my fork:
There is still a lot left to do, but the algorithms are all there and I believe they numerically match Alex's code. I will be adding python wrappers and tests over the coming days, as well as tidying up source files and trying to get it all to build with Bazel.
Meantime, I have a few questions:
streams
Is it correct to call standard CUDA kernels and operations on the op's stream by launching with
context->eigen_device<Device>().stream()?stream.ThenLaunch()
I think there is a way to launch kernels directly on a tensorflow stream - but there's not much information on TypedKernel or Kernel that I can find. Do I need to worry about this?
cusolver:
I use cusolver for the cholesky op. This isn't yet wrapped in Stream.h so I'm linking/calling it raw. This adds a dependency on linking cusolver into the framework, and maybe wrapping it as you guys have wrapped cublas.
edit: I will also work on reflecting the style/formatting recommendations made on Alex's code.
harpone commentedon Aug 3, 2016
@RuiShu (and others): are you aware that
grad(logdet(A)) = transpose(inv(A))? Unless it's actually faster to compute the grad of the cholesky decomp (and to the other operations), then IMO it might make sense to use the cholesky decomp in getting the value of the logdet and to use the inverse-transpose form for the gradient?RuiShu commentedon Aug 5, 2016
@harpone It's been a while since I've checked up on this issue. Generally, it'd be nice to not compute the inverse explicitly. Since I was primarily interested in logdets within the context of gaussian processes, there are ways of getting around it (such as something that ultimately requires something like the cholesky decomp in the backwards pass as well :P)
In any case, I was under the impression that Alex's contribution (as well as his work in GPFlow in general) has effectively solved the issue.
EverettYou commentedon May 26, 2017
If the matrix is not positive definite, how to implement logdet(A) and its gradient without calculating det(A) explicit? (Because det(A) itself can overflow easily.)
harpone commentedon May 26, 2017
@EverettYou as for the numerical value, you could now use
s, u, v = tf.svd(M), where s is an array of the singular values (which are eigenvalues of M since M is square). Then log det(M) = log det(P S P^{-1}) = log det(S) = log(prod_i(s[i])) = sum_i log(s[i]), i.e. just take log of s and sum. Note that since M is not necessarily pos def, you'd actually need log abs det so just take the abs of s first. The gradient is the same, since log(-1) is a constant (imaginary number but a constant nevertheless).I don't know about the status of the gradient... haven't thought about this in a while. You could define the gradient by RegisterGradient as
grad(logdet(M)) = transpose(inv(M)), but as @RuiShu mentioned, it would be nice to avoid taking the inverse, at least if the matrix is big...README.md: fix link; rm obvious text (tensorflow#367)
Update XNNPACK in XLA