🚀 中文句子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 許可證。