mini batch and stochastic

ipynb6 kBPython · 3 cellsExtra material of chapter 3

In [1]
# -*- coding: utf-8 -*- mini Batch
"""
Mini-batch Gradient Descent on f(x)=x^2 — Smooth Video Animation (NO SAVING)
"""

import numpy as np
import matplotlib
matplotlib.use("Qt5Agg")   # IMPORTANT for real animations
import matplotlib.pyplot as plt

# Target function
def f(x):
    return x**2

# Visualization grid
xx = np.linspace(-100, 100, 800)
yy = f(xx)

# Hyperparameters
x = 80.0                       # starting point
eta = 0.01                     # learning rate
steps = 600                    # number of iterations
m = 32                         # mini-batch size
rng = np.random.default_rng(0)

# Enable interactive mode
plt.ion()

# Create persistent figure ONCE
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(xx, yy, linewidth=2, label=r"$f(x)=x^2$")
point, = ax.plot([], [], "ro", markersize=10, label="Current $x_t$")
path,  = ax.plot([], [], "b--", alpha=0.4)  # trail (history)

ax.set_xlim(-100, 100)
ax.set_ylim(0, 10000)
ax.set_xlabel("x")
ax.set_ylabel("f(x)")
ax.legend()

# To store trajectory for smoother visual effect
xs, ys = [], []

# Main animation loop
for t in range(steps):

    # Sample mini-batch Z_1,...,Z_m ~ N(0,1)
    z_batch = rng.normal(0.0, 1.0, size=m)

    # Gradient: g = 2(a_i - z_j) mean → g = 2(x - mean(z_j))
    g = 2.0 * (x - z_batch.mean())

    # Update
    x = x - eta * g

    # Store for trail
    xs.append(x)
    ys.append(f(x))

    # Update moving point + trail
    point.set_data([x], [f(x)])
    path.set_data(xs, ys)

    ax.set_title(
        f"Mini-batch GD on $x^2$  |  m={m}  |  iter={t}  |  x={x:.2f}  |  g=2(x - mean(z))"
    )

    # Smooth animation
    fig.canvas.draw()
    fig.canvas.flush_events()
    plt.pause(0.01)

plt.ioff()
plt.show()
In [ ]
#Stochastic
# -*- coding: utf-8 -*-
"""
Stochastic Gradient Descent on f(x)=x^2 — Smooth Video Animation (NO SAVING)
"""

import numpy as np
import matplotlib
matplotlib.use("Qt5Agg")     # <<< IMPORTANT for REAL animation
import matplotlib.pyplot as plt

# Target function
def f(x):
    return x**2

# Visualization grid
xx = np.linspace(-100, 100, 800)
yy = f(xx)

# SGD hyperparameters
x = 80.0                         # initial point
eta = 0.7                       # learning rate
steps = 800                      # number of updates
rng = np.random.default_rng(0)   # reproducible randomness

# Activate interactive mode
plt.ion()

# Create persistent figure
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(xx, yy, linewidth=2, label=r"$f(x)=x^2$")
point, = ax.plot([], [], "ro", markersize=10, label="Current $x_t$")
path,  = ax.plot([], [], "b--", alpha=0.4, label="Trajectory")

ax.set_xlim(-100, 100)
ax.set_ylim(0, 10000)
ax.set_xlabel("x")
ax.set_ylabel("f(x)")
ax.legend()

# Store trajectory
xs, ys = [], []

# Animation loop
for t in range(steps):

    # Draw one sample Z ~ N(0,1)
    z = rng.normal(0.0, 1.0)

    # Stochastic gradient: g = 2(x - z)
    g = 2.0 * (x - z)

    # Update
    x = x - eta * g

    # Record trajectory
    xs.append(x)
    ys.append(f(x))

    # Update point + trail
    point.set_data([x], [f(x)])
    path.set_data(xs, ys)

    ax.set_title(
        f"Stochastic GD on $x^2$  |  iter={t}  |  x={x:.2f}  |  g = 2(x - z)"
    )

    # Smooth animation
    fig.canvas.draw()
    fig.canvas.flush_events()
    plt.pause(0.05)

plt.ioff()
plt.show()
In [ ]