# Gradiens of gradients using ReverseDiff

**URL:** <https://discourse.julialang.org/t/gradiens-of-gradients-using-reversediff/11661>\
**Category:** Numerics\
**Created:** [June 13, 2018, 7:42pm UTC](https://discourse.julialang.org/t/gradiens-of-gradients-using-reversediff/11661 "2018-06-13T19:42:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Goysa2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goysa2/32/15517_2.png) [@Goysa2](https://discourse.julialang.org/u/Goysa2)\
**Post date:** [June 13, 2018, 7:42pm UTC](https://discourse.julialang.org/t/gradiens-of-gradients-using-reversediff/11661/1 "2018-06-13T19:42:56Z")

</div>

Hi,  
This is a duplicate of the issue I created here:  
[https://github.com/JuliaDiff/ReverseDiff.jl/issues/105](https://github.com/JuliaDiff/ReverseDiff.jl/issues/105)

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

```julia
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)

```julia
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!
