python - Longformer 获取last_hidden_​​state

标签 python nlp pytorch huggingface-transformers

我正在尝试遵循此处的huggingface文档中的示例https://huggingface.co/transformers/model_doc/longformer.html :

import torch
from transformers import LongformerModel, LongformerTokenizer
model = LongformerModel.from_pretrained('allenai/longformer-base-4096')
tokenizer = LongformerTokenizer.from_pretrained('allenai/longformer-base-4096')
SAMPLE_TEXT = ' '.join(['Hello world! '] * 1000)  # long input document
input_ids = torch.tensor(tokenizer.encode(SAMPLE_TEXT)).unsqueeze(0)  # batch of size 1
# Attention mask values -- 0: no attention, 1: local attention, 2: global attention
attention_mask = torch.ones(input_ids.shape, dtype=torch.long, device=input_ids.device) # initialize to local attention
global_attention_mask = torch.zeros(input_ids.shape, dtype=torch.long, device=input_ids.device) # initialize to global attention to be deactivated for all tokens
global_attention_mask[:, [1, 4, 21,]] = 1  # Set global attention to random tokens for the sake of this example
                                    # Usually, set global attention based on the task. For example,
                                    # classification: the <s> token
                                    # QA: question tokens
                                    # LM: potentially on the beginning of sentences and paragraphs
outputs = model(input_ids, attention_mask=attention_mask, global_attention_mask=global_attention_mask, output_hidden_states= True)
sequence_output = outputs[0].last_hidden_state
pooled_output = outputs.pooler_output

我认为这将返回示例文本的文档嵌入。 但是,我遇到了以下错误:

AttributeError: 'Tensor' object has no attribute 'last_hidden_state'

为什么不能调用last_hidden_​​state?

最佳答案

不通过索引选择:

sequence_output = outputs.last_hidden_state

outputs 是一个具有以下属性的 LongformerBaseModelOutputWithPooling 对象:

print(outputs.keys())

输出:

odict_keys(['last_hidden_state', 'pooler_output', 'hidden_states'])

调用outputs[0]outputs.last_hidden_​​state都会给你相同的张量,但这个张量没有名为last_hidden_​​state的属性>.

关于python - Longformer 获取last_hidden_​​state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66655023/

相关文章:

python - 删除具有相反单词的重复二元组

python - [ orth , pos , tag , lema 和 text ] 的 spaCy 文档

python - 使用 Look Behind 或 Look Ahead 函数查找匹配项时的正则表达式模式

python - HTTP 错误 503 : Service Unavailable when trying to download MNIST data

python - python的time.sleep()有多准确?

python - 在Python列表中移动元素

Python Seaborn FacetGrid 不同的 y 轴

python - 退出脚本后 cd 到目录(系统独立方式,纯粹在 python 中)

python-3.x - Pytorch 灰度输入到 Vgg

python - 是否可以使用 pyTorch 创建一个 FIFO 队列?