Existing machine learning frameworks and libraries represent batching, and other properties of data, only implicitly. Your machine learning data is a large
Typically, this might represent that you have (say) a batch of 100 samples, where each sample is a 50-long sequence of 256×256 images. This is great for performance, but array operations often become much more cumbersome as a result. Especially if you manipulate dimensions at runtime as an optimisation, debugging models can become extremely fiddly, with a proliferation of
<code>X × Y × Z</code>
arrays and no information about where they came from.
</p>
<p>
Flux introduces a new approach where the batch dimension is represented explicitly as part of the data. For example:
object into a model, the model is ultimately working with a single array, which means there's no performance overhead and we get the full benefit of standard batching.
</p>
<p>
Turning a set of vectors into a matrix is fairly easy anyway, so what's the big deal? Well, it gets more interesting as we start working with more complex data. Say we were working with 4×4 images:
The design of batching is still a fairly early work in progress, though it's used in a few places in the system. For example, all Flux models expect to be given
<code>Batch</code>
objects which are unwrapped into raw arrays for the computation. Models will convert their arguments if necessary, so it's convenient to call a model with a single data point like
<code>f([1,2,3])</code>
.
</p>
<p>
Right now, the
<code>Batch</code>
or
<code>Seq</code>
types always stack along the left-most dimension. In future, this will be customisable, and Flux will provide implementations of common functions that are generic across the batch dimension. This brings the following benefits:
</p>
<ul>
<li>
<p>
Code can be written in a batch-agnostic way or be generic across batching strategies.
</p>
</li>
<li>
<p>
Batching and optimisations, like switching batch dimensions, can be expressed by the programmer with compiler support; fewer code changes are required and optimisations are guaranteed not to break the model.
</p>
</li>
<li>
<p>
This also opens the door for more automatic optimisations, e.g. having the compiler explore the search base of possible batching combinations.
</p>
</li>
</ul>
<p>
Here's a more detailed illustration of how it might look for code to be "generic across batching". Take for example a weight matrix
<code>W</code>
times a vector
<code>x</code>
, as used in a logistic regression or a simple neural network:
</p>
<pre><codeclass="language-julia"> W * x => y
(10×28) * (28) => (10)</code></pre>
<p>
If we want to work with a batch of 50
<code>x</code>
s, one option is to stack the data into a matrix of size
<code>28 × 50</code>
.
</p>
<pre><codeclass="language-julia"> W * x => y
(10×28) * (28×50) => (10×50)</code></pre>
<p>
This works, but we may find that it's slow or doesn't fit well with the rest of the model, which batches on the first dimension. For that reason we may instead want to put the data in a
<code>50 × 28</code>
matrix and alter the code as follows:
</p>
<pre><codeclass="language-julia"> x * W' => y
(50×28) * (28×10) => (50×10)</code></pre>
<p>
to make the shapes work out. This code change is not ideal; in more complex cases it can become fiddly and error-prone, and it means that the code is less reusable, tied to a particular implementation strategy.
</p>
<p>
There's an alternative. We keep the same code, but represent the batched
<code>x</code>
s as either a
<code>Batch{Vector,1}</code>
or a
<code>Batch{Vector,2}</code>
, depending on how the data is stacked. Then we can simply overload
<code>*</code>
as follows:
</p>
<pre><codeclass="language-julia">*(W::Matrix, x::Batch{Vector,1}) = x * W'
*(W::Matrix, x::Batch{Vector,2}) = W * x</code></pre>
<p>
This means that we can always write
<code>W*x</code>
, and the code is reusable in a larger network regardless of the overall batching approach. Moreover, Julia's type system ensures there's no runtime cost to doing this, and we can compile the code appropriately for backends like TensorFlow as well.