dropout():参数“输入”(位置 1)必须是张量,而不是当使用带有 Huggingface 的 Bert 时的 str

Posted

技术标签:

【中文标题】dropout():参数“输入”(位置 1)必须是张量,而不是当使用带有 Huggingface 的 Bert 时的 str【英文标题】:dropout(): argument 'input' (position 1) must be Tensor, not str when using Bert with Huggingface 【发布时间】:2021-03-12 21:17:27 【问题描述】:

我的代码运行良好,当我今天尝试在不更改任何内容的情况下运行它时,出现以下错误:

dropout(): argument 'input' (position 1) must be Tensor, not str

如果能提供帮助将不胜感激。 可能是数据加载器的问题?

【问题讨论】:

欢迎来到 ***。在发布问题之前,请查看指南。如果您想从 SO 获得答案,您的问题需要可重现,以便其他人帮助您。 ***.com/help/how-to-ask。这可能是代码中任何地方的问题。 【参考方案1】:

如果你使用 HuggingFace,这个information 可能会有用。我有同样的错误,并通过在退出之前在模型类中添加参数 return_dict=False 来修复它: 输出 = 模型(**输入,return_dict=False)

【讨论】:

【参考方案2】:

我也在处理同一个 repo。有一个可能名为 Bert_Arch 的类继承了 nn.Module,并且该类有一个名为 forward 的重写方法。在 forward 方法中,只需将参数 'return_dict=False' 添加到 self.bert() 方法调用中。 替换

_, cls_hs = self.bert(sent_id, attention_mask=mask)

_, cls_hs = self.bert(sent_id, attention_mask=mask, return_dict=False)

【讨论】:

【参考方案3】:

如果您使用的是 Hugging Face 转换器库,则在转换器 v4 库上运行以 v3 编写的代码时会弹出此错误。要解决它,只需在加载模型时添加return_dict=False,如下所示:

model = BertModel.from_pretrained("bert-base-cased")
outputs = model(**inputs, return_dict=False)

model = BertModel.from_pretrained("bert-base-cased", return_dict=False)
outputs = model(**inputs)

我希望这会有所帮助。它对我有用。

参考:https://huggingface.co/transformers/migration.html

【讨论】:

以上是关于dropout():参数“输入”(位置 1)必须是张量,而不是当使用带有 Huggingface 的 Bert 时的 str的主要内容,如果未能解决你的问题,请参考以下文章

dropout理解~简易理解

Keras:LSTM dropout 和 LSTM 循环 dropout 的区别

带有noise_shape的Keras Dropout

TF-tf.nn.dropout介绍

在 Embedding Layer 之后应用 Dropout Layer 是不是与通过 LSTM dropout 参数应用 dropout 具有相同的效果?

caffe中关于(ReLU层,Dropout层,BatchNorm层,Scale层)输入输出层一致的问题