# Calculation of Hessian of a loss function w.r.t. dense layer weight matrices

**URL:** <https://discourse.julialang.org/t/calculation-of-hessian-of-a-loss-function-w-r-t-dense-layer-weight-matrices/103697>\
**Category:** Performance\
**Tags:** hessian\
**Created:** [September 9, 2023, 7:23am UTC](https://discourse.julialang.org/t/calculation-of-hessian-of-a-loss-function-w-r-t-dense-layer-weight-matrices/103697 "2023-09-09T07:23:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harsh\_Choudhary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harsh_choudhary/32/52779_2.png) [@Harsh\_Choudhary](https://discourse.julialang.org/u/Harsh_Choudhary)\
**Post date:** [September 9, 2023, 7:23am UTC](https://discourse.julialang.org/t/calculation-of-hessian-of-a-loss-function-w-r-t-dense-layer-weight-matrices/103697/1 "2023-09-09T07:23:51Z")

</div>

Hi, I am trying to train a dummy Neural Net which is given as: y = transpose(W1)_sigmoid(W2_x) where W1 and W2 are 10x1 and x is a scalar input. I am using second-order optimization and for that purpose, I need to calculate the hessian. Using the below code generates error:

hess = hessian((W1,W2) → (transpose(W2) \* sigmoid.(W1\*x))[1], W1, W2)

MethodError: no method matching hessian(::var"#264#265", ::Matrix{Float64}, ::Matrix{Float64})  
Closest candidates are:  
hessian(::Any, ::Any) at C:\Users\choud.julia\packages\Zygote\4SSHS\src\lib\grad.jl:62

Stacktrace:  
[1] top-level scope  
@ In[132]:5

I would like to calculate $\Delta\_w$y so that I can check for a second order optimization method.
