# Problem defining a Neural Network with gradient as Loss function

**URL:** <https://discourse.julialang.org/t/problem-defining-a-neural-network-with-gradient-as-loss-function/69164>\
**Category:** Machine Learning\
**Created:** [October 4, 2021, 3:19am UTC](https://discourse.julialang.org/t/problem-defining-a-neural-network-with-gradient-as-loss-function/69164 "2021-10-04T03:19:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![munozariasjm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/munozariasjm/32/29678_2.png) [@munozariasjm](https://discourse.julialang.org/u/munozariasjm)\
**Post date:** [October 4, 2021, 3:19am UTC](https://discourse.julialang.org/t/problem-defining-a-neural-network-with-gradient-as-loss-function/69164/1 "2021-10-04T03:19:07Z")

</div>

Hi, I’m trying to setup a simple network who’s Loss Function is the Laplacian of the NN with respect to the gradient. Then, the loss function is:

```julia
function 𝕃1(x)
    f(j) = NNsolver(j) #Output of the NN
    ∇²(n) = Zygote.diaghessian(n->f(n)[1],n) #Diagonal hessian (since 1D)
    ∇²(x)[1][1] #Output a scalar
end

```

And the training process is also pretty straight forward:

```julia
function trainer()
    for point=Rx
        p=[point]
        gs = gradient(params(NNsolver)) do # NN model
          𝕃1(p)
        end
        update!(opt, params(NNsolver), gs)
      end

end

```

Nonetheless, the function call outputs the following error (which I dont understand since I am not reshaping any array for example):

```julia
Mutating arrays is not supported -- called setindex!(::Vector{Float64}, _...)

```

Would really appreciate any help and thanks in advance!

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [October 8, 2021, 9:26pm UTC](https://discourse.julialang.org/t/problem-defining-a-neural-network-with-gradient-as-loss-function/69164/2 "2021-10-08T21:26:52Z")

</div>

Without an [MWE](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) that includes enough of `NNsolver` to trigger the error, we won’t have enough information to help you troubleshoot this. A full stack trace would be very much appreciated as well.
