🚀 SimCSE(sup)
SimCSE(sup) 是一個用於句子相似度任務的模型。它基於特定的語言模型,在多箇中文數據集上進行評估,能夠將句子編碼為嵌入向量,並計算句子之間的餘弦相似度。
🚀 快速開始
本模型可用於將句子編碼為嵌入向量,還能計算兩個句子之間的餘弦相似度。以下是使用示例:
import torch
from transformers import BertTokenizer
from transformers import BertModel
from sklearn.metrics.pairwise import cosine_similarity
simcse_sup_path = "hellonlp/simcse-roberta-base-zh"
tokenizer = BertTokenizer.from_pretrained(simcse_sup_path)
MODEL = BertModel.from_pretrained(simcse_sup_path)
def get_vector_simcse(sentence):
"""
預測simcse的語義向量。
"""
input_ids = torch.tensor(tokenizer.encode(sentence)).unsqueeze(0)
output = MODEL(input_ids)
return output.last_hidden_state[:, 0].squeeze(0)
embeddings = get_vector_simcse("武漢是一個美麗的城市。")
print(embeddings.shape)
def get_similarity_two(sentence1, sentence2):
vec1 = get_vector_simcse(sentence1).tolist()
vec2 = get_vector_simcse(sentence2).tolist()
similarity_list = cosine_similarity([vec1], [vec2]).tolist()[0][0]
return similarity_list
sentence1 = '你好嗎'
sentence2 = '你還好嗎'
result = get_similarity_two(sentence1,sentence2)
print(result)
📦 數據集列表
以下數據集均為中文數據集:
數據集 |
訓練集規模 |
驗證集規模 |
測試集規模 |
ATEC |
62477 |
20000 |
20000 |
BQ |
100000 |
10000 |
10000 |
LCQMC |
238766 |
8802 |
12500 |
PAWSX |
49401 |
2000 |
2000 |
STS - B |
5231 |
1458 |
1361 |
SNLI |
146828 |
2699 |
2618 |
MNLI |
122547 |
2932 |
2397 |
📊 模型列表
評估數據集為中文,我們在不同方法上使用了相同的語言模型 RoBERTa base。此外,考慮到部分數據集的測試集規模較小,可能導致評估準確性偏差較大,這裡的評估數據同時使用了訓練集、驗證集和測試集,最終評估結果採用 加權平均 (w - avg) 方法。
模型 |
STS - B(w - avg) |
ATEC |
BQ |
LCQMC |
PAWSX |
平均 |
BERT - Whitening |
65.27 |
- |
- |
- |
- |
- |
SimBERT |
70.01 |
- |
- |
- |
- |
- |
SBERT - Whitening |
71.75 |
- |
- |
- |
- |
- |
[BAAI/bge - base - zh](https://huggingface.co/BAAI/bge - base - zh) |
78.61 |
- |
- |
- |
- |
- |
[hellonlp/simcse - base - zh(sup)](https://huggingface.co/hellonlp/simcse - roberta - base - zh) |
80.96 |
- |
- |
- |
- |
- |
📄 許可證
本項目採用 MIT 許可證。