Download code/multi-hop/plot_loss_contour_simple.py from fhyfhy/Implicit-Reasoning-checkpoints: direct link, hf CLI and curl.
- Browser
- Download file 2.73 kB
-
https://huggingface.co/fhyfhy/Implicit-Reasoning-checkpoints/resolve/main/code/multi-hop/plot_loss_contour_simple.py
- Command line
-
hf download hf://fhyfhy/Implicit-Reasoning-checkpoints/code/multi-hop/plot_loss_contour_simple.py
-
curl -L -o plot_loss_contour_simple.py https://huggingface.co/fhyfhy/Implicit-Reasoning-checkpoints/resolve/main/code/multi-hop/plot_loss_contour_simple.py
2.73 kB
| #!/usr/bin/env python3 | |
| """ | |
| 简化版 loss contour 绘图:只画两个 loss landscape 并排,powerlaw trajectory。 | |
| """ | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from matplotlib.colors import LinearSegmentedColormap | |
| # ===== 加载数据 ===== | |
| data = np.load("plots/loss_contour_2d_multihop_data.npz", allow_pickle=True) | |
| alpha_vals = data["alpha_vals"] | |
| beta_vals = data["beta_vals"] | |
| loss_uniform = data["loss_uniform"] | |
| loss_powerlaw = data["loss_powerlaw"] | |
| pl_traj_alpha = data["pl_traj_alpha"] | |
| pl_traj_beta = data["pl_traj_beta"] | |
| # ===== 绘图设置 ===== | |
| cmap_name = "terrain" | |
| def _plot_gradient_trajectory(ax, traj_alpha, traj_beta, linewidth=1.2, s=15): | |
| """画渐变轨迹:红色(Init) -> 白色(Final)""" | |
| n = len(traj_alpha) | |
| colors = [(1, i/(n-1), i/(n-1)) for i in range(n)] # 红到白 | |
| # 线段 | |
| for i in range(n - 1): | |
| ax.plot(traj_alpha[i:i+2], traj_beta[i:i+2], | |
| color=colors[i], linewidth=linewidth, zorder=3) | |
| # 散点 | |
| ax.scatter(traj_alpha, traj_beta, c=colors, s=s, zorder=4, edgecolors='none') | |
| # Init/Final 标记 | |
| ax.scatter(traj_alpha[0], traj_beta[0], c='red', s=s*3, marker='o', | |
| zorder=5, edgecolors='white', linewidths=0.5, label='Init') | |
| ax.scatter(traj_alpha[-1], traj_beta[-1], c='white', s=s*3, marker='o', | |
| zorder=5, edgecolors='gray', linewidths=0.5, label='Final') | |
| # ===== 创建图 ===== | |
| fig, axes = plt.subplots(1, 2, figsize=(7.0, 3.2)) | |
| # 共享 colorbar 范围 | |
| vmin = min(loss_powerlaw.min(), loss_uniform.min()) | |
| vmax = max(loss_powerlaw.max(), loss_uniform.max()) | |
| levels = np.linspace(vmin, vmax, 50) | |
| titles = ["Powerlaw (α=1.1)", "Uniform"] | |
| grids = [loss_powerlaw, loss_uniform] | |
| for ax, title, grid in zip(axes, titles, grids): | |
| # Contour (转置 grid 使 x=alpha, y=beta) | |
| cf = ax.contourf(alpha_vals, beta_vals, grid.T, levels=levels, | |
| cmap=cmap_name, extend='both') | |
| # Trajectory | |
| _plot_gradient_trajectory(ax, pl_traj_alpha, pl_traj_beta) | |
| # 样式 | |
| ax.set_title(title, fontsize=10) | |
| ax.set_xticks([]) | |
| ax.set_yticks([]) | |
| ax.legend(loc='upper left', fontsize=6, framealpha=0.8) | |
| # Colorbar | |
| fig.subplots_adjust(left=0.02, right=0.88, bottom=0.05, top=0.90, wspace=0.08) | |
| cbar_ax = fig.add_axes([0.90, 0.15, 0.02, 0.65]) | |
| cbar = fig.colorbar(cf, cax=cbar_ax) | |
| cbar.ax.tick_params(labelsize=7) | |
| # 保存 | |
| fig.savefig("plots/loss_contour_2d_multihop_simple.pdf", bbox_inches='tight', dpi=150) | |
| fig.savefig("plots/loss_contour_2d_multihop_simple.png", bbox_inches='tight', dpi=150) | |
| print("Saved: plots/loss_contour_2d_multihop_simple.pdf") | |
| print("Saved: plots/loss_contour_2d_multihop_simple.png") | |
| plt.close() | |