pytorch_pretrained_bert换成transformers踩坑

本文以文本分类为例叙述

步骤

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

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

报错:

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

2、去除 output_all_encoded_layers=False

如下:

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

继续报错:

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

3、添加return_dict=False

如下:

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

问题解决

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

_, 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)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值