使用hf推荐的语句引用的时候,发生"No sentence-transformers model found..." feedback
使用hf推荐的语句引用的时候,发生以下报错。
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("uer/sbert-base-chinese-nli")
No sentence-transformers model found with name /Users/peter42/.cache/torch/sentence_transformers/uer_sbert-base-chinese-nli. Creating a new one with MEAN pooling.
请问这个是什么原因?sbert的sentence transformer包没有能加载上这个model?
虽然代码还是可以反馈数值,例如sentence similarity, 但是这种情况下,和这个模型有关系么?还是sentence-transformers自己加载了一个缺省model继续走下去了?
请问解决了吗
I have the same problem
I have the same problem
i have the same problem
I have the same problem. To solve this, I use the transformers to invoke the model like this.
###
sentences = ['那个人很开心', '那个人非常开心']
import torch
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("uer/sbert-base-chinese-nli",cache_dir=r"Your path")
model = AutoModel.from_pretrained("uer/sbert-base-chinese-nli")
tokens = tokenizer(
sentences,
padding = True,
max_length = 128,
truncation=True)
seq = torch.tensor(tokens['input_ids'])
mask = torch.tensor(tokens['attention_mask'])
sentence_embeddings=model(seq,mask)
print("sentence_embeddings",sentence_embeddings) #You can see the type is BaseModelOutputWithPoolingAndCrossAttentions
last_hidden,_= model(input_ids=seq, attention_mask=mask)[:2]
pooled_output = torch.mean(last_hidden,dim=1)
print("last_hidden",last_hidden)
print("last_hidden.shape",last_hidden.shape)
print("pooled_output ",pooled_output )
print("pooled_output.shape ",pooled_output.shape)
from sklearn.metrics.pairwise import paired_cosine_distances
cosine_score = 1 - paired_cosine_distances([pooled_output.detach().numpy()[0]],[pooled_output.detach().numpy()[1]])
print(cosine_score)# Load model directly
###
So, you can find the origin model may not have a pooling layer,which lead to the wrong and the SentenceTransformer automatically create a pooling layer for feature normalization.
By using my code invoke model, you will get the similarity is [0.98678935], while the sbert pooling output is [0.98649204] .
So, as a result,it's ok ,feel free to use it by SentenceTransformer .