# Gradient calculation in PINN

**URL:** <https://discourse.julialang.org/t/gradient-calculation-in-pinn/61525>\
**Category:** General Usage\
**Tags:** flux, zygote\
**Created:** [May 20, 2021, 3:48pm UTC](https://discourse.julialang.org/t/gradient-calculation-in-pinn/61525 "2021-05-20T15:48:12Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![JunichiFukui](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@JunichiFukui](https://discourse.julialang.org/u/JunichiFukui)\
**Post date:** [May 21, 2021, 4:30am UTC](https://discourse.julialang.org/t/gradient-calculation-in-pinn/61525/2 "2021-05-21T04:30:59Z")

</div>

I may have found the information I was looking for on how to find the first derivative in the discussion shown in the link below.

[Gradient of Flux model wrt to weights](https://discourse.julialang.org/t/gradient-of-flux-model-wrt-to-weights/61405)

Based on this information, the part of the PINN programme I would like to create would look like this.

```julia
using Flux
model = Flux.Dense(24,32)
baseline = Flux.params(model)
input = [-1 1 -1 1 -1 1 -1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 -1 -1 1 1 1 1]
output = model(input)
grad = Flux.jacobian(x -> model(x), input)

```

In the program, `input` is location of 8 points in 3-dimensional domain (X[-1, 1], Y[-1, 1], Z[-1, 1]), and `output` is velocity in X, Y, Z direction and pressure at the each points.

I am looking for a way to calculate the second order derivative of the output to the input of this neural network, and when I use the Hessian function I get the following error message

```julia
grad2 = Flux.hessian(x -> model(x), some_input)
ERROR: output an array, so the gradient is not defined. Perhaps you wanted jacobian.

```

Could you tell me how to solve this error?

---

_[View the full topic](https://discourse.julialang.org/t/gradient-calculation-in-pinn/61525)._
