From 2540027d05631789fcf1a9c82441c5eb80403ee9 Mon Sep 17 00:00:00 2001 From: Logan Kilpatrick <23kilpatrick23@gmail.com> Date: Fri, 26 Nov 2021 07:24:13 -0800 Subject: [PATCH 1/6] Update functor.jl --- src/functor.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/functor.jl b/src/functor.jl index ee9eb1d543..4c253280cc 100644 --- a/src/functor.jl +++ b/src/functor.jl @@ -48,6 +48,12 @@ function params!(p::Params, x, seen = IdSet()) end end +""" + params(model) + +Given a model or specific layers from a model, return the trainable parameters +such that they can be used as input to the `train!` function. +""" function params(m...) ps = Params() params!(ps, m) From a12569aff690802f66b50742fe14aa38e4226902 Mon Sep 17 00:00:00 2001 From: Logan Kilpatrick <23kilpatrick23@gmail.com> Date: Fri, 26 Nov 2021 08:27:55 -0800 Subject: [PATCH 2/6] Update src/functor.jl Co-authored-by: Michael Abbott <32575566+mcabbott@users.noreply.github.com> --- src/functor.jl | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/functor.jl b/src/functor.jl index 4c253280cc..28eb472c24 100644 --- a/src/functor.jl +++ b/src/functor.jl @@ -51,8 +51,18 @@ end """ params(model) -Given a model or specific layers from a model, return the trainable parameters -such that they can be used as input to the `train!` function. +Given a model or specific layers from a model, create a `Params` object pointing to its trainable parameters. + +This can be used with [`gradient`](@ref), or as input to the [`Flux.train!`](@ref Flux.train!) function. + +# Examples +```jldoctest +julia> params(Chain(Dense(ones(2,3))), softmax) +Params([[1.0 1.0 1.0; 1.0 1.0 1.0], [0.0, 0.0]]) + +julia> params(BatchNorm(2, relu)) +Params([Float32[0.0, 0.0], Float32[1.0, 1.0]]) +``` """ function params(m...) ps = Params() From 4d56e755aba58197f6fde946ef3de4ed9666b892 Mon Sep 17 00:00:00 2001 From: Logan Kilpatrick <23kilpatrick23@gmail.com> Date: Fri, 26 Nov 2021 09:03:51 -0800 Subject: [PATCH 3/6] Add note on custom behavior --- src/functor.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/functor.jl b/src/functor.jl index 28eb472c24..25565d0bed 100644 --- a/src/functor.jl +++ b/src/functor.jl @@ -55,6 +55,8 @@ Given a model or specific layers from a model, create a `Params` object pointing This can be used with [`gradient`](@ref), or as input to the [`Flux.train!`](@ref Flux.train!) function. +The behaviour of `params` on custom types can be customized using [`Functor.@functor`](@ref) or [`Flux.trainable`](@ref). + # Examples ```jldoctest julia> params(Chain(Dense(ones(2,3))), softmax) From be50da058f8c057536e71bcaf88a96d22229bf39 Mon Sep 17 00:00:00 2001 From: Logan Kilpatrick <23kilpatrick23@gmail.com> Date: Fri, 26 Nov 2021 20:44:49 -0800 Subject: [PATCH 4/6] Update src/functor.jl Co-authored-by: Brian Chen --- src/functor.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/functor.jl b/src/functor.jl index 25565d0bed..c4ccd5924b 100644 --- a/src/functor.jl +++ b/src/functor.jl @@ -50,6 +50,7 @@ end """ params(model) + params(layers...) Given a model or specific layers from a model, create a `Params` object pointing to its trainable parameters. From 428b1d9ea7feda2a5ffd95ae53c783d3e7fff5fc Mon Sep 17 00:00:00 2001 From: Logan Kilpatrick <23kilpatrick23@gmail.com> Date: Sat, 27 Nov 2021 08:46:34 -0800 Subject: [PATCH 5/6] Add params to the Model parameters section --- docs/src/training/training.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/src/training/training.md b/docs/src/training/training.md index fa8573d6a6..845a22d8a6 100644 --- a/docs/src/training/training.md +++ b/docs/src/training/training.md @@ -70,6 +70,10 @@ Such an object contains a reference to the model's parameters, not a copy, such 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). +```@docs +Flux.params +``` + ## Datasets The `data` argument of `train!` provides a collection of data to train with (usually a set of inputs `x` and target outputs `y`). For example, here's a dummy dataset with only one data point: From e21b094ab60fc5ebc6b511ce11c5f4cfba766430 Mon Sep 17 00:00:00 2001 From: Logan Kilpatrick <23kilpatrick23@gmail.com> Date: Sat, 27 Nov 2021 09:12:53 -0800 Subject: [PATCH 6/6] Fix link to Gradient --- src/functor.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functor.jl b/src/functor.jl index c4ccd5924b..179259a3c6 100644 --- a/src/functor.jl +++ b/src/functor.jl @@ -54,7 +54,7 @@ end Given a model or specific layers from a model, create a `Params` object pointing to its trainable parameters. -This can be used with [`gradient`](@ref), or as input to the [`Flux.train!`](@ref Flux.train!) function. +This can be used with the `gradient` function, see [Taking Gradients](@ref), or as input to the [`Flux.train!`](@ref Flux.train!) function. The behaviour of `params` on custom types can be customized using [`Functor.@functor`](@ref) or [`Flux.trainable`](@ref).