# Flux: Feed minibatch into Neural Network

**URL:** <https://discourse.julialang.org/t/flux-feed-minibatch-into-neural-network/32072>\
**Category:** New to Julia\
**Tags:** flux\
**Created:** [December 9, 2019, 9:52pm UTC](https://discourse.julialang.org/t/flux-feed-minibatch-into-neural-network/32072 "2019-12-09T21:52:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![patdax](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@patdax](https://discourse.julialang.org/u/patdax)\
**Post date:** [December 9, 2019, 9:52pm UTC](https://discourse.julialang.org/t/flux-feed-minibatch-into-neural-network/32072/1 "2019-12-09T21:52:00Z")

</div>

I built the following model:

```julia
function my_model(n)
    conv_1 = Conv((8,8), 3 => 32, stride=(4,4), relu)
    conv_2 = Conv((4,4), 32 =>64, stride=(2,2), relu)
    conv_3 = Conv((3,3), 64 =>64, stride=(1,1), relu)
    model = Chain(
    x -> x / 255,
    conv_1,
    conv_2,
    conv_3,
    x -> reshape(x, (:, 1)),
    Dense(2304, 512, relu),
    Dense(512, n),
    )
    return model
end

```

and tested it in the following way, where the data is stored in (width, height, # channels, # batches) order:

```julia
model = my_model(6)
test_input = rand(UInt8, (80, 80, 3, 1))
test_batch = rand(UInt8, (80, 80, 3, 32))
model(test_input)
model(test_batch)

```

When testing the model with test\_batch I get a dimension mismatch error. (DimensionMismatch(“A has dimensions (512,2304) but B has dimensions (73728,1)”)). It seems as if the reshape command does not work for the batch in this way. Actually, I thought I can feed my network a single input as well as a batch when storing the data in WHCN order. Could anybody help me with this?

---

<div class="post-metadata">

**Author:** ![maetshju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maetshju/32/4013_2.png) [@maetshju](https://discourse.julialang.org/u/maetshju)\
**Post date:** [December 10, 2019, 10:45pm UTC](https://discourse.julialang.org/t/flux-feed-minibatch-into-neural-network/32072/2 "2019-12-10T22:45:40Z")

</div>

Dense layers in Flux expect that batches will be ordered as columns, but your model reshapes every tensor to an n×1 matrix. The dense layer after your call to `reshape` is expecting a 2304×m matrix, though.

You luck out with the dimensions matching for `test_input`, where you’re giving a 2304×1 matrix to your dense layer. However, the dimensions don’t match with `test_batch`, where you’re giving a 73728×1 matrix to your dense layer. If you instead use `x -> reshape(x, (:, size(x, 4)))`, you’ll get a matrix with all of your batches along the columns. Your `test_batch` variable, for example, will get reshaped to a 2304×32 matrix, which the dense layer can accept.
