Skip to main content

A pretrained NLP model for every NLP tasks

Project description

English|简体中文

./.metas/ERNIE_milestone.png

ERNIE是百度开创性提出的基于知识增强的持续学习语义理解框架,该框架将大数据预训练与多源丰富知识相结合,通过持续学习技术,不断吸收海量文本数据中词汇、结构、语义等方面的知识,实现模型效果不断进化。ERNIE在情感分析、文本匹配、自然语言推理、词法分析、阅读理解、智能问答等16个公开数据集上全面显著超越世界领先技术,在国际权威的通用语言理解评估基准GLUE上,得分首次突破90分,获得全球第一。在今年3月落下帷幕的全球最大语义评测SemEval 2020上,ERNIE摘得5项世界冠军, 该技术也被全球顶级科技商业杂志《麻省理工科技评论》官方网站报道,相关创新成果也被国际顶级学术会议AAAI、IJCAI收录。ERNIE在工业界得到了大规模应用,如搜索引擎、新闻推荐、广告系统、语音交互、智能客服等。

提醒: ERNIE老版本代码已经迁移至repro分支,欢迎使用我们全新升级的基于动静结合的新版ERNIE套件进行开发。另外,也欢迎上EasyDL体验更丰富的功能(如ERNIE 2.0、ERNIE 2.1、ERNIE领域模型等)。

新闻

  • 2020.5.20:
    • 欢迎试用动态图实现的 ERNIE:
      • 基于PaddlePaddle v1.8使用 ERNIE 进行 Pretrain 和 Finetune.
      • 动态执行, 所见即所得。
      • 大规模分布式训练。
      • 易于部署。
      • 通过Aistudio 教程快速入门NLP。
      • 向后兼容老版 checkpoint。
    • ERNIE-GEN 模型正式开源! (点击进入)
      • 最强文本生成预训练模型正式开源,相关工作已被 IJCAI-2020 收录。
        • 首次把 ERNIE 预训练技术能力扩展至文本生成领域,在多个典型任务上取得最佳。
        • 您现在即可下载论文报告的所有模型(包含 base/large/large-160G)。
      • 首次在预训练阶段加入span-by-span 生成任务,让模型每次能够生成一个语义完整的片段。
      • 提出填充式生成机制和噪声感知机制来缓解曝光偏差问题。
      • 精巧的 Mulit-Flow Attention 实现框架。
  • 2020.4.30 发布ERNIESage, 一种新型图神经网络模型,采用ERNIE做为aggreagtor. 由PGL实现。
  • 2020.3.27 在SemEval2020五项子任务上夺冠
  • 2019.12.26 GLUE榜第一名
  • 2019.11.6 发布ERNIE Tiny
  • 2019.7.7 发布ERNIE 2.0
  • 2019.3.16 发布ERNIE 1.0

导航

快速上手

import numpy as np
import paddle.fluid.dygraph as D
from ernie.tokenizing_ernie import ErnieTokenizer
from ernie.modeling_ernie import ErnieModel

D.guard().__enter__() # activate paddle `dygrpah` mode

model = ErnieModel.from_pretrained('ernie-1.0')    # Try to get pretrained model from server, make sure you have network connection
tokenizer = ErnieTokenizer.from_pretrained('ernie-1.0')

ids, _ = tokenizer.encode('hello world')
ids = D.to_variable(np.expand_dims(ids, 0))  # insert extra `batch` dimension
pooled, encoded = model(ids)                 # eager execution
print(pooled.numpy())                        # convert  results to numpy

教程

手边没有GPU?欢迎在AIStudio中直接试用 ERNIE. (请选择最新版本的教程并申请GPU运行环境)

  1. 从0开始学ERNIE
  2. 情感识别
  3. 完形填空
  4. 知识蒸馏
  5. 万事不决问ERNIE

安装

1. 安装 PaddlePaddle

本项目依赖PaddlePaddle 1.7.0+, 请参考这里安装 PaddlePaddle。

2. 安装 ERNIE
pip install paddle-ernie

或者

git clone -b dygraph https://github.com/PaddlePaddle/ERNIE.git --single-branch
cd ERNIE
pip install -r requirements.txt
pip setup.py -e .
3. 下载预训练模型(可选)
Model Description
ERNIE 1.0 Base 中文 L12H768A12
ERNIE Tiny L3H1024A16
ERNIE 2.0 Base 英文 base: L12H768A12
ERNIE 2.0 Large 英文 large: L24H1024A16
ERNIE Gen base 英文 L12H768A12
ERNIE Gen Large 英文 L24H1024A16
4. 下载数据集

英文数据集

运行脚本,下载GLUE datasets.

请将数据目录整理成以下格式,方便在后续 demo 教程中使用(通过--data_dir参数将数据路径传入训练脚本);

data/xnli
├── dev
│   └── 1
├── test   └── 1
└── train
    └── 1

示例数据(MNLI任务测试、训练集合)。

中文数据

数据集 描述
XNLI XNLI 是由 Facebook 和纽约大学的研究者联合构建的自然语言推断数据集,包括 15 种语言的数据。我们用其中的中文数据来评估模型的语言理解能力。链接
ChnSentiCorp ChnSentiCorp 是一个中文情感分析数据集,包含酒店、笔记本电脑和书籍的网购评论。
MSRA-NER MSRA-NER (SIGHAN2006) 数据集由微软亚研院发布,其目标是识别文本中具有特定意义的实体,包括人名、地名、机构名。
NLPCC2016-DBQA NLPCC2016-DBQA 是由国际自然语言处理和中文计算会议 NLPCC 于 2016 年举办的评测任务,其目标是从候选中找到合适的文档作为问题的答案。链接
CMRC2018 CMRC2018 是中文信息学会举办的评测,评测的任务是抽取类阅读理解。链接

