为什么变压器解码器总是产生和黄金标签相同长度的输出



我正在使用经过微调的BART模型生成一些摘要,我注意到了一些奇怪的事情。如果我将标签提供给模型,它将始终生成与标签相同长度的摘要,而如果我不将标签传递给模型,则它将生成长度为1024(最大BART序列长度(的输出。这是出乎意料的,所以我试图了解下面的可复制示例是否存在任何问题/错误

from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
model=AutoModelForSeq2SeqLM.from_pretrained('facebook/bart-large-cnn')
tokenizer=AutoTokenizer.from_pretrained('facebook/bart-large-cnn')

sentence_to_summarize = ['This is a text to summarise. I just went for a walk in the park and saw very large crowds gathering to watch an impromptu football match']
encoded_dict = tokenizer.batch_encode_plus(sentence_to_summarize, return_tensors='pt', max_length=1024, padding='max_length')
input_ids = encoded_dict['input_ids']
attention_mask = encoded_dict['attention_mask']
label = tokenizer.encode('I went to the park', return_tensors='pt')

请注意以下两种情况
情况1:

output = model(input_ids=input_ids, attention_mask=attention_mask)
print(output['logits'].shape)

打印的形状是torch.Size([1, 1024, 50264])

案例2

output = model(input_ids=input_ids, attention_mask=attention_mask, labels=label)
print(output['logits'].shape)

打印的形状是CCD_ 2,其中7是标签CCD_
理想情况下,摘要模型将了解何时生成EOS令牌,但这不应总是导致黄金输出(即标签(的长度相同的摘要。为什么标签长度会以这种方式影响模型输出?

我认为情况1和2之间的唯一区别是,在第二种情况下,输出也包含损失值,但我不认为这会以任何方式影响logits

原始示例不使用标签参数

https://huggingface.co/docs/transformers/v4.22.1/en/model_doc/bart#transformers.BartForConditionalGeneration.forward.example

标签参数是可选的,我认为不用于汇总

https://huggingface.co/docs/transformers/v4.22.1/en/model_doc/bart#transformers.BartForConditionalGeneration.forward.labels

最新更新