PyTorch Bert TypeError:Forward()获得意外的关键字参数'标签' [英] PyTorch BERT TypeError: forward() got an unexpected keyword argument 'labels'

查看:0
本文介绍了PyTorch Bert TypeError:Forward()获得意外的关键字参数'标签'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PyTorch转换器培训BERT模型(遵循教程here)。

本教程中的以下语句

loss = model(b_input_ids, token_type_ids=None, attention_mask=b_input_mask, labels=b_labels)

指向

TypeError: forward() got an unexpected keyword argument 'labels'

以下是完整的错误,

TypeError                                 Traceback (most recent call last)
<ipython-input-53-56aa2f57dcaf> in <module>
     26         optimizer.zero_grad()
     27         # Forward pass
---> 28         loss = model(b_input_ids, token_type_ids=None, attention_mask=b_input_mask, labels=b_labels)
     29         train_loss_set.append(loss.item())
     30         # Backward pass

~/anaconda3/envs/systreviewclassifi/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--> 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

TypeError: forward() got an unexpected keyword argument 'labels'

我似乎搞不清楚ward()函数需要什么样的参数。

存在类似的问题here,但我仍然不知道解决方案是什么。

系统信息:

  • 操作系统:Ubuntu 16.04 LTS
  • Python版本:3.6.x
  • 火炬版本:1.3.0
  • 火炬愿景版本:0.4.1
  • 火炬变压器版本:1.2.0

推荐答案

据我所知,bertModel不接受forward()函数中的标签。查看forward函数参数。

我怀疑您正在尝试微调BertModel for Sequence分类任务,而API为BertForSequenceClassification提供了一个类。正如您所看到的,它的ward()函数定义:

def forward(self, input_ids, attention_mask=None, token_type_ids=None,
            position_ids=None, head_mask=None, labels=None):

请注意,ward()方法返回以下内容。

Outputs: `Tuple` comprising various elements depending on the configuration (config) and inputs:
        **loss**: (`optional`, returned when ``labels`` is provided) ``torch.FloatTensor`` of shape ``(1,)``:
            Classification (or regression if config.num_labels==1) loss.
        **logits**: ``torch.FloatTensor`` of shape ``(batch_size, config.num_labels)``
            Classification (or regression if config.num_labels==1) scores (before SoftMax).
        **hidden_states**: (`optional`, returned when ``config.output_hidden_states=True``)
            list of ``torch.FloatTensor`` (one for the output of each layer + the output of the embeddings)
            of shape ``(batch_size, sequence_length, hidden_size)``:
            Hidden-states of the model at the output of each layer plus the initial embedding outputs.
        **attentions**: (`optional`, returned when ``config.output_attentions=True``)
            list of ``torch.FloatTensor`` (one for each layer) of shape ``(batch_size, num_heads, sequence_length, sequence_length)``:
            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads. 

希望这能有所帮助!

这篇关于PyTorch Bert TypeError:Forward()获得意外的关键字参数&#39;标签&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