---
title: "Understanding Optimizers in Machine Learning"
format: 
  revealjs:
    auto-animate: true
editor: visual
---

# Understanding Optimizers in Machine Learning

## Overview

This presentation will dive into various optimizers used in training neural networks. We'll explore their paths on a loss landscape and understand their distinct behaviors through visual examples.

---

## What is an Optimizer?

Optimizers are algorithms or methods used to change the attributes of the neural network such as weights and learning rate to reduce the losses. Optimizers help to get results faster and more efficiently.

---

## Key Concepts

-   **Gradient Descent**
-   **Stochastic Gradient Descent (SGD)**
-   **Momentum**
-   **Adam** 

Each optimizer will be visualized to illustrate how they navigate the loss landscape during the training process.

---

## Gradient Descent

### Pros and Cons


::: {.columns}
::: {.column}
- **Pros**
    - Simple and easy to understand.
    - Effective for small datasets.
:::

::: {.column}
- **Cons**
    - Slow convergence.
    - Sensitive to the choice of learning rate.
    - Can get stuck in local minima.
:::
:::

---

## Stochastic Gradient Descent (SGD)

### Pros and Cons
::: {.columns}
::: {.column}
- **Pros**
    - Faster convergence than standard gradient descent.
    - Less memory intensive as it uses mini-batches.
:::

::: {.column}
- **Cons**
    - Variability in the training updates can lead to unstable convergence.
    - Requires careful tuning of learning rate.
:::
:::
---

## Momentum

### Pros and Cons
::: {.columns}
::: {.column}
- **Pros**
    - Accelerates SGD in the right direction, thus faster convergence.
    - Reduces oscillations.
:::

::: {.column}
- **Cons**
    - Introduces a new hyperparameter to tune (momentum coefficient).
    - Can overshoot if not configured properly.
:::
:::
---

## Adam (Adaptive Moment Estimation)

### Pros and Cons

::: {.columns}
::: {.column}
- **Pros**
    - Computationally efficient.
    - Works well with large datasets and high-dimensional spaces.
    - Adjusts the learning rate automatically.
:::

::: {.column}
- **Cons**
    - Can lead to suboptimal solutions in certain cases.
    - Might be computationally more intensive due to maintaining moment estimates for each parameter.
:::
:::
---

## RMSprop

RMSprop is an adaptive learning rate method which was designed as a solution to Adagrad's radically diminishing learning rates.

### Pros and Cons

::: {.columns}
::: {.column}
- **Pros**
    - Balances the step size decrease, making it more robust.
    - Works well in online and non-stationary settings.
:::

::: {.column}
- **Cons**
    - Still requires careful tuning of learning rate.
    - Not as widely supported in frameworks as Adam.
:::
:::
---

## AdaMax

AdaMax is a variation of Adam based on the infinity norm which might be more stable than the method based on the L2 norm.

### Pros and Cons

::: {.columns}
::: {.column}
- **Pros**
    - Suitable for datasets with outliers and noise.
    - More stable than Adam in certain scenarios.
:::

::: {.column}
- **Cons**
    - Less commonly used and tested than Adam.
    - May require more hyperparameter tuning compared to Adam.
:::
:::
---



## Loss Function and Its Gradient

We will use a simple quadratic function as our loss landscape to visualize how different optimizers navigate towards the minimum.

```{python}
#| echo: true
# Define the loss function and its gradient
def loss_function(x, y):
    return x**2 + y**2

def gradient(x, y):
    return 2*x, 2*y

```

---

## Simulating Optimizer Paths

Let's simulate the paths that different optimizers take on the loss surface.

---

## Visualizing the Optimizer Paths

This visualization shows the paths taken by SGD, Momentum, and Adam through the loss landscape.

---
## Conclusion

Understanding these paths helps us choose the right optimizer based on the specific needs of our machine learning model.
