# Flux.@functor won't work on custom layer (empty parameters)

**URL:** https://discourse.julialang.org/t/flux-functor-wont-work-on-custom-layer-empty-parameters/101587
**Category:** Machine Learning
**Tags:** flux
**Created:** [July 13, 2023, 6:58pm UTC](https://discourse.julialang.org/t/flux-functor-wont-work-on-custom-layer-empty-parameters/101587 "2023-07-13T18:58:17Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Bizzi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bizzi/32/51484_2.png) [@Bizzi](https://discourse.julialang.org/u/Bizzi)
#### Post date: [July 13, 2023, 6:58pm UTC](https://discourse.julialang.org/t/flux-functor-wont-work-on-custom-layer-empty-parameters/101587/1 "2023-07-13T18:58:17Z")

</div>

I’m having a hard time understanding the behavior of the `@functor` macro. See the snippets below: For the first one, I follow the tutorial on custom layers step by step and it works. For the second one, I alter the code slightly for my own custom layer and `@functor` is no longer able to peek into the struct, leading to empty `params`.

Can anyone share any insights on why the two codes work differently? Thanks in advance.  
The tutorial:

```julia
using Flux

#Custom Layer from the Tutorial
struct Affine
    W
    b
end

# Some constructor
Affine(in::Integer, out::Integer) = Affine(randn(out, in), randn(out))
# Overload call, so the object can be used as a function
(m::Affine)(x) = m.W * x .+ m.b
  
a = Affine(10, 5)
a(rand(10)) # Works: 5-element vector

Flux.@functor Affine 
Flux.params(a) #Works

```

My code:

```julia
#My Custom Layer
struct MyLayer
    a
    b
    c
end
 
# Some constructor
MyLayer(a::Real, b::Real) = MyLayer(a,b,-b)  
# Overload call, so the object can be used as a function
(m::MyLayer)(x) = [x[1],m.a*x[2] + m.b*x[1]]
  
b = MyLayer(1.,2.)
b(rand(2)) # Works: 2-element vector

Flux.@functor MyLayer 
Flux.params(b) # Doesn't work: Params([])

```

Got these results on a fresh VSCode session, using Flux v.0.13.13.

---

<div class="post-metadata">

### Author: ![avikpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avikpal/32/6550_2.png) [@avikpal](https://discourse.julialang.org/u/avikpal)
#### Post date: [July 13, 2023, 7:01pm UTC](https://discourse.julialang.org/t/flux-functor-wont-work-on-custom-layer-empty-parameters/101587/2 "2023-07-13T19:01:50Z")

</div>

`@functor` won’t extract fields that are scalar. You need the parameters to be arrays.

---

<div class="post-metadata">

### Author: ![Bizzi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bizzi/32/51484_2.png) [@Bizzi](https://discourse.julialang.org/u/Bizzi)
#### Post date: [July 13, 2023, 7:15pm UTC](https://discourse.julialang.org/t/flux-functor-wont-work-on-custom-layer-empty-parameters/101587/3 "2023-07-13T19:15:03Z")

</div>

Thank you, that’s exactly it.

---

<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: [July 19, 2023, 5:46am UTC](https://discourse.julialang.org/t/flux-functor-wont-work-on-custom-layer-empty-parameters/101587/4 "2023-07-19T05:46:35Z")

</div>

Functors.jl will, but Optimisers.jl won’t handle them. We tried making it do so a while back, but it turns out that people store a bunch of scalars they don’t want to be trainable in their models. We might consider changing this in a future (very breaking) version, but for now [Optimizing scalars · Issue #92 · FluxML/Optimisers.jl · GitHub](https://github.com/FluxML/Optimisers.jl/issues/92) has further background info.
