Files
CompanionGuard-RL/paper/sections/05_moduleB.tex
zhangsiyuan 804ebd2f77 feat: add paper/ LaTeX draft, English data scripts, update progress docs
- paper/: 22-page LaTeX framework (7/10 sections complete, compiles cleanly)
  main.tex + 10 section files + refs.bib + compiled PDF (329KB)
- code/scripts/: three English dataset generation & merging scripts
  generate_english.py / generate_english_targeted.py / merge_v5.py
- CLAUDE.md: update paper writing status, add paper/ file map entry
- state.md: add section 8 paper writing progress (2026-05-15)
- .gitignore: add LaTeX build artifact exclusion rules

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-18 11:19:39 +08:00

177 lines
6.5 KiB
TeX
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

% ============================================================
\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$}
\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 \\
\todo{Llama Guard v2} & \todo{} & \todo{} & \todo{} & \todo{} \\
\todo{WildGuard} & \todo{} & \todo{} & \todo{} & \todo{} \\
\todo{OpenAI Moderation} & \todo{} & \todo{} & \todo{} & \todo{} \\
\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})。
\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{消融实验}
\todo{消融实验表格待补充需GPU重训上下文信号消融Response-only / History+Response / Full}