-
-
Notifications
You must be signed in to change notification settings - Fork 611
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
update Embedding layer #1656
Open
manikyabard
wants to merge
9
commits into
FluxML:master
Choose a base branch
from
manikyabard:cl/embed
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
update Embedding layer #1656
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cb3a4ca
Embedding special case for outputsize
manikyabard eb489c3
Apply suggestions from code review
manikyabard f702260
update Embedding constructor
manikyabard 5ff8280
updated Embedding docstring
manikyabard 73d7281
updated and exported Embedding
manikyabard 6e1e66d
updated Embedding tests
manikyabard 2d80696
add outputsize special case for NNlib.gather
manikyabard a2f0961
Update src/layers/basic.jl
manikyabard ef13026
updated tests and outputsize gather
manikyabard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -483,7 +483,8 @@ function Base.show(io::IO, m::Parallel) | |
end | ||
|
||
""" | ||
Embedding(in => out; init=randn) | ||
Embedding(in => out; init=randn32) | ||
Embedding(weight::AbstractMatrix) | ||
|
||
A lookup table that stores embeddings of dimension `out` | ||
for a vocabulary of size `in`. | ||
|
@@ -493,41 +494,39 @@ The input to the layer can be either a vector of indexes | |
or the corresponding [onehot encoding](@ref Flux.OneHotArray). | ||
|
||
# Examples | ||
```jldoctest | ||
julia> vocab_size, embed_size = 1000, 4; | ||
|
||
julia> model = Flux.Embedding(vocab_size => embed_size) | ||
Embedding(1000 => 4) # 4_000 parameters | ||
|
||
julia> vocab_idxs = [1, 722, 53, 220, 3]; | ||
```jldoctest | ||
julia> m = Embedding(reshape(-6:45, 2, 26) .+ 0.01f0) | ||
Embedding(26 => 2) | ||
|
||
julia> x = Flux.OneHotMatrix(vocab_idxs, vocab_size); summary(x) | ||
"1000×5 OneHotMatrix(::Vector{Int64}) with eltype Bool" | ||
julia> m(5) # embedding vector for 5th element | ||
2-element Vector{Float32}: | ||
2.01 | ||
3.01 | ||
|
||
julia> model(x) |> summary | ||
"4×5 Matrix{Float32}" | ||
julia> m([6, 15, 15]) # applied to a batch | ||
2×3 Matrix{Float32}: | ||
4.01 22.01 22.01 | ||
5.01 23.01 23.01 | ||
|
||
julia> model(vocab_idxs) == model(x) | ||
julia> ans == m(Flux.onehotbatch("foo", 'a':'z')) | ||
true | ||
``` | ||
""" | ||
struct Embedding{W} | ||
struct Embedding{W <: AbstractMatrix} | ||
weight::W | ||
end | ||
|
||
@functor Embedding | ||
|
||
Embedding((in, out)::Pair{<:Integer, <:Integer}; init = randn32) = Embedding(init(out, in)) | ||
Embedding(dims::Pair{<:Integer, <:Integer}; init = randn32) = Embedding(init(last(dims), first(dims))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the old constructor should be deprecated |
||
|
||
(m::Embedding)(x::Integer) = m.weight[:, x] | ||
(m::Embedding)(x::AbstractVector) = NNlib.gather(m.weight, x) | ||
(m::Embedding)(x::AbstractArray) = reshape(m(vec(x)), :, size(x)...) | ||
mcabbott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function (m::Embedding)(x::Union{OneHotVector{T,L}, OneHotMatrix{T,L}}) where {T,L} | ||
size(m.weight, 2) == L || throw(DimensionMismatch("Matrix column must correspond with OneHot size: $(size(m.weight, 2)) != $L")) | ||
return m(onecold(x)) | ||
end | ||
(m::Embedding)(x::AbstractArray{Bool}) = reshape(m(reshape(x, size(x, 1), :)), :, size(x)[2:end]...) | ||
(m::Embedding)(x::AbstractVecOrMat{Bool}) = m.weight * x # handles OneHotLikeVector, OneHotLikeMatrix | ||
|
||
function Base.show(io::IO, m::Embedding) | ||
print(io, "Embedding(", size(m.weight, 2), " => ", size(m.weight, 1), ")") | ||
print(io, "Embedding($(size(m.weight, 2)) => $(size(m.weight, 1)))") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 old example was much clearer.
This constructor (
Embed(weight)
) is not even part of the docstring, we should add itThere 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.
Indeed on the constructor.
The virtue of this example is that it doesn't have random numbers, so it can be a doctest. My hope is that
onehotbatch("foo", 'a':'z')
might connect with26
well enough to be easy to follow. Maybe it can be made clearer somehow?