Neural Network Mini Batch Gradient Descent
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In the realm of machine learning, neural networks have become a pivotal tool due to their ability to perform well on a myriad of complex tasks. However, training these models involves optimization challenges. Among the optimization techniques used, Mini-Batch Gradient Descent has gained popularity owing to its balance of computational efficiency and performance.
Gradient Descent in a Nutshell
Gradient Descent is an optimization algorithm used to minimize the cost function of models by iteratively adjusting the model's parameters. It does this by computing the gradient (or partial derivatives) of the cost function with respect to the parameters and updating the parameters in the opposite direction of the gradient.
The general formula for updating a parameter using Gradient Descent is:
where: • is the parameter vector. • is the learning rate. • is the cost function.
Types of Gradient Descent
- Batch Gradient Descent: Processes the entire dataset to compute the gradient at every step. Although exact, it can be computationally heavy and slow.
- Stochastic Gradient Descent (SGD): Processes one training example per iteration. It's much faster but has high variance in updates, which may lead to non-convergence or oscillations around the minimum.
- Mini-Batch Gradient Descent: Strikes a balance by processing small, random subsets (mini-batches) of the training data. This method combines the advantages of Batch Gradient Descent and SGD - providing a stable convergence path while maintaining computational efficiency.
Understanding Mini-Batch Gradient Descent
Mini-Batch Procedure
In Mini-Batch Gradient Descent, the dataset is divided into smaller subsets called mini-batches. The model parameters are updated using the gradient of the cost function averaged over each mini-batch.
Consider the following steps in the Mini-Batch Gradient Descent algorithm:
- Initialize Parameters: Start with random weights and biases.
- Shuffle the Dataset: Randomly shuffle the training data to ensure randomness in mini-batches.
- Create Mini-Batches: Divide the shuffled dataset into
bmini-batches (wherebis typically between 32 to 512). - Iterate Over Each Mini-Batch: • Compute predictions using the current model parameters. • Compute the error/loss using the cost function. • Calculate the gradient of the loss concerning each parameter. • Update the model parameters using the gradient and a predefined learning rate.
- Repeat: Iterate the process over several epochs until convergence.
Key Advantages
• Efficient Computation: Reduces computation time compared to Batch Gradient Descent. • Faster Convergence: Typically converges faster than SGD due to balanced variance. • Better Generalization: Provides regularizing effects due to noise inherent in mini-batches, potentially improving generalization.
Choosing Mini-Batch Size
The choice of mini-batch size b
plays an essential role, influencing both the model's performance and training time. Common practices involve:
• Small Batch Sizes: Leads to noisy updates and can help escape sharper minima, improving generalization. • Large Batch Sizes: Blurs the noise but may settle in sharper minima, possibly hurting generalization but improving computational efficiency.
A mini-batch size of around 64 is often a good starting point, but the optimal size may vary depending on specific datasets and hardware constraints.
Mathematical Formulation
For a given neural network with parameters , a loss function for a single data point is given by:
where is the model's prediction for input .
For a mini-batch containing examples, the average loss is:
The gradient update rule is:
where denotes the gradient of the loss with respect to the parameters over a mini-batch.
Applications and Considerations
• Deep Learning Frameworks: Libraries like TensorFlow and PyTorch have built-in support for mini-batch gradient descent, allowing users to specify the batch size easily.
• Memory Requirements: Mini-Batch Gradient Descent requires storing the data in memory; therefore, constraints on batch size may be imposed by available GPU/CPU memory.
• Learning Rate Adjustments: Optimal learning rates may vary; learning rate scheduling can be effective in converging to an optimal solution.
Key Points Summary
| Technique | Pros | Cons |
| Batch Gradient Descent | Accurate with global optimum reach | Slow and computationally expensive |
| Stochastic Gradient Descent | Fast with frequent updates | High variance and potential divergence |
| Mini-Batch Gradient Descent | Efficient with balanced updates | Requires parameter tuning |
Conclusion
Mini-Batch Gradient Descent offers a compelling optimization strategy for training neural networks. By combining speed, efficiency, and stability, it has become a standard practice in large-scale machine learning applications. Understanding the nuances of mini-batch selection, learning rate adjustments, and their effects on convergence is crucial for leveraging this technique effectively.
Related reading
- Neural Network Mysterious ReLu
- Neural Network No hidden layers vs Logistic Regression?
- Neural Network not learning - MNIST data - Handwriting recognition
- Neural Network Ordinal Classification for Age
- Neural network weighting
- NFA minimization without determinization
- Neural Network System Identification
- Neural Network to predict nth square

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.