JuliaDiff / JuliaDiff/ReverseDiff.jl

Gradients of gradients

Open
#105 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
393
Forks
60
Avg merge
18h 24m
Merged PRs (30d)
8

Description

Hi!
I am trying to get Hessian-vector product, without having to explicitly compute the Hessian matrix.
I have the following example:

using ReverseDiff

function f(x)
    m = length(x)
    return 100.0 * sum((x[i] - x[i - 1]^2)^2 for i=2:m) + (1.0 - x[1])^2
end

n = 2
x = [0.150369, 0.8463333]
u = [0.284309, 0.927797]

F = x -> ReverseDiff.gradient(f, x)
ϕᵤ(x) = F(x)'*u
ReverseDiff.gradient(ϕᵤ,x)

Which gives the exact same thing as if I did: ReverseDiff.hessian(f, x)*u.

I know that calling ReverseDiff.gradient(f, x) isn't the most efficient way to use ReverseDiff, so I tried to improve my code by doing: (everything before the definition of F remains the same)

tape = ReverseDiff.compile(ReverseDiff.GradientTape(f, rand(n)))
F = x -> ReverseDiff.gradient!(g, tape, x)
ϕᵤ(x) = F(x)'*u
ReverseDiff.gradient(ϕᵤ,x)

But if I do that, the final answer is a Vector of zeros, which is not the expected result. What am I doing wrong?
Is there a way to do what I want efficiently and correctly?

Thanks for your help!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the ReverseDiff.gradient and gradient! calls, the compiled GradientTape, and the hessian entry point shown in the example. Trace why nested differentiation produces zeros and determine the supported efficient approach for a Hessian-vector product. Done means the example returns the expected nonzero result without explicitly forming the Hessian.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.