Skip to content

Gradient for tf.cholesky #367

Closed
Closed

Description

@RuiShu

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.

Thanks!

Activity

  1. vrv commented on Nov 28, 2015

    @vrv

    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.

  2. girving commented on Dec 7, 2015

    @girving
    Contributor

    @yaroslavvb: Were you pondering linear algebra gradients?

  3. yaroslavvb commented on Jan 23, 2016

    @yaroslavvb
    Contributor

    Just curious, what is the use-case for differentiable Cholesky?

  4. RuiShu commented on Jan 29, 2016

    @RuiShu
    Author

    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

  5. rmlarsen commented on Jan 29, 2016

    @rmlarsen
    Contributor

    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.

  6. cesarsalgado commented on Feb 25, 2016

    @cesarsalgado
    Contributor

    This new paper may be of interest: Differentiation of the Cholesky decomposition

  7. alexggmatthews commented on Mar 2, 2016

    @alexggmatthews
    Contributor

    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.

  8. 2 remaining items

  9. martinwicke commented on Mar 8, 2016

    @martinwicke
    Member

    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).

  10. rmlarsen commented on Mar 8, 2016

    @rmlarsen
    Contributor

    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:

    @rmlarsen https://github.com/rmlarsen Would you like to help?

    —
    Reply to this email directly or view it on GitHub
    #367 (comment)
    .

  11. alexggmatthews commented on Mar 8, 2016

    @alexggmatthews
    Contributor

    OK thanks. I'll put in a pull request.

  12. rmlarsen commented on Apr 8, 2016

    @rmlarsen
    Contributor

    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.

  13. c0g commented on May 1, 2016

    @c0g
    Contributor

    @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:

    • 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.

  14. harpone commented on Aug 3, 2016

    @harpone

    @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?

  15. RuiShu commented on Aug 5, 2016

    @RuiShu
    Author

    @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.

  16. added
    type:feature
    and removed on Feb 9, 2017
  17. EverettYou commented on May 26, 2017

    @EverettYou

    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.)

  18. harpone commented on May 26, 2017

    @harpone

    @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...

  19. added a commit that references this issue on May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type:feature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions