# Zygote Warning within FluxOptTools - 'cannot track gradients'

**URL:** https://discourse.julialang.org/t/zygote-warning-within-fluxopttools-cannot-track-gradients/96630
**Category:** Machine Learning
**Tags:** flux, zygote
**Created:** [March 26, 2023, 3:02pm UTC](https://discourse.julialang.org/t/zygote-warning-within-fluxopttools-cannot-track-gradients/96630 "2023-03-26T15:02:03Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![gnicolosi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnicolosi/32/48031_2.png) [@gnicolosi](https://discourse.julialang.org/u/gnicolosi)
#### Post date: [March 26, 2023, 3:02pm UTC](https://discourse.julialang.org/t/zygote-warning-within-fluxopttools-cannot-track-gradients/96630/1 "2023-03-26T15:02:03Z")

</div>

I am using NNs created using `Flux` to parameterize a function which I want to approximate in an optimal control problem. I formulate the associated optimization problem and call `Optim`, which works thanks to `FLuXOptTools`. However, I am getting the following message:

```julia
Warning: `ForwardDiff.derivative(f, x)` within Zygote cannot track gradients with respect to `f`,   
│ and `f` appears to be a closure, or a struct with fields (according to `issingletontype(typeof(f))`).
│ typeof(f) = var"#7#8"{Vector{Float64}}
└ @ Zygote C:\Users\Gabriel\.julia\packages\Zygote\TSj5C\src\lib\forward.jl:158

```

_Does anyone know why I am getting this?_ I’ve tried perusing around `Zygote` but couldn’t understand where this is coming from. I am afraid this has to do with second derivatives, so it ends up not affecting my algorithm since I am using first-order methods only (?).

As a matter of fact, the objective function decreases as well as the gradient norm, but it gets stuck halfway through. Most likely a numerical issue, and not related to the warning above, but figured I would share nonetheless… Thank you!

---

<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: [March 27, 2023, 9:01pm UTC](https://discourse.julialang.org/t/zygote-warning-within-fluxopttools-cannot-track-gradients/96630/2 "2023-03-27T21:01:02Z")

</div>

This is in dire need of a [MWE](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757). It’s most likely that some code is inside the function being optimized is using ForwardDiff without your knowledge. This may be intentional (on the library author’s part) or due to a misunderstanding of the higher-level libraries.

---

<div class="post-metadata">

### Author: ![gnicolosi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnicolosi/32/48031_2.png) [@gnicolosi](https://discourse.julialang.org/u/gnicolosi)
#### Post date: [March 27, 2023, 11:12pm UTC](https://discourse.julialang.org/t/zygote-warning-within-fluxopttools-cannot-track-gradients/96630/3 "2023-03-27T23:12:15Z")

</div>

It was indeed a `ForwardDiff` I had in my code. Not in the library, but in my own code. It was conflicting with the backward pass from `Zygote`. Thank you for clarifying.
