天池中药说明书实体识别挑战冠军方案;中文命名实体识别;NER; BERT-CRF & BERT-SPAN & BERT-MRC;Pytorch

Overview

Chinese-DeepNER-Pytorch

天池中药说明书实体识别挑战冠军方案开源

贡献者:

zxx飞翔的鱼: https://github.com/z814081807

我是蛋糕王:https://github.com/WuHuRestaurant

数青峰:https://github.com/zchaizju

后续官方开放数据集后DeepNER项目会进行优化升级,包含完整的数据处理、训练、验证、测试、部署流程,提供详细的代码注释、模型介绍、实验结果,提供更普适的基于预训练的中文命名实体识别方案,开箱即用,欢迎Star!

(代码框架基于pytorch and transformers, 框架复用性、解耦性、易读性较高,很容易修改迁移至其他NLP任务中)

环境

python3.7
pytorch==1.6.0 +
transformers==2.10.0
pytorch-crf==0.7.2

项目目录说明

DeepNER
│
├── data                                    # 数据文件夹
│   ├── mid_data                            # 存放一些中间数据
│   │   ├── crf_ent2id.json                 # crf 模型的 schema
│   │   └── span_ent2id.json                # span 模型的 schema
│   │   └── mrc_ent2id.json                 # mrc 模型的 schema
│
│   ├── raw_data                            # 转换后的数据
│   │   ├── dev.json                        # 转换后的验证集
│   │   ├── test.json                       # 转换后的初赛测试集
│   │   ├── pseudo.json                     # 转换后的半监督数据
│   │   ├── stack.json                      # 转换后的全体数据
│   └── └── train.json                      # 转换后的训练集
│
├── out                                     # 存放训练好的模型
│   ├── ...           
│   └── ...                                      
│
├── src
│   ├── preprocess                  
│   │   ├── convert_raw_data.py             # 处理转换原始数据
│   │   └── processor.py                    # 转换数据为 Bert 模型的输入
│   ├── utils                      
│   │   ├── attack_train_utils.py           # 对抗训练 FGM / PGD
│   │   ├── dataset_utils.py                # torch Dataset
│   │   ├── evaluator.py                    # 模型评估
│   │   ├── functions_utils.py              # 跨文件调用的一些 functions
│   │   ├── model_utils.py                  # Span & CRF & MRC model (pytorch)
│   │   ├── options.py                      # 命令行参数|   └── trainer.py                      # 训练器
|
├── competition_predict.py                  # 复赛数据推理并提交
├── README.md                               # ...
├── convert_test_data.py                    # 将复赛 test 转化成 json 格式
├── run.sh                                  # 运行脚本
└── main.py                                 # main 函数 (主要用于训练/评估)

使用说明

预训练使用说明

数据转换

注:已提供转换好的数据 无需运行

python src/preprocessing/convert_raw_data.py

训练阶段

bash run.sh

注:脚本中指定的 BERT_DIR 指BERT所在文件夹,需要把 BERT 下载到指定文件夹中

BERT-CRF模型训练
task_type='crf'
mode='train' or 'stack'  train:单模训练与验证 ; stack:5折训练与验证

swa_start: swa 模型权重平均开始的 epoch
attack_train'pgd' / 'fgm' / '' 对抗训练 fgm 训练速度慢一倍, pgd 慢两倍,pgd 本次数据集效果明显
BERT-SPAN模型训练
task_type='span'
mode:同上
attack_train: 同上
loss_type: 'ce':交叉熵; 'ls_ce'label_smooth; 'focal': focal loss
BERT-MRC模型训练
task_type='mrc'
mode:同上
attack_train: 同上
loss_type: 同上

预测复赛 test 文件 (上述模型训练完成后)

注:暂无数据运行,等待官方数据开源后可运行

# convert_test_data
python convert_test_data.py
# predict
python competition_predict.py

赛题背景

任务描述

人工智能加速了中医药领域的传承创新发展,其中中医药文本的信息抽取部分是构建中医药知识图谱的核心部分,为上层应用如临床辅助诊疗系统的构建(CDSS)等奠定了基础。本次NER挑战需要抽取中药药品说明书中的关键信息,包括药品、药物成分、疾病、症状、证候等13类实体,构建中医药药品知识库。

数据探索分析

本次竞赛训练数据有三个特点:

  • 中药药品说明书以长文本居多

- 医疗场景下的标注样本不足

- 标签分布不平衡

核心思路

数据预处理

首先对说明书文本进行预清洗与长文本切分。预清洗部分对无效字符进行过滤。针对长文本问题,采用两级文本切分的策略。切分后的句子可能过短,将短文本归并,使得归并后的文本长度不超过设置的最大长度。此外,利用全部标注数据构造实体知识库,作为领域先验词典。

