🚀 中文句子BERT
本项目的模型可用于句子相似度任务,通过预训练学习句子的语义表示,能够有效提取句子嵌入向量,为文本语义理解提供有力支持。
🚀 快速开始
你可以使用此模型提取句子嵌入向量,用于句子相似度任务。这里我们使用余弦距离来计算嵌入向量的相似度:
>>> from sentence_transformers import SentenceTransformer
>>> model = SentenceTransformer('uer/sbert-base-chinese-nli')
>>> sentences = ['那个人很开心', '那个人非常开心']
>>> sentence_embeddings = model.encode(sentences)
>>> from sklearn.metrics.pairwise import paired_cosine_distances
>>> cosine_score = 1 - paired_cosine_distances([sentence_embeddings[0]],[sentence_embeddings[1]])
✨ 主要特性
这是由 UER-py 预训练的句子嵌入模型,该模型在 这篇论文 中被介绍。此外,该模型也可以由 TencentPretrain 进行预训练,相关内容在 这篇论文 中有介绍。TencentPretrain 继承了 UER-py 的优点,支持参数超过十亿的模型,并将其扩展为一个多模态预训练框架。
📦 安装指南
文档未提及具体安装步骤,可参考相关库(如sentence-transformers
)的官方文档进行安装。
📚 详细文档
训练数据
使用 ChineseTextualInference 作为训练数据。
训练过程
该模型在 腾讯云 上通过 UER-py 进行微调。我们在预训练模型 chinese_roberta_L-12_H-768 的基础上,以序列长度 128 进行了五个轮次的微调。在每个轮次结束时,当模型在验证集上达到最佳性能时,会保存该模型。
python3 finetune/run_classifier_siamese.py --pretrained_model_path models/cluecorpussmall_roberta_base_seq512_model.bin-250000 \
--vocab_path models/google_zh_vocab.txt \
--config_path models/sbert/base_config.json \
--train_path datasets/ChineseTextualInference/train.tsv \
--dev_path datasets/ChineseTextualInference/dev.tsv \
--learning_rate 5e-5 --epochs_num 5 --batch_size 64
最后,我们将预训练模型转换为 Huggingface 格式:
python3 scripts/convert_sbert_from_uer_to_huggingface.py --input_model_path models/finetuned_model.bin \
--output_model_path pytorch_model.bin \
--layers_num 12
BibTeX 引用和引用信息
@article{reimers2019sentence,
title={Sentence-bert: Sentence embeddings using siamese bert-networks},
author={Reimers, Nils and Gurevych, Iryna},
journal={arXiv preprint arXiv:1908.10084},
year={2019}
}
@article{zhao2019uer,
title={UER: An Open-Source Toolkit for Pre-training Models},
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: A Scalable and Flexible Toolkit for Pre-training Models of Different Modalities},
author={Zhao, Zhe and Li, Yudong and Hou, Cheng and Zhao, Jing and others},
journal={ACL 2023},
pages={217},
year={2023}
📄 许可证
本项目采用 Apache-2.0 许可证。