julia> add(a, b) = a+b
add (generic function with 1 method)
julia> reduce(add, rand(5,5), dims = (1, 2))
ERROR: MethodError: no method matching reducedim_init(::typeof(identity), ::typeof(add), ::Array{Float64,2}, ::Tuple{Int64,Int64})
It seems like Julia should just do something sensible and generic here, like map
does already – is there a specific reason not too, or is it just unimplemented?
What are you expecting the generic reducedim
to return? A n
-dimensional array, with reduce
operation on each of the dims
?
Yes, it should behave identically to how it does with +
:
julia> reduce(+, rand(5,5), dims = (1, 2))
1×1 Array{Float64,2}:
12.549021410347184
FWIW I just came across this as well, trying to "flatten" a 2D array of 2D arrays into a single larger 2D array.
As = [rand(5, 4) for _ in 1:4, _ in 1:5]
reduce(hcat, reduce(vcat, As, dims=1))
Bump, we were hit by this.
I think I'm being hit by this, https://github.com/FluxML/Zygote.jl/issues/594
A "sensible and generic" default might be to pass the first two elements along a dimension to the operator and then iterate over the remaining items. But what do you do if a dimension has size 1?
Conversely, adding the suggested reducedim_init
method makes it easy to support dimensions of size 1. So my question is, how big of a problem is this really?
Most helpful comment
Yes, it should behave identically to how it does with
+
: