# Average of Zygote gradients

**URL:** <https://discourse.julialang.org/t/average-of-zygote-gradients/98303>\
**Category:** Machine Learning\
**Tags:** question\
**Created:** [May 4, 2023, 7:47am UTC](https://discourse.julialang.org/t/average-of-zygote-gradients/98303 "2023-05-04T07:47:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoBertimus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertimus/32/48105_2.png) [@RoBertimus](https://discourse.julialang.org/u/RoBertimus)\
**Post date:** [May 4, 2023, 7:47am UTC](https://discourse.julialang.org/t/average-of-zygote-gradients/98303/1 "2023-05-04T07:47:57Z")

</div>

Hi Everyone,  
I would like to know how I can manually calculate for example the mean over an array of gradients of a neural network. I have the following setup :

- In the training loop I calculate the gradient of the loss

```julia
grad = Flux.gradient((m) -> abs2(y[i] - m(x[i])), net)[1]

```

- at the end I want to have for example N gradients and compute the mean over these before i do the update of the network parameters. How can I perform this computation given that the gradients are computed as above.

Thanks.

---

<div class="post-metadata">

**Author:** ![RoBertimus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertimus/32/48105_2.png) [@RoBertimus](https://discourse.julialang.org/u/RoBertimus)\
**Post date:** [May 4, 2023, 9:28am UTC](https://discourse.julialang.org/t/average-of-zygote-gradients/98303/2 "2023-05-04T09:28:43Z")

</div>

Ok so I solved it with a naive approach by looping over the contents of the array and computing the mean, but is there any standard “Flux” way of computing this?

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [May 4, 2023, 12:11pm UTC](https://discourse.julialang.org/t/average-of-zygote-gradients/98303/3 "2023-05-04T12:11:42Z")

</div>

It sounds like you want [`Optimisers.AccumGrad`](https://fluxml.ai/Optimisers.jl/dev/api/#Optimisers.AccumGrad)
