【问题标题】:What is the meaning of the second output of Huggingface's Bert?Huggingface的Bert第二个输出是什么意思?
【发布时间】:2020-05-31 05:50:13
【问题描述】:

在 huggingface 实现中使用基本 BERT 模型的 vanilla 配置,我得到一个长度为 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 会导致更多元组元素(如隐藏状态),但我没有找到默认配置输出的这个“神秘”元组元素的任何描述。

关于它是什么以及它的用途有什么想法吗?

【问题讨论】:

    标签: python deep-learning pytorch huggingface-transformers


    【解决方案1】:

    在这种情况下,输出是一个 (last_hidden_state, pooler_output) 的元组。您可以在here找到有关返回可能是什么的文档。

    【讨论】:

      猜你喜欢
      • 2011-10-22
      • 2020-09-18
      • 2016-10-05
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      相关资源
      最近更新 更多