语言: 中文
任务标签: 句子相似度
标签:
- 句子转换器
- 特征提取
- 句子相似度
- 转换器
许可证: Apache-2.0
小部件示例:
- 源句: "那个人很开心"
对比句:
- "那个人非常开心"
- "那只猫很开心"
- "那个人在吃东西"
中文句子BERT模型
模型描述
本模型是由UER-py预训练的句子嵌入模型,相关论文见此处。此外,该模型也可通过腾讯预训练框架TencentPretrain进行预训练,该框架继承UER-py支持十亿级参数模型,并扩展为多模态预训练框架,详见此论文。
使用方法
该模型可用于提取句子嵌入以计算相似度。以下示例使用余弦距离计算句子嵌入相似度:
>>> 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]])
训练数据
使用中文文本推理数据集ChineseTextualInference进行训练。
训练过程
模型基于chinese_roberta_L-12_H-768预训练模型,在腾讯云上通过UER-py微调。训练配置如下:
- 序列长度:128
- 训练轮次:5
- 每轮结束后保存开发集性能最优的模型
训练命令:
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
引用信息
@article{reimers2019sentence,
title={Sentence-bert: 基于孪生BERT网络的句子嵌入},
author={Reimers, Nils and Gurevych, Iryna},
journal={arXiv预印本 arXiv:1908.10084},
year={2019}
}
@article{zhao2019uer,
title={UER: 预训练模型开源工具包},
author={赵哲等},
journal={EMNLP-IJCNLP 2019},
pages={241},
year={2019}
}
@article{zhao2023tencentpretrain,
title={TencentPretrain: 多模态预训练模型的可扩展工具包},
author={赵哲等},
journal={ACL 2023},
pages={217},
year={2023}