# \[Flux\] Issue while computing Hessian MethodError: objects of type Tracker.Grads are not callable

**URL:** <https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980>\
**Category:** Machine Learning\
**Tags:** flux\
**Created:** [August 26, 2019, 3:32am UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980 "2019-08-26T03:32:38Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![stochastic\_guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stochastic_guy/32/9877_2.png) [@stochastic\_guy](https://discourse.julialang.org/u/stochastic_guy)\
**Post date:** [August 26, 2019, 3:32am UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/1 "2019-08-26T03:32:38Z")

</div>

I’m new to Flux and I’m trying to compute the hessian.  
Here’s the code:

```julia
grad = Flux.gradient(() -> loss(x, y), Flux.params(model))
hess = Flux.gradient(grad, Flux.params(model))

```

But when running the line to compute hessian, it’s throwing the following error  
**MethodError: objects of type Tracker.Grads are not callable**

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [August 26, 2019, 4:53am UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/2 "2019-08-26T04:53:02Z")

</div>

Did you see the keyword nest or allownest = true?

---

<div class="post-metadata">

**Author:** ![stochastic\_guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stochastic_guy/32/9877_2.png) [@stochastic\_guy](https://discourse.julialang.org/u/stochastic_guy)\
**Post date:** [August 26, 2019, 5:44am UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/3 "2019-08-26T05:44:42Z")

</div>

Nope. But I just tried adding `nest=true` to the grad and still it’s showing the same error.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [August 26, 2019, 6:48am UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/4 "2019-08-26T06:48:30Z")

</div>

You are trying to take the gradient of an object, not a function. Create a function that takes the gradient and take the gradient of that

```julia
grad = ()->Flux.gradient(() -> loss(x, y), Flux.params(model))
hess = Flux.gradient(grad, Flux.params(model))

```

---

<div class="post-metadata">

**Author:** ![stochastic\_guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stochastic_guy/32/9877_2.png) [@stochastic\_guy](https://discourse.julialang.org/u/stochastic_guy)\
**Post date:** [August 26, 2019, 2:23pm UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/5 "2019-08-26T14:23:18Z")

</div>

Your suggestion solved the issue partially. But after that I was getting the error **outputs are not scalar**

The bellow code is working, but I’m not sure if what I’m doing is correct.

```julia
grad() = Tracker.gradient(() -> loss(x, y), Flux.params(model), nest = true)
hess = forward(grad,Flux.params(model))

```

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [August 26, 2019, 10:46pm UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/6 "2019-08-26T22:46:18Z")

</div>

You can also try the built in function  
[https://github.com/FluxML/Tracker.jl/blob/642bab12c9b37e7b29fd32925f63cdd9df107df0/src/back.jl#L180](https://github.com/FluxML/Tracker.jl/blob/642bab12c9b37e7b29fd32925f63cdd9df107df0/src/back.jl#L180)

---

<div class="post-metadata">

**Author:** ![stochastic\_guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stochastic_guy/32/9877_2.png) [@stochastic\_guy](https://discourse.julialang.org/u/stochastic_guy)\
**Post date:** [August 27, 2019, 3:37pm UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/7 "2019-08-27T15:37:19Z")

</div>

Tried that and got the following error

> **ERROR:** MethodError: no method matching jacobian(::TrackedArray{…,Array{Float64,2}}, ::Array{Float64,2}, ::Array{Float64,2})

---

<div class="post-metadata">

**Author:** ![stochastic\_guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stochastic_guy/32/9877_2.png) [@stochastic\_guy](https://discourse.julialang.org/u/stochastic_guy)\
**Post date:** [August 27, 2019, 7:10pm UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/8 "2019-08-27T19:10:04Z")

</div>

@ChrisRackauckas, @MikeInnes Please help…

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 27, 2019, 7:11pm UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/9 "2019-08-27T19:11:38Z")

</div>

Hessian is the Jacobian of the gradient, and it’s already in the library:

[https://github.com/FluxML/Tracker.jl/blob/67a6b35a1fec6b5f2ca0a5721a16feba95e58cdc/src/back.jl#L180](https://github.com/FluxML/Tracker.jl/blob/67a6b35a1fec6b5f2ca0a5721a16feba95e58cdc/src/back.jl#L180)

---

<div class="post-metadata">

**Author:** ![stochastic\_guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stochastic_guy/32/9877_2.png) [@stochastic\_guy](https://discourse.julialang.org/u/stochastic_guy)\
**Post date:** [August 27, 2019, 7:12pm UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/10 "2019-08-27T19:12:48Z")

</div>

Got this error while trying that…

**ERROR:** MethodError: no method matching jacobian(::TrackedArray{…,Array{Float64,2}}, ::Array{Float64,2}, ::Array{Float64,2})  
[/quote]

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 27, 2019, 7:17pm UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/11 "2019-08-27T19:17:48Z")

</div>

> [@stochastic\_guy](#):
>
> **ERROR:** MethodError: no method matching jacobian(::TrackedArray{…,Array{Float64,2}}, ::Array{Float64,2}, ::Array{Float64,2})

you need to be computing the Jacobian of a vector function on a vector.

---

<div class="post-metadata">

**Author:** ![stochastic\_guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stochastic_guy/32/9877_2.png) [@stochastic\_guy](https://discourse.julialang.org/u/stochastic_guy)\
**Post date:** [August 27, 2019, 7:28pm UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/12 "2019-08-27T19:28:50Z")

</div>

But I think I have defined a generic function

```julia
loss(x,y) = -sum(y.*log.(model(x)) .- (1 .- y).*log.(1 .- model(x))) * 1 // size(y, 2)

```

This is my loss function.

I also tried the loss functions that came with Flux and the ones defined in the Model zoo. No luck!

---

<div class="post-metadata">

**Author:** ![stochastic\_guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stochastic_guy/32/9877_2.png) [@stochastic\_guy](https://discourse.julialang.org/u/stochastic_guy)\
**Post date:** [August 28, 2019, 2:52pm UTC](https://discourse.julialang.org/t/flux-issue-while-computing-hessian-methoderror-objects-of-type-tracker-grads-are-not-callable/27980/13 "2019-08-28T14:52:23Z")

</div>

@denizyuret Is it possible to compute hessian in Knet ? If so, how to do it?

Thanks in advance 🙂
