gpt4 book ai didi

python - PyTorch BERT 类型错误 : forward() got an unexpected keyword argument 'labels'

转载 作者:行者123 更新时间:2023-12-03 16:15:33 30 4
gpt4 key购买 nike

使用 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'

我似乎无法弄清楚 forward() 函数期望什么样的参数。

有类似问题 here ,但我仍然不明白解决方案是什么。

系统信息:
  • 操作系统:Ubuntu 16.04 LTS
  • Python 版本:3.6.x
  • 火炬版本:1.3.0
  • Torch Vision 版本:0.4.1
  • PyTorch 转换器版本:1.2.0
  • 最佳答案

    据我所知,BertModel 在 forward() 中不带标签。功能。查看 forward函数参数。

    我怀疑您正在尝试为序列分类任务微调 BertModel,而 API 为该任务提供了一个类 BertForSequenceClassification .如您所见,其 forward() 函数定义:

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

    请注意,forward() 方法返回以下内容。
    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.

    希望这可以帮助!

    关于python - PyTorch BERT 类型错误 : forward() got an unexpected keyword argument 'labels' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58454157/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com