File size: 4,440 Bytes
bd8608a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | """
Build the Claim 5 figures from the two ResNet-50 / CIFAR-10 self-distillation runs:
* K=8, n=5000, eta=0.4 -> weak-teacher regime: clear U-shape (min at t*=2).
* K=4, n=10000, eta=0.4/0.6 -> strong-teacher regime: monotone denoising.
Together they show the denoising->forgetting trade-off and its regime dependence.
Produces a Plotly HTML (logbook) + CSV (raw) and a matplotlib PNG (poster).
"""
import sys, json, numpy as np, pandas as pd
sys.path.insert(0, "src")
from plotting import new_fig, save, PALETTE
import plotly.graph_objects as go
ext = json.load(open("outputs/claim5/cifar_results_K8_n5000.json"))["curves"]
base = json.load(open("outputs/claim5/cifar_results_K4_n10000.json"))["curves"]
e04 = ext["0.4"] # weak teacher, K=8 -> U-shape
b04 = base["0.4"] # strong teacher, K=4 -> denoising
b06 = base.get("0.6") # strong teacher, K=4 -> denoising
rows = []
tstar = int(np.argmin(e04))
from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=2, horizontal_spacing=0.13, subplot_titles=(
"Weak teacher (n=5000, K=8): U-shape", "Strong teacher (n=10000, K=4): denoising"))
fig.add_trace(go.Scatter(x=list(range(len(e04))), y=e04, mode="lines+markers",
line=dict(color=PALETTE[3], width=3), marker=dict(size=9), name="η=0.4 (n=5k)"), 1, 1)
fig.add_trace(go.Scatter(x=[tstar], y=[e04[tstar]], mode="markers", showlegend=True,
marker=dict(symbol="star", size=18, color=PALETTE[3], line=dict(color="black", width=1)),
name=f"t*={tstar}"), 1, 1)
fig.add_trace(go.Scatter(x=list(range(len(b04))), y=b04, mode="lines+markers",
line=dict(color=PALETTE[0], width=3), marker=dict(size=8), name="η=0.4 (n=10k)"), 1, 2)
if b06:
fig.add_trace(go.Scatter(x=list(range(len(b06))), y=b06, mode="lines+markers",
line=dict(color=PALETTE[2], width=3), marker=dict(size=8), name="η=0.6 (n=10k)"), 1, 2)
fig.update_xaxes(title_text="iteration t", row=1, col=1)
fig.update_xaxes(title_text="iteration t", row=1, col=2)
fig.update_yaxes(title_text="test error (%)", row=1, col=1)
fig.update_yaxes(title_text="test error (%)", row=1, col=2)
fig.update_layout(template="plotly_white", width=900, height=430,
title="Claim 5: ResNet-50 / CIFAR-10 self-distillation — denoising vs forgetting",
margin=dict(l=60, r=20, t=80, b=50))
for name, arr, cfg in [("weak_n5k_K8_eta0.4", e04, "n=5000,K=8"),
("strong_n10k_K4_eta0.4", b04, "n=10000,K=4"),
("strong_n10k_K4_eta0.6", b06 or [], "n=10000,K=4")]:
for t, v in enumerate(arr):
rows.append(dict(series=name, config=cfg, t=t, test_error=v))
save(fig, pd.DataFrame(rows), "outputs/claim5", "claim5_cifar")
print(f"weak-teacher eta=0.4 (n=5000,K=8): U-shape, t*={tstar}, "
f"min={e04[tstar]:.1f}% vs t0={e04[0]:.1f}%, t8={e04[-1]:.1f}%")
print(f"strong-teacher eta=0.4 (n=10000,K=4): {b04[0]:.1f}% -> {min(b04):.1f}% (denoising)")
# ---- poster matplotlib PNG ----
import matplotlib; matplotlib.use("Agg"); import matplotlib.pyplot as plt
plt.rcParams.update({"font.size": 20, "axes.linewidth": 1.6, "font.family": "DejaVu Sans",
"axes.spines.top": False, "axes.spines.right": False})
fig2, (a1, a2) = plt.subplots(1, 2, figsize=(8.2, 3.9))
a1.plot(range(len(e04)), e04, color="#C1443C", lw=3, marker="o", ms=9,
markeredgecolor="white", markeredgewidth=1.3)
a1.scatter([tstar], [e04[tstar]], marker="*", s=460, color="#C1443C", edgecolor="k", zorder=6)
a1.set_title("weak teacher (n=5k, K=8)\nU-shape, t*=2", fontsize=16)
a1.set_xlabel("iteration t"); a1.set_ylabel("test error (%)")
a2.plot(range(len(b04)), b04, color="#2D5F8B", lw=3, marker="s", ms=9,
markeredgecolor="white", markeredgewidth=1.2, label="η=0.4")
a2.plot(range(len(b06)), b06, color="#4E9B47", lw=3, marker="^", ms=9,
markeredgecolor="white", markeredgewidth=1.2, label="η=0.6")
a2.set_title("strong teacher (n=10k, K=4)\nmonotone denoising", fontsize=16)
a2.set_xlabel("iteration t"); a2.set_ylabel("test error (%)")
a2.legend(frameon=False, fontsize=15)
for ax in (a1, a2):
ax.grid(alpha=0.18, linewidth=1.0); ax.tick_params(width=1.6, length=6)
fig2.tight_layout()
fig2.savefig("poster_images/fig_cifar.png", dpi=200, bbox_inches="tight", facecolor="white")
print("wrote poster_images/fig_cifar.png (2-panel)")
|