当前位置:   article > 正文

pytorch_pretrained_bert换成transformers踩坑_forward() got an unexpected keyword argument 'outp

forward() got an unexpected keyword argument 'output_all_encoded_layers

本文以文本分类为例叙述

步骤

1、前向传播时,pytorch_pretrained_bert是以下内容

        _, pooled = self.bert(context, token_type_ids=types,
                              attention_mask=mask,
                              output_all_encoded_layers=False)
  • 1
  • 2
  • 3

报错:

    result = self.forward(*input, **kwargs)
TypeError: forward() got an unexpected keyword argument 'output_all_encoded_layers'
  • 1
  • 2

2、去除 output_all_encoded_layers=False

如下:

        _, pooled = self.bert(context, token_type_ids=types,
                              attention_mask=mask)
  • 1
  • 2

继续报错:

    if input.dim() == 2 and bias is not None:
AttributeError: 'str' object has no attribute 'dim'
  • 1
  • 2

3、添加return_dict=False

如下:

_, pooled = self.bert(context, token_type_ids=types,
                              attention_mask=mask,return_dict=False)
  • 1
  • 2

问题解决

注:若得不到解决
尝试如下三种方式

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

_, cls_hs = self.bert(sent_id, attention_mask=mask)[:2]

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/286473
推荐阅读
相关标签
  

闽ICP备14008679号