1 import numpy as np
   2 import matplotlib.pyplot as plt
   3 from matplotlib.patches import Rectangle
   4 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   5 setup_style()
   6 
   7 params = {
   8     'Learning Rate': ([1e-4, 3e-4, 1e-3, 3e-3, 1e-2],
   9                       [88.2, 91.5, 92.3, 90.1, 85.4],
  10                       [86.0, 89.8, 90.5, 87.3, 82.1],
  11                       [90.1, 93.0, 94.0, 92.5, 88.2]),
  12     'Hidden Dim': ([64, 128, 256, 512, 1024],
  13                    [87.5, 90.2, 92.3, 92.1, 91.8],
  14                    [85.2, 88.0, 90.1, 89.8, 89.5],
  15                    [89.5, 92.0, 94.2, 94.0, 93.8]),
  16     'Num Layers': ([2, 4, 6, 8, 12],
  17                    [86.3, 89.8, 92.3, 91.5, 90.2],
  18                    [83.8, 87.5, 90.0, 89.2, 87.8],
  19                    [88.5, 91.8, 94.5, 93.5, 92.2]),
  20     'Dropout': ([0.0, 0.1, 0.2, 0.3, 0.5],
  21                 [89.1, 91.2, 92.3, 91.8, 88.5],
  22                 [86.8, 89.0, 90.1, 89.5, 86.0],
  23                 [91.0, 93.2, 94.2, 93.8, 90.5]),
  24 }
  25 
  26 fig, axes = plt.subplots(2, 2, figsize=(5.0, 4.9))   # 示例尺寸;保留模板比例,按实际显示尺寸检查字号与布局
  27 for idx, (name, (x_vals, y_mean, y_min, y_max)) in enumerate(params.items()):
  28     ax = axes.flat[idx]
  29     x_pos = range(len(x_vals))
  30 
  31     # Gradient fill between min and max
  32     for layer, alpha in enumerate([0.20, 0.12, 0.06]):
  33         shrink = layer * 0.3
  34         ax.fill_between(x_pos,
  35                         np.array(y_min) + shrink,
  36                         np.array(y_max) - shrink,
  37                         alpha=alpha, color=PALETTE[idx], linewidth=0)
  38 
  39     # Min/max boundary lines
  40     ax.plot(x_pos, y_min, '--', color=PALETTE[idx], linewidth=0.8, alpha=0.4)
  41     ax.plot(x_pos, y_max, '--', color=PALETTE[idx], linewidth=0.8, alpha=0.4)
  42 
  43     # Mean line
  44     ax.plot(x_pos, y_mean, 'o-', color=PALETTE[idx], linewidth=2, markersize=7,
  45             markeredgecolor='white', markeredgewidth=1.2)
  46 
  47     # Optimal point highlight
  48     best_i = np.argmax(y_mean)
  49     ax.scatter(best_i, y_mean[best_i], s=150, color=PALETTE[idx], zorder=5,
  50                edgecolor='white', linewidth=2.5)
  51 
  52     # Optimal region shading (±1 of best)
  53     opt_left = max(0, best_i - 1)
  54     opt_right = min(len(x_vals) - 1, best_i + 1)
  55     ax.axvspan(opt_left - 0.3, opt_right + 0.3, alpha=0.08, color=COLORS['up'],
  56                label='Optimal region')
  57 
  58     ax.annotate(f'{y_mean[best_i]:.1f}', xy=(best_i, y_mean[best_i]),
  59                 xytext=(0, 12), textcoords='offset points', fontsize=9, fontweight='bold',
  60                 ha='center', color=PALETTE[idx],
  61                 bbox=dict(boxstyle='round,pad=0.2', facecolor='white',
  62                           edgecolor=PALETTE[idx], alpha=0.9, linewidth=0.5))
  63 
  64     ax.set_xticks(x_pos)
  65     ax.set_xticklabels([str(v) for v in x_vals], fontsize=8)
  66     ax.set_xlabel(name, fontsize=10)
  67     ax.set_ylabel('Accuracy (%)', fontsize=10)
  68     ax.spines['top'].set_visible(False)
  69     ax.spines['right'].set_visible(False)
  70     ax.grid(alpha=0.15, linestyle='--')
  71     ax.set_title(f'({chr(97 + idx)})', fontsize=12, fontweight='bold', loc='left', pad=3)
  72 
  73 fig.tight_layout(pad=0.5)
  74 save_fig(fig, 'figures/fig_hyperparam.pdf')