keras简单使用bert

之前一直使用tf来使用bert,看着keras简单,闲来无事,玩玩。并且总结一下。把各种模型和权重的关系梳理一下。

1 bert库

1.1 bert4keras

keras-bert的github
bert4keras的github
【特别注意】这里是在conda虚拟环境下操作的,请看Could not load dynamic library cudart64_100.dll

pip3 install tensorflow-gpu==1.15.0 keras==2.3.1 bert4keras==0.8.8 # keras_bert==0.86.0
# keras4bert要求keras为2.3.1
# pip3 install keras==2.4.3

1.2 标准测试数据集

为了测试,总结了一些数据集列表。

名称链接
中文文本分类数据集THUCNewshttps://pan.baidu.com/s/1OC2uMoVHAWtX4mfOv6urCg 提取码: qphu
bojone整理的情感二分类数据集https://github.com/bojone/bert4keras/tree/master/examples/datasets
SophonPlus整理的中文各种语料https://github.com/SophonPlus/ChineseNlpCorpus
CLUEbenchmark中文语言理解基准数据集https://github.com/CLUEbenchmark/CLUE

1.3 bert权重列表

这里参考bert4keras总结而来,按照组织划分,非常清楚,对各种自研训练出来的权重可以自行选择。

名称链接
Google原版berthttps://github.com/google-research/bert(一直往下看即可)
Google原版alberthttps://github.com/google-research/ALBERT
Google原版ELECTRAhttps://github.com/google-research/electra
Google原版T5模型https://github.com/google-research/text-to-text-transfer-transformer

brightmart版alberthttps://github.com/brightmart/albert_zh
brightmart版robertahttps://github.com/brightmart/roberta_zh 中文主页
brightmart版转换后的bojone版本alberthttps://github.com/bojone/albert_zh
bojone版本LaBSE[多国语言BERT]https://github.com/bojone/labse
bojone版本Chinese-GEN模型https://github.com/bojone/chinese-gen
追一科技训练的语言模型https://github.com/ZhuiyiTechnology/pretrained-models
GPT_OpenAIhttps://github.com/bojone/CDial-GPT-tf
GPT2_MLhttps://github.com/imcaspar/gpt2-ml

哈工大版robertahttps://github.com/ymcui/Chinese-BERT-wwm
哈工大版ELECTRAhttps://github.com/ymcui/Chinese-ELECTRA

华为的NEZHAhttps://github.com/huawei-noah/Pretrained-Language-Model/tree/master/NEZHA-TensorFlow
华为的NEZHA-GENhttps://github.com/huawei-noah/Pretrained-Language-Model/tree/master/NEZHA-Gen-TensorFlow

CLUE版ELECTRAhttps://github.com/CLUEbenchmark/ELECTRA

【注意】

  • 【1】Google官方发布的BERT-base, Chinese是以中文的字为粒度进行切分训练
  • 【2】Uncased指忽略大小写,在BERT训练时将语料全部小写化处理
  • 【3】bert在训练时,句首添加 [CLS],句子对<A,B>A后面添加 [SEP] [MASK]用于遮罩
  • 【4】WWM指Whole Word Masking(全词掩盖,全词/整词Mask),是BERT训练时语料预处理的一个trick,就是在做mask对字时如果是一个完整的词应该也顺带mask掉,比如想把苹果的果给mask,那么应该直接mask苹果
说明样例
原始文本使用语言模型来预测下一个词的probability。
分词文本使用 语言 模型预测 下 一个 词 的 probability
原始Mask输入[CLS] 使 用 语 言 [MASK] 型[MASK] 测 下 一 个 词 的 pro [MASK] ##lity 。 [SEP]
全词Mask输入[CLS] 使 用 语 言 [MASK] [MASK][MASK] [MASK] 下 一 个 词 的 [MASK] [MASK] [MASK] 。 [SEP]

2 keras版本

为了跑通相关的代码,协调keras、tensorflow1.15.0、tensorflow2.3.0、pytorch==1.6.0和cuda10.1,需要安装和配置好一些环境,请参考Could not load dynamic library cudart64_100.dll

如果需要将TensorFlow版本BERT各种权重转换为Pytorch,可使用https://huggingface.co/transformers/converting_tensorflow_models.html


2.1 下载各种权重

权重评价表现参考https://github.com/CLUEbenchmark/CLUE


名称链接
BERT-Base, Chinese来源 Chinese Simplified and Traditional, 12-layer, 768-hidden, 12-heads, 110M parameters
albert_small_google_zh(累积学习10亿个样本,google版本)来源 速度比bert_base快4倍;LCQMC测试集上比Bert下降仅0.9个点;去掉adam后模型大小18.5M;使用方法,见 #下游任务 Fine-tuning on Downstream Task
albert_tiny_google_zh(累积学习10亿个样本,google版本来源 模型大小16M、性能与albert_tiny_zh一致)
Google,albert Base Chinese来源
Google,albert Large Chinese来源
Google,albert XLarge Chinese来源
Google,albert XxLarge Chinese来源
RoBERTa-wwm-ext-large, Chinese来源 chinese_roberta_wwm_large_ext_L-24_H-1024_A-16 密码 u6gC
RoBERTa-wwm-ext, Chinese, Chinese来源 chinese_roberta_wwm_ext_L-12_H-768_A-12 密码 Xe1p

2.2 标准测试example

为做一个完整的总结,这里总结列一下各位优秀开源工作者提供的各种测试example code。

名称支持库链接
追一科技提供的各种bert样例代码kerashttps://github.com/ZhuiyiTechnology/pretrained-models/tree/master/examples README
bert4keras的examplekerashttps://github.com/bojone/bert4keras/tree/master/examples
CLUE中文语言理解测评基准exampletf1.12https://github.com/CLUEbenchmark/CLUE PyCLUE

以前只是浅浅看看paper,后面再一一分析各个模型内部代码,里面到底做了什么事情。

Reference

bert_in_keras
bert4keras

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值