# Gradient of gradient

**URL:** <https://discourse.julialang.org/t/gradient-of-gradient/49660>\
**Category:** Machine Learning\
**Created:** [November 6, 2020, 1:36am UTC](https://discourse.julialang.org/t/gradient-of-gradient/49660 "2020-11-06T01:36:26Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![martenlienen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martenlienen/32/18572_2.png) [@martenlienen](https://discourse.julialang.org/u/martenlienen)\
**Post date:** [November 6, 2020, 8:42am UTC](https://discourse.julialang.org/t/gradient-of-gradient/49660/8 "2020-11-06T08:42:05Z")

</div>

PyTorch cannot backpropagate through mutations and neither can Zygote. The expression `fill!(similar(y), 1)` depends on `x` through `y` and mutates its arguments (see the exclamation mark). You know that there is no real dependency on the value of `y` because the outcome is constant but Zygote will still try to differentiate through it. So you should rewrite it without mutations, for example

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

```

---

_[View the full topic](https://discourse.julialang.org/t/gradient-of-gradient/49660)._