支持的NLP任务

  • 使用 动态图 模型进行finetune:
python3 ./demo/finetune_classifier_dygraph.py \
    --from_pretrained ernie_1.0 \
    --data_dir ./data/xnli
  • 分布式 finetune

paddle.distributed.launch 是一个进程管理器,我们采用它在每一张GPU上启动一个python进程,并配置相应的环境变量以进行分布式训练:

当采用分布式训练时,我们采用max_steps做为终止条件而非epoch, 这样处理是为了避免进程间死锁。 另外值得注意的是训练集需要在不同的进程间进行切分;以避免所有进程训练同一份数据造成的过拟合。

示例脚本(请确保你有两张以上GPU卡):

python3 -m paddle.distributed.launch \
./demo/finetune_classifier_dygraph_distributed.py \
    --data_dir data/mnli \
    --max_steps 10000 \
    --from_pretrained ernie2.0-en

更多示例脚本:

  1. 情感分析
  2. 语义匹配
  3. 命名实体识别(NER)
  4. 机器阅读理解
  5. 文本摘要生成

推荐超参数设置:

任务 batch size learning rate
CoLA 32 / 64 (base) 3e-5
SST-2
64 / 256 (base) 2e-5
STS-B 128 5e-5
QQP 256 3e-5(base)/5e-5(large)
MNLI 256 / 512 (base) 3e-5
QNLI 256 2e-5
RTE 16 / 4 (base) 2e-5(base)/3e-5(large)
MRPC 16 / 32 (base) 3e-5
WNLI 8 2e-5
XNLI 512 1e-4(base)/4e-5(large)
CMRC2018 64 3e-5
DRCD 64 5e-5(base)/3e-5(large)
MSRA-NER(SIGHAN2006) 16 5e-5(base)/1e-5(large)
ChnSentiCorp 24 5e-5(base)/1e-5(large)
LCQMC 32 2e-5(base)/5e-6(large)
NLPCC2016-DBQA 64 2e-5(base)/1e-5(large)

预训练 (ERNIE 1.0)

请见这里

在线预测

如果finetune_classifier_dygraph.py中指定了--inference_model_dir参数,funetune脚本会将你的模型序列化并产出可以直接部署线上预测的inference_model.

关于生产环境中使用线上预测代码的实现细节,请见C++ inference API. 或者你可以使用propeller启动一个多GPU预测服务(需要GPU环境),只需执行:

python -m propeller.tools.start_server -m /path/to/saved/inference_model  -p 8881

即可启动预测服务;随后在Python端采用如下命令访问该服务(仅限 python3):

from propeller.service.client import InferenceClient
from ernie.tokenizing_ernie import ErnieTokenizer

client = InferenceClient('tcp://localhost:8881')
tokenizer = ErnieTokenizer.from_pretrained('ernie-1.0')
ids, sids = tokenizer.encode('hello world')
ids = np.expand_dims(ids, 0)
sids = np.expand_dims(sids, 0)
result = client(ids, sids)

你也可从此处下载一个预先制作好的ernie-1.0 base模型的 inference_model. 该模型没有经过finetune,一般可以用做上层模型结构的 feature-base finetune或者做为一个文本特征抽取器。 因为该模行由老版API 产出,在进行客户端请求时需要在输入tensor后面追加一个维度:

ids = np.expand_dims(ids, -1) # ids.shape==[BATCH, SEQLEN, 1]

蒸馏

知识蒸馏是进行ERNIE模型压缩、加速的有效方式;关于知识蒸馏的实现细节请参见这里

文献引用

ERNIE 1.0

@article{sun2019ernie,
  title={Ernie: Enhanced representation through knowledge integration},
  author={Sun, Yu and Wang, Shuohuan and Li, Yukun and Feng, Shikun and Chen, Xuyi and Zhang, Han and Tian, Xin and Zhu, Danxiang and Tian, Hao and Wu, Hua},
  journal={arXiv preprint arXiv:1904.09223},
  year={2019}
}

ERNIE 2.0

@article{sun2019ernie20,
  title={ERNIE 2.0: A Continual Pre-training Framework for Language Understanding},
  author={Sun, Yu and Wang, Shuohuan and Li, Yukun and Feng, Shikun and Tian, Hao and Wu, Hua and Wang, Haifeng},
  journal={arXiv preprint arXiv:1907.12412},
  year={2019} 
}

ERNIE-GEN

@article{xiao2020ernie-gen,
  title={ERNIE-GEN: An Enhanced Multi-Flow Pre-training and Fine-tuning Framework for Natural Language Generation},
  author={Xiao, Dongling and Zhang, Han and Li, Yukun and Sun, Yu and Tian, Hao and Wu, Hua and Wang, Haifeng},
  journal={arXiv preprint arXiv:2001.11314},
  year={2020}
}

若希望复现 paper 中的所有实验,请切换至本repo的repro分支。

讨论组

  • ERNIE官方主页
  • Github Issues: bug reports, feature requests, install issues, usage issues, etc.
  • QQ 群: 760439550 (ERNIE discussion group).
  • Forums: discuss implementations, research, etc.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

paddle-ernie-0.0.2.dev1.tar.gz (24.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page