1 import numpy as np 2 import matplotlib.pyplot as plt 3 from mpl_toolkits.axes_grid1.inset_locator import inset_axes 4 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten 5 setup_style() 6 7 np.random.seed(42) 8 epochs = np.arange(1, 101) 9 train_loss = 2.5 * np.exp(-0.04 * epochs) + 0.15 + np.random.normal(0, 0.02, 100) 10 val_loss = 2.5 * np.exp(-0.035 * epochs) + 0.25 + np.random.normal(0, 0.03, 100) 11 train_acc = (1 - train_loss / 3.0 + np.random.normal(0, 0.005, 100)) * 100 12 val_acc = (1 - val_loss / 3.0 + np.random.normal(0, 0.008, 100)) * 100 13 14 # LR schedule (cosine decay) 15 lr = 1e-3 * (0.5 * (1 + np.cos(np.pi * epochs / 100))) 16 17 fig, ax1 = plt.subplots(figsize=(8, 5)) 18 ax2 = ax1.twinx() 19 20 # Loss curves with gradient fill 21 l1, = ax1.plot(epochs, train_loss, color=PALETTE[0], linewidth=1.8, label='Train Loss') 22 l2, = ax1.plot(epochs, val_loss, color=PALETTE[0], linewidth=1.8, linestyle='--', 23 label='Val Loss', alpha=0.7) 24 # Gradient fill under train loss 25 for layer, alpha in enumerate([0.15, 0.08, 0.03]): 26 ax1.fill_between(epochs, train_loss.min() - 0.1 + layer * 0.05, 27 train_loss - layer * 0.03, 28 alpha=alpha, color=PALETTE[0], linewidth=0) 29 30 # Accuracy curves 31 l3, = ax2.plot(epochs, train_acc, color=PALETTE[1], linewidth=1.8, label='Train Acc') 32 l4, = ax2.plot(epochs, val_acc, color=PALETTE[1], linewidth=1.8, linestyle='--', 33 label='Val Acc', alpha=0.7) 34 35 # Best epoch (min val loss) — star marker 36 best_epoch = np.argmin(val_loss) + 1 37 ax1.scatter(best_epoch, val_loss[best_epoch - 1], marker='*', s=200, color=COLORS['down'], 38 edgecolor='white', linewidth=1, zorder=6) 39 ax1.annotate(f'Best: epoch {best_epoch}\nloss={val_loss[best_epoch - 1]:.3f}', 40 xy=(best_epoch, val_loss[best_epoch - 1]), 41 xytext=(best_epoch + 15, val_loss[best_epoch - 1] + 0.3), 42 fontsize=8, fontweight='bold', color=COLORS['down'], 43 arrowprops=dict(arrowstyle='->', color=COLORS['down'], lw=1.2), 44 bbox=dict(boxstyle='round,pad=0.3', facecolor='white', 45 edgecolor=COLORS['down'], alpha=0.9)) 46 47 # Early stopping line 48 early_stop = best_epoch + 10 49 ax1.axvline(x=early_stop, color=COLORS['ref_line'], linestyle=':', linewidth=1, alpha=0.6) 50 ax1.text(early_stop + 1, ax1.get_ylim()[1] * 0.9, 'Early\nStopping', 51 fontsize=7.5, color=COLORS['ref_line'], style='italic') 52 53 # LR schedule inset 54 ax_inset = inset_axes(ax1, width='30%', height='25%', loc='center right', 55 borderpad=2) 56 ax_inset.plot(epochs, lr * 1000, color=PALETTE[2], linewidth=1.2) 57 ax_inset.set_xlabel('Epoch', fontsize=6) 58 ax_inset.set_ylabel('LR (×10⁻³)', fontsize=6) 59 ax_inset.tick_params(labelsize=5) 60 ax_inset.set_title('LR Schedule', fontsize=7, fontweight='bold') 61 ax_inset.grid(alpha=0.15, linestyle='--') 62 for spine in ax_inset.spines.values(): 63 spine.set_linewidth(0.5) 64 65 ax1.set_xlabel('Epoch', fontsize=11) 66 ax1.set_ylabel('Loss', fontsize=11, color=PALETTE[0]) 67 ax2.set_ylabel('Accuracy (%)', fontsize=11, color=PALETTE[1]) 68 ax1.tick_params(axis='y', labelcolor=PALETTE[0]) 69 ax2.tick_params(axis='y', labelcolor=PALETTE[1]) 70 71 lines = [l1, l2, l3, l4] 72 ax1.legend(lines, [l.get_label() for l in lines], frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=9, loc='center left') 73 ax1.grid(alpha=0.15, linestyle='--') 74 ax1.spines['top'].set_visible(False) 75 fig.tight_layout() 76 save_fig(fig, 'figures/fig_training_curve.pdf')