# Is there an efficient way to compute the Hessian of a NN?

**URL:** https://discourse.julialang.org/t/is-there-an-efficient-way-to-compute-the-hessian-of-a-nn/26971
**Category:** Machine Learning
**Tags:** flux
**Created:** [July 30, 2019, 10:08am UTC](https://discourse.julialang.org/t/is-there-an-efficient-way-to-compute-the-hessian-of-a-nn/26971 "2019-07-30T10:08:33Z")
**Posts on this page:** 1
**Showing post:** 11

<div class="post-metadata">

### Author: ![elperkerson62](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elperkerson62/32/597_2.png) [@elperkerson62](https://discourse.julialang.org/u/elperkerson62)
#### Post date: [November 11, 2019, 11:10pm UTC](https://discourse.julialang.org/t/is-there-an-efficient-way-to-compute-the-hessian-of-a-nn/26971/11 "2019-11-11T23:10:34Z")

</div>

> [@HenriDeh](#):
>
> `Flux.jacobian(::Chain, ::AbstractArray)` gives the full jacobian of a neural network. `Flux.hessian` however throws the error “output is not scalar”.

I had this same issue and resolved it using the `sum` function:

```julia
using Flux

m = Chain(Dense(2, 1))
x = zeros(2)
# Flux.hessian(m, x) throws "ERROR: Function output is not scalar"
# Flux.hessian(v->m(v)[1], x) throws "ERROR: Nested AD not defined for getindex"
Flux.hessian(v->sum(m(v)), x) # works

Tracked 2×2 Array{Float64,2}:
 0.0 0.0
 0.0 0.0

```

---

_[View the full topic](https://discourse.julialang.org/t/is-there-an-efficient-way-to-compute-the-hessian-of-a-nn/26971)._
