What is the best way to concat an array of tensors

Assuming I have an array of tensors, I would like to concat them along their fourth dimension such as

xs = [rand(28, 28, 1, 1) for _ in 1:60000]
cat(xs..., dims=4) |> size

It will take up to 40GB memory and will not stop within 10mins (I have to end it), so I am wondering what’s the correct way to achieve this?

You can do reduce((xs,x)->cat(xs,x; dims=4), xs)
But preallocating an arrays and assigning it with a loop would be far more efficient.

julia> xs = [rand(28, 28, 1, 1) for _ in 1:60000];

julia> X = zeros(28, 28, 1, 60000);

julia> for i in 1:length(xs)
           X[:, :, 1:1, i:i] .= xs[i]
       end

Thanks. It seems reduce((xs,x)->cat(xs,x; dims=4), xs) is still very slow. Maybe I should adopt preallocating and assigning.

reduce(hcat, ... and reduce(vcat, ... have special efficient methods, but this does not exist for higher dimensions.

1 Like