|
3 | 3 | Flux provides utility functions which can be used to initialize your layers
|
4 | 4 | or to regularly execute callback functions.
|
5 | 5 |
|
6 |
| -## Layer Initialization |
| 6 | +## Layer Initialisation |
7 | 7 |
|
8 |
| -These are primarily useful if you are planning to write your own layers. |
9 |
| -Flux initializes convolutional layers and recurrent cells with `glorot_uniform` |
10 |
| -by default. |
11 |
| -To change the default on an applicable layer, pass the desired function with the |
12 |
| -`init` keyword. For example: |
| 8 | +Flux initialises convolutional layers and recurrent cells with `glorot_uniform` by default. |
| 9 | +Most layers accept a function as an `init` keyword, which replaces this default. For example: |
13 | 10 |
|
14 | 11 | ```jldoctest; setup = :(using Flux)
|
15 |
| -julia> conv = Conv((3, 3), 1 => 8, relu; init=Flux.glorot_normal) |
16 |
| -Conv((3, 3), 1 => 8, relu) # 80 parameters |
| 12 | +julia> conv = Conv((3, 3), 3 => 2, relu; init=Flux.glorot_normal) |
| 13 | +Conv((3, 3), 3 => 2, relu) # 56 parameters |
| 14 | +
|
| 15 | +julia> conv.bias |
| 16 | +2-element Vector{Float32}: |
| 17 | + 0.0 |
| 18 | + 0.0 |
| 19 | +``` |
| 20 | + |
| 21 | +Note that `init` creates the weight array, but not the bias vector. |
| 22 | + |
| 23 | +Many of the initialisation functions accept keywords such as `gain`, |
| 24 | +and a random number generator. To make it easy to pass these to layers, |
| 25 | +there are methods which return a function: |
| 26 | + |
| 27 | +```jldoctest; setup = :(using Flux, Random) |
| 28 | +julia> Dense(4 => 5, tanh; init=Flux.glorot_uniform(gain=2)) |
| 29 | +Dense(4 => 5, tanh) # 25 parameters |
| 30 | +
|
| 31 | +julia> Dense(4 => 5, tanh; init=Flux.randn32(MersenneTwister(1))) |
| 32 | +Dense(4 => 5, tanh) # 25 parameters |
17 | 33 | ```
|
18 | 34 |
|
19 | 35 | ```@docs
|
20 | 36 | Flux.glorot_uniform
|
21 | 37 | Flux.glorot_normal
|
22 | 38 | Flux.kaiming_uniform
|
23 | 39 | Flux.kaiming_normal
|
| 40 | +Flux.truncated_normal |
24 | 41 | Flux.orthogonal
|
25 | 42 | Flux.sparse_init
|
| 43 | +Flux.identity_init |
| 44 | +Flux.ones32 |
| 45 | +Flux.rand32 |
26 | 46 | ```
|
27 | 47 |
|
28 | 48 | ## Changing the type of model parameters
|
29 | 49 |
|
| 50 | +The default `eltype` for models is `Float32` since models are often trained/run on GPUs. |
| 51 | +The `eltype` of model `m` can be changed to `Float64` by `f64(m)`: |
| 52 | + |
30 | 53 | ```@docs
|
31 | 54 | Flux.f64
|
32 | 55 | Flux.f32
|
33 | 56 | ```
|
34 | 57 |
|
35 |
| -The default `eltype` for models is `Float32` since models are often trained/run on GPUs. The `eltype` of model `m` can be changed to `Float64` by `f64(m)`, or to `Float32` by `f32(m)`. |
36 |
| - |
37 | 58 | ## Model Building
|
38 | 59 |
|
39 | 60 | Flux provides some utility functions to help you generate models in an automated fashion.
|
|
0 commit comments