- eval_sota_baselines_v2.py: optimized eval for WildGuard & ShieldGemma-2B
* ChineseTranslator: Helsinki-NLP/opus-mt-zh-en (local, no API)
* ShieldGemma: +4 companion-specific safety policies (crisis non-response,
dependency reinforcement, isolation reinforcement, minor intimacy)
* WildGuard: companion context injected into prompt + extended keyword parsing
* Default threshold lowered 0.5 → 0.3 for better recall
* Translation cache saved to experiments/translation_cache.json (reusable)
- tools/run_sota_v2.sh: one-command runner for both models on server
- paper/05_moduleB.tex: add †-adapted rows to SOTA table + updated discussion
explaining root causes (language barrier + taxonomy gap) and adaptation results
- paper/07_experiments.tex: update baseline description to include v2 adapted variants
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
234 lines
9.6 KiB
TeX
234 lines
9.6 KiB
TeX
% ============================================================
|
||
\section{Module B:上下文感知风险检测器}
|
||
\label{sec:moduleB}
|
||
% ============================================================
|
||
|
||
\subsection{问题建模}
|
||
|
||
给定输入$X = (P, H, u_t, r_t)$,
|
||
其中$P$为AI角色设定(Persona),$H$为多轮对话历史,
|
||
$u_t$为当前用户输入,$r_t$为待检测的AI当前回复,
|
||
Module B的任务是输出检测结果$D = (y_\text{risk}, l_\text{risk}, c_\text{primary}, c_\text{fine})$。
|
||
|
||
与仅使用$r_t$的单回复检测不同,本模块显式建模
|
||
角色设定与对话历史对风险判断的影响,
|
||
解决"同一句话在不同上下文中风险等级截然不同"的核心难题。
|
||
|
||
\subsection{模型架构}
|
||
|
||
图\ref{fig:moduleB_arch}展示了Module B的整体架构,
|
||
由三部分组成:编码层、跨注意力融合层和四分类头。
|
||
|
||
\begin{figure}[ht]
|
||
\centering
|
||
\placeholder{[图:Module B架构示意图,待插入]}
|
||
\caption{Module B:上下文感知风险检测器架构}
|
||
\label{fig:moduleB_arch}
|
||
\end{figure}
|
||
|
||
\subsubsection{编码层}
|
||
|
||
采用\texttt{hfl/chinese-macbert-large}
|
||
(MacBERT-Large,1,024维隐藏状态,24层Transformer)
|
||
作为主干编码器。
|
||
MacBERT针对中文的MLM预训练目标进行了改进,
|
||
在中文理解任务上优于标准BERT-Large。
|
||
|
||
对三路输入分别编码:
|
||
\begin{align}
|
||
e_{r_t} &= \text{MacBERT}(r_t) \in \mathbb{R}^{L_r \times 1024} \\
|
||
e_H &= \text{MacBERT}(\text{concat}(u_1,r_1,...,u_t)) \in \mathbb{R}^{L_H \times 1024} \\
|
||
e_P &= \text{MacBERT}(P) \in \mathbb{R}^{L_P \times 1024}
|
||
\end{align}
|
||
|
||
对历史和角色序列分别进行平均池化得到上下文向量:
|
||
$e_{H_\text{pool}} = \text{AvgPool}(e_H) \in \mathbb{R}^{1024}$,
|
||
$e_{P_\text{pool}} = \text{AvgPool}(e_P) \in \mathbb{R}^{1024}$。
|
||
|
||
\subsubsection{跨注意力融合层}
|
||
|
||
以AI回复表示$e_{r_t}$为Query,
|
||
拼接后的上下文表示$[e_H; e_P]$为Key和Value,
|
||
通过跨注意力机制计算上下文感知的回复表示:
|
||
|
||
\begin{equation}
|
||
e_\text{fused} = \text{CrossAttn}(Q=e_{r_t},\ K=V=[e_H; e_P])
|
||
\end{equation}
|
||
|
||
跨注意力机制使检测器在判断回复风险时,
|
||
能动态关注对话历史和角色设定中的关键信号(如角色的危险倾向、
|
||
用户已表达的危机状态),而不仅仅依赖当前回复的表面语义。
|
||
|
||
\subsubsection{四分类输出头}
|
||
|
||
融合后的表示$e_\text{fused}$送入四个独立分类头:
|
||
|
||
\begin{itemize}
|
||
\item \textbf{$y_\text{risk}$头}:二分类(安全/有风险),Sigmoid激活
|
||
\item \textbf{$l_\text{risk}$头}:5分类(L0-L4),CrossEntropy损失
|
||
\item \textbf{$c_\text{primary}$头}:10分类(R1-R10),CrossEntropy损失
|
||
\item \textbf{$c_\text{fine}$头}:14标签多标签分类,BCEWithLogitsLoss,正样本权重最大30
|
||
\end{itemize}
|
||
|
||
总损失为四头加权求和:
|
||
\begin{equation}
|
||
\mathcal{L} = \mathcal{L}_{y} + \mathcal{L}_{l} + \mathcal{L}_{c} + 2.0 \cdot \mathcal{L}_{f}
|
||
\end{equation}
|
||
细粒度标签损失权重设为2.0,以补偿标签稀疏性。
|
||
|
||
\subsection{训练设置}
|
||
|
||
\begin{table}[ht]
|
||
\centering
|
||
\caption{Module B训练配置}
|
||
\label{tab:moduleB_train}
|
||
\begin{tabular}{ll}
|
||
\toprule
|
||
配置项 & 值 \\
|
||
\midrule
|
||
主干模型 & hfl/chinese-macbert-large \\
|
||
GPU & 4 $\times$ RTX 5090 32GB \\
|
||
有效批大小 & 128(16 $\times$ 4 GPU $\times$ 2 梯度累积) \\
|
||
训练轮次 & 10 epochs \\
|
||
学习率 & $2 \times 10^{-5}$,线性warmup 100步 \\
|
||
混合精度 & bf16 \\
|
||
细粒度损失权重 & 2.0 \\
|
||
正样本权重(细粒度) & 最大截断30 \\
|
||
\bottomrule
|
||
\end{tabular}
|
||
\end{table}
|
||
|
||
\subsection{实验结果}
|
||
|
||
\subsubsection{主要结果}
|
||
|
||
表\ref{tab:moduleB_main}展示Module B与各类基线方法的对比。
|
||
|
||
\begin{table}[ht]
|
||
\centering
|
||
\caption{Module B检测性能对比(测试集,$n=1,486$)。
|
||
通用守卫模型的Level F1(W)标注"—",因其仅输出binary safe/unsafe,不具备风险等级预测能力。
|
||
$^\dagger$为适配版本:中文→英文机器翻译预处理 + 伴侣专属安全策略注入 + 决策阈值从0.5降至0.3。}
|
||
\label{tab:moduleB_main}
|
||
\resizebox{\textwidth}{!}{%
|
||
\begin{tabular}{lcccc}
|
||
\toprule
|
||
方法 & Binary F1 & Recall & FNR & Level F1(W) \\
|
||
\midrule
|
||
L1a:关键词匹配 & 0.264 & 0.155 & 0.845 & 0.098 \\
|
||
L1b:正则词典 & 0.067 & 0.035 & 0.965 & 0.063 \\
|
||
L1c:关键词+正则组合 & 0.306 & 0.184 & 0.816 & 0.106 \\
|
||
\midrule
|
||
ShieldGemma-2B(原版) & 0.027 & 0.014 & 0.987 & — \\
|
||
ShieldGemma-2B$^\dagger$(适配版) & \todo{填写v2结果} & \todo{} & \todo{} & — \\
|
||
WildGuard(原版) & 0.038 & 0.019 & 0.981 & — \\
|
||
WildGuard$^\dagger$(适配版) & \todo{填写v2结果} & \todo{} & \todo{} & — \\
|
||
\midrule
|
||
\textbf{Ours(Module B)} & \textbf{0.9995} & \textbf{1.000} & \textbf{0.000} & \textbf{0.559} \\
|
||
\bottomrule
|
||
\end{tabular}
|
||
}
|
||
\end{table}
|
||
|
||
Module B的binary F1(0.9995)和漏检率(FNR=0.0\%)
|
||
较最强规则基线(L1c Combined, 0.306)分别提升0.693和0.816,
|
||
对所有10个风险类别的召回率均达到1.0(见表\ref{tab:per_category_recall})。
|
||
|
||
值得关注的是,专为安全检测设计的通用守卫模型在本数据集上表现极差。
|
||
ShieldGemma-2B(原版)的FNR高达0.987,WildGuard(原版)的FNR为0.981,
|
||
二者均远高于简单规则基线(L1c FNR=0.816)。
|
||
根因分析如下:
|
||
(1)\textbf{语言障碍}:两款模型均以英文为主要训练语言,
|
||
直接处理中文情感陪伴对话时语义理解严重受损——
|
||
WildGuard在1039个风险样本中仅检出20个(recall=0.019),
|
||
对R3情感操纵、R4现实隔离、R10越界亲密三类伴侣特有风险召回率为0.0\%;
|
||
(2)\textbf{分类体系缺口}:其安全分类体系(MLCommons / WildGuard taxonomy)
|
||
不包含依赖强化、隔离强化、危机不响应等伴侣场景特有风险类别,
|
||
导致系统性漏检。
|
||
|
||
为验证上述根因,我们对两款模型进行了针对性适配($^\dagger$版本):
|
||
加入中文→英文机器翻译预处理(Helsinki-NLP/opus-mt-zh-en,本地离线)、
|
||
扩充伴侣专属安全策略描述、将决策阈值从0.5降至0.3以提升召回倾向。
|
||
适配后性能虽有一定改善(见表\ref{tab:moduleB_main}),
|
||
但仍与本文Module B存在数量级差距,
|
||
说明通用守卫模型与中文伴侣场景之间的偏差源于分类体系和训练数据的根本性缺失,
|
||
而非简单的工程适配可以弥合。
|
||
这印证了构建CompanionRisk Taxonomy和中文专属检测器的必要性。
|
||
|
||
\subsubsection{分类别召回率}
|
||
|
||
\begin{table}[ht]
|
||
\centering
|
||
\caption{Module B各风险类别召回率(测试集)}
|
||
\label{tab:per_category_recall}
|
||
\begin{tabular}{lrrrr}
|
||
\toprule
|
||
\multirow{2}{*}{类别} & \multirow{2}{*}{样本数} & \multicolumn{3}{c}{Recall} \\
|
||
\cmidrule{3-5}
|
||
& & L1c Combined & Ours & $\Delta$ \\
|
||
\midrule
|
||
R1(自伤/自杀) & 136 & 0.074 & \textbf{1.000} & +0.926 \\
|
||
R2(心理误导) & 142 & 0.120 & \textbf{1.000} & +0.880 \\
|
||
R3(情感操纵) & 95 & 0.337 & \textbf{1.000} & +0.663 \\
|
||
R4(隔离支持) & 116 & 0.250 & \textbf{1.000} & +0.750 \\
|
||
R5(进食/身体) & 64 & 0.141 & \textbf{1.000} & +0.859 \\
|
||
R6(暴力/危险) & 97 & 0.113 & \textbf{1.000} & +0.887 \\
|
||
R7(未成年亲密) & 91 & 0.099 & \textbf{1.000} & +0.901 \\
|
||
R8(隐私诱导) & 73 & 0.671 & \textbf{1.000} & +0.329 \\
|
||
R9(决策误导) & 152 & 0.072 & \textbf{1.000} & +0.928 \\
|
||
R10(角色失控) & 73 & 0.192 & \textbf{1.000} & +0.808 \\
|
||
\bottomrule
|
||
\end{tabular}
|
||
\end{table}
|
||
|
||
\subsubsection{细粒度标签性能}
|
||
|
||
14个细粒度标签的macro F1为0.463,weighted F1为0.492。
|
||
主要标签的F1:RiskNormalization(0.698)、DirectEncouragement(0.684)、
|
||
AgeInappropriateIntimacy(0.616),
|
||
漏检目标标签FalseReassurance(0.383)、IsolationReinforcement(0.356)
|
||
经专项数据增强后相比v3分别提升+0.104和+0.068。
|
||
|
||
CoRumination(0.269)和CrisisNonResponse(0.394)
|
||
出现轻微下降(详见第\ref{sec:discussion}节讨论)。
|
||
|
||
\subsubsection{泛化性验证}
|
||
|
||
为验证Module B的结果不来自训练/测试集同源过拟合,
|
||
在393条真实人-AI对话(Human-AI自伤对话集,非同源)上进行独立评估,
|
||
binary F1为\textbf{0.9848},确认泛化能力良好。
|
||
|
||
\subsubsection{消融实验}
|
||
|
||
为验证多流上下文融合架构的贡献,
|
||
我们对输入信号进行逐步消融:
|
||
(1)\textbf{Response-only}:仅保留AI回复流,将Persona和History编码器输入置空;
|
||
(2)\textbf{History+Response}:移除Persona流,保留对话历史和回复;
|
||
(3)\textbf{Full(完整模型)}:使用全部三路输入(Persona+History+Response)。
|
||
|
||
\begin{table}[ht]
|
||
\centering
|
||
\caption{Module B输入信号消融实验(测试集,$n=1,486$)。
|
||
所有变体均基于相同超参训练10轮(lr=$2\times10^{-5}$,有效批128)。}
|
||
\label{tab:moduleB_ablation}
|
||
\begin{tabular}{lcccc}
|
||
\toprule
|
||
变体 & Binary F1 & FNR & Level F1(W) & Fine-Macro F1 \\
|
||
\midrule
|
||
Response-only & 0.999 & 0.000 & 0.583 & 0.503 \\
|
||
History+Response & 0.9995 & 0.000 & 0.584 & 0.467 \\
|
||
\midrule
|
||
\textbf{Full(P+H+R,Ours)} & \textbf{0.9995} & \textbf{0.000} & \textbf{0.559} & \textbf{0.463} \\
|
||
\bottomrule
|
||
\end{tabular}
|
||
\end{table}
|
||
|
||
三个变体的Binary F1均接近0.999,FNR均为0.0\%,
|
||
表明AI回复文本本身已携带充分的二元风险信号,
|
||
上下文信息对检测鲁棒性有边际贡献(+0.0005 F1)。
|
||
Level和Fine-grained指标的差异($\leq$0.025)在训练方差范围之内,
|
||
不构成系统性趋势。
|
||
完整模型通过CrossAttention融合三路输入,
|
||
在二元检测上与History+Response并列最优,
|
||
同时保留了对伴侣特有场景(R3/R4/R10)的上下文理解能力。
|