# Batch/batch in Flux?

**URL:** <https://discourse.julialang.org/t/batch-batch-in-flux/41735>\
**Category:** Machine Learning\
**Tags:** flux\
**Created:** [June 19, 2020, 4:47pm UTC](https://discourse.julialang.org/t/batch-batch-in-flux/41735 "2020-06-19T16:47:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mefob](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@mefob](https://discourse.julialang.org/u/mefob)\
**Post date:** [June 19, 2020, 4:47pm UTC](https://discourse.julialang.org/t/batch-batch-in-flux/41735/1 "2020-06-19T16:47:20Z")

</div>

I am building my own ODE layer and would like to make sure it works with batches as well as with individual vectors.  
The input and output to my layer are a NxM 2D arrays, so I will also need to reshape to couple e.g. to a Dense layer.

I am not sure how to make this work for batches.

The v0.1 Flux [documentation](https://fluxml.ai/Flux.jl/v0.1/apis/batching.html) (strangely, this is what a search for “Flux julia batch” returns first) hints at a `Batch` interface with a very useful `Flux.Batch` struct:

```julia
julia> xs = Batch([[1,2,3], [4,5,6]])
2-element Batch of Vector{Int64}:
 [1,2,3]
 [4,5,6]

```

I did not find this in the latest source, but utils.jl has a function batch that just batches into an array along the leftmost index.

```julia
julia> xs = Flux.batch([[1,2,3], [4,5,6]])
3×2 Array{Int64,2}:
 1 4
 2 5
 3 6

```

This function just returns an `Array{Int64,2}` so my layer can’t identify this as a batch.

- Has `Batch` been replaced by `batch`? Is `batch` the correct way to do batching?

- How can I write a generic layer that works with and without batches?

- How can I make sure that reshaping operations work transparently, i.e. if I couple a Dense layer to a MyLayer that accepts a 10x10 2D array as an input:  
`m=Chain(Dense(100,100), x->reshape(x, (10,10)), MyLayer((10,10)))`?  
`m` should work both on an Array with 100 points and on a batch of such vectors.

---

<div class="post-metadata">

**Author:** ![avikpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avikpal/32/6550_2.png) [@avikpal](https://discourse.julialang.org/u/avikpal)\
**Post date:** [June 26, 2020, 3:26pm UTC](https://discourse.julialang.org/t/batch-batch-in-flux/41735/2 "2020-06-26T15:26:43Z")

</div>

> How can I write a generic layer that works with and without batches?

The simplest solution is to just the data without a batch to have a batch size of 1.

> How can I make sure that reshaping operations work transparently, i.e. if I couple a Dense layer to a MyLayer that accepts a 10x10 2D array as an input:  
> `m=Chain(Dense(100,100), x->reshape(x, (10,10)), MyLayer((10,10)))` ?  
> `m` should work both on an Array with 100 points and on a batch of such vectors.

If you treat the “unbatched” data to have batch size of 1 then `m=Chain(Dense(100,100), x->reshape(x, (10,10, :)), MyLayer((10,10)))` should just work (assuming MyLayer works for batched data.

---

<div class="post-metadata">

**Author:** ![mefob](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@mefob](https://discourse.julialang.org/u/mefob)\
**Post date:** [June 29, 2020, 2:04pm UTC](https://discourse.julialang.org/t/batch-batch-in-flux/41735/3 "2020-06-29T14:04:09Z")

</div>

Thank you! I will change the code to work with batch size one.
