Huggingface的Bert的第二个输出是什么意思?
What is the meaning of the second output of Huggingface's Bert?
在 huggingface 实现中使用基本 BERT 模型的香草配置,我得到一个长度为 2 的元组。
import torch
import transformers
from transformers import AutoModel,AutoTokenizer
bert_name="bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(bert_name)
BERT = AutoModel.from_pretrained(bert_name)
e=tokenizer.encode('I am hoping for the best', add_special_tokens=True)
q=BERT(torch.tensor([e]))
print (len(q)) #Output: 2
第一个元素是我期望收到的 - 每个输入标记的 768 维嵌入。
print (e) #Output : [101, 1045, 2572, 5327, 2005, 1996, 2190, 102]
print (q[0].shape) #Output : torch.Size([1, 8, 768])
但是元组中的第二个元素是什么?
print (q[1].shape) # torch.Size([1, 768])
它与每个令牌的编码大小相同。
但它是什么?
也许是 [CLS] 令牌的副本,表示整个编码文本的分类?
让我们检查一下。
a= q[0][:,0,:]
b=q[1]
print (torch.eq(a,b)) #Output : Tensor([[False, False, False, .... False]])
不!
复制最后一个标记的嵌入(无论出于何种原因)怎么样?
c= q[0][:,-1,:]
b=q[1]
print (torch.eq(a,c)) #Output : Tensor([[False, False, False, .... False]])
所以,也不是那样。
文档讨论了如何更改 config
可以导致更多的元组元素(如隐藏状态),但我没有找到任何关于默认配置输出的 "mysterious" 元组元素的描述.
关于它是什么以及它的用途有什么想法吗?
这种情况下的输出是 (last_hidden_state
, pooler_output
) 的元组。您可以找到有关 returns 可能是什么的文档 here.
在 huggingface 实现中使用基本 BERT 模型的香草配置,我得到一个长度为 2 的元组。
import torch
import transformers
from transformers import AutoModel,AutoTokenizer
bert_name="bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(bert_name)
BERT = AutoModel.from_pretrained(bert_name)
e=tokenizer.encode('I am hoping for the best', add_special_tokens=True)
q=BERT(torch.tensor([e]))
print (len(q)) #Output: 2
第一个元素是我期望收到的 - 每个输入标记的 768 维嵌入。
print (e) #Output : [101, 1045, 2572, 5327, 2005, 1996, 2190, 102]
print (q[0].shape) #Output : torch.Size([1, 8, 768])
但是元组中的第二个元素是什么?
print (q[1].shape) # torch.Size([1, 768])
它与每个令牌的编码大小相同。 但它是什么?
也许是 [CLS] 令牌的副本,表示整个编码文本的分类?
让我们检查一下。
a= q[0][:,0,:]
b=q[1]
print (torch.eq(a,b)) #Output : Tensor([[False, False, False, .... False]])
不!
复制最后一个标记的嵌入(无论出于何种原因)怎么样?
c= q[0][:,-1,:]
b=q[1]
print (torch.eq(a,c)) #Output : Tensor([[False, False, False, .... False]])
所以,也不是那样。
文档讨论了如何更改 config
可以导致更多的元组元素(如隐藏状态),但我没有找到任何关于默认配置输出的 "mysterious" 元组元素的描述.
关于它是什么以及它的用途有什么想法吗?
这种情况下的输出是 (last_hidden_state
, pooler_output
) 的元组。您可以找到有关 returns 可能是什么的文档 here.