I'm using Python Keras package for neural network. This is the link. Is batch_size equals to number of test samples? From Wikipedia we have this information:

However, in other cases, evaluating the sum-gradient may require expensive evaluations of the gradients from all summand functions. When the training set is enormous and no simple formulas exist, evaluating the sums of gradients becomes very expensive, because evaluating the gradient requires evaluating all the summand functions' gradients. To economize on the computational cost at every iteration, stochastic gradient descent samples a subset of summand functions at every step. This is very effective in the case of large-scale machine learning problems.

Above information is describing test data? Is this same as batch_size in keras (Number of samples per gradient update)?


The batch size defines the number of samples that will be propagated through the network.

For instance, let's say you have 1050 training samples and you want to set up a batch_size equal to 100. The algorithm takes the first 100 samples (from 1st to 100th) from the training dataset and trains the network. Next, it takes the second 100 samples (from 101st to 200th) and trains the network again. We can keep doing this procedure until we have propagated all samples through of the network. Problem might happen with the last set of samples. In our example, we've used 1050 which is not divisible by 100 without remainder. The simplest solution is just to get the final 50 samples and train the network.

Advantages of using a batch size < number of all samples:

  • It requires less memory. Since you train the network using fewer samples, the overall training procedure requires less memory. That's especially important if you are not able to fit the whole dataset in your machine's memory.

  • Typically networks train faster with mini-batches. That's because we update the weights after each propagation. In our example we've propagated 11 batches (10 of them had 100 samples and 1 had 50 samples) and after each of them we've updated our network's parameters. If we used all samples during propagation we would make only 1 update for the network's parameter.

Disadvantages of using a batch size < number of all samples:

  • The smaller the batch the less accurate the estimate of the gradient will be. In the figure below, you can see that the direction of the mini-batch gradient (green color) fluctuates much more in comparison to the direction of the full batch gradient (blue color).

Gradient directions for different batch setups

Stochastic is just a mini-batch with batch_size equal to 1. In that case, the gradient changes its direction even more often than a mini-batch gradient.

  • $\begingroup$ Thank you for answer. do you work with Keras? anyway to set test data in this package? $\endgroup$ – user2991243 May 22 '15 at 9:50
  • 3
    $\begingroup$ No, I didn't. This is popular technique in neural networks and this terminology you can see in different libraries, books and articles. Do you want check test data error in every epoch or just verify model after training? $\endgroup$ – itdxer May 22 '15 at 9:55
  • $\begingroup$ Yes. That's true. Similar structure we have in MATLAB but i found only train and validation data-sets here. I think here in this package validation data-set is same as test data but there isn't early stopping so we don't have any real validation data. $\endgroup$ – user2991243 May 22 '15 at 9:57
  • 1
    $\begingroup$ The network also converges faster as the number of updates is considerable higher. Setting up the mini batch size is kind of an art, too small and you risk making your learning too stochastic, faster but will converge to unreliable models, too big and it wont fit into memory and still take ages. $\endgroup$ – Ramalho May 23 '15 at 0:06
  • 1
    $\begingroup$ Typically when people say online learning they mean batch_size=1. The idea behind online learning is that you update your model as soon as you see the example. With larger batch size it means that first you are looking through the multiple samples before doing update. In RNN size of the batch can have different meanings. Usually, It's common to split training sequence into window of fixed size (like 10 words). In this case including 100 of these windows during the training will mean that you have batch_size=100. $\endgroup$ – itdxer Aug 7 '17 at 18:29

In the neural network terminology:

  • one epoch = one forward pass and one backward pass of all the training examples
  • batch size = the number of training examples in one forward/backward pass. The higher the batch size, the more memory space you'll need.
  • number of iterations = number of passes, each pass using [batch size] number of examples. To be clear, one pass = one forward pass + one backward pass (we do not count the forward pass and backward pass as two different passes).

Example: if you have 1000 training examples, and your batch size is 500, then it will take 2 iterations to complete 1 epoch.

FYI: Tradeoff batch size vs. number of iterations to train a neural network

  • $\begingroup$ But whats the difference between using [batch size] numbers of examples and train the network on each example and proceed with the next [batch size] numbers examples. Since you pass one example through the network and apply SGD and take the next example and so on it will make no difference if the batch size is 10 or 1000 or 100000. After [batch size] numbers of examples is done the next example of the next batch will follow. It only makes a difference if [batch size] numbers of example will pass [number of iterations] times the network and then proceed with the next [batch size] examples. $\endgroup$ – Erhard Dinhobl Mar 26 '17 at 8:31
  • $\begingroup$ An important distinction is that the learning step (one step) is applied once for every batch, while you have to cycle through all batches to make one epoch. So the difference is also algorithmic not only in memory: bigger batches mean that you average the gradient over more samples. $\endgroup$ – meduz Sep 7 '18 at 7:19

When solving with a CPU an Optimization Problem, you Iteratively apply an Algorithm over some Input Data. In each of these iterations you usually update a Metric of your problem doing some Calculations on the Data. Now when the size of your data is large it might need a considerable amount of time to complete every iteration, and may consume a lot of resources. So sometimes you choose to apply these iterative calculations on a Portion of the Data to save time and computational resources. This portion is the batch_size and the process is called (in the Neural Network Lingo) batch data processing. When you apply your computations on all your data, then you do online data processing. I guess the terminology comes from the 60s, and even before. Does anyone remember the .bat DOS files? But of course the concept incarnated to mean a thread or portion of the data to be used.


The documentation for Keras about batch size can be found under the fit function in the Models (functional API) page

batch_size: Integer or None. Number of samples per gradient update. If unspecified, batch_size will default to 32.

If you have a small dataset, it would be best to make the batch size equal to the size of the training data. First try with a small batch then increase to save time. As itdxer mentioned, there's a tradeoff between accuracy and speed.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.