I have an array of tuples of weight and bias gradients for my neural network (so, an array of matrix-vector tuples). How can I sum over them, ideally without an explicit loop?
As a small example, let
arr = [(rand(2,2), rand(2)), (rand(2,2), rand(2))]
I tried using sum(arr)
, but that didn’t work since addition is not defined for tuples. I tried broadcasting with sum.(arr)
, but that gave a dimension mismatch error.
Currently, I’m using sum(first.(arr)), sum(last.(arr))
, but this seems inefficient and isn’t extensible to larger tuples. I don’t have Julia 1.6 installed, but based on the NEWS.md it seems like I might be able to use something like reduce(.+, arr)
. Is there an better solution that works for v1.5?