魔搭下载地址https://www.modelscope.cn/models/Jerry0/text2vec-large-chinese
1. 推理代码
from sentence_transformers import SentenceTransformer
import torch
import numpy as np
from scipy.spatial.distance import cosinedevice = torch.device("cuda")# 加载模型
model = SentenceTransformer('/home/sky/model_data/text2vec-large-chinese').to(device)# 定义一些句子
sentences = ['这是一个美好的一天。','我喜欢在公园里散步。','明天会更好。',
]# 生成句子嵌入
embeddings = model.encode(sentences, convert_to_tensor=True, device=device)# 将张量转换回numpy数组以便打印
embeddings_np = [emb.cpu().numpy() for emb in embeddings]# 打印每个句子的向量表示
for sentence, embedding in zip(sentences, embeddings_np):print(f"句子: {sentence}")print(f"嵌入: {embedding[:10]}... (仅显示前10个元素)")print()# 目标字符串
target_sentence = '今天的天气真好。'# 生成目标字符串的嵌入
target_embedding = model.encode([target_sentence], convert_to_tensor=True, device=device)[0].cpu().numpy()# 计算相似度
similarities = [1 - cosine(target_embedding, emb) for emb in embeddings_np] # 余弦相似度# 找到最匹配的句子
most_similar_index = np.argmax(similarities)
most_similar_sentence = sentences[most_similar_index]
most_similar_score = similarities[most_similar_index]print(f"最匹配的句子: {most_similar_sentence}")
print(f"相似度得分: {most_similar_score:.4f}")