# Flux -- decompose layers + parameters?

**URL:** <https://discourse.julialang.org/t/flux-decompose-layers-parameters/58026>\
**Category:** New to Julia\
**Created:** [March 26, 2021, 3:34pm UTC](https://discourse.julialang.org/t/flux-decompose-layers-parameters/58026 "2021-03-26T15:34:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 26, 2021, 3:34pm UTC](https://discourse.julialang.org/t/flux-decompose-layers-parameters/58026/1 "2021-03-26T15:34:28Z")

</div>

A silly question… I used simple Flux networks 2 years ago, and I notice there have been some changes… Suppose I have a network named `md` (…model…):

1. In the old system, I could pick out layer no. `i` by `md.layers[i]`.  
 → Am I right in that this has been replaced by `md[i]`?
2. In the old system, I could pick bias vector `b` and weight matrix `W` by `md.b.data` and `md.W.data`.  
 → Am I right in that now, I do `par = params(md)` and then `par[i]` for `i in 1:2:end` gives the bias vectors `b`, while `par[i]` for `i in 2:2:end` gives the weight matrices `W`?
3. In the old version of `Flux`, it was a little tricky to specify the data type of the bias vectors and weight matrices.  
 → Is this simpler, now?

[I ask because I didn’t find documentation for `params`, etc. in the documentation… perhaps it would have been simpler if had available a PDF file or an ePUB file which I could search in. Anyyway, I’m planning a Julia intro seminar for my colleagues, and want to show off Flux.]

---

<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:** [March 26, 2021, 5:38pm UTC](https://discourse.julialang.org/t/flux-decompose-layers-parameters/58026/2 "2021-03-26T17:38:04Z")

</div>

> [@BLI](#):
>
> In the old system, I could pick out layer no. `i` by `md.layers[i]` .  
> → Am I right in that this has been replaced by `md[i]` ?

Nope, you can still do `md.layers[i]` if `md` is a `Chain`. `md[i]` is definitely preferred though.

> [@BLI](#):
>
> In the old system, I could pick bias vector `b` and weight matrix `W` by `md.b.data` and `md.W.data` .  
> → Am I right in that now, I do `par = params(md)` and then `par[i]`  
> for `i in 1:2:end` gives the bias vectors `b` , while `par[i]` for `i in 2:2:end` gives the weight matrices `W` ?

Because Flux no longer uses Tracker, you can just reference `md.b` and `md.W` directly. `params` is mostly useful as a way around listing every single parameter array in a model when calling `gradient`.

> [@BLI](#):
>
> In the old version of `Flux` , it was a little tricky to specify the data type of the bias vectors and weight matrices.  
> → Is this simpler, now?

Do you mean e.g. `Float64` vs `Float32`? Flux now has helper functions that will allow you to convert a layer or an entire model’s weights between both (`f64` and `f32` respectively).

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 26, 2021, 5:55pm UTC](https://discourse.julialang.org/t/flux-decompose-layers-parameters/58026/3 "2021-03-26T17:55:21Z")

</div>

> [@ToucheSir](#):
>
> Flux now has helper functions that will allow you to convert a layer or an entire model’s weights between both ( `f64` and `f32` respectively).

What are these helper functions? Are they discussed in the documentation, or only in some tutorials?

[Would they be something like `f64(md)` or `convert(f64, md)`, or something?]

---

<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:** [March 26, 2021, 6:01pm UTC](https://discourse.julialang.org/t/flux-decompose-layers-parameters/58026/4 "2021-03-26T18:01:53Z")

</div>

Not directly. [GPU Support · Flux](https://fluxml.ai/Flux.jl/stable/gpu/) and [Advanced Model Building · Flux](https://fluxml.ai/Flux.jl/stable/models/advanced/) talk about the underlying functionality, but I assume the helpers weren’t documented because a) they’re [quite simple](https://github.com/FluxML/Flux.jl/blob/e2b73e142067a99e88cc393c3fdd51b1e2029cf2/src/functor.jl#L77-L78), and b) most people never bothered to use them because the default of Float32 works well enough for them. If you wouldn’t mind filing an issue (or preferably, a PR) for the docs, we can add a note about both.
