有没有办法从huggingface 的meta-llama/Llama-2-13b-chat-hf 获取句子嵌入?
\n模特链接:https://huggingface.co/meta-llama/Llama-2-13b-chat-hf
\n我尝试使用拥抱面孔中的 transfomer.Automodel 模块来获取嵌入,但结果看起来并不符合预期。实施方式参见以下链接。参考:https ://github.com/Muennighoff/sgpt#ametry-semantic-search-be\xc2\xa0
\nartificial-intelligence huggingface-transformers huggingface large-language-model llama
我正在尝试从 BERT 模型中的隐藏状态中获取句子向量。查看这里的 Huggingface BertModel 说明,其中说:
from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-multilingual-cased')
model = BertModel.from_pretrained("bert-base-multilingual-cased")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
Run Code Online (Sandbox Code Playgroud)
所以首先要注意,因为它在网站上,它 /not/ 运行。你得到:
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'BertTokenizer' object is not callable
Run Code Online (Sandbox Code Playgroud)
但它看起来像是一个小改动修复了它,因为您不直接调用标记器,而是要求它对输入进行编码:
encoded_input = tokenizer.encode(text, return_tensors="pt")
output = model(encoded_input)
Run Code Online (Sandbox Code Playgroud)
好的,除此之外,我得到的张量的形状与我预期的不同:
>>> output[0].shape
torch.Size([1,11,768])
Run Code Online (Sandbox Code Playgroud)
这是很多层。哪个是用于句子嵌入的正确层? [0]? [-1]? 平均几个?我的目标是能够与这些进行余弦相似度,所以我需要一个适当的 1xN 向量而不是 NxK 张量。
我看到流行的bert-as-a-service …