|
1 |
| -# Basic Layers |
| 1 | +# Built-in Layer Types |
2 | 2 |
|
| 3 | +If you started at the beginning of the guide, then you have already met the |
| 4 | +basic [`Dense`](@ref) layer, and seen [`Chain`](@ref) for combining layers. |
3 | 5 | These core layers form the foundation of almost all neural networks.
|
4 | 6 |
|
| 7 | +The `Dense` exemplifies several features: |
| 8 | + |
| 9 | +* It contains an an [activation function](@ref man-activations), which is broadcasted over the output. Because this broadcast can be fused with other operations, doing so is more efficient than applying the activation function separately. |
| 10 | + |
| 11 | +* It take an `init` keyword, which accepts a function acting like `rand`. That is, `init(2,3,4)` should create an array of this size. Flux has [many such functions](@ref man-init-funcs) built-in. All make a CPU array, moved later with [`gpu`](@ref Flux.gpu) if desired. |
| 12 | + |
| 13 | +* The bias vector is always intialised [`Flux.zeros32`](@ref). The keyword `bias=false` will turn this off, i.e. keeping the bias permanently zero. |
| 14 | + |
| 15 | +* It is annotated with [`@functor`](@ref Functors.@functor), which means that [`params`](@ref Flux.params) will see the contents, and [`gpu`](@ref Flux.gpu) will move their arrays to the GPU. |
| 16 | + |
| 17 | +By contrast, `Chain` itself contains no parameters, but connects other layers together. |
| 18 | +The section on [dataflow layers](@ref man-dataflow-layers) introduces others like this, |
| 19 | + |
| 20 | +## Fully Connected |
| 21 | + |
5 | 22 | ```@docs
|
6 |
| -Chain |
7 | 23 | Dense
|
| 24 | +Flux.Bilinear |
| 25 | +Flux.Scale |
8 | 26 | ```
|
9 | 27 |
|
10 |
| -## Convolution and Pooling Layers |
| 28 | +Perhaps `Scale` isn't quite fully connected, but it may be thought of as `Dense(Diagonal(s.weights), s.bias)`, and LinearAlgebra's `Diagonal` is a matrix which just happens to contain many zeros. |
| 29 | + |
| 30 | +## Convolution Models |
11 | 31 |
|
12 | 32 | These layers are used to build convolutional neural networks (CNNs).
|
13 | 33 |
|
| 34 | +They all expect images in what is called WHCN order: a batch of 32 colour images, each 50 x 50 pixels, will have `size(x) == (50, 50, 3, 32)`. A single grayscale image might instead have `size(x) == (28, 28, 1, 1)`. |
| 35 | + |
| 36 | +Besides images, 2D data, they also work with 1D data, where for instance stereo sound recording with 1000 samples might have `size(x) == (1000, 2, 1)`. They will also work with 3D data, `ndims(x) == 5`, where again the last two dimensions are channel and batch. |
| 37 | + |
| 38 | +To understand how strides and padding work, the article by [Dumoulin & Visin](https://arxiv.org/abs/1603.07285) has great illustrations. |
| 39 | + |
14 | 40 | ```@docs
|
15 | 41 | Conv
|
16 | 42 | Conv(weight::AbstractArray)
|
17 |
| -AdaptiveMaxPool |
18 |
| -MaxPool |
19 |
| -GlobalMaxPool |
20 |
| -AdaptiveMeanPool |
21 |
| -MeanPool |
22 |
| -GlobalMeanPool |
23 |
| -DepthwiseConv |
24 | 43 | ConvTranspose
|
25 | 44 | ConvTranspose(weight::AbstractArray)
|
26 | 45 | CrossCor
|
27 | 46 | CrossCor(weight::AbstractArray)
|
| 47 | +DepthwiseConv |
28 | 48 | SamePad
|
29 | 49 | Flux.flatten
|
30 | 50 | ```
|
31 | 51 |
|
32 |
| -## Upsampling Layers |
| 52 | +### Pooling |
| 53 | + |
| 54 | +These layers are commonly used after a convolution layer, and reduce the size of its output. They have no trainable parameters. |
| 55 | + |
| 56 | +```@docs |
| 57 | +AdaptiveMaxPool |
| 58 | +MaxPool |
| 59 | +GlobalMaxPool |
| 60 | +AdaptiveMeanPool |
| 61 | +MeanPool |
| 62 | +GlobalMeanPool |
| 63 | +``` |
| 64 | + |
| 65 | +## Upsampling |
| 66 | + |
| 67 | +The opposite of pooling, these layers increase the size of an array. They have no trainable parameters. |
33 | 68 |
|
34 | 69 | ```@docs
|
35 | 70 | Upsample
|
36 | 71 | PixelShuffle
|
37 | 72 | ```
|
38 | 73 |
|
39 |
| -## Recurrent Layers |
| 74 | +## Embedding Vectors |
40 | 75 |
|
41 |
| -Much like the core layers above, but can be used to process sequence data (as well as other kinds of structured data). |
| 76 | +These layers accept an index, and return a vector (or several indices, and several vectors). The possible embedding vectors are learned parameters. |
42 | 77 |
|
43 | 78 | ```@docs
|
44 |
| -RNN |
45 |
| -LSTM |
46 |
| -GRU |
47 |
| -GRUv3 |
48 |
| -Flux.Recur |
49 |
| -Flux.reset! |
| 79 | +Flux.Embedding |
| 80 | +Flux.EmbeddingBag |
50 | 81 | ```
|
51 | 82 |
|
52 |
| -## Other General Purpose Layers |
| 83 | +## [Dataflow Layers, or Containers](@id man-dataflow-layers) |
53 | 84 |
|
54 |
| -These are marginally more obscure than the Basic Layers. |
55 |
| -But in contrast to the layers described in the other sections are not readily grouped around a particular purpose (e.g. CNNs or RNNs). |
| 85 | +The basic `Chain(F, G, H)` applies the layers it contains in sequence, equivalent to `H ∘ G ∘ F`. Flux has some other layers which contain layers, but connect them up in a more complicated way: `SkipConnection` allows ResNet's residual connection. |
56 | 86 |
|
57 | 87 | ```@docs
|
| 88 | +Chain |
| 89 | +Flux.activations |
58 | 90 | Maxout
|
59 | 91 | SkipConnection
|
60 | 92 | Parallel
|
61 |
| -Flux.Bilinear |
62 |
| -Flux.Scale |
63 |
| -Flux.Embedding |
| 93 | +PairwiseFusion |
| 94 | +``` |
| 95 | + |
| 96 | +## Recurrent Models |
| 97 | + |
| 98 | +Much like the core layers above, but can be used to process sequence data (as well as other kinds of structured data). |
| 99 | + |
| 100 | +```@docs |
| 101 | +RNN |
| 102 | +LSTM |
| 103 | +GRU |
| 104 | +GRUv3 |
| 105 | +Flux.Recur |
| 106 | +Flux.reset! |
64 | 107 | ```
|
65 | 108 |
|
66 | 109 | ## Normalisation & Regularisation
|
67 | 110 |
|
68 |
| -These layers don't affect the structure of the network but may improve training times or reduce overfitting. |
| 111 | +These layers don't affect the structure of the network but may improve training times or reduce overfitting. Some of them contain trainable parameters, while others do not. |
69 | 112 |
|
70 | 113 | ```@docs
|
71 |
| -Flux.normalise |
72 | 114 | BatchNorm
|
73 | 115 | Dropout
|
74 |
| -Flux.dropout |
75 | 116 | AlphaDropout
|
76 | 117 | LayerNorm
|
77 | 118 | InstanceNorm
|
78 | 119 | GroupNorm
|
| 120 | +Flux.normalise |
| 121 | +Flux.dropout |
79 | 122 | ```
|
80 | 123 |
|
81 |
| -### Testmode |
| 124 | +### Test vs. Train |
| 125 | + |
| 126 | +Several normalisation layers behave differently under training and inference (testing). By default, Flux will automatically determine when a layer evaluation is part of training or inference. |
| 127 | + |
| 128 | +!!! warning |
| 129 | + This automatic train/test detection works best with Zygote, the default |
| 130 | + automatic differentiation package. It may not work with other packages |
| 131 | + such as Tracker, Yota, or ForwardDiff. |
82 | 132 |
|
83 |
| -Many normalisation layers behave differently under training and inference (testing). By default, Flux will automatically determine when a layer evaluation is part of training or inference. Still, depending on your use case, it may be helpful to manually specify when these layers should be treated as being trained or not. For this, Flux provides `Flux.testmode!`. When called on a model (e.g. a layer or chain of layers), this function will place the model into the mode specified. |
| 133 | +The functions `Flux.trainmode!` and `Flux.testmode!` let you manually specify which behaviour you want. When called on a model, they will place all layers within the model into the specified mode. |
84 | 134 |
|
85 | 135 | ```@docs
|
86 | 136 | Flux.testmode!
|
|
0 commit comments