Contour Plot

Neural Network Loss Landscape

Optimization surface for machine learning showing local minima and gradient descent trajectory.

Output
Neural Network Loss Landscape
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

# Create complex optimization landscape
x = np.linspace(-3, 3, 250)
y = np.linspace(-3, 3, 250)
X, Y = np.meshgrid(x, y)

# Rastrigin-like function
A = 10
Z = A * 2 + (X**2 - A * np.cos(2 * np.pi * X)) + (Y**2 - A * np.cos(2 * np.pi * Y))
Z = np.log1p(Z)

# ML/AI dark theme
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(10, 8))
fig.patch.set_facecolor('#0d1117')
ax.set_facecolor('#0d1117')

# Custom colormap - cool blues
colors = ['#0d1117', '#0a2a4a', '#0a4a7a', '#0a6aaa', '#2a8aca', 
          '#4aaadd', '#6acaee', '#8aeaff', '#aaffff', '#ffffff']
cmap = LinearSegmentedColormap.from_list('ml_blue', colors, N=256)

# Filled contour
cf = ax.contourf(X, Y, Z, levels=30, cmap=cmap)

# Subtle contour lines
cs = ax.contour(X, Y, Z, levels=15, colors='#8aeaff', linewidths=0.3, alpha=0.3)

# Mark global minimum with glow
ax.scatter([0], [0], color='#ff6b6b', s=200, marker='*', zorder=6, 
           edgecolors='white', linewidths=1.5)
ax.scatter([0], [0], color='#ff6b6b', s=400, marker='*', zorder=5, alpha=0.3)

# Gradient descent path
np.random.seed(42)
path_x, path_y = [2.5], [2.5]
for _ in range(35):
    dx = -0.12 * path_x[-1] + 0.015 * np.random.randn()
    dy = -0.12 * path_y[-1] + 0.015 * np.random.randn()
    path_x.append(path_x[-1] + dx)
    path_y.append(path_y[-1] + dy)

ax.plot(path_x, path_y, '-', color='#ffa502', linewidth=2, alpha=0.9, zorder=4)
ax.scatter(path_x[::5], path_y[::5], color='#ffa502', s=30, zorder=5, edgecolors='white', linewidths=0.5)

# Styled colorbar
cbar = plt.colorbar(cf, ax=ax, pad=0.02, shrink=0.85)
cbar.ax.set_facecolor('#0d1117')
cbar.set_label('log(Loss + 1)', fontsize=11, color='#c9d1d9', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#c9d1d9')
cbar.outline.set_edgecolor('#30363d')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#c9d1d9', fontsize=9)

# Labels
ax.set_xlabel('Weight 1', fontsize=13, color='#c9d1d9', fontweight='600', labelpad=10)
ax.set_ylabel('Weight 2', fontsize=13, color='#c9d1d9', fontweight='600', labelpad=10)
ax.set_title('Neural Network Loss Landscape', fontsize=16, color='white', 
             fontweight='bold', pad=20)

# Style axes
ax.tick_params(colors='#8b949e', labelsize=10, length=0)
for spine in ax.spines.values():
    spine.set_visible(False)

ax.set_aspect('equal')
plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Pairwise Data

Did this help you?

Support PyLucid to keep it free & growing

Support