模型:
⚠️ 免责声明:huggingface模型目前与detoxify库有不同的结果(见问题 here )。建议使用来自 https://github.com/unitaryai/detoxify 的最新模型。
用于预测3个Jigsaw挑战中有害评论的训练模型和代码:有害评论分类、有害评论中的意外偏见、多语言有害评论分类。
由 Laura Hanu 开发,其使命是通过解释上下文中的视觉内容来阻止在线上的有害内容。
依赖包括:
| Challenge | Year | Goal | Original Data Source | Detoxify Model Name | Top Kaggle Leaderboard Score | Detoxify Score |
|---|---|---|---|---|---|---|
| 12321321 | 2018 | build a multi-headed model that’s capable of detecting different types of of toxicity like threats, obscenity, insults, and identity-based hate. | Wikipedia Comments | original | 0.98856 | 0.98636 |
| 12322321 | 2019 | build a model that recognizes toxicity and minimizes this type of unintended bias with respect to mentions of identities. You'll be using a dataset labeled for identity mentions and optimizing a metric designed to measure unintended bias. | Civil Comments | unbiased | 0.94734 | 0.93639 |
| 12323321 | 2020 | build effective multilingual models | Wikipedia Comments + Civil Comments | multilingual | 0.9536 | 0.91655* |
*分数无法直接进行比较,因为其是在提供的验证集上得出的,而不是在测试集上进行的。当测试标签可用时会更新。
值得一提的是,最高的排行榜得分是通过使用模型集合实现的。该库的目的是构建一个用户友好且易于使用的工具。
如果评论中包含与咒骂、侮辱或亵渎有关的词语,则很可能被分类为有害,无论作者的语气或意图是什么,例如幽默的/自嘲的。这可能对已经处于弱势的少数群体产生一些偏见。
该库的预期用途是用于研究目的,即基于反映真实世界人口统计数据的仔细构建的数据集进行微调,以及帮助内容审查员更快地标记有害内容。
有关有害性或仇恨言论检测中不同偏见风险的一些有用资源:
多语言模型已经在7种不同的语言上进行了训练,因此只能在以下语言上进行测试:英语、法语、西班牙语、意大利语、葡萄牙语、土耳其语或俄语。
# install detoxify pip install detoxify
from detoxify import Detoxify
# each model takes in either a string or a list of strings
results = Detoxify('original').predict('example text')
results = Detoxify('unbiased').predict(['example text 1','example text 2'])
results = Detoxify('multilingual').predict(['example text','exemple de texte','texto de ejemplo','testo di esempio','texto de exemplo','örnek metin','пример текста'])
# optional to display results nicely (will need to pip install pandas)
import pandas as pd
print(pd.DataFrame(results, index=input_text).round(5))
有关详细信息,请查看预测部分。
所有挑战都有一个有害性标签。有害性标签代表了最多10名标注者的综合评级,评级方案如下:
有关标签方案的更多信息可在 here 中找到。
该挑战包括以下标签:
该挑战有2种类型的标签:主要的有害性标签和一些额外的身份标签,代表评论中提到的身份。
仅在测试集中(公共和私有的组合)中有超过500个示例的身份被包括在训练中作为额外标签,并在评估计算中使用。
使用的身份标签:
可用的所有身份标签的完整列表可在 here 中找到。
由于该挑战结合了前两个挑战的数据,它包括以上所有标签,但最终评估仅针对有害性主标签。
首先,安装依赖项
# clone project git clone https://github.com/unitaryai/detoxify # create virtual env python3 -m venv toxic-env source toxic-env/bin/activate # install project pip install -e detoxify cd detoxify # for training pip install -r requirements.txt
训练模型摘要:
| Model name | Transformer type | Data from |
|---|---|---|
| original | bert-base-uncased | Toxic Comment Classification Challenge |
| unbiased | roberta-base | Unintended Bias in Toxicity Classification |
| multilingual | xlm-roberta-base | Multilingual Toxic Comment Classification |
对于快速预测,可以直接在评论上运行示例脚本,或从包含评论列表的txt文件中运行。
# load model via torch.hub python run_prediction.py --input 'example' --model_name original # load model from from checkpoint path python run_prediction.py --input 'example' --from_ckpt_path model_path # save results to a .csv file python run_prediction.py --input test_set.txt --model_name original --save_to results.csv # to see usage python run_prediction.py --help
可以从最新发布版本或通过PyTorch Hub API下载检查点,名称如下:
model = torch.hub.load('unitaryai/detoxify','toxic_bert')
在Python中导入detoxify:
from detoxify import Detoxify
results = Detoxify('original').predict('some text')
results = Detoxify('unbiased').predict(['example text 1','example text 2'])
results = Detoxify('multilingual').predict(['example text','exemple de texte','texto de ejemplo','testo di esempio','texto de exemplo','örnek metin','пример текста'])
# to display results nicely
import pandas as pd
print(pd.DataFrame(results,index=input_text).round(5))
如果您尚未拥有Kaggle帐户:
您需要创建一个帐户才能下载数据。
转到“我的帐户”并单击“创建新API令牌”-这将下载一个kaggle.json文件。
确保此文件位于~/.kaggle中。
# create data directory mkdir jigsaw_data cd jigsaw_data # download data kaggle competitions download -c jigsaw-toxic-comment-classification-challenge kaggle competitions download -c jigsaw-unintended-bias-in-toxicity-classification kaggle competitions download -c jigsaw-multilingual-toxic-comment-classification
python create_val_set.py python train.py --config configs/Toxic_comment_classification_BERT.json
python train.py --config configs/Unintended_bias_toxic_comment_classification_RoBERTa.json
训练分为两个阶段。首先,在所有可用数据上进行训练,然后只在第一个挑战的翻译版本上进行训练。
可以从Kaggle上下载 translated data ,语言有法语、西班牙语、意大利语、葡萄牙语、土耳其语和俄语(测试集中提供的语言)。
# stage 1 python train.py --config configs/Multilingual_toxic_comment_classification_XLMR.json # stage 2 python train.py --config configs/Multilingual_toxic_comment_classification_XLMR_stage2.json
tensorboard --logdir=./saved
此挑战根据所有标签的平均AUC得分进行评估。
python evaluate.py --checkpoint saved/lightning_logs/checkpoints/example_checkpoint.pth --test_csv test.csv
此挑战根据一种新的偏见度量标准进行评估,该度量标准结合了不同的AUC得分以平衡整体性能。有关此度量标准的更多信息,请参见 here 。
python evaluate.py --checkpoint saved/lightning_logs/checkpoints/example_checkpoint.pth --test_csv test.csv # to get the final bias metric python model_eval/compute_bias_metric.py
此挑战根据主要有害标签的AUC得分进行评估。
python evaluate.py --checkpoint saved/lightning_logs/checkpoints/example_checkpoint.pth --test_csv test.csv
@misc{Detoxify,
title={Detoxify},
author={Hanu, Laura and {Unitary team}},
howpublished={Github. https://github.com/unitaryai/detoxify},
year={2020}
}