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

Pretty printing for DataLoader #122

Merged
merged 2 commits into from
Oct 2, 2022
Merged

Pretty printing for DataLoader #122

merged 2 commits into from
Oct 2, 2022

Conversation

mcabbott
Copy link
Contributor

@mcabbott mcabbott commented Sep 23, 2022

Before:

julia> array_loader
DataLoader{Matrix{Float64}, Random._GLOBAL_RNG}([0.4162601998517451 0.6319257141697698  
0.3534065570971672 0.2042920171820276; 0.6498054094075275 0.5207088722535103  
0.6810047653022318 0.15479239537021516;  ; 0.43750205789271324 0.9278765112318127  
0.4973122624864813 0.4984282396624422; 0.8271657274672851 0.4403513166790436  
0.9147954294101035 0.08763411914124453], 2, 100, true, false, Random._GLOBAL_RNG())

julia> train_loader
DataLoader{NamedTuple{(:data, :label), Tuple{Matrix{Float64}, Vector{Char}}}, 
Random._GLOBAL_RNG}((data = [0.4162601998517451 0.6319257141697698  
0.3534065570971672 0.2042920171820276; 0.6498054094075275 0.5207088722535103  
0.6810047653022318 0.15479239537021516;  ; 0.43750205789271324 0.9278765112318127  
0.4973122624864813 0.4984282396624422; 0.8271657274672851 0.4403513166790436  
0.9147954294101035 0.08763411914124453], label = ['q', 'l', 'p', 'g', 'h', 'i', 'f', 'z', 'b', 'q'    'y', 'q', 'r', 
'j', 'q', 'q', 'u', 'c', 'p', 'p']), 5, 100, true, true, Random._GLOBAL_RNG())

After:

julia> array_loader
50-element DataLoader(::Matrix{Float64}, batchsize=2)
  starting with:
  10×2 Matrix{Float64}

julia> train_loader
20-element DataLoader(::NamedTuple{(:data, :label), Tuple{Matrix{Float64}, Vector{Char}}}, 
shuffle=true, batchsize=5)
  starting with:
  (; data = 10×5 Matrix{Float64}, label = 5-element Vector{Char})

This prints a summary, because it seems useful to see the types & sizes. To see any actual data, you would have to call first(ans) or something.

Thoughts?

@codecov-commenter
Copy link

codecov-commenter commented Sep 23, 2022

Codecov Report

Merging #122 (9043acc) into main (a73692e) will increase coverage by 0.36%.
The diff coverage is 96.15%.

@@            Coverage Diff             @@
##             main     #122      +/-   ##
==========================================
+ Coverage   88.36%   88.72%   +0.36%     
==========================================
  Files          13       13              
  Lines         533      559      +26     
