Files
CompanionGuard-RL/paper/sections/05_moduleB.tex

219 lines
8.5 KiB
TeX
Raw Normal View History

% ============================================================
\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-Large1,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-L4CrossEntropy损失
\item \textbf{$c_\text{primary}$}10分类R1-R10CrossEntropy损失
\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 \\
有效批大小 & 12816 $\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$)。
通用守卫模型ShieldGemma-2B、WildGuard的Level F1(W)标注"—"
因其仅输出binary safe/unsafe不具备风险等级预测能力。}
\label{tab:moduleB_main}
\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 \\
ShieldGemma-2B & 0.027 & 0.014 & 0.987 &\\
WildGuard & 0.038 & 0.019 & 0.981 &\\
\midrule
\textbf{OursModule B} & \textbf{0.9995} & \textbf{1.000} & \textbf{0.000} & \textbf{0.559} \\
\bottomrule
\end{tabular}
\end{table}
Module B的binary F10.9995和漏检率FNR=0.0\%
较最强规则基线L1c Combined, 0.306分别提升0.693和0.816
对所有10个风险类别的召回率均达到1.0(见表\ref{tab:per_category_recall})。
值得关注的是,专为安全检测设计的通用守卫模型在本数据集上表现极差。
ShieldGemma-2B的FNR高达0.987WildGuard的FNR为0.981
二者均远高于简单规则基线L1c FNR=0.816)。
主要原因在于1上述模型均以英文为主要训练语言
对中文情感陪伴对话的语义理解能力严重不足——WildGuard在1039个风险样本中
仅检出20个recall=0.019且对R3情感操纵、R4现实隔离、R10越界亲密
三类伴侣特有风险的召回率为0.0\%
2其安全分类体系MLCommons / WildGuard taxonomy缺乏伴侣场景特有风险类别
导致系统性漏检。
这印证了构建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.463weighted F1为0.492。
主要标签的F1RiskNormalization0.698、DirectEncouragement0.684)、
AgeInappropriateIntimacy0.616
漏检目标标签FalseReassurance0.383、IsolationReinforcement0.356
经专项数据增强后相比v3分别提升+0.104和+0.068。
CoRumination0.269和CrisisNonResponse0.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{FullP+H+ROurs} & \textbf{0.9995} & \textbf{0.000} & \textbf{0.559} & \textbf{0.463} \\
\bottomrule
\end{tabular}
\end{table}
三个变体的Binary F1均接近0.999FNR均为0.0\%
表明AI回复文本本身已携带充分的二元风险信号
上下文信息对检测鲁棒性有边际贡献(+0.0005 F1
Level和Fine-grained指标的差异$\leq$0.025)在训练方差范围之内,
不构成系统性趋势。
完整模型通过CrossAttention融合三路输入
在二元检测上与History+Response并列最优
同时保留了对伴侣特有场景R3/R4/R10的上下文理解能力。