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

Add docstring for params #1786

Merged
merged 7 commits into from
Nov 30, 2021
Merged

Add docstring for params #1786

merged 7 commits into from
Nov 30, 2021

Conversation

logankilpatrick
Copy link
Member

@logankilpatrick logankilpatrick commented Nov 26, 2021

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.

src/functor.jl Outdated Show resolved Hide resolved

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).
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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?

Copy link
Member

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.

Copy link
Member

@ToucheSir ToucheSir Nov 27, 2021

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?

Copy link
Member

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.

Copy link
Member Author

@logankilpatrick logankilpatrick Nov 27, 2021

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).

Copy link
Member

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/

Copy link
Member

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 :)

Copy link
Member Author

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 Show resolved Hide resolved
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))
Copy link
Member

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...)

Copy link
Member

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]

Copy link
Member

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).

Copy link
Member

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

Copy link
Member

@ToucheSir ToucheSir Nov 27, 2021

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]>
@logankilpatrick
Copy link
Member Author

Also, which file or section should we add the params docs too? I am not quite sure where it fits in best.

@ToucheSir
Copy link
Member

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.

@ToucheSir
Copy link
Member

Actually, https://fluxml.ai/Flux.jl/stable/training/training/#Model-parameters doesn't look too bad.

@logankilpatrick
Copy link
Member Author

Actually, https://fluxml.ai/Flux.jl/stable/training/training/#Model-parameters doesn't look too bad.

Agreed

@logankilpatrick
Copy link
Member Author

@darsnack
Copy link
Member

Is it worth mentioning that a vector of arrays can also be passed to params? This allows very manual, fine-grained control over collecting parameters.

@ToucheSir
Copy link
Member

I think that would fit with my note in #1786 (comment) that any vector of arrays passed to params will be unpacked, even if they are nested within another functor-able value.

@logankilpatrick
Copy link
Member Author

@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.

@DhairyaLGandhi
Copy link
Member

Since params([1,23]) would resolve correctly and the remaining issue is with Params I believe we can safely merge this. Params explicitly requires an array and that is documented too, but if one feels that an extra note that says numerical arrays need to be wrapped in a "container" array is required, that's fine too but that is an orthogonal change to this docstring.

mcabbott
mcabbott previously approved these changes Nov 30, 2021
Copy link
Member

@mcabbott mcabbott left a 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.

src/functor.jl Outdated Show resolved Hide resolved
Copy link
Member

@mcabbott mcabbott left a 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?

bors bot added a commit that referenced this pull request Nov 30, 2021
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]>
@bors
Copy link
Contributor

bors bot commented Nov 30, 2021

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"}

Copy link
Member

@ToucheSir ToucheSir left a comment

Choose a reason for hiding this comment

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

Done.

@mcabbott
Copy link
Member

bors r+

@bors
Copy link
Contributor

bors bot commented Nov 30, 2021

Build succeeded:

@bors bors bot merged commit 8fa4317 into master Nov 30, 2021
@mcabbott mcabbott deleted the logankilpatrick-patch-7 branch November 30, 2021 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants