我寫了這個:
def forward(self, x):
x = self.bert(x)
x = x.view(x.shape[0], -1)
x = self.fc(self.dropout(self.bn(x)))
return x
但它不能很好地作業,錯誤是“MaskedLMOutput”物件沒有屬性“視圖”。我正在考慮輸入可能不是“張量”型別,所以我將其更改如下:
def forward(self, x):
x = torch.tensor(x) # this part
x = self.bert(x)
x = x.view(x.shape[0], -1)
x = self.fc(self.dropout(self.bn(x)))
return x
但它仍然出錯,同樣的錯誤'MaskedLMOutput'物件沒有屬性'view'。
有人可以告訴我如何解決這個問題嗎?非常感謝。
整個錯誤資訊在這里:
------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Input In [5], in <cell line: 8>()
6 optimizer = optim.Adam(bert_punc.parameters(), lr=learning_rate_top)
7 criterion = nn.CrossEntropyLoss()
----> 8 bert_punc, optimizer, best_val_loss = train(bert_punc, optimizer, criterion, epochs_top,
9 data_loader_train, data_loader_valid, save_path, punctuation_enc, iterations_top, best_val_loss=1e9)
Input In [3], in train(model, optimizer, criterion, epochs, data_loader_train, data_loader_valid, save_path, punctuation_enc, iterations, best_val_loss)
17 inputs.requires_grad = False
18 labels.requires_grad = False
---> 19 output = model(inputs)
20 loss = criterion(output, labels)
21 loss.backward()
File ~\anaconda3\lib\site-packages\torch\nn\modules\module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don't have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
File ~\anaconda3\lib\site-packages\torch\nn\parallel\data_parallel.py:166, in DataParallel.forward(self, *inputs, **kwargs)
163 kwargs = ({},)
165 if len(self.device_ids) == 1:
--> 166 return self.module(*inputs[0], **kwargs[0])
167 replicas = self.replicate(self.module, self.device_ids[:len(inputs)])
168 outputs = self.parallel_apply(replicas, inputs, kwargs)
File ~\anaconda3\lib\site-packages\torch\nn\modules\module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don't have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
File D:\BertPunc-original\model.py:21, in BertPunc.forward(self, x)
18 x = torch.tensor(x)
19 x = self.bert(x)
---> 21 x = x.view(x.shape[0], -1)
22 x = self.fc(self.dropout(self.bn(x)))
23 return x
AttributeError: 'MaskedLMOutput' object has no attribute 'view'
uj5u.com熱心網友回復:
我認為這應該可以幫助您解決錯誤。https://stackoverflow.com/a/72601533/13748930 self.bert(x) 之后的輸出是 MaskedLMOutput 類的一個物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/491222.html
標籤:Python 张量流 火炬 拥抱变形金刚 伯特语言模型
上一篇:當我從D類呼叫C().__init__()時,為什么C().__init__()的結果會列印兩次?
下一篇:jinja2.exceptions.UndefinedError:'forms.ContactFormobject'沒有屬性'message'
