# Flux.jl: params() and gradient() ocnfusion

**URL:** <https://discourse.julialang.org/t/flux-jl-params-and-gradient-ocnfusion/66775>\
**Category:** Machine Learning\
**Created:** [August 21, 2021, 11:45am UTC](https://discourse.julialang.org/t/flux-jl-params-and-gradient-ocnfusion/66775 "2021-08-21T11:45:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex-ZA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-za/32/27612_2.png) [@Alex-ZA](https://discourse.julialang.org/u/Alex-ZA)\
**Post date:** [August 21, 2021, 11:45am UTC](https://discourse.julialang.org/t/flux-jl-params-and-gradient-ocnfusion/66775/1 "2021-08-21T11:45:41Z")

</div>

Hello everyone,

I apologise if this is not the area to ask Flux questions. If not, please direct to the correct place.

I have been going through the Flux.jl documentation and came across [this code](https://fluxml.ai/Flux.jl/stable/models/basics/).

```julia
julia> x = [2, 1];

julia> y = [2, 0];

julia> gs = gradient(params(x, y)) do
         f(x, y)
       end
Grads(...)

julia> gs[x]
2-element Vector{Int64}:
 0
 2

julia> gs[y]
2-element Vector{Int64}:
  0
 -2

```

I understand how the `do end` block works, but I am struggling to understand the use and function of `params`. I cannot find good documentation on it. If I missed it, please point me towards it!

Thank you for your time.

---

<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:** [August 21, 2021, 3:51pm UTC](https://discourse.julialang.org/t/flux-jl-params-and-gradient-ocnfusion/66775/2 "2021-08-21T15:51:08Z")

</div>

Welcome!

I actually want to turn this question around and ask you what you found to be deficient with the existing explanation in the paragraph above that code block. This feedback could be useful for improving the docs themselves.

---

<div class="post-metadata">

**Author:** ![Alex-ZA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-za/32/27612_2.png) [@Alex-ZA](https://discourse.julialang.org/u/Alex-ZA)\
**Post date:** [August 23, 2021, 7:45am UTC](https://discourse.julialang.org/t/flux-jl-params-and-gradient-ocnfusion/66775/3 "2021-08-23T07:45:11Z")

</div>

Hi @ToucheSir, thank you for your reply! I accept your turn around 🙂

One would be able to figure out how params() works given the context of that specific example, but I would just like to see some documentation on the function itself. Unless I am missing something and being unreasonable?

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [August 23, 2021, 10:37am UTC](https://discourse.julialang.org/t/flux-jl-params-and-gradient-ocnfusion/66775/4 "2021-08-23T10:37:23Z")

</div>

I’m not an authority on this, but my guess is that `params` returns a `Params` struct and this is used for dispatch to disambigue the “normal” use of `gradient` which assumes that any arguments after the first shall be used as inputs to the first argument (which is always a function).

One possible reason why this is not very well described is that it is considered a temporary and somewhat ugly patch to bridge how Flux used gradients to update parameters in the pre-Zygote days. I think it has survied a fair bit longer than anyone intended/hoped it would.

As for the function `params`, it basically just traveres the model structure and searches for arrays with numbers using `Functors.jl` and puts them in a `Params` struct.

---

<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:** [August 23, 2021, 4:13pm UTC](https://discourse.julialang.org/t/flux-jl-params-and-gradient-ocnfusion/66775/5 "2021-08-23T16:13:04Z")

</div>

@DrChainsaw hit the nail on the head here. That said, we should add some documentation for this function, even if it just points to the tutorial. Do you mind filing an issue?
