Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# ================================================================
|
| 2 |
-
# 教育大模型MIA攻防研究 - Gradio演示系统
|
| 3 |
-
#
|
| 4 |
-
#
|
| 5 |
# ================================================================
|
| 6 |
|
| 7 |
import os
|
|
@@ -59,12 +59,11 @@ except FileNotFoundError:
|
|
| 59 |
for s in [0.005, 0.01, 0.015, 0.02, 0.025, 0.03]:
|
| 60 |
k = f"perturbation_{s}"
|
| 61 |
perturb_results[k] = {m: v*0.85 for m, v in mia_results["baseline"].items()}
|
| 62 |
-
# 模拟方差变大
|
| 63 |
perturb_results[k]["member_loss_std"] = np.sqrt(0.03**2 + s**2)
|
| 64 |
perturb_results[k]["non_member_loss_std"] = np.sqrt(0.03**2 + s**2)
|
| 65 |
|
| 66 |
# ================================================================
|
| 67 |
-
# 全局图表配置
|
| 68 |
# ================================================================
|
| 69 |
COLORS = {
|
| 70 |
'bg': '#FFFFFF',
|
|
@@ -81,8 +80,6 @@ COLORS = {
|
|
| 81 |
'ls_colors': ['#A0C4FF', '#70A1FF', '#478EFF', '#007AFF'],
|
| 82 |
'op_colors': ['#98F5E1', '#6EE7B7', '#34D399', '#10B981', '#059669', '#047857'],
|
| 83 |
}
|
| 84 |
-
|
| 85 |
-
# 图表宽度配置 (为了适配双雷达图)
|
| 86 |
CHART_W = 14
|
| 87 |
|
| 88 |
def apply_light_style(fig, ax_or_axes):
|
|
@@ -102,15 +99,15 @@ def apply_light_style(fig, ax_or_axes):
|
|
| 102 |
ax.set_axisbelow(True)
|
| 103 |
|
| 104 |
# ================================================================
|
| 105 |
-
#
|
| 106 |
# ================================================================
|
| 107 |
LS_KEYS = ["baseline", "smooth_eps_0.02", "smooth_eps_0.05", "smooth_eps_0.1", "smooth_eps_0.2"]
|
| 108 |
-
LS_LABELS_PLOT = ["Baseline",
|
| 109 |
LS_LABELS_MD = ["基线(Baseline)", "LS(ε=0.02)", "LS(ε=0.05)", "LS(ε=0.1)", "LS(ε=0.2)"]
|
| 110 |
|
| 111 |
OP_SIGMAS = [0.005, 0.01, 0.015, 0.02, 0.025, 0.03]
|
| 112 |
OP_KEYS = [f"perturbation_{s}" for s in OP_SIGMAS]
|
| 113 |
-
OP_LABELS_PLOT = [
|
| 114 |
OP_LABELS_MD = [f"OP(σ={s})" for s in OP_SIGMAS]
|
| 115 |
|
| 116 |
ALL_KEYS = LS_KEYS + OP_KEYS
|
|
@@ -130,12 +127,8 @@ bl_acc = gu("baseline")
|
|
| 130 |
bl_m_mean = gm("baseline", "member_loss_mean")
|
| 131 |
bl_nm_mean = gm("baseline", "non_member_loss_mean")
|
| 132 |
|
| 133 |
-
TYPE_CN = {'calculation': '基础计算', 'word_problem': '应用题',
|
| 134 |
-
'concept': '概念问答', 'error_correction': '错题订正'}
|
| 135 |
|
| 136 |
-
# ================================================================
|
| 137 |
-
# 效用评估题库
|
| 138 |
-
# ================================================================
|
| 139 |
np.random.seed(777)
|
| 140 |
EVAL_POOL = []
|
| 141 |
_types = ['calculation']*120 + ['word_problem']*90 + ['concept']*60 + ['error_correction']*30
|
|
@@ -149,8 +142,7 @@ for _i in range(300):
|
|
| 149 |
else: _q,_ans=f"{_a} x {_b} = ?",str(_a*_b)
|
| 150 |
elif _t == 'word_problem':
|
| 151 |
_a,_b = int(np.random.randint(5,200)), int(np.random.randint(3,50))
|
| 152 |
-
_tpls = [(f"{_a} apples, ate {_b}, left?",str(_a-_b)),
|
| 153 |
-
(f"{_a} per group, {_b} groups, total?",str(_a*_b))]
|
| 154 |
_q,_ans = _tpls[_i%len(_tpls)]
|
| 155 |
elif _t == 'concept':
|
| 156 |
_cs = [("area","Area = space occupied by a shape"),("perimeter","Perimeter = total boundary length")]
|
|
@@ -165,41 +157,27 @@ for _i in range(300):
|
|
| 165 |
EVAL_POOL.append(item)
|
| 166 |
|
| 167 |
# ================================================================
|
| 168 |
-
# 图表绘制函数
|
| 169 |
# ================================================================
|
| 170 |
def fig_gauge(loss_val, m_mean, nm_mean, thr, m_std, nm_std):
|
| 171 |
-
fig, ax = plt.subplots(figsize=(10, 2.6))
|
| 172 |
-
|
| 173 |
-
ax.
|
| 174 |
-
|
| 175 |
-
xlo = min(m_mean - 3.0 * m_std, loss_val - 0.005)
|
| 176 |
-
xhi = max(nm_mean + 3.0 * nm_std, loss_val + 0.005)
|
| 177 |
-
|
| 178 |
-
ax.axvspan(xlo, thr, alpha=0.2, color=COLORS['accent'])
|
| 179 |
-
ax.axvspan(thr, xhi, alpha=0.2, color=COLORS['danger'])
|
| 180 |
-
|
| 181 |
ax.axvline(m_mean, color=COLORS['accent'], lw=2, ls=':', alpha=0.8, zorder=2)
|
| 182 |
ax.text(m_mean - 0.002, 1.02, f'Member Mean\n{m_mean:.4f}', ha='right', va='bottom', fontsize=9, color=COLORS['accent'], transform=ax.get_xaxis_transform())
|
| 183 |
-
|
| 184 |
ax.axvline(nm_mean, color=COLORS['danger'], lw=2, ls=':', alpha=0.8, zorder=2)
|
| 185 |
ax.text(nm_mean + 0.002, 1.02, f'Non-Member Mean\n{nm_mean:.4f}', ha='left', va='bottom', fontsize=9, color=COLORS['danger'], transform=ax.get_xaxis_transform())
|
| 186 |
-
|
| 187 |
ax.axvline(thr, color=COLORS['text_dim'], lw=2.5, ls='--', zorder=3)
|
| 188 |
ax.text(thr, 1.25, f'Threshold\n{thr:.4f}', ha='center', va='bottom', fontsize=10, fontweight='bold', color=COLORS['text_dim'], transform=ax.get_xaxis_transform())
|
| 189 |
-
|
| 190 |
mc = COLORS['accent'] if loss_val < thr else COLORS['danger']
|
| 191 |
ax.plot(loss_val, 0.5, marker='o', ms=16, color='white', mec=mc, mew=3, zorder=5, transform=ax.get_xaxis_transform())
|
| 192 |
ax.text(loss_val, 0.75, f'Current Loss\n{loss_val:.4f}', ha='center', fontsize=11, fontweight='bold', color=mc, transform=ax.get_xaxis_transform())
|
| 193 |
-
|
| 194 |
ax.text((xlo+thr)/2, 0.25, 'MEMBER', ha='center', fontsize=12, color=COLORS['accent'], alpha=0.6, fontweight='bold', transform=ax.get_xaxis_transform())
|
| 195 |
ax.text((thr+xhi)/2, 0.25, 'NON-MEMBER', ha='center', fontsize=12, color=COLORS['danger'], alpha=0.6, fontweight='bold', transform=ax.get_xaxis_transform())
|
| 196 |
-
|
| 197 |
ax.set_xlim(xlo, xhi); ax.set_yticks([])
|
| 198 |
for s in ax.spines.values(): s.set_visible(False)
|
| 199 |
-
ax.spines['bottom'].set_visible(True); ax.spines['bottom'].set_color(COLORS['grid'])
|
| 200 |
-
ax.
|
| 201 |
-
ax.set_xlabel('Loss Value', fontsize=11, color=COLORS['text'], fontweight='medium')
|
| 202 |
-
plt.tight_layout(pad=0.5)
|
| 203 |
return fig
|
| 204 |
|
| 205 |
def fig_auc_bar():
|
|
@@ -221,66 +199,25 @@ def fig_auc_bar():
|
|
| 221 |
|
| 222 |
def fig_radar():
|
| 223 |
ms = ['AUC', 'Atk Acc', 'Prec', 'Recall', 'F1', 'TPR@5%', 'TPR@1%', 'Gap']
|
| 224 |
-
mk = ['auc', 'attack_accuracy', 'precision', 'recall', 'f1',
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
ag = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist() + [0]
|
| 228 |
-
|
| 229 |
-
fig, axes = plt.subplots(1, 2, figsize=(CHART_W + 2, 7),
|
| 230 |
-
subplot_kw=dict(polar=True))
|
| 231 |
-
fig.patch.set_facecolor('white')
|
| 232 |
-
|
| 233 |
-
# --- 左图: 5个标签平滑模型 (替换LaTeX) ---
|
| 234 |
-
ls_cfgs = [
|
| 235 |
-
("Baseline", "baseline", '#F04438'),
|
| 236 |
-
(r"LS($\epsilon$=0.02)", "smooth_eps_0.02", '#B2DDFF'),
|
| 237 |
-
(r"LS($\epsilon$=0.05)", "smooth_eps_0.05", '#84CAFF'),
|
| 238 |
-
(r"LS($\epsilon$=0.1)", "smooth_eps_0.1", '#2E90FA'),
|
| 239 |
-
(r"LS($\epsilon$=0.2)", "smooth_eps_0.2", '#7A5AF8'),
|
| 240 |
-
]
|
| 241 |
-
|
| 242 |
-
# --- 右图: Baseline + 6个输出扰动 (替换LaTeX) ---
|
| 243 |
-
op_cfgs = [
|
| 244 |
-
("Baseline", "baseline", '#F04438'),
|
| 245 |
-
(r"OP($\sigma$=0.005)", "perturbation_0.005", '#A6F4C5'),
|
| 246 |
-
(r"OP($\sigma$=0.01)", "perturbation_0.01", '#6CE9A6'),
|
| 247 |
-
(r"OP($\sigma$=0.015)", "perturbation_0.015", '#32D583'),
|
| 248 |
-
(r"OP($\sigma$=0.02)", "perturbation_0.02", '#12B76A'),
|
| 249 |
-
(r"OP($\sigma$=0.025)", "perturbation_0.025", '#039855'),
|
| 250 |
-
(r"OP($\sigma$=0.03)", "perturbation_0.03", '#027A48'),
|
| 251 |
-
]
|
| 252 |
-
|
| 253 |
-
for ax_idx, (ax, cfgs, title) in enumerate([
|
| 254 |
-
(axes[0], ls_cfgs, 'Label Smoothing (5 models)'),
|
| 255 |
-
(axes[1], op_cfgs, 'Output Perturbation (7 configs)')
|
| 256 |
-
]):
|
| 257 |
-
ax.set_facecolor('white')
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
mx.append(val_max if val_max > 0 else 1)
|
| 263 |
|
|
|
|
|
|
|
|
|
|
| 264 |
for nm, ky, cl in cfgs:
|
| 265 |
-
v = [gm(ky, m_key) / mx[i] for i, m_key in enumerate(mk)]
|
| 266 |
-
v
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
ax.set_xticks(ag[:-1])
|
| 274 |
-
ax.set_xticklabels(ms, fontsize=9, color=COLORS['text'])
|
| 275 |
-
ax.set_yticklabels([])
|
| 276 |
-
ax.set_title(title, fontsize=11, fontweight='700',
|
| 277 |
-
color=COLORS['text'], pad=18)
|
| 278 |
-
ax.legend(loc='upper right',
|
| 279 |
-
bbox_to_anchor=(1.35 if ax_idx == 1 else 1.30, 1.12),
|
| 280 |
-
fontsize=8, framealpha=0.9, edgecolor=COLORS['grid'])
|
| 281 |
-
ax.spines['polar'].set_color(COLORS['grid'])
|
| 282 |
-
ax.grid(color=COLORS['grid'], alpha=0.5)
|
| 283 |
-
|
| 284 |
plt.tight_layout()
|
| 285 |
return fig
|
| 286 |
|
|
@@ -307,7 +244,7 @@ def fig_perturb_dist():
|
|
| 307 |
ax.hist(mp, bins=bins, alpha=0.6, color=COLORS['accent'], label='Mem+noise', density=True, edgecolor='white')
|
| 308 |
ax.hist(np_, bins=bins, alpha=0.6, color=COLORS['danger'], label='Non+noise', density=True, edgecolor='white')
|
| 309 |
pa = gm(f'perturbation_{s}', 'auc')
|
| 310 |
-
ax.set_title(
|
| 311 |
ax.legend(fontsize=9, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'])
|
| 312 |
plt.tight_layout(); return fig
|
| 313 |
|
|
@@ -327,7 +264,7 @@ def fig_roc_curves():
|
|
| 327 |
fpr, tpr, _ = roc_curve(y_true, y_scores); ax.plot(fpr, tpr, color=COLORS['danger'], lw=2.5, label=f'Baseline (AUC={bl_auc:.4f})')
|
| 328 |
for i, s in enumerate(OP_SIGMAS):
|
| 329 |
rng_m = np.random.RandomState(42); rng_nm = np.random.RandomState(137); mp = ml_base + rng_m.normal(0, s, len(ml_base)); np_ = nl_base + rng_nm.normal(0, s, len(nl_base)); y_scores_p = np.concatenate([-mp, -np_]); fpr_p, tpr_p, _ = roc_curve(y_true, y_scores_p); auc_p = roc_auc_score(y_true, y_scores_p)
|
| 330 |
-
ax.plot(fpr_p, tpr_p, color=COLORS['op_colors'][i], lw=2, label=
|
| 331 |
ax.plot([0,1], [0,1], '--', color=COLORS['text_dim'], lw=1.5, label='Random'); ax.set_xlabel('False Positive Rate', fontsize=12, fontweight='medium'); ax.set_ylabel('True Positive Rate', fontsize=12, fontweight='medium'); ax.set_title('ROC Curves: Output Perturbation', fontsize=14, fontweight='bold', pad=15); ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'], loc='lower right'); plt.tight_layout()
|
| 332 |
return fig
|
| 333 |
|
|
@@ -367,9 +304,9 @@ def fig_tradeoff():
|
|
| 367 |
def fig_auc_trend():
|
| 368 |
fig, axes = plt.subplots(1, 2, figsize=(16, 6.5)); apply_light_style(fig, axes); ax = axes[0]; eps_vals = [0.0, 0.02, 0.05, 0.1, 0.2]; auc_vals = [gm(k, 'auc') for k in LS_KEYS]; acc_vals = [gu(k) for k in LS_KEYS]
|
| 369 |
ax2 = ax.twinx(); line1 = ax.plot(eps_vals, auc_vals, 'o-', color=COLORS['danger'], lw=3, ms=9, label='MIA AUC (left)', zorder=5); line2 = ax2.plot(eps_vals, acc_vals, 's--', color=COLORS['accent'], lw=3, ms=9, label='Utility % (right)', zorder=5); ax.axhline(0.5, color=COLORS['text_dim'], ls=':', alpha=0.5)
|
| 370 |
-
ax.set_xlabel(
|
| 371 |
ax = axes[1]; sig_vals = OP_SIGMAS; auc_op = [gm(k, 'auc') for k in OP_KEYS]; ax.plot(sig_vals, auc_op, 'o-', color=COLORS['success'], lw=3, ms=9, zorder=5, label='MIA AUC'); ax.axhline(bl_auc, color=COLORS['danger'], ls='--', lw=2, alpha=0.6, label=f'Baseline ({bl_auc:.4f})'); ax.axhline(0.5, color=COLORS['text_dim'], ls=':', alpha=0.5, label='Random (0.5)'); ax.fill_between(sig_vals, auc_op, bl_auc, alpha=0.2, color=COLORS['success'], label='AUC Reduction')
|
| 372 |
-
ax.set_xlabel(
|
| 373 |
return fig
|
| 374 |
|
| 375 |
def fig_loss_gap_waterfall():
|
|
@@ -403,7 +340,6 @@ def cb_sample(src):
|
|
| 403 |
"""
|
| 404 |
return md, clean_text(s.get('question', '')), clean_text(s.get('answer', ''))
|
| 405 |
|
| 406 |
-
# 🌟 下拉框选项也全部替换为 ε 和 σ
|
| 407 |
ATK_CHOICES = (
|
| 408 |
["基线模型 (Baseline)"] +
|
| 409 |
[f"标签平滑 (ε={e})" for e in [0.02, 0.05, 0.1, 0.2]] +
|
|
@@ -786,16 +722,19 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 786 |
|
| 787 |
---
|
| 788 |
"""
|
|
|
|
| 789 |
for sigma in OP_SIGMAS:
|
| 790 |
k = f"perturbation_{sigma}"
|
| 791 |
detail_md += f"""\
|
| 792 |
### 输出扰动 OP(σ={sigma})
|
| 793 |
| 指标 | 值 | vs基线 | 变化 |
|
| 794 |
|---|---|---|---|
|
| 795 |
-
| AUC | {gm(k,'auc'):.4f} | {bl_auc:.4f} | {gm(k,'auc')-bl_auc:+.4f} |
|
| 796 |
| 攻击准确率 | {gm(k,'attack_accuracy'):.4f} | {gm('baseline','attack_accuracy'):.4f} | {gm(k,'attack_accuracy')-gm('baseline','attack_accuracy'):+.4f} |
|
| 797 |
| F1 | {gm(k,'f1'):.4f} | {gm('baseline','f1'):.4f} | {gm(k,'f1')-gm('baseline','f1'):+.4f} |
|
| 798 |
| TPR@5%FPR | {gm(k,'tpr_at_5fpr'):.4f} | {gm('baseline','tpr_at_5fpr'):.4f} | {gm(k,'tpr_at_5fpr')-gm('baseline','tpr_at_5fpr'):+.4f} |
|
|
|
|
|
|
|
| 799 |
| 效用 | {bl_acc:.1f}% | {bl_acc:.1f}% | 0.0% (零损失) |
|
| 800 |
|
| 801 |
---
|
|
@@ -851,7 +790,6 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 851 |
| σ=0.025 | {gm('perturbation_0.025','auc'):.4f} | {bl_auc-gm('perturbation_0.025','auc'):.4f} | {bl_acc:.1f}% |
|
| 852 |
| σ=0.03 | {gm('perturbation_0.03','auc'):.4f} | {bl_auc-gm('perturbation_0.03','auc'):.4f} | {bl_acc:.1f}% |
|
| 853 |
|
| 854 |
-
|
| 855 |
### 结论四:最佳实践建议
|
| 856 |
|
| 857 |
> **推荐组合方案: LS(ε=0.1) + OP(σ=0.02)**
|
|
|
|
| 1 |
# ================================================================
|
| 2 |
+
# 教育大模型MIA攻防研究 - Gradio演示系统 v8.0 无死角终极版
|
| 3 |
+
# 1. 修复:彻底弃用容易渲染失败的 LaTeX,全面采用原生 Unicode ε 和 σ
|
| 4 |
+
# 2. 修复:补齐“输出扰动”在详细分析表格中丢失的 TPR@1%FPR 和 Loss差距
|
| 5 |
# ================================================================
|
| 6 |
|
| 7 |
import os
|
|
|
|
| 59 |
for s in [0.005, 0.01, 0.015, 0.02, 0.025, 0.03]:
|
| 60 |
k = f"perturbation_{s}"
|
| 61 |
perturb_results[k] = {m: v*0.85 for m, v in mia_results["baseline"].items()}
|
|
|
|
| 62 |
perturb_results[k]["member_loss_std"] = np.sqrt(0.03**2 + s**2)
|
| 63 |
perturb_results[k]["non_member_loss_std"] = np.sqrt(0.03**2 + s**2)
|
| 64 |
|
| 65 |
# ================================================================
|
| 66 |
+
# 全局图表配置
|
| 67 |
# ================================================================
|
| 68 |
COLORS = {
|
| 69 |
'bg': '#FFFFFF',
|
|
|
|
| 80 |
'ls_colors': ['#A0C4FF', '#70A1FF', '#478EFF', '#007AFF'],
|
| 81 |
'op_colors': ['#98F5E1', '#6EE7B7', '#34D399', '#10B981', '#059669', '#047857'],
|
| 82 |
}
|
|
|
|
|
|
|
| 83 |
CHART_W = 14
|
| 84 |
|
| 85 |
def apply_light_style(fig, ax_or_axes):
|
|
|
|
| 99 |
ax.set_axisbelow(True)
|
| 100 |
|
| 101 |
# ================================================================
|
| 102 |
+
# 核心修复 1:使用标准 Unicode ε 和 σ,告别乱码
|
| 103 |
# ================================================================
|
| 104 |
LS_KEYS = ["baseline", "smooth_eps_0.02", "smooth_eps_0.05", "smooth_eps_0.1", "smooth_eps_0.2"]
|
| 105 |
+
LS_LABELS_PLOT = ["Baseline", "LS(ε=0.02)", "LS(ε=0.05)", "LS(ε=0.1)", "LS(ε=0.2)"]
|
| 106 |
LS_LABELS_MD = ["基线(Baseline)", "LS(ε=0.02)", "LS(ε=0.05)", "LS(ε=0.1)", "LS(ε=0.2)"]
|
| 107 |
|
| 108 |
OP_SIGMAS = [0.005, 0.01, 0.015, 0.02, 0.025, 0.03]
|
| 109 |
OP_KEYS = [f"perturbation_{s}" for s in OP_SIGMAS]
|
| 110 |
+
OP_LABELS_PLOT = [f"OP(σ={s})" for s in OP_SIGMAS]
|
| 111 |
OP_LABELS_MD = [f"OP(σ={s})" for s in OP_SIGMAS]
|
| 112 |
|
| 113 |
ALL_KEYS = LS_KEYS + OP_KEYS
|
|
|
|
| 127 |
bl_m_mean = gm("baseline", "member_loss_mean")
|
| 128 |
bl_nm_mean = gm("baseline", "non_member_loss_mean")
|
| 129 |
|
| 130 |
+
TYPE_CN = {'calculation': '基础计算', 'word_problem': '应用题', 'concept': '概念问答', 'error_correction': '错题订正'}
|
|
|
|
| 131 |
|
|
|
|
|
|
|
|
|
|
| 132 |
np.random.seed(777)
|
| 133 |
EVAL_POOL = []
|
| 134 |
_types = ['calculation']*120 + ['word_problem']*90 + ['concept']*60 + ['error_correction']*30
|
|
|
|
| 142 |
else: _q,_ans=f"{_a} x {_b} = ?",str(_a*_b)
|
| 143 |
elif _t == 'word_problem':
|
| 144 |
_a,_b = int(np.random.randint(5,200)), int(np.random.randint(3,50))
|
| 145 |
+
_tpls = [(f"{_a} apples, ate {_b}, left?",str(_a-_b)), (f"{_a} per group, {_b} groups, total?",str(_a*_b))]
|
|
|
|
| 146 |
_q,_ans = _tpls[_i%len(_tpls)]
|
| 147 |
elif _t == 'concept':
|
| 148 |
_cs = [("area","Area = space occupied by a shape"),("perimeter","Perimeter = total boundary length")]
|
|
|
|
| 157 |
EVAL_POOL.append(item)
|
| 158 |
|
| 159 |
# ================================================================
|
| 160 |
+
# 图表绘制函数
|
| 161 |
# ================================================================
|
| 162 |
def fig_gauge(loss_val, m_mean, nm_mean, thr, m_std, nm_std):
|
| 163 |
+
fig, ax = plt.subplots(figsize=(10, 2.6)); fig.patch.set_facecolor(COLORS['bg']); ax.set_facecolor(COLORS['panel'])
|
| 164 |
+
xlo = min(m_mean - 3.0 * m_std, loss_val - 0.005); xhi = max(nm_mean + 3.0 * nm_std, loss_val + 0.005)
|
| 165 |
+
ax.axvspan(xlo, thr, alpha=0.2, color=COLORS['accent']); ax.axvspan(thr, xhi, alpha=0.2, color=COLORS['danger'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
ax.axvline(m_mean, color=COLORS['accent'], lw=2, ls=':', alpha=0.8, zorder=2)
|
| 167 |
ax.text(m_mean - 0.002, 1.02, f'Member Mean\n{m_mean:.4f}', ha='right', va='bottom', fontsize=9, color=COLORS['accent'], transform=ax.get_xaxis_transform())
|
|
|
|
| 168 |
ax.axvline(nm_mean, color=COLORS['danger'], lw=2, ls=':', alpha=0.8, zorder=2)
|
| 169 |
ax.text(nm_mean + 0.002, 1.02, f'Non-Member Mean\n{nm_mean:.4f}', ha='left', va='bottom', fontsize=9, color=COLORS['danger'], transform=ax.get_xaxis_transform())
|
|
|
|
| 170 |
ax.axvline(thr, color=COLORS['text_dim'], lw=2.5, ls='--', zorder=3)
|
| 171 |
ax.text(thr, 1.25, f'Threshold\n{thr:.4f}', ha='center', va='bottom', fontsize=10, fontweight='bold', color=COLORS['text_dim'], transform=ax.get_xaxis_transform())
|
|
|
|
| 172 |
mc = COLORS['accent'] if loss_val < thr else COLORS['danger']
|
| 173 |
ax.plot(loss_val, 0.5, marker='o', ms=16, color='white', mec=mc, mew=3, zorder=5, transform=ax.get_xaxis_transform())
|
| 174 |
ax.text(loss_val, 0.75, f'Current Loss\n{loss_val:.4f}', ha='center', fontsize=11, fontweight='bold', color=mc, transform=ax.get_xaxis_transform())
|
|
|
|
| 175 |
ax.text((xlo+thr)/2, 0.25, 'MEMBER', ha='center', fontsize=12, color=COLORS['accent'], alpha=0.6, fontweight='bold', transform=ax.get_xaxis_transform())
|
| 176 |
ax.text((thr+xhi)/2, 0.25, 'NON-MEMBER', ha='center', fontsize=12, color=COLORS['danger'], alpha=0.6, fontweight='bold', transform=ax.get_xaxis_transform())
|
|
|
|
| 177 |
ax.set_xlim(xlo, xhi); ax.set_yticks([])
|
| 178 |
for s in ax.spines.values(): s.set_visible(False)
|
| 179 |
+
ax.spines['bottom'].set_visible(True); ax.spines['bottom'].set_color(COLORS['grid']); ax.tick_params(colors=COLORS['text_dim'], width=1)
|
| 180 |
+
ax.set_xlabel('Loss Value', fontsize=11, color=COLORS['text'], fontweight='medium'); plt.tight_layout(pad=0.5)
|
|
|
|
|
|
|
| 181 |
return fig
|
| 182 |
|
| 183 |
def fig_auc_bar():
|
|
|
|
| 199 |
|
| 200 |
def fig_radar():
|
| 201 |
ms = ['AUC', 'Atk Acc', 'Prec', 'Recall', 'F1', 'TPR@5%', 'TPR@1%', 'Gap']
|
| 202 |
+
mk = ['auc', 'attack_accuracy', 'precision', 'recall', 'f1', 'tpr_at_5fpr', 'tpr_at_1fpr', 'loss_gap']
|
| 203 |
+
N = len(ms); ag = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist() + [0]
|
| 204 |
+
fig, axes = plt.subplots(1, 2, figsize=(CHART_W + 2, 7), subplot_kw=dict(polar=True)); fig.patch.set_facecolor('white')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
+
# 完全使用 Unicode 希腊字母
|
| 207 |
+
ls_cfgs = [("Baseline", "baseline", '#F04438'), ("LS(ε=0.02)", "smooth_eps_0.02", '#B2DDFF'), ("LS(ε=0.05)", "smooth_eps_0.05", '#84CAFF'), ("LS(ε=0.1)", "smooth_eps_0.1", '#2E90FA'), ("LS(ε=0.2)", "smooth_eps_0.2", '#7A5AF8')]
|
| 208 |
+
op_cfgs = [("Baseline", "baseline", '#F04438'), ("OP(σ=0.005)", "perturbation_0.005", '#A6F4C5'), ("OP(σ=0.01)", "perturbation_0.01", '#6CE9A6'), ("OP(σ=0.015)", "perturbation_0.015", '#32D583'), ("OP(σ=0.02)", "perturbation_0.02", '#12B76A'), ("OP(σ=0.025)", "perturbation_0.025", '#039855'), ("OP(σ=0.03)", "perturbation_0.03", '#027A48')]
|
|
|
|
| 209 |
|
| 210 |
+
for ax_idx, (ax, cfgs, title) in enumerate([(axes[0], ls_cfgs, 'Label Smoothing (5 models)'), (axes[1], op_cfgs, 'Output Perturbation (7 configs)')]):
|
| 211 |
+
ax.set_facecolor('white')
|
| 212 |
+
mx = [max(gm(k, m_key) for _, k, _ in cfgs) for m_key in mk]; mx = [m if m > 0 else 1 for m in mx]
|
| 213 |
for nm, ky, cl in cfgs:
|
| 214 |
+
v = [gm(ky, m_key) / mx[i] for i, m_key in enumerate(mk)]; v += [v[0]]
|
| 215 |
+
ax.plot(ag, v, 'o-', lw=2.8 if ky == 'baseline' else 1.8, label=nm, color=cl, ms=5, alpha=0.95 if ky == 'baseline' else 0.85)
|
| 216 |
+
ax.fill(ag, v, alpha=0.10 if ky == 'baseline' else 0.04, color=cl)
|
| 217 |
+
ax.set_xticks(ag[:-1]); ax.set_xticklabels(ms, fontsize=10, color=COLORS['text']); ax.set_yticklabels([])
|
| 218 |
+
ax.set_title(title, fontsize=12, fontweight='700', color=COLORS['text'], pad=18)
|
| 219 |
+
ax.legend(loc='upper right', bbox_to_anchor=(1.35 if ax_idx == 1 else 1.30, 1.12), fontsize=9, framealpha=0.9, edgecolor=COLORS['grid'])
|
| 220 |
+
ax.spines['polar'].set_color(COLORS['grid']); ax.grid(color=COLORS['grid'], alpha=0.5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
plt.tight_layout()
|
| 222 |
return fig
|
| 223 |
|
|
|
|
| 244 |
ax.hist(mp, bins=bins, alpha=0.6, color=COLORS['accent'], label='Mem+noise', density=True, edgecolor='white')
|
| 245 |
ax.hist(np_, bins=bins, alpha=0.6, color=COLORS['danger'], label='Non+noise', density=True, edgecolor='white')
|
| 246 |
pa = gm(f'perturbation_{s}', 'auc')
|
| 247 |
+
ax.set_title(f'OP(σ={s})\nAUC={pa:.4f}', fontsize=11, fontweight='semibold'); ax.set_xlabel('Loss', fontsize=10)
|
| 248 |
ax.legend(fontsize=9, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'])
|
| 249 |
plt.tight_layout(); return fig
|
| 250 |
|
|
|
|
| 264 |
fpr, tpr, _ = roc_curve(y_true, y_scores); ax.plot(fpr, tpr, color=COLORS['danger'], lw=2.5, label=f'Baseline (AUC={bl_auc:.4f})')
|
| 265 |
for i, s in enumerate(OP_SIGMAS):
|
| 266 |
rng_m = np.random.RandomState(42); rng_nm = np.random.RandomState(137); mp = ml_base + rng_m.normal(0, s, len(ml_base)); np_ = nl_base + rng_nm.normal(0, s, len(nl_base)); y_scores_p = np.concatenate([-mp, -np_]); fpr_p, tpr_p, _ = roc_curve(y_true, y_scores_p); auc_p = roc_auc_score(y_true, y_scores_p)
|
| 267 |
+
ax.plot(fpr_p, tpr_p, color=COLORS['op_colors'][i], lw=2, label=f'OP(σ={s}) (AUC={auc_p:.4f})')
|
| 268 |
ax.plot([0,1], [0,1], '--', color=COLORS['text_dim'], lw=1.5, label='Random'); ax.set_xlabel('False Positive Rate', fontsize=12, fontweight='medium'); ax.set_ylabel('True Positive Rate', fontsize=12, fontweight='medium'); ax.set_title('ROC Curves: Output Perturbation', fontsize=14, fontweight='bold', pad=15); ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'], loc='lower right'); plt.tight_layout()
|
| 269 |
return fig
|
| 270 |
|
|
|
|
| 304 |
def fig_auc_trend():
|
| 305 |
fig, axes = plt.subplots(1, 2, figsize=(16, 6.5)); apply_light_style(fig, axes); ax = axes[0]; eps_vals = [0.0, 0.02, 0.05, 0.1, 0.2]; auc_vals = [gm(k, 'auc') for k in LS_KEYS]; acc_vals = [gu(k) for k in LS_KEYS]
|
| 306 |
ax2 = ax.twinx(); line1 = ax.plot(eps_vals, auc_vals, 'o-', color=COLORS['danger'], lw=3, ms=9, label='MIA AUC (left)', zorder=5); line2 = ax2.plot(eps_vals, acc_vals, 's--', color=COLORS['accent'], lw=3, ms=9, label='Utility % (right)', zorder=5); ax.axhline(0.5, color=COLORS['text_dim'], ls=':', alpha=0.5)
|
| 307 |
+
ax.set_xlabel('Label Smoothing ε', fontsize=12, fontweight='medium'); ax.set_ylabel('MIA AUC', fontsize=12, fontweight='medium', color=COLORS['danger']); ax2.set_ylabel('Utility (%)', fontsize=12, fontweight='medium', color=COLORS['accent']); ax.set_title('Label Smoothing Trends', fontsize=14, fontweight='bold', pad=15); ax.tick_params(axis='y', labelcolor=COLORS['danger']); ax2.tick_params(axis='y', labelcolor=COLORS['accent']); ax2.spines['right'].set_color(COLORS['accent']); ax2.spines['left'].set_color(COLORS['danger']); lines = line1 + line2; labels = [l.get_label() for l in lines]; ax.legend(lines, labels, fontsize=10, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'])
|
| 308 |
ax = axes[1]; sig_vals = OP_SIGMAS; auc_op = [gm(k, 'auc') for k in OP_KEYS]; ax.plot(sig_vals, auc_op, 'o-', color=COLORS['success'], lw=3, ms=9, zorder=5, label='MIA AUC'); ax.axhline(bl_auc, color=COLORS['danger'], ls='--', lw=2, alpha=0.6, label=f'Baseline ({bl_auc:.4f})'); ax.axhline(0.5, color=COLORS['text_dim'], ls=':', alpha=0.5, label='Random (0.5)'); ax.fill_between(sig_vals, auc_op, bl_auc, alpha=0.2, color=COLORS['success'], label='AUC Reduction')
|
| 309 |
+
ax.set_xlabel('Perturbation σ', fontsize=12, fontweight='medium'); ax.set_ylabel('MIA AUC', fontsize=12, fontweight='medium'); ax.set_title('Output Perturbation Trends', fontsize=14, fontweight='bold', pad=15); ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text']); plt.tight_layout()
|
| 310 |
return fig
|
| 311 |
|
| 312 |
def fig_loss_gap_waterfall():
|
|
|
|
| 340 |
"""
|
| 341 |
return md, clean_text(s.get('question', '')), clean_text(s.get('answer', ''))
|
| 342 |
|
|
|
|
| 343 |
ATK_CHOICES = (
|
| 344 |
["基线模型 (Baseline)"] +
|
| 345 |
[f"标签平滑 (ε={e})" for e in [0.02, 0.05, 0.1, 0.2]] +
|
|
|
|
| 722 |
|
| 723 |
---
|
| 724 |
"""
|
| 725 |
+
# 🌟🌟🌟 这里就是修复缺失表格行的核心位置! 🌟🌟🌟
|
| 726 |
for sigma in OP_SIGMAS:
|
| 727 |
k = f"perturbation_{sigma}"
|
| 728 |
detail_md += f"""\
|
| 729 |
### 输出扰动 OP(σ={sigma})
|
| 730 |
| 指标 | 值 | vs基线 | 变化 |
|
| 731 |
|---|---|---|---|
|
| 732 |
+
| AUC | {gm(k,'auc'):.4f} | {bl_auc:.4f} | {gm(k,'auc')-bl_auc:+.4f} ({(gm(k,'auc')-bl_auc)/bl_auc*100:+.1f}%) |
|
| 733 |
| 攻击准确率 | {gm(k,'attack_accuracy'):.4f} | {gm('baseline','attack_accuracy'):.4f} | {gm(k,'attack_accuracy')-gm('baseline','attack_accuracy'):+.4f} |
|
| 734 |
| F1 | {gm(k,'f1'):.4f} | {gm('baseline','f1'):.4f} | {gm(k,'f1')-gm('baseline','f1'):+.4f} |
|
| 735 |
| TPR@5%FPR | {gm(k,'tpr_at_5fpr'):.4f} | {gm('baseline','tpr_at_5fpr'):.4f} | {gm(k,'tpr_at_5fpr')-gm('baseline','tpr_at_5fpr'):+.4f} |
|
| 736 |
+
| TPR@1%FPR | {gm(k,'tpr_at_1fpr'):.4f} | {gm('baseline','tpr_at_1fpr'):.4f} | {gm(k,'tpr_at_1fpr')-gm('baseline','tpr_at_1fpr'):+.4f} |
|
| 737 |
+
| Loss差距 | {gm(k,'loss_gap'):.4f} | {gm('baseline','loss_gap'):.4f} | {gm(k,'loss_gap')-gm('baseline','loss_gap'):+.4f} |
|
| 738 |
| 效用 | {bl_acc:.1f}% | {bl_acc:.1f}% | 0.0% (零损失) |
|
| 739 |
|
| 740 |
---
|
|
|
|
| 790 |
| σ=0.025 | {gm('perturbation_0.025','auc'):.4f} | {bl_auc-gm('perturbation_0.025','auc'):.4f} | {bl_acc:.1f}% |
|
| 791 |
| σ=0.03 | {gm('perturbation_0.03','auc'):.4f} | {bl_auc-gm('perturbation_0.03','auc'):.4f} | {bl_acc:.1f}% |
|
| 792 |
|
|
|
|
| 793 |
### 结论四:最佳实践建议
|
| 794 |
|
| 795 |
> **推荐组合方案: LS(ε=0.1) + OP(σ=0.02)**
|