<p>From the recent Deep Learning book by Goodfellow et al., <a href="http://www.deeplearningbook.org/contents/optimization.html" rel="noreferrer">chapter 8</a>:</p>
<blockquote>
<p>Minibatch sizes are generally driven by the following factors:</p>
<ul>
<li>Larger batches provide a more accurate estimate of the gradient, but
with less than linear returns.</li>
<li>Multicore architectures are usually
underutilized by extremely small batches. This motivates using some
absolute minimum batch size, below which there is no reduction in the
time to process a minibatch.</li>
<li>If all examples in the batch are to be
processed in parallel (as is typically the case), then the amount of
memory scales with the batch size. For many hardware setups this is
the limiting factor in batch size.</li>
<li>Some kinds of hardware achieve
better runtime with speciﬁc sizes of arrays. Especially when using
GPUs, it is common for power of 2 batch sizes to offer better runtime.
Typical power of 2 batch sizes range from 32 to 256, with 16 sometimes
being attempted for large models.</li>
<li>Small batches can offer a
regularizing effect (Wilson and Martinez, 2003), perhaps due to the
noise they add to the learning process. Generalization error is often
best for a batch size of 1. Training with such a small batch size
might require a small learning rate to maintain stability because of
the high variance in the estimate of the gradient. The total runtime
can be very high as a result of the need to make more steps, both
because of the reduced learning rate and because it takes more steps
to observe the entire training set.</li>
</ul>
</blockquote>
<p>Which in practice usually means &quot;<em>in powers of 2 and the larger the better, provided that the batch fits into your (GPU) memory</em>&quot;.</p>
<p>You might want also to consult several good posts here in Stack Exchange:</p>
<ul>
<li><a href="https://stats.stackexchange.com/questions/164876/tradeoff-batch-size-vs-number-of-iterations-to-train-a-neural-network">Tradeoff batch size vs. number of iterations to train a neural network</a></li>
<li><a href="https://stackoverflow.com/questions/40535679/selection-of-mini-batch-size-for-neural-network-regression">Selection of Mini-batch Size for Neural Network Regression</a></li>
<li><a href="https://stats.stackexchange.com/questions/140811/how-large-should-the-batch-size-be-for-stochastic-gradient-descent">How large should the batch size be for stochastic gradient descent?</a></li>
</ul>
<p>Just keep in mind that the paper by Keskar et al. '<a href="https://arxiv.org/abs/1609.04836" rel="noreferrer">On Large-Batch Training for Deep Learning: Generalization Gap and Sharp Minima</a>', quoted by several of the posts above, has received <a href="https://arxiv.org/abs/1703.04933" rel="noreferrer">some objections</a> by other respectable researchers of the deep learning community.</p>
<p>Hope this helps...</p>
<p><strong>UPDATE</strong> (Dec 2017):</p>
<p>There is a new paper by Yoshua Bengio &amp; team, <a href="https://arxiv.org/abs/1711.04623" rel="noreferrer">Three Factors Influencing Minima in SGD</a> (Nov 2017); it is worth reading in the sense that it reports new theoretical &amp; experimental results on the interplay between learning rate and batch size.</p>
<p><strong>UPDATE</strong> (Mar 2021):</p>
<p>Of interest here is also another paper from 2018, <a href="https://arxiv.org/abs/1804.07612" rel="noreferrer">Revisiting Small Batch Training for Deep Neural Networks</a> (h/t to Nicolas Gervais), which runs contrary to <em>the larger the better</em> advice; quoting from the abstract:</p>
<blockquote>
<p>The best performance has been consistently obtained for mini-batch sizes between m=2 and m=32, which contrasts with recent work advocating the use of mini-batch sizes in the thousands.</p>
</blockquote>
How to calculate optimal batch size
<p>Sometimes I run into a problem:</p>
<pre><code>OOM when allocating tensor with shape
</code></pre>
<p>e.g.</p>
<pre><code>OOM when allocating tensor with shape (1024, 100, 160)
</code></pre>
<p>Where 1024 is my batch size and I don't know what's the rest. If I reduce the batch size or the number of neurons in the model, it runs fine.</p>
<p>Is there a generic way to calculate optimal batch size based on model and GPU memory, so the program doesn't crash?</p>
<p>In short: I want the largest batch size possible in terms of my model, which will fit into my GPU memory and won't crash the program.</p>