Optimization Rule in Deep Neural Networks
Last Updated :
03 Mar, 2025
In machine learning, optimizers and loss functions are two fundamental components that help improve a model’s performance.
- A loss function evaluates a model's effectiveness by computing the difference between expected and actual outputs. Common loss functions include log loss, hinge loss, and mean square loss.
- An optimizer improves the model by adjusting its parameters (weights and biases) to minimize the loss function value. Examples include RMSProp, ADAM, and SGD (Stochastic Gradient Descent).
The optimizer’s role is to find the best combination of weights and biases that leads to the most accurate predictions.
Gradient Descent
Gradient Descent is a popular optimization method for training machine learning models. It works by iteratively adjusting the model parameters in the direction that minimizes the loss function.
Key Steps in Gradient Descent
- Initialize parameters: Randomly initialize the model parameters.
- Compute the gradient: Calculate the gradient (derivative) of the loss function with respect to the parameters.
- Update parameters: Adjust the parameters by moving in the opposite direction of the gradient, scaled by the learning rate.
Formula :
θ_{(k+1)} = θ_k - α ∇J(θ_k)
where,
- θ_{(k+1)} is the updated parameter vector at the (k+1)^{th} iteration.
- \theta_k is the current parameter vector at the kth iteration.
- \alpha is the learning rate, which is a positive scalar that determines the step size for each iteration.
- ∇J(θ_k) is the gradient of the cost or loss function J with respect to the parameters \theta_{k}
Gradient Descent with Armijo Goldstein Condition
This variant ensures that the step size is large enough to effectively reduce the objective function, using a line search that satisfies the Armijo condition.
Condition:
f(x^{t-1} + α∇f(x^{t-1})) - f(x^{t-1}) \ge c α ||∇f(x^{t-1})||^2
Where:
- \alpha is the step size.
- c is a constant between 0 and 1.
Gradient Descent with Armijo Full Relaxation Condition
Incorporates both first and second derivatives (Hessian matrix) to determine a more optimal step size for the update.
Condition:
f(x^{t-1} + α∇f(x^{t-1})) - f(x^{t-1}) \ge c α ||∇f(x^{t-1})||^2 + \frac{b}{2} α^2∇f(x^{t-1})^T H(x) ∇f(x^{t-1})
Where:
- H(x) is the Hessian matrix.
- c and b are constants between 0 and 1.
Variants of Gradient Descent
1. Stochastic Gradient Descent (SGD)
Stochastic Gradient Descent (SGD) updates the model parameters after each training example, making it more efficient for large datasets compared to traditional Gradient Descent, which uses the entire dataset for each update.
Steps:
- Select a training example.
- Compute the gradient of the loss function.
- Update the model parameters.
- Advantages: Requires less memory and may find new minima.
- Disadvantages: Noisier, requiring more iterations to converge.
2. Mini Batch Stochastic Gradient Descent
Mini-batch stochastic gradient descent consists of a predetermined number of training examples, smaller than the full dataset. This approach combines the advantages of the previously mentioned variants. In one epoch, following the creation of fixed-size mini-batches, we execute the following steps:
- Select a mini-batch.
- Compute the mean gradient of the mini-batch.
- Apply the mean gradient obtained in step 2 to update the model's weights.
- Repeat steps 1 to 2 for all the mini-batches that have been created.
- Advantages: Requires medium amount of memory and less time required to converge when compared to SGD
- Disadvantage: May get stuck at local minima
3. SGD with Momentum
Momentum helps accelerate convergence by smoothing out the noisy gradients of SGD, thus reducing fluctuations and improving the speed of convergence.
v_{(t+1)} = β * v_t + (1 - β) * ∇J(θ_t)
Where:
- v_t is the momentum at time t.
- \beta is the momentum coefficient.
- ∇J(θ_t) is the gradient at time t.
Then, the model parameters are updated using:
θ_{(t+1)} = θ_t - α * v_{(t+1)}
- Advantages: Mitigates oscillations, reduces variance, and faster convergence.
- Disadvantages: Requires tuning the momentum coefficient \beta.
Advanced Optimizers
1. AdaGrad
AdaGrad adapts the learning rate for each parameter based on the historical gradient information. The learning rate decreases over time, making AdaGrad effective for sparse features.
θ_{(t+1)} = θ_t - \frac{α}{\sqrt{G_t + ε}} * ∇J(θ_t)
Where:
- G_t is the sum of squared gradients.
- \varepsilon is a small constant to avoid division by zero.
- Advantages: Adapts the learning rate, improving training efficiency.
- Disadvantages: Learning rate decays too quickly, causing slow convergence.
2. RMSProp
RMSProp improves upon AdaGrad by introducing a decay factor to prevent the learning rate from decreasing too rapidly.
E[g^2]_t = γ * E[g^2]_{(t-1)} + (1 - γ) * (∇J(θ_t))^2
θ_{(t+1)} = θ_t - \frac{α}{\sqrt{E[g^2]_t + ε}} * ∇J(θ_t)
Where:
- \gamma is the decay rate.
- E[g^2]_t is the exponentially moving average of squared gradients.
- Advantages: Prevents excessive decay of learning rates.
- Disadvantages: Computationally expensive due to the additional parameter.
3. Adam (Adaptive Moment Estimation)
Adam combines the advantages of Momentum and RMSProp. It uses both the first moment (mean) and second moment (variance) of gradients to adapt the learning rate for each parameter.
- Update the first moment: m_t = β_1 * m_{(t-1)} + (1 - β_1) * ∇J(θ_t)
- Update the second moment: v_t = β_2 * v_{(t-1)} + (1 - β_2) * (∇J(θ_t))^2
- Bias correction: \hat{m}_t = \frac{m_t}{1 - β_1^t}, \quad \hat{v}_t = \frac{v_t}{1 - β_2^t}
- Update parameters: θ_{(t+1)} = θ_t - \frac{α}{\sqrt{\hat{v}_t + ε}} * \hat{m}_t
Where:
- \beta_{1} and β_2 are the decay rates for the first and second moments.
- \varepsilon is a small constant to prevent division by zero.
- Advantages: Fast convergence.
- Disadvantages: Requires significant memory due to the need to store first and second moment estimates.
Comparison of Optimizers
| Optimizer | Advantages | Disadvantages |
|---|
| SGD | Simple, easy to implement | Slow convergence, requires tuning |
|---|
| Mini-Batch SGD | Faster than SGD | Computationally expensive, stuck in local minima |
|---|
| SGD with Momentum | Faster convergence, reduces noise | Requires careful tuning of βββ |
|---|
| AdaGrad | Adaptive learning rates | Decays too fast, slow convergence |
|---|
| RMSProp | Prevents fast decay of learning rates | Computationally expensive |
|---|
| Adam | Fast, combines momentum and RMSProp | Memory-intensive, computationally expensive |
|---|
Each optimizer has its own strengths and weaknesses. The choice of optimizer depends on the specific problem, dataset characteristics, and the computational resources available. Adam is often the default choice due to its robust performance, but each situation may call for a different optimizer to achieve optimal results.
Similar Reads
Optimization in Neural Networks and Newton's Method
In machine learning, optimizers and loss functions are two components that help improve the performance of the model. A loss function measures the performance of a model by measuring the difference between the output expected from the model and the actual output obtained from the model. Mean square
12 min read
Unconstrained Optimization Techniques in Neural Networks
Unconstrained optimization plays a crucial role in the training of neural networks. Unlike constrained optimization, where the solution must satisfy certain constraints, unconstrained optimization seeks to minimize (or maximize) an objective function without any restrictions on the variable values.
4 min read
Numpy Gradient - Descent Optimizer of Neural Networks
NumPy Gradient Descent Optimizer is a commonly used optimization algorithm in neural network training that is based on the gradient descent algorithm. It is used to minimize the cost function of a neural network model, by adjusting the model's weights and biases through a series of iterations. The b
6 min read
A single neuron neural network in Python
Neural networks are the core of deep learning, a field that has practical applications in many different areas. Today neural networks are used for image classification, speech recognition, object detection, etc. Now, Let's try to understand the basic unit behind all these states of art techniques.A
3 min read
What is Forward Propagation in Neural Networks?
Forward propagation is the fundamental process in a neural network where input data passes through multiple layers to generate an output. It is the process by which input data passes through each layer of neural network to generate output. In this article, weâll more about forward propagation and se
4 min read
Neural Network Pruning in Deep Learning
As deep learning models have grown larger and more complex, they have also become more resource-intensive in terms of computational power and memory. In many real-world applications, especially on edge devices like mobile phones or embedded systems, these resource-heavy models are not feasible to de
8 min read
Siamese Neural Network in Deep Learning
Siamese Neural Networks (SNNs) are a specialized type of neural network designed to compare two inputs and determine their similarity. Unlike traditional neural networks, which process a single input to produce an output, SNNs take two inputs and pass them through identical subnetworks. In this arti
7 min read
Recurrent Neural Networks in R
Recurrent Neural Networks (RNNs) are a type of neural network that is able to process sequential data, such as time series, text, or audio. This makes them well-suited for tasks such as language translation, speech recognition, and time series prediction. In this article, we will explore how to impl
5 min read
Batch Size in Neural Network
Batch size is a hyperparameter that determines the number of training records used in one forward and backward pass of the neural network. In this article, we will explore the concept of batch size, its impact on training, and how to choose the optimal batch size. Prerequisites: Neural Network, Grad
5 min read
Epoch in Neural Network using R
Deep Learning is a subfield of machine learning and artificial intelligence that focuses on training neural networks to perform various tasks, such as image recognition, natural language processing, and reinforcement learning. When training a deep learning model, the concept of an "epoch" is fundame
8 min read