Skip to content

Commit a228382

Browse files
authored
Merge pull request #4269 from Shantnu-singh/main
added Adam optimizer
2 parents 20f6a0b + 210886f commit a228382

File tree

1 file changed

+116
-0
lines changed
  • docs/Deep Learning/Optimizers in Deep Learning

1 file changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Add Adam in Deep Learning Optimizers
2+
3+
This Section contains an explanation and implementation of the Adam optimization algorithm used in deep learning. Adam (Adaptive Moment Estimation) is a popular optimizer that combines the benefits of two other widely used methods: AdaGrad and RMSProp.
4+
5+
## Table of Contents
6+
- [Introduction](#introduction)
7+
- [Mathematical Explanation](#mathematical-explanation)
8+
- [Adam in Gradient Descent](#adam-in-gradient-descent)
9+
- [Update Rule](#update-rule)
10+
- [Implementation in Keras](#implementation-in-keras)
11+
- [Results](#results)
12+
- [Advantages of Adam](#advantages-of-adam)
13+
- [Limitations of Adam](#limitations-of-adam)
14+
15+
16+
## Introduction
17+
18+
Adam is an optimization algorithm that computes adaptive learning rates for each parameter. It combines the advantages of the AdaGrad and RMSProp algorithms by using estimates of the first and second moments of the gradients. Adam is widely used in deep learning due to its efficiency and effectiveness.
19+
20+
## Mathematical Explanation
21+
22+
### Adam in Gradient Descent
23+
24+
Adam optimizes the stochastic gradient descent by calculating individual adaptive learning rates for each parameter based on the first and second moments of the gradients.
25+
26+
### Update Rule
27+
28+
The update rule for Adam is as follows:
29+
30+
1. Compute the first moment estimate (mean of gradients):
31+
32+
$$
33+
m_t = \beta_1 m_{t-1} + (1 - \beta_1) g_t
34+
$$
35+
36+
2. Compute the second moment estimate (uncentered variance of gradients):
37+
38+
$$
39+
v_t = \beta_2 v_{t-1} + (1 - \beta_2) g_t^2
40+
$$
41+
42+
3. Correct the bias for the first moment estimate:
43+
44+
$$
45+
\hat{m}_t = \frac{m_t}{1 - \beta_1^t}
46+
$$
47+
48+
4. Correct the bias for the second moment estimate:
49+
50+
$$
51+
\hat{v}_t = \frac{v_t}{1 - \beta_2^t}
52+
$$
53+
54+
5. Update the parameters:
55+
56+
$$
57+
\theta_t = \theta_{t-1} - \frac{\eta}{\sqrt{\hat{v}_t} + \epsilon} \hat{m}_t
58+
$$
59+
60+
where:
61+
- $\theta$ are the model parameters
62+
- $\eta$ is the learning rate
63+
- $\beta_1$ and $\beta_2$ are the exponential decay rates for the moment estimates
64+
- $\epsilon$ is a small constant to prevent division by zero
65+
- $g_t$ is the gradient at time step $t$
66+
67+
## Implementation in Keras
68+
69+
Simple implementation of the Adam optimizer using Keras:
70+
71+
```python
72+
import numpy as np
73+
from keras.models import Sequential
74+
from keras.layers import Dense
75+
from keras.optimizers import Adam
76+
77+
# Generate data
78+
X_train = np.random.rand(1000, 20)
79+
y_train = np.random.randint(2, size=(1000, 1))
80+
81+
# Define a model
82+
model = Sequential()
83+
model.add(Dense(64, activation='relu', input_dim=20))
84+
model.add(Dense(1, activation='sigmoid'))
85+
86+
# Compile the model with Adam optimizer
87+
optimizer = Adam(learning_rate=0.001)
88+
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
89+
90+
# Train the model
91+
model.fit(X_train, y_train, epochs=50, batch_size=32)
92+
```
93+
94+
In this example:
95+
- We generate some dummy data for training.
96+
- We define a simple neural network model with one hidden layer.
97+
- We compile the model using the Adam optimizer with a learning rate of 0.001.
98+
- We train the model for 50 epochs with a batch size of 32.
99+
100+
101+
## Results
102+
103+
The results of the training process, including the loss and accuracy, will be displayed after each epoch. You can adjust the learning rate and other hyperparameters to see how they affect the training process.
104+
105+
## Advantages of Adam
106+
107+
1. **Adaptive Learning Rates**: Adam computes adaptive learning rates for each parameter, which helps in faster convergence.
108+
2. **Momentum**: Adam includes momentum, which helps in smoothing the optimization path and avoiding local minima.
109+
3. **Bias Correction**: Adam includes bias correction, improving convergence in the early stages of training.
110+
4. **Robustness**: Adam works well in practice for a wide range of problems, including those with noisy gradients or sparse data.
111+
112+
## Limitations of Adam
113+
114+
1. **Hyperparameter Sensitivity**: The performance of Adam is sensitive to the choice of hyperparameters ($\beta_1$, $\beta_2$, $\eta$), which may require careful tuning.
115+
2. **Memory Usage**: Adam requires additional memory to store the first and second moments, which can be significant for large models.
116+
3. **Generalization**: Models trained with Adam might not generalize as well as those trained with simpler optimizers like SGD in certain cases.

0 commit comments

Comments
 (0)