How to sum array of tuples

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 it seems like I might be able to use something like reduce(.+, arr). Is there an better solution that works for v1.5?

reduce((x, y) -> x .+ y, arr)

Does this work for you? (untested) .|> sum

Thanks, this seems to do the job quite nicely! Am I correct that the lambda can just be replaced with a .+ in version 1.6?

I believe so, yes.


This works, but I decided not to use it because it is less memory-efficient.