From 27949693f334b4d803526efa585cc957320e957c Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Mon, 2 Mar 2020 12:40:19 +0530 Subject: [PATCH] refactor --- docs/make.jl | 3 +- docs/src/models/advanced.md | 61 +++++++++++++++++++++++++++++++++++ docs/src/models/basics.md | 18 +---------- docs/src/training/training.md | 15 +-------- 4 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 docs/src/models/advanced.md diff --git a/docs/make.jl b/docs/make.jl index b950e959..365cdfc0 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -13,7 +13,8 @@ makedocs(modules=[Flux, NNlib], ["Basics" => "models/basics.md", "Recurrence" => "models/recurrence.md", "Regularisation" => "models/regularisation.md", - "Model Reference" => "models/layers.md"], + "Model Reference" => "models/layers.md", + "Advanced Model Building" => "models/advanced.md"], "Training Models" => ["Optimisers" => "training/optimisers.md", "Training" => "training/training.md"], diff --git a/docs/src/models/advanced.md b/docs/src/models/advanced.md new file mode 100644 index 00000000..4a023709 --- /dev/null +++ b/docs/src/models/advanced.md @@ -0,0 +1,61 @@ +# Advanced Model Building and Customisation + +Here we will try and describe usage of some more advanced features that Flux provides to give more control over model building. + +## Customising Parameter Collection for a Model + +Taking reference from our example `Affine` layer from the [basics](basics.md#Building-Layers-1). + +By default all the fields in the `Affine` type are collected as its parameters, however, in some cases it may be desired to hold other metadata in our "layers" that may not be needed for training, and are hence supposed to be ignored while the parameters are collected. With Flux, it is possible to mark the fields of our layers that are trainable in two ways. + +The first way of achieving this is through overloading the `trainable` function. + +```julia-repl +julia> @functor Affine + +julia> a = Affine(rand(3,3), rand(3)) +Affine{Array{Float64,2},Array{Float64,1}}([0.66722 0.774872 0.249809; 0.843321 0.403843 0.429232; 0.683525 0.662455 0.065297], [0.42394, 0.0170927, 0.544955]) + +julia> Flux.params(a) # default behavior +Params([[0.66722 0.774872 0.249809; 0.843321 0.403843 0.429232; 0.683525 0.662455 0.065297], [0.42394, 0.0170927, 0.544955]]) + +julia> Flux.trainable(a::Affine) = (a.W, a.b,) + +julia> Flux.params(a) +Params([[0.66722 0.774872 0.249809; 0.843321 0.403843 0.429232; 0.683525 0.662455 0.065297]]) +``` + +Only the fields returned by `trainable` will be collected as trainable parameters of the layer when calling `Flux.params`. + +Another way of achieving this is through the `@functor` macro directly. Here, we can mark the fields we are interested in by grouping them in the second argument: + +```julia +Flux.@functor Affine (W,) +``` + +However, doing this requires the `struct` to have a corresponding constructor that accepts those parameters. + +## Freezing Layer Parameters + +When it is desired to not include all the model parameters (for e.g. transfer learning), we can simply not pass in those layers into our call to `params`. + +Consider the simple multi-layer model where we want to omit optimising the first two `Dense` layers. This setup would look something like so: + +```julia +m = Chain( + Dense(784, 64, σ), + Dense(64, 32), + Dense(32, 10), softmax) + +ps = Flux.params(m[3:end]) +``` + +`ps` now holds a reference to only the parameters of the layers passed to it. + +During training, now the gradients would only be applied to the last `Dense` layer (and the `softmax` layer, but that is stateless so doesn't have any parameters), so only that would have its parameters changed. + +`Flux.params` also takes multiple inputs to make it easy to collect parameters from heterogenous models with a single call. A simple demonstration would be if we wanted to omit optimising the second `Dense` layer in the previous example. It would look something like this: + +```julia +Flux.params(m[1], m[3:end]) +``` diff --git a/docs/src/models/basics.md b/docs/src/models/basics.md index 3f43f29d..a5e3ca9a 100644 --- a/docs/src/models/basics.md +++ b/docs/src/models/basics.md @@ -220,20 +220,4 @@ Flux.@functor Affine This enables a useful extra set of functionality for our `Affine` layer, such as [collecting its parameters](../training/optimisers.md) or [moving it to the GPU](../gpu.md). -By default all the fields in the `Affine` type are collected as its parameters, however, in some cases it may be desired to hold other metadata in our "layers" that may not be needed for training, and are hence supposed to be ignored while the parameters are collected. With Flux, it is possible to mark the fields of our layers that are trainable in two ways. - -The first way of achieving this is through overloading the `trainable` function. - -```julia -Flux.trainable(a::Affine) = (a.W, a.b,) -``` - -Only the fields returned by `trainable` will be collected as trainable parameters of the layer when calling `Flux.params`. - -Another way of achieving this is through the `@functor` macro. Here, wee can mark the fields we are interested in like so: -Another way of achieving this is through the `@functor` macro. Here, we can mark the fields we are interested in by grouping them in the second argument: -```julia -Flux.@functor Affine (W,) -``` - -However, doing this requires the `struct` to have a corresponding constructor that accepts those parameters. +For some more helpful tricks, including parameter freezing, please checkout the [advanced usage guide](advacned.md). diff --git a/docs/src/training/training.md b/docs/src/training/training.md index 7680a776..153d0278 100644 --- a/docs/src/training/training.md +++ b/docs/src/training/training.md @@ -43,20 +43,7 @@ Such an object contains a reference to the model's parameters, not a copy, such When it is desired to not include all the model parameters (for e.g. transfer learning), we can simply not pass in those layers into our call to `params`. -Consider the simple multi-layer model where we want to omit optimising the second layer. This setup would look something like so: - -```julia -m = Chain( - Dense(784, 64, σ), - Dense(64, 32), - Dense(32, 10), softmax) - -ps = Flux.params(m[1], m[3:end]) -``` - -`ps` now holds a reference to only the parameters of the layers passed to it. - -Handling all the parameters on a layer by layer basis is explained in the [Layer Helpers](../models/basics.md) section. +Handling all the parameters on a layer by layer basis is explained in the [Layer Helpers](../models/basics.md) section. Also, for freezing model parameters, see the [Advanced Usage Guide](../models/advanced.md). ## Datasets