# How to add norm of gradient to a loss function?

**URL:** <https://discourse.julialang.org/t/how-to-add-norm-of-gradient-to-a-loss-function/69873>\
**Category:** Machine Learning\
**Tags:** flux, zygote\
**Created:** [October 16, 2021, 2:08am UTC](https://discourse.julialang.org/t/how-to-add-norm-of-gradient-to-a-loss-function/69873 "2021-10-16T02:08:17Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![AlexLewandowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexlewandowski/32/18856_2.png) [@AlexLewandowski](https://discourse.julialang.org/u/AlexLewandowski)\
**Post date:** [October 16, 2021, 9:02pm UTC](https://discourse.julialang.org/t/how-to-add-norm-of-gradient-to-a-loss-function/69873/4 "2021-10-16T21:02:51Z")

</div>

I think what I am trying to do used to be possible in Zygote, based on the thread here: [Gradient of gradient - #8 by martenlienen](https://discourse.julialang.org/t/gradient-of-gradient/49660/8)

The suggestion:

```julia
using Flux
net = Dense(10, 1)
x = randn(10, 128) # dims, batch

function pred(x, net)
    y, pullback = Zygote.pullback(net, x)
    grads = pullback(ones(size(y)))[1]
    return grads
end

gradient(() -> sum(pred(x, net)), params(net)

```

Now throws the same error:

```julia
ERROR: Mutating arrays is not supported -- called copyto!(::Matrix{Float64}, _...)

```

Based on the comments in the linked thread, this used to work fine.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-add-norm-of-gradient-to-a-loss-function/69873)._
