语言: 中文
数据集: CLUECorpusSmall
示例输入:
中文ALBERT模型
模型描述
这是由UER-py预训练的中文ALBERT模型系列,相关论文见此处。此外,这些模型也可通过TencentPretrain进行预训练,该框架继承UER-py支持十亿级参数模型,并扩展为多模态预训练框架,详见此论文。
您可以从UER-py模型库页面下载模型,或通过HuggingFace获取:
|
下载链接 |
ALBERT-Base |
[L=12/H=768 (基础版)][base] |
ALBERT-Large |
[L=24/H=1024 (大型版)][large] |
使用方式
可直接通过文本生成管道使用该模型:
>>> from transformers import BertTokenizer, AlbertForMaskedLM, FillMaskPipeline
>>> tokenizer = BertTokenizer.from_pretrained("uer/albert-base-chinese-cluecorpussmall")
>>> model = AlbertForMaskedLM.from_pretrained("uer/albert-base-chinese-cluecorpussmall")
>>> unmasker = FillMaskPipeline(model, tokenizer)
>>> unmasker("中国的首都是[MASK]京。")
[
{'sequence': '中 国 的 首 都 是 北 京 。',
'score': 0.8528032898902893,
'token': 1266,
'token_str': '北'},
{'sequence': '中 国 的 首 都 是 南 京 。',
'score': 0.07667620480060577,
'token': 1298,
'token_str': '南'},
{'sequence': '中 国 的 首 都 是 东 京 。',
'score': 0.020440367981791496,
'token': 691,
'token_str': '东'},
{'sequence': '中 国 的 首 都 是 维 京 。',
'score': 0.010197942145168781,
'token': 5335,
'token_str': '维'},
{'sequence': '中 国 的 首 都 是 汴 京 。',
'score': 0.0075391442514956,
'token': 3745,
'token_str': '汴'}
]
在PyTorch中获取文本特征的示例:
from transformers import BertTokenizer, AlbertModel
tokenizer = BertTokenizer.from_pretrained("uer/albert-base-chinese-cluecorpussmall")
model = AlbertModel.from_pretrained("uer/albert-base-chinese-cluecorpussmall")
text = "用你喜欢的任何文本替换我。"
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
TensorFlow版本:
from transformers import BertTokenizer, TFAlbertModel
tokenizer = BertTokenizer.from_pretrained("uer/albert-base-chinese-cluecorpussmall")
model = TFAlbertModel.from_pretrained("uer/albert-base-chinese-cluecorpussmall")
text = "用你喜欢的任何文本替换我。"
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
训练数据
使用CLUECorpusSmall作为训练语料。
训练流程
模型通过UER-py在腾讯云上预训练。先以128序列长度训练1,000,000步,再以512序列长度追加训练250,000步。不同规模模型采用相同超参数。
以ALBERT-Base为例:
第一阶段:
python3 preprocess.py --corpus_path corpora/cluecorpussmall_bert.txt \
--vocab_path models/google_zh_vocab.txt \
--dataset_path cluecorpussmall_albert_seq128_dataset.pt \
--seq_length 128 --processes_num 32 --data_processor albert
python3 pretrain.py --dataset_path cluecorpussmall_albert_seq128_dataset.pt \
--vocab_path models/google_zh_vocab.txt \
--config_path models/albert/base_config.json \
--output_model_path models/cluecorpussmall_albert_base_seq128_model.bin \
--world_size 8 --gpu_ranks 0 1 2 3 4 5 6 7 \
--total_steps 1000000 --save_checkpoint_steps 100000 --report_steps 50000 \
--learning_rate 1e-4 --batch_size 64
第二阶段:
python3 preprocess.py --corpus_path corpora/cluecorpussmall_bert.txt \
--vocab_path models/google_zh_vocab.txt \
--dataset_path cluecorpussmall_albert_seq512_dataset.pt \
--seq_length 512 --processes_num 32 --data_processor albert
python3 pretrain.py --dataset_path cluecorpussmall_albert_seq512_dataset.pt \
--vocab_path models/google_zh_vocab.txt \
--pretrained_model_path models/cluecorpussmall_albert_base_seq128_model.bin-1000000 \
--config_path models/albert/base_config.json \
--output_model_path models/cluecorpussmall_albert_base_seq512_model.bin \
--world_size 8 --gpu_ranks 0 1 2 3 4 5 6 7 \
--total_steps 1000000 --save_checkpoint_steps 100000 --report_steps 50000 \
--learning_rate 1e-4 --batch_size 64
最后转换为Huggingface格式:
python3 scripts/convert_albert_from_uer_to_huggingface.py --input_model_path models/cluecorpussmall_albert_base_seq512_model.bin-1000000 \
--output_model_path pytorch_model.bin
引用信息
@article{lan2019albert,
title={ALBERT: 一种轻量级BERT用于语言表示自监督学习},
author={Lan, Zhenzhong and Chen, Mingda and Goodman, Sebastian and Gimpel, Kevin and Sharma, Piyush and Soricut, Radu},
journal={arXiv预印本 arXiv:1909.11942},
year={2019}
}
@article{zhao2019uer,
title={UER: 预训练模型开源工具包},
author={Zhao, Zhe and Chen, Hui and Zhang, Jinbin and Zhao, Xin and Liu, Tao and Lu, Wei and Chen, Xi and Deng, Haotang and Ju, Qi and Du, Xiaoyong},
journal={EMNLP-IJCNLP 2019},
pages={241},
year={2019}
}
@article{zhao2023tencentpretrain,
title={TencentPretrain: 支持多模态预训练的可扩展灵活工具包},
author={Zhao, Zhe and Li, Yudong and Hou, Cheng and Zhao, Jing and others},
journal={ACL 2023},
pages={217},
year={2023}