-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Improve keyword argument handling in @compact
#16
Conversation
41700fe
to
2ad64e2
Compare
Codecov ReportPatch coverage:
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more. Additional details and impacted files@@ Coverage Diff @@
## master #16 +/- ##
==========================================
+ Coverage 78.64% 79.14% +0.50%
==========================================
Files 6 6
Lines 206 211 +5
==========================================
+ Hits 162 167 +5
Misses 44 44
☔ View full report in Codecov by Sentry. |
@mcabbott I saw you added a fourth case to #12 (reply in thread). I don't think that affects this PR though, which just fixes keyword arguments. |
src/compact.jl
Outdated
in = 5 | ||
out = 7 | ||
d = @compact(; in, out, W=randn(out, in), b=zeros(out), act=relu) do x |
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.
Would be better not to hit Base.in
. Also, what's gained by storing this, or indeed storing act=relu
?
in = 5 | |
out = 7 | |
d = @compact(; in, out, W=randn(out, in), b=zeros(out), act=relu) do x | |
d_in, d_out = 3, 7 | |
layer = @compact(W=randn(d_out, d_in), b=zeros(d_out), act=relu) do x |
And maybe instead show how to make the equivalent Flux layer?
layer([1,2,3]) ≈ Dense(layer.variables.W, zeros(7), relu)([1,2,3]) # 7-element Vector
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. (The act
is indeed not necessary, but it's just a matter of style and since it's harmless I didn't want to mess with it here)
# process keyword arguments | ||
if Meta.isexpr(_kwexs[1], :parameters) # handle keyword arguments provided after semicolon | ||
kwexs1 = map(ex -> ex isa Symbol ? Expr(:kw, ex, ex) : ex, _kwexs[1].args) | ||
_kwexs = _kwexs[2:end] | ||
else | ||
kwexs1 = () | ||
end | ||
kwexs2 = map(ex -> Expr(:kw, ex.args...), _kwexs) # handle keyword arguments provided before semicolon | ||
kwexs = (kwexs1..., kwexs2...) |
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.
This logic handles the first case below, but can it be done nicely on all arguments to handle also the second case here?
julia> macro pr(exs...)
@show exs; nothing
end
@pr (macro with 1 method)
julia> @pr(; a=1, b) do x
x+a+b
end
exs = (:((x,)->begin
#= REPL[57]:2 =#
x + a + b
end), :($(Expr(:parameters, :($(Expr(:kw, :a, 1))), :b))))
julia> @pr(x -> x+a+b; a=1, b)
exs = (:($(Expr(:parameters, :($(Expr(:kw, :a, 1))), :b))), :(x->begin
#= REPL[58]:1 =#
x + a + b
end))
julia> @pr(x -> x+a+b, a=1, b=2)
exs = (:(x->begin
#= REPL[59]:1 =#
x + a + b
end), :(a = 1), :(b = 2))
Or might it be better to have the function call itself with arguments in re-arranged order?
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. I added
Fluxperimental.jl/src/compact.jl
Lines 125 to 126 in 9ab4b87
@warn "function stringifying does not yet handle all cases, using empty string" | |
"" |
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.
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.
Ok. So we merge this, and work on any further refinements in #17?
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, that sounds good.
9ab4b87
to
157dacd
Compare
157dacd
to
f84e34a
Compare
One question is whether this counts as a breaking change. This used to work, and the doc example did something similar, so probably it is?
|
Yes, it is a breaking change -- I indeed did have to change the linear activation example, which previously defined |
Can you edit Project.toml, and then we merge? |
This PR fixes issues with the keyword argument handling of
@compact
, noted in #12 (comment).CC @mcabbott @MilesCranmer
Attempts to break are most welcome 🙃 Note that this does not address all the points brought up in #12 (comment), only the ones related to keyword arguments.
(NB: there's some noise in the test file due to a7d0851, you may wish to exclude it from the diff when reviewing).