首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ValueError:您必须指定decoder_input_ids或decoder_inputs_embeds

ValueError:您必须指定decoder_input_ids或decoder_inputs_embeds
EN

Stack Overflow用户
提问于 2020-12-04 08:42:56
回答 1查看 5.3K关注 0票数 2

尝试将question-generation t5模型转换为torchscript 模型,同时运行此错误

ValueError:您必须指定decoder_input_ids或decoder_inputs_embeds

这是我在colab上运行的代码。

代码语言:javascript
运行
复制
!pip install -U transformers==3.0.0
!python -m nltk.downloader punkt

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch

model = AutoModelForSeq2SeqLM.from_pretrained('valhalla/t5-base-qg-hl')

t_input =  'Python is a programming language. It is developed by <hl> Guido Van Rossum <hl>. </s>'

tokenizer = AutoTokenizer.from_pretrained('valhalla/t5-base-qg-hl', return_tensors = 'pt')

def _tokenize(
    inputs,
    padding=True,
    truncation=True,
    add_special_tokens=True,
    max_length=64
):
    inputs = tokenizer.batch_encode_plus(
        inputs, 
        max_length=max_length,
        add_special_tokens=add_special_tokens,
        truncation=truncation,
        padding="max_length" if padding else False,
        pad_to_max_length=padding,
        return_tensors="pt"
    )
    return inputs

token = _tokenize(t_input, padding=True, truncation=True)


traced_model = torch.jit.trace(model, [token['input_ids'], token['attention_mask']] )
torch.jit.save(traced_model, "traced_t5.pt")

得到了这个错误

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-f9b449524ef1> in <module>()
     32 
     33 
---> 34 traced_model = torch.jit.trace(model, [token['input_ids'], token['attention_mask']] )
     35 torch.jit.save(traced_model, "traced_t5.pt")

7 frames
/usr/local/lib/python3.6/dist-packages/transformers/modeling_t5.py in forward(self, input_ids, attention_mask, encoder_hidden_states, encoder_attention_mask, inputs_embeds, head_mask, past_key_value_states, use_cache, output_attentions, output_hidden_states)
    682         else:
    683             if self.is_decoder:
--> 684                 raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
    685             else:
    686                 raise ValueError("You have to specify either input_ids or inputs_embeds")

ValueError: You have to specify either decoder_input_ids or decoder_inputs_embeds

如何解决这一问题?或者有更好的方法将t5模型转换为torchscript

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-06 20:38:20

Update:参考答案,如果您要将t5导出到onnx,则可以使用fastT5库轻松完成。

我知道是什么引起了这个问题。由于上述模型是顺序,所以它有一个编码器和一个解码器。我们需要把特征传递到编码器和标签(目标)到解码器。

代码语言:javascript
运行
复制
traced_model = torch.jit.trace(model, 
                               (input_ids, attention_mask, decoder_input_ids, decoder_attention_mask)
                               )
torch.jit.save(traced_model, "qg_model.pt")

decoder_input_ids是问题的标识ids (这里的问题是一个标签)。

即使创建了torchscript模型,它也没有抱脸 t5那样的generate()方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65140400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档