# Calculating hessian of a NN w.r.t params

**URL:** <https://discourse.julialang.org/t/calculating-hessian-of-a-nn-w-r-t-params/55911>\
**Category:** New to Julia\
**Tags:** question, zygote\
**Created:** [February 24, 2021, 5:15am UTC](https://discourse.julialang.org/t/calculating-hessian-of-a-nn-w-r-t-params/55911 "2021-02-24T05:15:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stash-196](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stash-196/32/21611_2.png) [@stash-196](https://discourse.julialang.org/u/stash-196)\
**Post date:** [February 24, 2021, 5:15am UTC](https://discourse.julialang.org/t/calculating-hessian-of-a-nn-w-r-t-params/55911/1 "2021-02-24T05:15:47Z")

</div>

How would you calculate a hessian of a Neural Network w.r.t. it’s parameters?

For instance, a hessian of the loss function below

```julia
using Flux: Chain, Dense, σ, crossentropy, params
using Zygote
model = Chain(
    x -> reshape(x, :, size(x, 4)),
    Dense(2, 5),
    Dense(5, 1),
    x -> σ.(x)
)
n_data = 5
input = randn(2, 1, 1, n_data)
target = randn(1, n_data)
loss = model -> Flux.crossentropy(model(input), target)

```

I can get a gradient w.r.t parameters in two ways…

```julia
Zygote.gradient(model -> loss(model), model)

```

or

```julia
grad = Zygote.gradient(() -> loss(model), params(model))
grad[params(model)[1]]

```

However, I can’t find a way to get a hessian w.r.t its parameters. (I want to do something like `Zygote.hessian(model -> loss(model), model)`, but I can’t)
