Adagrad , short for adaptive gradient, is a gradient based optimizer that automatically tunes its learning rate in the training process. The learning rate is updated parameter wise, i.e. we have a different learning rate for each of the parameters.

The parameters associated with frequently occurring features have small updates (low learning rate), and the parameters associated with seldom occurring features have bigger updates (high learning rate).

$€€g\left\{t,i\right\} = \nabla J \left(\theta\left\{t,i\right\}\right)€€$

Where $€€g_\left\{t,i\right\}€€$ is the gradient of the objective function with respect to the parameter $€€\theta_i€€$

The parameter is updated as follows,

$\theta\left\{t+1,i\right\}=\theta\left\{t,i\right\} - \eta \cdot \frac\left\{g\left\{t,i\right\}\right\}\left\{\sqrt\left\{G\left\{t,ii\right\}\right\}+\epsilon\right\}$

Here $€€\theta\left\{t,i\right\}€€$ is the parameter to be updated, $€€G\left\{t,ii\right\}€€$ is the sum of the square of all the gradient till time t. We can see that the learning rate is adjusted according to the previous encountered gradients. $€€eta€€$ is the Base Learning Rate.

Here, the base learning rate is usually initialized to 0.01.

$€€\epsilon€€$ is used for numeric stability. Its value is $€€10^\left\{-8\right\}€€$ by default.

### Learning Rate Decay

It is a technique where a large learning rate is adopted in the beginning of the training process and then it is decayed by the certain factor after pre-defined epochs. Higher learning rate decay suggests that the initial learning rate will decay more in the epochs.

Setting a learning rate decay might potentially slow the training process since we decrease the learning rate.

### Code Implementation

    # importing the library
import torch
import torch.nn as nn

x = torch.randn(10, 3)
y = torch.randn(10, 2)

# Build a fully connected layer.
linear = nn.Linear(3, 2)

# Build MSE loss function and optimizer.
criterion = nn.MSELoss()

# Forward pass.
pred = linear(x)

# Compute loss.
loss = criterion(pred, y)
print('loss:', loss.item())

optimizer.step()


Last updated on Dec 21, 2022