“您必须指定 input_ids 或 input_embeds”,但我确实指定了 input_ids

Uri*_*ren 6 bert-language-model huggingface-transformers

我训练了一个基于 BERT 的编码器解码器模型 ( EncoderDecoderModel),ed_model并以 HuggingFace 的 Transformers 模块命名。

我用的BertTokenizer是命名为input_tokenizer

我用以下方法标记了输入:

txt = "Some wonderful sentence to encode"
inputs = input_tokenizer(txt, return_tensors="pt").to(device)
print(inputs)
Run Code Online (Sandbox Code Playgroud)

输出清楚地表明 ainput_ids是返回字典


{'input_ids': tensor([[ 101, 5660, 7975, 2127, 2053, 2936, 5061,  102]], device='cuda:0'), 'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 0, 0]], device='cuda:0'), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1]], device='cuda:0')}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试预测时,我收到此错误: ed_model.forward(**inputs)

ValueError:您必须指定 input_ids 或 input_embeds

有任何想法吗 ?

Uri*_*ren 8

嗯,显然这是一个已知问题,例如:This issues of T5

问题是代码中可能存在重命名过程,因为我们使用编码器-解码器架构,所以我们有 2 种类型的输入 id。

解决办法是显式指定输入id的类型

ed_model.forward(decoder_input_ids=inputs['input_ids'],**inputs) 
Run Code Online (Sandbox Code Playgroud)

我希望它被记录在某个地方,但现在你知道了:-)