# Zygote error. "Only reference types can be differentiated with \`Params\`."

**URL:** <https://discourse.julialang.org/t/zygote-error-only-reference-types-can-be-differentiated-with-params/38224>\
**Category:** Machine Learning\
**Created:** [April 26, 2020, 6:45am UTC](https://discourse.julialang.org/t/zygote-error-only-reference-types-can-be-differentiated-with-params/38224 "2020-04-26T06:45:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emmanuel-R8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmanuel-r8/32/11839_2.png) [@Emmanuel-R8](https://discourse.julialang.org/u/Emmanuel-R8)\
**Post date:** [April 26, 2020, 6:45am UTC](https://discourse.julialang.org/t/zygote-error-only-reference-types-can-be-differentiated-with-params/38224/1 "2020-04-26T06:45:10Z")

</div>

I have been updating the Lotka-Volterra example in the model zoo. The call to `train!` fails with an error in Zygote `src/compiler/interface.jl` line 129 with the error in the title.

The reason for the error is a mystery to me.

My code is at [https://github.com/Emmanuel-R8/model-zoo/blob/update-diffeq/contrib/diffeq/lotka-volterra.jl](https://github.com/Emmanuel-R8/model-zoo/blob/update-diffeq/contrib/diffeq/lotka-volterra.jl). Call to `train!` is on the very last line.

Has anybody seen that before?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 26, 2020, 1:11pm UTC](https://discourse.julialang.org/t/zygote-error-only-reference-types-can-be-differentiated-with-params/38224/2 "2020-04-26T13:11:01Z")

</div>

That’s really annoying. I already put in 2 PRs to update that blog post and they somehow got reverted:

[https://github.com/JuliaLang/www.julialang.org/pull/583](https://github.com/JuliaLang/www.julialang.org/pull/583)  
[https://github.com/JuliaLang/www.julialang.org/pull/582](https://github.com/JuliaLang/www.julialang.org/pull/582)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 26, 2020, 2:21pm UTC](https://discourse.julialang.org/t/zygote-error-only-reference-types-can-be-differentiated-with-params/38224/3 "2020-04-26T14:21:46Z")

</div>

Should be updated now, but the TeX needs help: [Update DiffEqFlux blog post by ChrisRackauckas · Pull Request #736 · JuliaLang/www.julialang.org · GitHub](https://github.com/JuliaLang/www.julialang.org/pull/736)

---

<div class="post-metadata">

**Author:** ![Emmanuel-R8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmanuel-r8/32/11839_2.png) [@Emmanuel-R8](https://discourse.julialang.org/u/Emmanuel-R8)\
**Post date:** [April 27, 2020, 3:20am UTC](https://discourse.julialang.org/t/zygote-error-only-reference-types-can-be-differentiated-with-params/38224/4 "2020-04-27T03:20:58Z")

</div>

Thanks Chris.

Looking at your diifs, I got the error to go away. I had forgotten to wrap the parameters using `params = Flux.params(p)`.

This brings 2 points:

- the error message is not really informative (to a novice like me);
- I had read `Flux.train!()` to debug the error. The very first line is `ps = Params(ps)` and I had concluded the wrapping was not necessary before calling the function. Clearly I am still missing something.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 27, 2020, 3:26am UTC](https://discourse.julialang.org/t/zygote-error-only-reference-types-can-be-differentiated-with-params/38224/5 "2020-04-27T03:26:19Z")

</div>

The Flux API can be a bit difficult to use because of the globals which is why we recommend a `sciml_train` workflow these days, as demonstrated in the README: [https://github.com/SciML/DiffEqFlux.jl](https://github.com/SciML/DiffEqFlux.jl)

---

<div class="post-metadata">

**Author:** ![jaynick](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jaynick](https://discourse.julialang.org/u/jaynick)\
**Post date:** [April 30, 2020, 5:43pm UTC](https://discourse.julialang.org/t/zygote-error-only-reference-types-can-be-differentiated-with-params/38224/6 "2020-04-30T17:43:15Z")

</div>

Can you clarify what you mean by “globals” in the Flux API?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 30, 2020, 6:23pm UTC](https://discourse.julialang.org/t/zygote-error-only-reference-types-can-be-differentiated-with-params/38224/7 "2020-04-30T18:23:40Z")

</div>

Flux uses global references to “know” what the “parameters” are everywhere. DiffEqFlux’s `sciml_train` has a vector `p`, and you use the values in `p`, and those values get updated. Making everything explicit can make things more clear IMO.

---

<div class="post-metadata">

**Author:** ![jaynick](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jaynick](https://discourse.julialang.org/u/jaynick)\
**Post date:** [May 2, 2020, 10:07pm UTC](https://discourse.julialang.org/t/zygote-error-only-reference-types-can-be-differentiated-with-params/38224/8 "2020-05-02T22:07:56Z")

</div>

Are you referring to the variables passed to params()?

I agree you are right that explicitly passing things is more clear and reliable,  
but the params() scheme has the advantage I think that it can differentiate  
through code that was not written for Flux.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 2, 2020, 11:01pm UTC](https://discourse.julialang.org/t/zygote-error-only-reference-types-can-be-differentiated-with-params/38224/9 "2020-05-02T23:01:47Z")

</div>

> [@jaynick](#):
>
> I agree you are right that explicitly passing things is more clear and reliable,  
> but the params() scheme has the advantage I think that it can differentiate  
> through code that was not written for Flux.

So can explicit parameter setups. In fact, it’s probably moreso for things that are explicit in parameters, because lots of libraries want you to pass the values you use.
