# bert4keras - Our light reimplement of bert for keras - 更清晰、更轻量级的keras版bert - 在线文档:http://bert4keras.spaces.ac.cn/ (还在构建中) ## 功能 目前已经实现: - 加载bert/roberta/albert的预训练权重进行finetune; - 实现语言模型、seq2seq所需要的attention mask; - 丰富的examples; - 从零预训练代码(支持TPU、多GPU,请看pretraining); - 兼容keras、tf.keras ## 使用 安装稳定版: ```shell pip install bert4keras ``` 安装最新版: ```shell pip install git+https://www.github.com/bojone/bert4keras.git ``` 使用例子请参考examples目录。 理论上兼容Python2和Python3,兼容tensorflow 1.14+和tensorflow 2.x,实验环境是Python 2.7、Tesorflow 1.14+以及Keras 2.3.1(已经在2.2.4、2.3.0、2.3.1、tf.keras下测试通过)。 **为了获得最好的体验,建议你使用Tensorflow 1.14 + Keras 2.3.1组合。**
关于环境组合 - 支持tf+keras和tf+tf.keras,后者需要提前传入环境变量TF_KERAS=1。 - 当使用tf+keras时,建议2.2.4 <= keras <= 2.3.1,以及 1.14 <= tf <= 2.2,不能使用tf 2.3+。 - keras 2.4+可以用,但事实上keras 2.4.x基本上已经完全等价于tf.keras了,因此如果你要用keras 2.4+,倒不如直接用tf.keras。
## 权重 目前支持加载的权重: - Google原版bert: https://github.com/google-research/bert - brightmart版roberta: https://github.com/brightmart/roberta_zh - 哈工大版roberta: https://github.com/ymcui/Chinese-BERT-wwm - Google原版albert[例子]: https://github.com/google-research/ALBERT - brightmart版albert: https://github.com/brightmart/albert_zh - 转换后的albert: https://github.com/bojone/albert_zh - 华为的NEZHA: https://github.com/huawei-noah/Pretrained-Language-Model/tree/master/NEZHA-TensorFlow - 华为的NEZHA-GEN: https://github.com/huawei-noah/Pretrained-Language-Model/tree/master/NEZHA-Gen-TensorFlow - 自研语言模型: https://github.com/ZhuiyiTechnology/pretrained-models - T5模型: https://github.com/google-research/text-to-text-transfer-transformer - GPT_OpenAI: https://github.com/bojone/CDial-GPT-tf - GPT2_ML: https://github.com/imcaspar/gpt2-ml - Google原版ELECTRA: https://github.com/google-research/electra - 哈工大版ELECTRA: https://github.com/ymcui/Chinese-ELECTRA - CLUE版ELECTRA: https://github.com/CLUEbenchmark/ELECTRA - LaBSE(多国语言BERT): https://github.com/bojone/labse - Chinese-GEN项目下的模型: https://github.com/bojone/chinese-gen - T5.1.1: https://github.com/google-research/text-to-text-transfer-transformer/blob/master/released_checkpoints.md#t511 - Multilingual T5: https://github.com/google-research/multilingual-t5/ 注意事项 - 注1:brightmart版albert的开源时间早于Google版albert,这导致早期brightmart版albert的权重与Google版的不完全一致,换言之两者不能直接相互替换。为了减少代码冗余,bert4keras的0.2.4及后续版本均只支持加载Google版以brightmart版中带Google字眼的权重。如果要加载早期版本的权重,请用0.2.3版本,或者考虑作者转换过的albert_zh。 - 注2:下载下来的ELECTRA权重,如果没有json配置文件的话,参考这里自己改一个(需要加上`type_vocab_size`字段)。