# Flux Get Forward Pass Results when Taking Gradient

**URL:** <https://discourse.julialang.org/t/flux-get-forward-pass-results-when-taking-gradient/88805>\
**Category:** Machine Learning\
**Tags:** question\
**Created:** [October 16, 2022, 4:04pm UTC](https://discourse.julialang.org/t/flux-get-forward-pass-results-when-taking-gradient/88805 "2022-10-16T16:04:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)\
**Post date:** [October 16, 2022, 4:04pm UTC](https://discourse.julialang.org/t/flux-get-forward-pass-results-when-taking-gradient/88805/1 "2022-10-16T16:04:54Z")

</div>

I was hoping to keep track of loss values while running training with Flux.jl. However, so far I have been unable to both evaluate my loss gradient and evaluate my loss at the same time. I have to run them separately:

```julia
ℓ = get_loss(stuff)
∇ = gradient(() -> get_loss(stuff), params)

```

However, I assume the gradient has to perform a forward pass during eval. I was hoping to define my own model that would store intermediate results in pre-allocated arrays, but Zygote doesn’t like `setindex!`.

Is there a way to both get the results of a forward pass and run a gradient eval in one go? Maybe there is a way other than `setindex!` to store results during a forward pass?

Thanks!

---

<div class="post-metadata">

**Author:** ![JLDC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jldc/32/17627_2.png) [@JLDC](https://discourse.julialang.org/u/JLDC)\
**Post date:** [October 16, 2022, 4:24pm UTC](https://discourse.julialang.org/t/flux-get-forward-pass-results-when-taking-gradient/88805/2 "2022-10-16T16:24:26Z")

</div>

You should be able to do so by using `Zygote.pullback()` as per the very last code snippet on this page: [Training · Flux](https://fluxml.ai/Flux.jl/stable/training/training/#Custom-Training-loops)

---

<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 16, 2022, 4:29pm UTC](https://discourse.julialang.org/t/flux-get-forward-pass-results-when-taking-gradient/88805/3 "2022-10-16T16:29:15Z")

</div>

[https://fluxml.ai/Zygote.jl/latest/utils/#Zygote.withgradient](https://fluxml.ai/Zygote.jl/latest/utils/#Zygote.withgradient)

---

<div class="post-metadata">

**Author:** ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)\
**Post date:** [October 16, 2022, 4:41pm UTC](https://discourse.julialang.org/t/flux-get-forward-pass-results-when-taking-gradient/88805/4 "2022-10-16T16:41:14Z")

</div>

Amazing! Thank you