==========================================
+ Hits          471      496      +25     
- Misses         62       63       +1     
Impacted Files Coverage Δ
src/eachobs.jl 87.20% <96.15%> (+3.87%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

if Base.haslength(e)
print(io, length(e), "-element ")
else
print(io, "Unknown-length ")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure this can happen, can DataLoader be used with iterators which don't support indexing? Are there any which allow indexing but don't have a length?

julia> DataLoader(1:10)
10-element DataLoader(::UnitRange{Int64})
  with first element:
  1-element UnitRange{Int64}

julia> DataLoader(x for x in 1:10)
10-element DataLoader(::Base.Generator{UnitRange{Int64}, typeof(identity)})
  with first element:Error showing value of type DataLoader{Base.Generator{UnitRange{Int64}, typeof(identity)}, Random._GLOBAL_RNG, Val{nothing}}:
ERROR: MethodError: no method matching getindex(::Base.Generator{UnitRange{Int64}, typeof(identity)}, ::UnitRange{Int64})
Stacktrace:
  [1] getobs(data::Base.Generator{UnitRange{Int64}, typeof(identity)}, idx::UnitRange{Int64})
    @ MLUtils ~/.julia/dev/MLUtils/src/observation.jl:49

Copy link
Member

Choose a reason for hiding this comment

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

I think DataLoader expects random access indexing (for shuffling, batching, etc.). Even an infinite data container must support this through getobs (falls back to getindex) despite being slow.

Copy link
Contributor

Choose a reason for hiding this comment

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

Random access indexing is required as Kyle said

Comment on lines +259 to +262
# Base uses this function for composable array printing, e.g. adjoint(view(::Matrix)))
function Base.showarg(io::IO, e::DataLoader, toplevel)
print(io, "DataLoader(")
Base.showarg(io, e.data, false)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The intention with overloading this (not just show) is that things like CuIterator(DataLoader(adjoint(::Matrix{... could then work without packages knowing about each other.

Copy link
Contributor Author

@mcabbott mcabbott Sep 23, 2022

Choose a reason for hiding this comment

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

Would be nice if that could also apply to something like Iterators.flatten(Iterators.repeated(d, n)), or Iterators.take(Iterators.cycle(d), n * length(d))... but these seem obscure & have very complicated types.

Maybe we should make repeat(d::DataLoader, epochs::Int) just work?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, but I thought there would be a default implementation for repeat that calls the iterator repeatedly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe there should be. I thought it was only arrays, but turns out to repeat strings too:

julia> methods(repeat)
# 6 methods for generic function "repeat" from Base:
 [1] repeat(A::AbstractArray; inner, outer)
     @ abstractarraymath.jl:392
 [2] repeat(A::AbstractArray, counts...)
     @ abstractarraymath.jl:355
 [3] repeat(c::Char, r::Integer)
     @ strings/string.jl:349
 [4] repeat(c::AbstractChar, r::Integer)
     @ strings/string.jl:348
 [5] repeat(s::Union{SubString{String}, String}, r::Integer)
     @ strings/substring.jl:251
 [6] repeat(s::AbstractString, r::Integer)
     @ strings/basic.jl:715

I guess it's eager for all of those, so perhaps unclear whether repeat((x for x in 1:3), 3) should make an iterator or collect.

Comment on lines +291 to +294
function _expanded_summary(xs::NamedTuple)
parts = ["$k = "*_expanded_summary(x) for (k,x) in zip(keys(xs), xs)]
"(; " * join(parts, ", ") * ")"
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is perhaps slightly odd, but the idea is to show what's inside something like this:

julia> nt = (x=rand(10,100), y=rand(Bool,10,100));

julia> summary(nt)  # no sizes
"NamedTuple{(:x, :y), Tuple{Matrix{Float64}, Matrix{Bool}}}"

julia> repr(nt)  # no sizes, too long
"(x = [0.4301236060985135 0.30528931378945046 0.9865249486891879 0.880700604424396 0.0411513866531249 0.5940861560957025 0.8857114440668031 0.07167460028948913 0.011754567396461968 0.1843492781834919 0.6304545382381233 0.4430950147392062 0.8014564359131793 0.8161412755930899 0.47800508950976983 0.11072415162810345 0.6459516433095668 0.6872"  20611 bytes  " 1 1 0 1 0 0 0 1 1 0 0 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 1 1 0 1 0 0 1 0 1 0 1 0 0 1 0 1 1 0 0 1 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 0 0 1 1 1 1; 1 1 0 0 0 0 1 1 1 0 1 0 1 0 0 1 1 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 1 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1 1 1 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0 0 1 1 1 0 1 1 0 0 1 1 0 1 1 1 0 1 1 1])"

julia> MLUtils._expanded_summary(nt)
"(; x = 10×100 Matrix{Float64}, y = 10×100 Matrix{Bool})"

Maybe ideally Base.summary would do something more useful here.

@lorenzoh
Copy link
Contributor

This looks good to me 👍

CI failure on Nightly seems to be this unrelated error: WARNING: both MLUtils and Base export "stack"; uses of it in module Main must be qualified

@mcabbott
Copy link
Contributor Author

Stack is #119 BTW.

@mcabbott mcabbott requested a review from lorenzoh October 1, 2022 02:51
if Base.haslength(e)
print(io, length(e), "-element ")
else
print(io, "Unknown-length ")
Copy link
Contributor

Choose a reason for hiding this comment

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

Random access indexing is required as Kyle said

Comment on lines +259 to +262
# Base uses this function for composable array printing, e.g. adjoint(view(::Matrix)))
function Base.showarg(io::IO, e::DataLoader, toplevel)
print(io, "DataLoader(")
Base.showarg(io, e.data, false)
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, but I thought there would be a default implementation for repeat that calls the iterator repeatedly?

@mcabbott mcabbott merged commit e247fb5 into main Oct 2, 2022
@mcabbott mcabbott deleted the show_dataloader branch October 2, 2022 02:49
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 this pull request may close these issues.

None yet

4 participants