运行带有BART编码器的huggingface BART模型解码器,该编码器已覆盖注意头

2024-04-26 13:24:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我期待着建立一个管道,应用拥抱面对巴特模型一步一步。一旦我建立了管道,我将寻求用预先培训/预定义的编码器注意头替换编码器注意头

我希望实施的管道如下:

  1. 标记化输入
  2. 通过编码器运行标记化输入,并调整注意层
  3. 通过解码器运行输出
  4. 将解码器的输出更改为文本摘要

目前,我的代码看起来像下面的注释,我被卡住了

from transformers import AutoTokenizer, AutoModel, BartConfig, EncoderDecoderModel

article = """Text to be summarised."""

model_name = "facebook/bart-large-cnn"

# Values of dictionaries are tensors
attention_heads = {"Cars": cars_encoder_attention_layer, 
                   "Countries": countries_encoder_attention_layer
                  }

model_name = "facebook/bart-large-cnn"
config = BartConfig.from_pretrained(model_name, output_hidden_states=True, output_attention=True)
tokenizer = AutoTokenizer.from_pretrained(model_name)
inputs = tokenizer(article, padding=True, truncation=True, return_tensors="pt")
model = AutoModel.from_pretrained(model_name)
model.config.output_attentions = True
outputs = model(**inputs)

# Overwrite the encoder attentions with the desired attention heads
outputs.encoder_attentions = attention_heads ["Cars"]

# Here I would take the overwritten encoder and insert into the decoder to generate the summary with the adjusted attention heads

Tags: thenamefrom标记trueencoderoutputmodel