Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend setup docstring to show custom struct #85

Merged
merged 3 commits into from
Jun 3, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/Optimisers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,33 @@ or [`update!`](@ref).

# Example
```jldoctest
julia> Optimisers.setup(Descent(0.1f0), (x = rand(3), y = (true, false), z = tanh))
(x = Leaf(Descent{Float32}(0.1), nothing), y = (nothing, nothing), z = nothing)
julia> m = (x = rand(3), y = (true, false), z = tanh);

julia> Optimisers.setup(Momentum(), m) # same field names as m
(x = Leaf(Momentum{Float32}(0.01, 0.9), [0.0, 0.0, 0.0]), y = (nothing, nothing), z = nothing)
```

The recursion into structures uses Functors.jl, and any new `struct`s containing parameters
need to be marked with `Functors.@functor` before use. Further refinements can, if necessary,
mcabbott marked this conversation as resolved.
Show resolved Hide resolved
be made by overloading [`trainable`](@ref).

```jldoctest
julia> struct Layer; mat; vec; end

julia> model = (lay = Layer(rand(2, 2), rand(2)), fun = sin);

julia> Optimisers.setup(Momentum(), model) # new struct is by default ignored
(lay = nothing, fun = nothing)

julia> using Functors; @functor Layer # annotate this type as containing parameters

julia> Optimisers.setup(Momentum(), model)
(lay = (mat = Leaf(Momentum{Float32}(0.01, 0.9), [0.0 0.0; 0.0 0.0]), vec = Leaf(Momentum{Float32}(0.01, 0.9), [0.0, 0.0])), fun = nothing)

julia> Optimisers.trainable(l::Layer) = (vec = l.vec,) # if necessary, ignore some parameters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My inclination is that we'd want a motivating example where @functor Layer (vec,) is not sufficient. The counter-argument would be that 99% of @functor use should not specify a list of properties because users may want to traverse to them with fmap and friends.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe best to leave this off here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


julia> Optimisers.setup(Momentum(), model)
(lay = (mat = nothing, vec = Leaf(Momentum{Float32}(0.01, 0.9), [0.0, 0.0])), fun = nothing)
```
"""
setup
Expand Down