Baseline: BERT-CRF

  • Baseline 细节
    • 预训练模型:选用 UER-large-24 layer[1],UER在RoBerta-wwm 框架下采用大规模优质中文语料继续训练,CLUE 任务中单模第一
    • 差分学习率:BERT层学习率2e-5;其他层学习率2e-3
    • 参数初始化:模型其他模块与BERT采用相同的初始化方式
    • 滑动参数平均:加权平均最后几个epoch模型的权重,得到更加平滑和表现更优的模型
  • Baseline bad-case分析

优化1:对抗训练

  • 动机:采用对抗训练缓解模型鲁棒性差的问题,提升模型泛化能力
  • 对抗训练是一种引入噪声的训练方式,可以对参数进行正则化,提升模型鲁棒性和泛化能力
    • Fast Gradient Method (FGM):对embedding层在梯度方向添加扰动
    • Projected Gradient Descent (PGD) [2]:迭代扰动,每次扰动被投影到规定范围内

优化2:混合精度训练(FP16)

  • 动机:对抗训练降低了计算效率,使用混合精度训练优化训练耗时
  • 混合精度训练
    • 在内存中用FP16做存储和乘法来加速
    • 用FP32做累加避免舍入误差
  • 损失放大
    • 反向传播前扩大2^k倍loss,防止loss下溢出
    • 反向传播后将权重梯度还原

优化3:多模型融合

  • 动机:baseline 错误集中于歧义性错误,采用多级医学命名实体识别系统以消除歧义性

  • 方法:差异化多级模型融合系统

    • 模型框架差异化:BERT-CRF & BERT-SPAN & BERT-MRC
    • 训练数据差异化:更换随机种子、更换句子切分长度(256、512)
    • 多级模型融合策略
  • 融合模型1——BERT-SPAN

    • 采用SPAN指针的形式替代CRF模块,加快训练速度
    • 以半指针-半标注的结构预测实体的起始位置,同时标注过程中给出实体类别
    • 采用严格解码形式,重叠实体选取logits最大的一个,保证准确率
    • 使用label smooth缓解过拟合问题

  • 融合模型2——BERT-MRC
    • 基于阅读理解的方式处理NER任务
      • query:实体类型的描述来作为query
      • doc:分句后的原始文本作为doc
    • 针对每一种类型构造一个样本,训练时有大量负样本,可以随机选取30%加入训练,其余丢弃,保证效率
    • 预测时对每一类都需构造一次样本,对解码输出不做限制,保证召回率
    • 使用label smooth缓解过拟合问题
    • MRC在本次数据集上精度表现不佳,且训练和推理效率较低,仅作为提升召回率的方案,提供代码仅供学习,不推荐日常使用

  • 多级融合策略
    • CRF/SPAN/MRC 5折交叉验证得到的模型进行第一级概率融合,将 logits 平均后解码实体
    • CRF/SPAN/MRC 概率融合后的模型进行第二级投票融合,获取最终结果

优化4:半监督学习

  • 动机:为了缓解医疗场景下的标注语料稀缺的问题, 我们使用半监督学习(伪标签)充分利用未标注的500条初赛测试集
  • 策略:动态伪标签
    • 首先使用原始标注数据训练一个基准模型M
    • 使用基准模型M对初赛测试集进行预测得到伪标签
    • 将伪标签加入训练集,赋予伪标签一个动态可学习权重(图中alpha),加入真实标签数据中共同训练得到模型M’

- tips:使用多模融合的基准模型减少伪标签的噪音;权重也可以固定,选取需多尝试哪个效果好,本质上是降低伪标签的loss权重,是缓解伪标签噪音的一种方法。

其他无明显提升的尝试方案

  • 取BERT后四层动态加权输出,无明显提升
  • BERT 输出后加上BiLSTM / IDCNN 模块,过拟合严重,训练速度大大降低
  • 数据增强,对同类实体词进行随机替换,以扩充训练数据
  • BERT-SPAN / MRC 模型采用focal loss / dice loss 等缓解标签不平衡
  • 利用构造的领域词典修正模型输出

最终线上成绩72.90%,复赛Rank 1,决赛Rank 1

Ref

[1] Zhao et al., UER: An Open-Source Toolkit for Pre-training Models, EMNLP-IJCNLP, 2019. [2] Madry et al., Towards Deep Learning Models Resistant to Adversarial Attacks, ICLR, 2018.

Owner
zxx飞翔的鱼
Speak less and do more.
zxx飞翔的鱼
A python script that will use hydra to get user and password to login to ssh, ftp, and telnet

Hydra-Auto-Hack A python script that will use hydra to get user and password to login to ssh, ftp, and telnet Project Description This python script w

2 Jan 16, 2022
Code for the paper "VisualBERT: A Simple and Performant Baseline for Vision and Language"

This repository contains code for the following two papers: VisualBERT: A Simple and Performant Baseline for Vision and Language (arxiv) with a short

