Is there something better than `reshape` for flattening data batches?

In the MINST example of Flux Model Zoo, the layer that “flattens” tensor data before the Dense layer uses reshape:

# Reshape 3d tensor into a 2d one, at this point it should be (3, 3, 32, N)
# which is where we get the 288 in the `Dense` layer below:
x -> reshape(x, :, size(x, 4))

Is this the recommended way of flattening data batches in cases like this?
Or are there more efficient ways of doing it? (reshape makes a copy of the data, which shouldn’t be necessary, I think.)

I’m pretty sure reshape is lazy in this case. Have you tried to verify it?

2 Likes

julia> b =reshape(a, :, size(a, 4))
8×2 Array{Float64,2}:
 -0.525545   0.429908
  0.779611   0.705216
 -0.689437   0.0846826
 -0.466572  -0.478073
  2.31005   -1.03075
 -0.372342   2.76499
 -0.299936   0.230115
  1.29893    0.112308

julia> b .= 0
8×2 Array{Float64,2}:
 0.0  0.0
 0.0  0.0
 0.0  0.0
 0.0  0.0
 0.0  0.0
 0.0  0.0
 0.0  0.0
 0.0  0.0

julia> a
2×2×2×2 Array{Float64,4}:
[:, :, 1, 1] =
 0.0  0.0
 0.0  0.0

[:, :, 2, 1] =
 0.0  0.0
 0.0  0.0

[:, :, 1, 2] =
 0.0  0.0
 0.0  0.0

[:, :, 2, 2] =
 0.0  0.0
 0.0  0.0

julia>

Oh, I was wrong. Thanks!