ChineseRobustText: Lightweight Robustness Evaluation for Chinese Text Classification Models
ChineseRobustText 是一个面向中文文本分类模型的轻量、可复现鲁棒性评测项目。项目借鉴 RobTest 的模型中心鲁棒性评测思想,并结合 CheckList、Robustness Gym、TextFlint 和 OpenAttack 中“行为测试、切片评估、多维扰动、攻击成功率”的评测视角,将早期单强度规则扰动扩展为多扰动强度、多随机种子和多指标的中文文本分类评测协议。
当前版本支持 ChnSentiCorp、CLUE-TNEWS 和 CLUE-OCNLI 三类中文分类任务,默认使用 hfl/chinese-roberta-wwm-ext,并提供 bert-base-chinese、hfl/chinese-roberta-wwm-ext、hfl/chinese-macbert-base 三个 encoder baseline 的训练/评测脚手架。评测覆盖 6 类中文输入扰动,并输出 Accuracy、Macro-F1、扰动覆盖率、资源命中覆盖率、字符变化比例和 Attack Success Rate (ASR)。这些指标能区分“模型真的稳”和“扰动资源没有实际命中样本”这两种情况,避免只看 Accuracy Drop 时过度乐观。
| 扰动类型 | 方法名 | 示例 |
|---|---|---|
| 同音字替换 | homophone |
安全 -> 按全 |
| 形近字替换 | shape |
未 -> 末 |
| 简繁转换 | traditional |
网络安全 -> 網絡安全 |
| 空格扰动 | space |
这个电影很好看 -> 这个 电 影 很 好 看 |
| 标点扰动 | punctuation |
随机插入中文标点 |
| 同义词替换 | synonym |
喜欢 -> 喜爱 |
当前资源规模:
- 同音字:126 个 key,255 个替换候选。
- 形近字:118 个 key,148 个替换候选。
- 同义词:101 个 key,191 个替换候选。
评测协议默认运行:
- 扰动强度:
--probs 0.05 0.15 0.30 - 随机种子:
--seeds 13 42 100 - 兼容旧命令:继续支持单次评测参数
--prob和--seed - 数据集:
--dataset_key chsenticorp|tnews|ocnli - 新增指标:
changed_rate、resource_coverage_rate、mean_resource_hits_per_example、mean_char_change_ratio、clean_macro_f1、perturbed_macro_f1、macro_f1_drop、num_attack_eval、attack_success_rate
ChineseRobustText/
configs/
chsenticorp_roberta.yaml
baselines.yaml
docs/
method.md
outputs/
robustness_results_multi.csv
robustness_summary_multi.csv
examples_multi.json
figures/
resources/
homophone_dict.json
shape_similar_dict.json
synonym_dict.json
scripts/
train.sh
robtest.sh
run_baselines.sh
src/
chinese_robustness/
task_data.py
perturbations.py
train_chinese_classifier.py
evaluate_robustness.py
metrics.py
utils.py
train_chinese_classifier.py
evaluate_robustness.py
run_baselines.py
建议使用 Python 3.10 或更高版本。
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt如果希望以可编辑模式安装:
pip install -e .Smoke test 使用内置的小样本和规则分类器,不需要下载 Hugging Face 模型或数据集,适合先验证扰动、指标、汇总表和图表流程是否能跑通。
python3 src/evaluate_robustness.py --smoke也可以验证 TNEWS/OCNLI 的本地 fake schema,不会下载数据:
python3 src/evaluate_robustness.py --smoke --dataset_key tnews --no_figures
python3 src/evaluate_robustness.py --smoke --dataset_key ocnli --no_figures也可以使用脚本入口:
bash scripts/robtest.sh --smoke如果只想复现旧版单强度/单 seed 行为:
python3 src/evaluate_robustness.py --smoke --prob 0.15 --seed 42ChnSentiCorp 全量训练命令:
python3 src/train_chinese_classifier.py \
--dataset_key chsenticorp \
--model_name hfl/chinese-roberta-wwm-ext \
--output_dir outputs/checkpoints/chsenticorp/chinese-roberta-wwm-ext \
--num_train_epochs 1 \
--train_batch_size 8 \
--eval_batch_size 32 \
--max_length 128调试时可以先使用小样本训练:
python3 src/train_chinese_classifier.py \
--dataset_key chsenticorp \
--model_name hfl/chinese-roberta-wwm-ext \
--train_subset 1000 \
--eval_subset 300 \
--num_train_epochs 1 \
--train_batch_size 8 \
--eval_batch_size 16 \
--max_length 128TNEWS 和 OCNLI 使用同一个入口;OCNLI 会自动走句对 tokenizer 输入:
python3 src/train_chinese_classifier.py \
--dataset_key tnews \
--model_name hfl/chinese-macbert-base \
--train_subset 1000 \
--eval_subset 300 \
--num_train_epochs 1
python3 src/train_chinese_classifier.py \
--dataset_key ocnli \
--model_name bert-base-chinese \
--train_subset 1000 \
--eval_subset 300 \
--num_train_epochs 1三数据集三模型的批量命令可以先打印出来审阅;默认不会执行训练:
python3 src/run_baselines.py \
--datasets chsenticorp tnews ocnli \
--models bert-base-chinese hfl/chinese-roberta-wwm-ext hfl/chinese-macbert-base确认算力和网络可用后才加 --execute。
默认评测会对每个扰动方法运行 3 个扰动强度和 3 个随机种子,并生成详细结果、均值标准差汇总、代表性样例和扰动强度曲线。
python3 src/evaluate_robustness.py \
--dataset_key chsenticorp \
--model_path outputs/checkpoints/chsenticorp/chinese-roberta-wwm-ext \
--max_samples 1200 \
--batch_size 32 \
--max_length 128 \
--output_csv outputs/robustness_results_multi.csv \
--summary_csv outputs/robustness_summary_multi.csv \
--examples_json outputs/examples_multi.json \
--figures_dir outputs/figures详细结果表字段:
model,dataset,perturbation,prob,seed,clean_acc,perturbed_acc,acc_drop,
robustness_score,clean_macro_f1,perturbed_macro_f1,macro_f1_drop,
num_examples,num_changed,changed_rate,mean_char_change_ratio,
num_resource_hits,resource_coverage_rate,mean_resource_hits_per_example,
num_clean_correct,num_attack_eval,attack_success_rate
汇总结果表按 model,dataset,perturbation,prob 聚合,输出每个指标的 mean、std 和 mean_std。图表目录默认生成:
accuracy_drop_by_prob.svgattack_success_rate_by_prob.svg
给定干净准确率 A_clean 和扰动后准确率 A_perturbed:
Accuracy Drop = A_clean - A_perturbed
Robustness Score = A_perturbed / A_clean
新增指标用于补足旧协议的盲点:
changed_rate:实际发生文本变化的样本比例,用来衡量扰动覆盖率。resource_coverage_rate:同音字、形近字或同义词资源在样本中至少命中一次的样本比例;非资源型扰动填 0。mean_resource_hits_per_example:每条样本平均命中的资源项数量,可解释为什么某类扰动 changed rate 偏低。mean_char_change_ratio:原文到扰动文本的平均字符编辑比例,用来衡量扰动强度。macro_f1_drop:Macro-F1 的下降,降低类别不均衡时只看 accuracy 的偏差。num_attack_eval:同时满足“干净样本预测正确”和“文本实际发生扰动”的样本数。attack_success_rate:在num_attack_eval分母上,扰动导致模型从正确变错误的比例。
因此,ASR 不会把未变化样本算进分母;如果一个同义词词典没有命中样本,它会反映为低 resource_coverage_rate 和低 changed_rate,而不是被误读为模型非常鲁棒。
当前仓库仍保留一次早期单强度实验结果,文件为 outputs/robustness_results_hf_full.csv。该结果使用 ChnSentiCorp 1,200 条测试样本、hfl/chinese-roberta-wwm-ext 微调模型、扰动概率 0.15 和单一随机种子。
| 扰动类型 | Clean Acc | Perturbed Acc | Acc Drop | Robustness Score | 样本数 |
|---|---|---|---|---|---|
homophone |
0.9317 | 0.9258 | 0.0058 | 0.9937 | 1200 |
shape |
0.9317 | 0.9300 | 0.0017 | 0.9982 | 1200 |
traditional |
0.9317 | 0.9308 | 0.0008 | 0.9991 | 1200 |
space |
0.9317 | 0.9317 | 0.0000 | 1.0000 | 1200 |
punctuation |
0.9317 | 0.9275 | 0.0042 | 0.9955 | 1200 |
synonym |
0.9317 | 0.9300 | 0.0017 | 0.9982 | 1200 |
这组结果只能说明模型在当前规则资源、当前扰动强度和当前样本覆盖下表现较稳定。由于部分字典覆盖有限,同义词和形近字扰动可能有较多样本未发生实际变化;论文中应优先报告新版 changed_rate、ASR 和多 seed 的均值标准差,再讨论 Accuracy Drop。
- CheckList:强调从行为能力维度测试 NLP 模型,而不是只报告单一准确率。
- Robustness Gym:强调按切片组织鲁棒性评测,便于定位模型在哪些输入子集上脆弱。
- TextFlint:提供多任务、多扰动的鲁棒性评测框架,可作为规则扰动设计的参考。
- OpenAttack:强调文本攻击成功率和攻击样例分析,适合作为 ASR 指标的来源之一。
- ERAS:关注中文 NLP 模型对中文分词歧义和 garden-path 现象的鲁棒性,适合作为后续中文特色扩展方向。
- 将 CLUE-TNEWS、OCNLI 和三类 encoder baseline 的全量实验跑完并整理为论文主表。
- 加入分词歧义、拼音输入错误、口语化错别字等更强中文特色扰动。
- 在不改变主实验轻量可复现定位的前提下,将 Qwen/GTE/LLM zero-shot 作为扩展实验。
本项目参考了 RobTest 的模型中心鲁棒性评测框架思想:
@article{chen2023adversarial,
title={From Adversarial Arms Race to Model-centric Evaluation: Motivating a Unified Automatic Robustness Evaluation Framework},
author={Chen, Yangyi and Gao, Hongcheng and Cui, Ganqu and Yuan, Lifan and Kong, Dehan and Wu, Hanlu and Shi, Ning and Yuan, Bo and Huang, Longtao and Xue, Hui and others},
journal={arXiv preprint arXiv:2305.18503},
year={2023}
}