1 import numpy as np
   2 import matplotlib.pyplot as plt
   3 from matplotlib.collections import LineCollection
   4 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   5 setup_style()
   6 
   7 x = np.linspace(-2, 2, 80)
   8 y = np.linspace(-2, 2, 80)
   9 X, Y = np.meshgrid(x, y)
  10 Z = 0.5 * (X ** 2 + Y ** 2) - 0.3 * np.cos(2 * np.pi * X) * np.cos(2 * np.pi * Y) + 1
  11 
  12 fig = plt.figure(figsize=(8, 6))
  13 ax = fig.add_subplot(111, projection='3d')
  14 surf = ax.plot_surface(X, Y, Z, cmap='coolwarm', alpha=0.65, edgecolor='none',
  15                        antialiased=True, rstride=2, cstride=2)
  16 
  17 # Contour projection on floor
  18 ax.contour(X, Y, Z, zdir='z', offset=Z.min() - 0.5, cmap='coolwarm', alpha=0.3, levels=15)
  19 
  20 # Optimization trajectory (simulated SGD path)
  21 np.random.seed(42)
  22 traj_x = [1.8]
  23 traj_y = [1.5]
  24 lr = 0.05
  25 for step in range(60):
  26     gx = traj_x[-1] - 0.3 * 2 * np.pi * np.sin(2 * np.pi * traj_x[-1]) * np.cos(2 * np.pi * traj_y[-1])
  27     gy = traj_y[-1] - 0.3 * 2 * np.pi * np.cos(2 * np.pi * traj_x[-1]) * np.sin(2 * np.pi * traj_y[-1])
  28     nx = traj_x[-1] - lr * gx + np.random.normal(0, 0.02)
  29     ny = traj_y[-1] - lr * gy + np.random.normal(0, 0.02)
  30     traj_x.append(np.clip(nx, -2, 2))
  31     traj_y.append(np.clip(ny, -2, 2))
  32 
  33 traj_x = np.array(traj_x)
  34 traj_y = np.array(traj_y)
  35 traj_z = 0.5 * (traj_x ** 2 + traj_y ** 2) - 0.3 * np.cos(2 * np.pi * traj_x) * np.cos(
  36     2 * np.pi * traj_y) + 1 + 0.05
  37 
  38 # Gradient-colored trajectory (red→blue)
  39 for k in range(len(traj_x) - 1):
  40     frac = k / (len(traj_x) - 1)
  41     color = plt.cm.coolwarm(1 - frac)  # red (early) → blue (late)
  42     ax.plot(traj_x[k:k + 2], traj_y[k:k + 2], traj_z[k:k + 2],
  43             color=color, linewidth=1.5, alpha=0.8)
  44 
  45 # Start and end markers
  46 ax.scatter([traj_x[0]], [traj_y[0]], [traj_z[0]], color=COLORS['down'], s=80,
  47            marker='^', edgecolor='white', linewidth=1, zorder=6, label='Start')
  48 ax.scatter([traj_x[-1]], [traj_y[-1]], [traj_z[-1]], color=COLORS['up'], s=80,
  49            marker='*', edgecolor='white', linewidth=1, zorder=6, label='End')
  50 
  51 # Global minimum
  52 ax.scatter([0], [0], [Z.min()], color=COLORS['down'], s=120, marker='*',
  53            edgecolor='white', zorder=5, label='Global Min')
  54 
  55 # Saddle point annotations
  56 saddle_pts = [(-1, 0), (0, -1), (1, 0)]
  57 for sx, sy in saddle_pts:
  58     sz = 0.5 * (sx ** 2 + sy ** 2) - 0.3 * np.cos(2 * np.pi * sx) * np.cos(2 * np.pi * sy) + 1
  59     ax.scatter([sx], [sy], [sz], color=PALETTE[2], s=40, marker='o',
  60                edgecolor='white', zorder=5)
  61     ax.text(sx, sy, sz + 0.15, 'saddle', fontsize=6, color=COLORS['ref_line'], ha='center')
  62 
  63 ax.set_xlabel('θ₁', fontsize=10, labelpad=6)
  64 ax.set_ylabel('θ₂', fontsize=10, labelpad=6)
  65 ax.set_zlabel('Loss', fontsize=10, labelpad=6)
  66 ax.view_init(elev=30, azim=135)
  67 ax.tick_params(labelsize=8)
  68 ax.legend(fontsize=8, loc='best')
  69 fig.colorbar(surf, shrink=0.4, aspect=15, pad=0.12)
  70 fig.tight_layout()
  71 save_fig(fig, 'figures/fig_loss_landscape.pdf')