# Initialize weights for Flux.Dense

**URL:** <https://discourse.julialang.org/t/initialize-weights-for-flux-dense/43590>\
**Category:** New to Julia\
**Tags:** flux\
**Created:** [July 23, 2020, 9:51pm UTC](https://discourse.julialang.org/t/initialize-weights-for-flux-dense/43590 "2020-07-23T21:51:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zazizoma](https://avatars.discourse-cdn.com/v4/letter/z/5f8ce5/32.png) [@zazizoma](https://discourse.julialang.org/u/zazizoma)\
**Post date:** [July 23, 2020, 9:51pm UTC](https://discourse.julialang.org/t/initialize-weights-for-flux-dense/43590/1 "2020-07-23T21:51:53Z")

</div>

Hello.  
I can’t figure out the syntax to initialize the weights for Flux.Dense.

> using Flux  
> w0 = Float32[1, 2, 3, 4]  
> nn = Dense(4, 1, relu; initW = w0)

produces the following error.

> MethodError: objects of type Array{Float32,2} are not callable

so it seems the initW needs to be a function of some kind.

I also tried

> nn.W = w0

and got error

> setfield! immutable struct of type Dense cannot be changed

which seems to be saying I can’t change the Dense object (?).

Thanks for looking!

(Julia 1.4.2 and Flux v0.11.0)

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 8, 2020, 11:50am UTC](https://discourse.julialang.org/t/initialize-weights-for-flux-dense/43590/2 "2020-08-08T11:50:49Z")

</div>

you need to provide functions for `initW` and `initb`:

```julia
using Flux

w0 = Float32[1, 2, 3, 4]
nn = Dense(4, 1, relu;
    initW = (out, in) -> w0, initb = out -> zeros(out))

# now you can verify it
nn.W
#4-element Array{Float32,1}:
# 1.0
# 2.0
# 3.0
# 4.0

```

However, the product `*` of `initW` and whatever you feed `nn` must be defined (and the addition `.+` between that and `initb`). For example:

```julia
w0 = [1 2 3 4; 1 2 3 4]
nn([1, 2, 3, 4))
#2-element Array{Float64,1}:
# 30.0
# 30.0

```
