# How to retrieve gradient value in custom Flux training loop?

**URL:** https://discourse.julialang.org/t/how-to-retrieve-gradient-value-in-custom-flux-training-loop/82435
**Category:** Machine Learning
**Tags:** question
**Created:** [June 8, 2022, 1:48pm UTC](https://discourse.julialang.org/t/how-to-retrieve-gradient-value-in-custom-flux-training-loop/82435 "2022-06-08T13:48:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![BatyLeo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/batyleo/32/35017_2.png) [@BatyLeo](https://discourse.julialang.org/u/BatyLeo)
#### Post date: [June 8, 2022, 1:48pm UTC](https://discourse.julialang.org/t/how-to-retrieve-gradient-value-in-custom-flux-training-loop/82435/1 "2022-06-08T13:48:44Z")

</div>

Hello,

I have a few questions about custom training loops in Flux. According to the [documentation](https://fluxml.ai/Flux.jl/v0.10/training/training/#Custom-Training-loops-1), I should write something like that :

```julia
function my_custom_train!(loss, ps, data, opt)
  ps = Params(ps)
  for d in data
    gs = gradient(ps) do
      training_loss = loss(d...)
      # Insert what ever code you want here that needs Training loss, e.g. logging
      return training_loss
    end
    # insert what ever code you want here that needs gradient
    # E.g. logging with TensorBoardLogger.jl as histogram so you can see if it is becoming huge
    update!(opt, ps, gs)
    # Here you might like to check validation set accuracy, and break out to do early stopping
  end
end

```

1. If the `ps` argument is `Flux.params(my_model)` like in the `Flux.train!` method, is the `ps = Params(ps)` row redundant ?
2. `gs` seems to be an instance of `Zygote.Grads`, how can I retrieve gradient value in the loop ? I tried `gs[ps]` and `gs[my_model]` without any success.

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [June 8, 2022, 2:00pm UTC](https://discourse.julialang.org/t/how-to-retrieve-gradient-value-in-custom-flux-training-loop/82435/2 "2022-06-08T14:00:31Z")

</div>

1. Yes, it shouldn’t be needed.
2. The `Grads` type contains the fields `grads` and `params`, so you can access them as `gs.grads`.

---

<div class="post-metadata">

### Author: ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)
#### Post date: [June 8, 2022, 2:02pm UTC](https://discourse.julialang.org/t/how-to-retrieve-gradient-value-in-custom-flux-training-loop/82435/3 "2022-06-08T14:02:15Z")

</div>

> 1. If the `ps` argument is `Flux.params(my_model)` like in the `Flux.train!` method, is the `ps = Params(ps)` row redundant ?

yes

> 1. `gs` seems to be an instance of `Zygote.Grads`, how can I retrieve gradient value in the loop ? I tried `gs[ps]` and `gs[my_model]` without any success.

```julia
for p in ps
   print(gs[p])
end

```

---

<div class="post-metadata">

### Author: ![BatyLeo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/batyleo/32/35017_2.png) [@BatyLeo](https://discourse.julialang.org/u/BatyLeo)
#### Post date: [June 8, 2022, 2:16pm UTC](https://discourse.julialang.org/t/how-to-retrieve-gradient-value-in-custom-flux-training-loop/82435/4 "2022-06-08T14:16:42Z")

</div>

Thank you for your help ! This is exactly what I was looking for.
