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

outdims: possible enhancements #1253

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions src/layers/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ outdims(c::Chain, isize) = foldr(outdims, reverse(c.layers), init = isize)
# see issue https://github.com/FluxML/Flux.jl/issues/702
# Johnny Chen -- @johnnychen94
# only slightly changed to better handle interaction with Zygote @dsweber2

# fallback
outdims(f::Function, isize::Tuple) = size(f(ones(Float32, isize..., 1)))[1:end-1] # since we aren't care about batch dimension, we are free to just set it to 1
Copy link
Member

Choose a reason for hiding this comment

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

why not leave f without type restrictions?


"""
activations(c::Chain, input)

Expand Down Expand Up @@ -144,13 +148,13 @@ Calculate the output dimensions given the input dimensions, `isize`.

```julia
m = Dense(10, 5)
outdims(m, (5, 2)) == (5,)
outdims(m, (10,)) == (5,)
outdims(m, (10, 2)) == (5, 2)
```
"""
function outdims(l::Dense, isize)
first(isize) == size(l.W, 2) || throw(DimensionMismatch("input size should equal to ($(size(l.W, 2)),), got $isize"))
return (size(l.W, 1),)
first(isize) == size(l.W, 2) || throw(DimensionMismatch("input size should equal to ($(size(l.W, 2)), ...), got $isize"))
return (size(l.W, 1), Base.tail(isize)...)
end

"""
Expand Down
1 change: 1 addition & 0 deletions test/layers/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import Flux: activations
m = Dense(10, 5)
@test_throws DimensionMismatch Flux.outdims(m, (5, 2)) == (5,)
@test Flux.outdims(m, (10,)) == (5,)
@test Flux.outdims(m, (10, 2)) == (5, 2)

m = Chain(Dense(10, 8, σ), Dense(8, 5), Dense(5, 2))
@test Flux.outdims(m, (10,)) == (2,)
Expand Down