# Adding parameters to custom layers in Flux

**URL:** <https://discourse.julialang.org/t/adding-parameters-to-custom-layers-in-flux/42018>\
**Category:** General Usage\
**Tags:** flux\
**Created:** [June 25, 2020, 1:01am UTC](https://discourse.julialang.org/t/adding-parameters-to-custom-layers-in-flux/42018 "2020-06-25T01:01:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmurray](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmurray/32/15806_2.png) [@jmurray](https://discourse.julialang.org/u/jmurray)\
**Post date:** [June 25, 2020, 1:01am UTC](https://discourse.julialang.org/t/adding-parameters-to-custom-layers-in-flux/42018/1 "2020-06-25T01:01:17Z")

</div>

At the output of my Flux model, I need to un-normalize one of the outputs. I only need to store a single variable value somewhere (I think the best place is in a custom layer), but I don’t know how to store an ancillary variable in the `struct` and have it work with the rest of the custom layer code.

Can someone kindly point me in the right direction? The code snippet mimics the `Dense` layer [definition](https://github.com/FluxML/Flux.jl/blob/master/src/layers/basic.jl) except for the parts for which I’ve added comments.

```julia
struct Layer{F,S<:AbstractArray,T<:AbstractArray}
    W::S
    b::T
    σ::F
    offset # desired ancillary variable used for normalization
end

Layer(W, b) = Layer(W, b, identity) # want to also set the value of Layer.offset here somehow...

function Layer(in::Integer, out::Integer, σ=identity)
    return Layer(randn(out, in), randn(out), σ)
end

Flux.@functor Layer

function (a::Layer)(x::AbstractArray)
    a.σ(a.W * x .+ a.b) .+ [i==1 ? a.offset : 0 for i in 1:length(a.b)] # undoes normalization in first output
end

my_layer = Layer(3, 3, softplus)

```

A working example of a custom layer _without_ any ancillary values is [seen here](https://discourse.julialang.org/t/flux-custom-layer/41481/4).
