-
-
Notifications
You must be signed in to change notification settings - Fork 608
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
Add docstring for params
#1786
Add docstring for params
#1786
Conversation
Co-authored-by: Michael Abbott <[email protected]>
|
||
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). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behaviour of `params` on custom types can be customized using [`Functor.@functor`](@ref) or [`Flux.trainable`](@ref). | |
The behaviour of `params` on custom types can be customized using [`Functors.@functor`](@ref) or [`Flux.trainable`](@ref). |
Unfortunately Functors.@functor
won't resolve because it's a cross-package link. Not sure what we can do about that, perhaps a manual link to the latest version of the Functors docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to CI there are many such failures, although they do not cause it to fail overall here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I saw a couple searching for other examples of this. Short of a solution in Documenter.jl, I guess our best option is a full link?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the page this wants to link to is here https://fluxml.ai/Flux.jl/stable/models/functors/#Functors.jl I think it ought to be possible at least to have an automatic link to the heading of the page. Maybe that's what you mean? Like https://raw.githubusercontent.com/JuliaLang/julia/master/doc/src/manual/arrays.md it seems you can write e.g.
## [Functors.jl](@id man-h2-functors)
and then refer to that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://juliadocs.github.io/Documenter.jl/stable/man/guide/#Cross-Referencing makes it seem like we can do [Functors.jl](@ref)
in the doc string and it will just work (assuming there is only one heading like that).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth a try. Also not in scope for this PR, but Functors.@functor
needs to be rendered correctly in the docs: https://fluxml.ai/Functors.jl/stable/api/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also trainable
isn't documented either. Looks like we'll need a follow-up PR :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many PR's to come : )
src/functor.jl
Outdated
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have an example and/or note that most built-in container types will be unpacked if passed into params
. This has bitten people frequently when they try to use it like Zygote.Params
and pass in a couple of arrays (Flux.params(array1)
will unpack every element of array1
instead of including the array itself...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or the other way around, it's Params(array)
which is surprising? Maybe something like:
julia> params([1, 2, 3])
Params([[1, 2, 3]])
julia> params(1, [2 2], (alpha=[3,3,3], beta=Ref(4), gamma=sin))
Params([[2 2], [3, 3, 3]])
julia> keys(ans.params.dict)
KeySet for a IdDict{Any, Nothing} with 2 entries. Keys:
[2 2]
[3, 3, 3]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I'm aware of?
julia> Flux.Params([1, [2 2], [3,3,3], Ref(4), sin])
Params([1, [2 2], [3, 3, 3], Base.RefValue{Int64}(4), sin])
The docstring for Params
is reasonably clear too (explicitly asks for an array).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I mean is by surprise is this difference, you can't give Params
just one naked array:
julia> x = [1,2,3]; gradient(() -> sum(x), Params(x))[x]
ERROR: KeyError: key [1, 2, 3] not found
julia> x = [1,2,3]; gradient(() -> sum(x), params(x))[x]
3-element Fill{Int64}, with entries equal to 1
If that's not what you meant, what did you mean people get bitten by?
Giving params
an array of arrays instead is OK:
julia> x = [1,2,3]; gradient(() -> sum(x), params([x, x]))[x]
3-element Fill{Int64}, with entries equal to 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the life of me I can't seem to find it the GH issue or discourse discussions for it, but I think it was an issue with not expecting arrays of non-numbers (or was it tuples?) to be unpacked. Whereas Params
documents this unpacking, params
doesn't and certainly doesn't document that it unpacks recursively when given a nested structure.
Co-authored-by: Brian Chen <[email protected]>
Also, which file or section should we add the |
I'm not sure either. Co-locating it with https://fluxml.ai/Flux.jl/stable/models/basics/#Building-Simple-Models is one option, but a little out of the way. WDYT about creating a new page under the "Training Models" section for parameter handling? It could just be the docstring for now, but that would give us an opportunity to expand later. |
Actually, https://fluxml.ai/Flux.jl/stable/training/training/#Model-parameters doesn't look too bad. |
Agreed |
Preview the PR here: https://fluxml.ai/Flux.jl/previews/PR1786/training/training/#Flux.params |
Is it worth mentioning that a vector of arrays can also be passed to |
I think that would fit with my note in #1786 (comment) that any vector of arrays passed to |
@ToucheSir Perhaps we can merge this and have one sweeping PR to try and get all the ref issues fixed in the docs? There are a lot that should be resolved. |
Since |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a good step forwards. Figuring out Functors links will involve touching many things, can be another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bors r+
But it may not listen, Brian you may need to approve?
1786: Add docstring for `params` r=mcabbott a=logankilpatrick `params` is used all over the model zoo and tutorials in Flux. There should be a docstring or we should not publicly use it. Not sure if the proposed docstring gets the job done 100% but this seems like a step in the right direction. Co-authored-by: Logan Kilpatrick <[email protected]> Co-authored-by: Michael Abbott <[email protected]>
This PR was included in a batch that successfully built, but then failed to merge into master. It will not be retried. Additional information: {"message":"1 review requesting changes and 1 approving review by reviewers with write access.","documentation_url":"https://docs.github.com/articles/about-protected-branches"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
bors r+ |
Build succeeded: |
params
is used all over the model zoo and tutorials in Flux. There should be a docstring or we should not publicly use it. Not sure if the proposed docstring gets the job done 100% but this seems like a step in the right direction.