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

Reduce hcat creates dense matrix #32

Open
simonmandlik opened this issue Jan 12, 2023 · 1 comment
Open

Reduce hcat creates dense matrix #32

simonmandlik opened this issue Jan 12, 2023 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@simonmandlik
Copy link

See here: FluxML/Flux.jl#1596

@mcabbott
Copy link
Member

Existing methods are here, a specialisation for reduce(hcat, ... could be added:

function Base.cat(x::OneHotLike{<:Any, <:Any, N}, xs::OneHotLike...; dims::Int) where N
if isone(dims) || _notall_onehot(x, xs...)
return cat(map(x -> convert(_onehot_bool_type(x), x), (x, xs...))...; dims = dims)
else
L = _nlabels(x, xs...)
return OneHotArray(cat(_indices(x), _indices.(xs)...; dims = dims - 1), L)
end
end
Base.hcat(x::OneHotLike, xs::OneHotLike...) = cat(x, xs...; dims = 2)
Base.vcat(x::OneHotLike, xs::OneHotLike...) =
vcat(map(x -> convert(_onehot_bool_type(x), x), (x, xs...))...)
# optimized concatenation for matrices and vectors of same parameters
Base.hcat(x::OneHotMatrix, xs::OneHotMatrix...) =
OneHotMatrix(reduce(vcat, _indices.(xs); init = _indices(x)), _nlabels(x, xs...))
Base.hcat(x::OneHotVector, xs::OneHotVector...) =
OneHotMatrix(reduce(vcat, _indices.(xs); init = _indices(x)), _nlabels(x, xs...))

I suspect these methods could use some stress-testing & tidying, e.g. cat(ohm, ohm; dims=Val(2)) looks like it will be an error (instead of falling back).

@mcabbott mcabbott added the help wanted Extra attention is needed label Jan 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants