亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PyTorch BERT TypeError: forward() got an

PyTorch BERT TypeError: forward() got an

森欄 2022-06-28 16:19:47
使用 PyTorch 轉換器訓練 BERT 模型(按照此處的教程進行操作)。教程中的以下聲明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() 函數期望什么樣的參數。這里有一個類似的問題,但我仍然不明白解決方案是什么。
查看完整描述

1 回答

?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

據我所知,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. 

希望這可以幫助!


查看完整回答
反對 回復 2022-06-28
  • 1 回答
  • 0 關注
  • 1575 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號