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

mapobs(f, DataLoader) should just work? #153

Open
mcabbott opened this issue Apr 26, 2023 · 1 comment · May be fixed by #181
Open

mapobs(f, DataLoader) should just work? #153

mcabbott opened this issue Apr 26, 2023 · 1 comment · May be fixed by #181

Comments

@mcabbott
Copy link
Contributor

I think the last case here should work like the 2nd-last, i.e. put the mapobs inside:

julia> using MLUtils: mapobs, DataLoader

julia> pr(x) = (@show(x); x);

julia> data = (a=[1 2 3; 4 5 6], b=[7,8,9]);

julia> mapobs(pr, data) |> collect
x = (a = [1, 4], b = 7)
x = (a = [2, 5], b = 8)
x = (a = [3, 6], b = 9)
3-element Vector{Any}:
 (a = [1, 4], b = 7)
 (a = [2, 5], b = 8)
 (a = [3, 6], b = 9)

julia> DataLoader(mapobs(pr, data); batchsize=2) |> collect
x = (a = [1 2; 4 5], b = [7, 8])
x = (a = [3; 6;;], b = [9])
2-element Vector{@NamedTuple{a::Matrix{Int64}, b::Vector{Int64}}}:
 (a = [1 2; 4 5], b = [7, 8])
 (a = [3; 6;;], b = [9])

julia> mapobs(pr, DataLoader(data; batchsize=2))
mapobs(pr, DataLoader(::@NamedTuple{a::Matrix{Int64}, b::Vector{Int64}}, batchsize=2); batched=:auto)

julia> collect(ans)
ERROR: MethodError: no method matching getindex(::DataLoader{@NamedTuple{a::Matrix{Int64}, b::Vector{Int64}}, Random._GLOBAL_RNG, Val{nothing}}, ::Int64)
Stacktrace:
  [1] getobs(::Type{SimpleTraits.Not{MLUtils.IsTable{DataLoader{@NamedTuple{a::Matrix{Int64}, b::Vector{Int64}}, Random._GLOBAL_RNG, Val{nothing}}}}}, data::DataLoader{@NamedTuple{a::Matrix{Int64}, b::Vector{Int64}}, Random._GLOBAL_RNG, Val{nothing}}, idx::Int64)
    @ MLUtils ~/.julia/dev/MLUtils/src/observation.jl:110

Are there any downsides to defining something like this? Or should it be done at some other level?

mapobs(f, d::DataLoader) = DataLoader(
    mapobs(f, d.data),
    d.batchsize,
    d.buffer,
    d.partial,
    d.shuffle,
    d.parallel,
    d.collate,
    d.rng,
)
@CarloLucibello
Copy link
Member

CarloLucibello commented Jan 24, 2025

Are there any downsides to defining something like this? Or should it be done at some other level?

I think that the straightforward interpretation of mapobs(f, dataloader) is that f is applied to the mini-batches from the dataloader, while in DataLoader(mapobs(f, d.data)) it is applied to single observations.

We can specialize mapobs(f, d::DataLoader) to return a DataLoader with collate function f ∘ batch though.

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 a pull request may close this issue.

2 participants