mcabbott / lazystack.jl Goto Github PK
View Code? Open in Web Editor NEW🥞
License: MIT License
🥞
License: MIT License
This is currently much slower than LazyArrays.Hcat
, surely it can be fixed:
Lines 24 to 29 in 19889ac
Now that JuliaLang/julia#43334 is merged, and available on Compat.jl, this package needs to decide what to do.
stack
should not be exported. Should it be LazyStack.stack
, or export lazystack
?Edit: Mostly done in #14 now. The remaining piece is:
dims
which the lazy struct does not. Perhaps it should learn, in order to have exactly the same rules?It seems version 0.1.2 (which would include #16) is not released yet (https://github.com/JuliaRegistries/General/blob/master/L/LazyStack/Versions.toml) even though the Project.toml file was updated. Could you tag a new release?
@JuliaRegistrator register()
This issue is used to trigger TagBot; feel free to unsubscribe.
If you haven't already, you should update your TagBot.yml
to include issue comment triggers.
Please see this post on Discourse for instructions and more details.
If you'd like for me to do this for you, comment TagBot fix
on this issue.
I'll open a PR within a few hours, please be patient!
From https://discourse.julialang.org/t/efficient-way-of-turning-iterator-into-a-matrix/38309:
julia> nums = (i^2 for i in Iterators.filter(_->rand(Bool), 1:100));
julia> Base.haslength(nums)
false
julia> stack(nums)'
1×44 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:
1 4 9 49 81 144 196 324 361 400 484 529 … 6241 6561 7396 7744 7921 8649 10000
julia> vecs = ([i,i^2] for i in Iterators.filter(_->rand(Bool), 1:100));
julia> stack(vecs)
2×54 Array{Int64,2}:
2 7 9 10 11 13 14 16 17 18 20 … 90 92 94 95 96 98 100
4 49 81 100 121 169 196 256 289 324 400 8100 8464 8836 9025 9216 9604 10000
julia> tups = ((i,i^2) for i in Iterators.filter(_->rand(Bool), 1:100));
julia> stack(tups)
ERROR: MethodError: no method matching vec(::Tuple{Int64,Int64})
julia> stack((i,i^2) for i in 1:100) # with a length, no problem
2×100 Array{Int64,2}:
1 2 3 4 5 6 7 8 9 10 11 12 … 94 95 96 97 98 99 100
1 4 9 16 25 36 49 64 81 100 121 144 8836 9025 9216 9409 9604 9801 10000
Perhaps the first of these should be an error:
julia> stack(OffsetArray(fill(i,5), rand(-2:2)) for i in 1:10)
5×10 OffsetArray(::Array{Int64,2}, 0:4, 1:10) with eltype Int64 with indices 0:4×1:10:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
julia> rstack(OffsetArray(fill(i,5), rand(-2:2)) for i in 1:10)
9×10 OffsetArray(::Array{Int64,2}, -1:7, 1:10) with eltype Int64 with indices -1:7×1:10:
1 0 0 4 0 6 7 8 0 0
1 0 0 4 5 6 7 8 0 10
1 0 0 4 5 6 7 8 0 10
1 2 0 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
0 2 3 0 5 0 0 0 9 10
0 2 3 0 0 0 0 0 9 0
0 2 3 0 0 0 0 0 9 0
0 0 3 0 0 0 0 0 0 0
Despite claiming to work with Zygote, generators currently don’t work:
julia> using Zygote, LazyStack
julia> gradient(x -> sum(stack([x,x'])), rand(2,2)) # vector of arrays
([2.0 2.0; 2.0 2.0],)
julia> gradient(x -> sum(stack(x,x')), rand(2,2)) # tuple of arrays
([2.0 2.0; 2.0 2.0],)
julia> gradient(x -> sum(stack(x' for x in [x,x])), rand(2,2)) # generator
ERROR: not yet!
In trying to solve FluxML/Zygote.jl#399,
using Zygote, SliceMap
gradient(x -> sum(hcat(x...) * sum(x)), [rand(2), rand(2)]) # errors, as in linked issue
gradient(x -> sum(reduce(hcat, x) * sum(x)), [rand(2), rand(2)]) # ok!
using LazyStack
gradient(x -> sum(stack(x) * sum(x)), [rand(2), rand(2)]) # BoundsError: attempt to access (1,) at index [2]
gradient(x -> sum(stack(x...) * sum(x)), [rand(2), rand(2)])
gradient(x -> sum(collect(stack(x)) * sum(x)), [rand(2), rand(2)]) # MethodError: no method matching view(::NamedTuple{(:slices,),Tuple{Array{SubArray
gradient(x -> sum(collect(stack(x...)) * sum(x)), [rand(2), rand(2)]) # Mutating arrays is not supported
Simpler:
rr = rand(2,2); vv = rand(2);
gradient(x -> sum(rr * stack(x)), [rand(2), rand(2)])[1][1] # ok
gradient(x -> sum(rr * stack(x...)), [rand(2), rand(2)])[1][1] # ok
gradient(x -> sum(stack(x) * rr), [rand(2), rand(2)])[1][1] # ok
gradient(x -> sum(stack(x) * vv), [rand(2), rand(2)])[1][1] # BoundsError: attempt to access (1,) at index [2]
gradient(x -> sum(stack(x...) * vv), [rand(2), rand(2)])[1][1] # error!
Even simpler:
xx = [rand(2), rand(2)];
stack(xx) * vv # BoundsError: attempt to access (1,) at index [2]
stack(xx)[1] # same
This is a bug, perhaps a new one:
julia> using LazyStack
julia> lazystack([1,2,3])
3×1 lazystack(::Tuple{Vector{Int64}}) with eltype Int64:
1
2
3
julia> Base.stack([1,2,3]) # correctly regards numbers as zero-dim
3-element Vector{Int64}:
1
2
3
It thinks you are doing this, for which Base requires another pair of brackets:
julia> lazystack([1,2,3], [4,5,6]) # too clever by half
3×2 lazystack(::Tuple{Vector{Int64}, Vector{Int64}}) with eltype Int64:
1 4
2 5
3 6
julia> Base.stack(([1,2,3], [4,5,6]))
3×2 Matrix{Int64}:
1 4
2 5
3 6
julia> Base.stack(([1,2,3],)) # also unambiguous
3×1 Matrix{Int64}:
1
2
3
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.