Natural Language Processing @UCLA 464 Jan 04, 2023
Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition

SEW (Squeezed and Efficient Wav2vec) The repo contains the code of the paper "Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speec

ASAPP Research 67 Dec 01, 2022
✨Rubrix is a production-ready Python framework for exploring, annotating, and managing data in NLP projects.

✨A Python framework to explore, label, and monitor data for NLP projects

Recognai 1.5k Jan 02, 2023
:hot_pepper: R²SQL: "Dynamic Hybrid Relation Network for Cross-Domain Context-Dependent Semantic Parsing." (AAAI 2021)

R²SQL The PyTorch implementation of paper Dynamic Hybrid Relation Network for Cross-Domain Context-Dependent Semantic Parsing. (AAAI 2021) Requirement

huybery 60 Dec 31, 2022
Library for fast text representation and classification.

fastText fastText is a library for efficient learning of word representations and sentence classification. Table of contents Resources Models Suppleme

Facebook Research 24.1k Jan 05, 2023
NL-Augmenter 🦎 → 🐍 A Collaborative Repository of Natural Language Transformations

NL-Augmenter 🦎 → 🐍 The NL-Augmenter is a collaborative effort intended to add transformations of datasets dealing with natural language. Transformat

684 Jan 09, 2023
Constituency Tree Labeling Tool

Constituency Tree Labeling Tool The purpose of this package is to solve the constituency tree labeling problem. Look from the dataset labeled by NLTK,

张宇 6 Dec 20, 2022
Use the power of GPT3 to execute any function inside your programs just by giving some doctests

gptrun Don't feel like coding today? Use the power of GPT3 to execute any function inside your programs just by giving some doctests. How is this diff

Roberto Abdelkader Martínez Pérez 11 Nov 11, 2022
Phrase-BERT: Improved Phrase Embeddings from BERT with an Application to Corpus Exploration

Phrase-BERT: Improved Phrase Embeddings from BERT with an Application to Corpus Exploration This is the official repository for the EMNLP 2021 long pa

70 Dec 11, 2022
Python library for Serbian Natural language processing (NLP)

SrbAI - Python biblioteka za procesiranje srpskog jezika SrbAI je projekat prikupljanja algoritama i modela za procesiranje srpskog jezika u jedinstve

Serbian AI Society 3 Nov 22, 2022
A high-level yet extensible library for fast language model tuning via automatic prompt search

ruPrompts ruPrompts is a high-level yet extensible library for fast language model tuning via automatic prompt search, featuring integration with Hugg

Sber AI 37 Dec 07, 2022
CPC-big and k-means clustering for zero-resource speech processing

The CPC-big model and k-means checkpoints used in Analyzing Speaker Information in Self-Supervised Models to Improve Zero-Resource Speech Processing.

Benjamin van Niekerk 5 Nov 23, 2022
A simple word search made in python

Word Search Puzzle A simple word search made in python Usage $ python3 main.py -h usage: main.py [-h] [-c] [-f FILE] Generates a word s

Magoninho 16 Mar 10, 2022
PyTorch original implementation of Cross-lingual Language Model Pretraining.

XLM NEW: Added XLM-R model. PyTorch original implementation of Cross-lingual Language Model Pretraining. Includes: Monolingual language model pretrain

Facebook Research 2.7k Dec 27, 2022
leaking paid token generator that was a shit lmao for 100$ haha

Discord-Token-Generator-Leaked leaking paid token generator that was a shit lmao for 100$ he selling it for 100$ wth here the code enjoy don't forget

Keevo 5 Apr 15, 2022
✔👉A Centralized WebApp to Ensure Road Safety by checking on with the activities of the driver and activating label generator using NLP.

AI-For-Road-Safety Challenge hosted by Omdena Hyderabad Chapter Original Repo Link : https://github.com/OmdenaAI/omdena-india-roadsafety Final Present

Prathima Kadari 7 Nov 29, 2022
Python bindings to the dutch NLP tool Frog (pos tagger, lemmatiser, NER tagger, morphological analysis, shallow parser, dependency parser)

Frog for Python This is a Python binding to the Natural Language Processing suite Frog. Frog is intended for Dutch and performs part-of-speech tagging

Maarten van Gompel 46 Dec 14, 2022
**NSFW** A chatbot based on GPT2-chitchat

DangBot -- 好怪哦,再来一句 卡群怪话bot,powered by GPT2 for Chinese chitchat Training Example: python train.py --lr 5e-2 --epochs 30 --max_len 300 --batch_size 8

Tommy Yang 11 Jul 21, 2022
CCQA A New Web-Scale Question Answering Dataset for Model Pre-Training

CCQA: A New Web-Scale Question Answering Dataset for Model Pre-Training This is the official repository for the code and models of the paper CCQA: A N

Meta Research 29 Nov 30, 2022