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

**URL:** <https://discourse.julialang.org/t/is-there-something-better-than-reshape-for-flattening-data-batches/31744>\
**Category:** Machine Learning\
**Created:** [December 2, 2019, 11:42am UTC](https://discourse.julialang.org/t/is-there-something-better-than-reshape-for-flattening-data-batches/31744 "2019-12-02T11:42:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [December 2, 2019, 11:42am UTC](https://discourse.julialang.org/t/is-there-something-better-than-reshape-for-flattening-data-batches/31744/1 "2019-12-02T11:42:45Z")

</div>

In the MINST example of [Flux Model Zoo](https://github.com/FluxML/model-zoo/blob/master/vision/mnist/conv.jl), the layer that “flattens” tensor data before the `Dense` layer uses `reshape`:

```julia
# 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.)

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 2, 2019, 11:46am UTC](https://discourse.julialang.org/t/is-there-something-better-than-reshape-for-flattening-data-batches/31744/2 "2019-12-02T11:46:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 2, 2019, 11:48am UTC](https://discourse.julialang.org/t/is-there-something-better-than-reshape-for-flattening-data-batches/31744/3 "2019-12-02T11:48:50Z")

</div>

```julia

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>

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [December 2, 2019, 12:16pm UTC](https://discourse.julialang.org/t/is-there-something-better-than-reshape-for-flattening-data-batches/31744/4 "2019-12-02T12:16:52Z")

</div>

Oh, I was wrong. Thanks!
