attributeError:' maskEdlMoutput'对象没有属性' view'

发布于 2025-02-06 20:14:56 字数 4782 浏览 3 评论 0原文

很抱歉打扰,当我评估某些型号时,我遇到了这个错误,并且找不到解决方案的好方法。
“ maskedlmoutput'means”是什么?有人可以告诉我如何解决这个问题吗?谢谢。

(attributeError:'maskEdlMoutput'对象没有属性'view')

from transformers import BertForMaskedLM

class BertPunc(nn.Module):  
    
    def __init__(self, segment_size, output_size, dropout):
        super(BertPunc, self).__init__()
        self.bert = BertForMaskedLM.from_pretrained('cl-tohoku/bert-base-japanese')
        self.bert_vocab_size = 32000
        self.bn = nn.BatchNorm1d(segment_size*self.bert_vocab_size)
        self.fc = nn.Linear(segment_size*self.bert_vocab_size, output_size)
        self.dropout = nn.Dropout(dropout)

    def forward(self, input):
        
        x = self.bert(input)             
        x = x.view(x.shape[0], -1)                   # wrong thing here
        x = self.fc(self.dropout(self.bn(x)))
        return x

我在jupyter笔记本中运行了此操作:

    def predictions(data_loader):
        y_pred = []
        y_true = []
        for inputs, labels in tqdm(data_loader, total=len(data_loader)):
            with torch.no_grad():
                inputs, labels = inputs.cuda(), labels.cuda()
                output = bert_punc(inputs)
                y_pred += list(output.argmax(dim=1).cpu().data.numpy().flatten())
                y_true += list(labels.cpu().data.numpy().flatten())
        return y_pred, y_true

def evaluation(y_pred, y_test):
    precision, recall, f1, _ = metrics.precision_recall_fscore_support(
        y_test, y_pred, average=None, labels=[1, 2, 3])
    overall = metrics.precision_recall_fscore_support(
        y_test, y_pred, average='macro', labels=[1, 2, 3])
    result = pd.DataFrame(
        np.array([precision, recall, f1]), 
        columns=list(punctuation_enc.keys())[1:], 
        index=['Precision', 'Recall', 'F1']
    )
    result['OVERALL'] = overall[:3]
    return result


y_pred_test, y_true_test = predictions(data_loader_test)
eval_test = evaluation(y_pred_test, y_true_test)
eval_test

在这里错误:

   ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    Input In [12], in <cell line: 1>()
    ----> 1 y_pred_test, y_true_test = predictions(data_loader_test)
          2 eval_test = evaluation(y_pred_test, y_true_test)
          3 eval_test
    
    Input In [10], in predictions(data_loader)
          5 with torch.no_grad():
          6     inputs, labels = inputs.cuda(), labels.cuda()
    ----> 7     output = bert_punc(inputs)
          8     y_pred += list(output.argmax(dim=1).cpu().data.numpy().flatten())
          9     y_true += list(labels.cpu().data.numpy().flatten())
    
    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 F:\BertPunc-master\model.py:19, in BertPunc.forward(self, input)
         16 def forward(self, input):
         18     x = self.bert(input)
    ---> 19     x = x.view(x.shape[0], -1)
         20     x = self.fc(self.dropout(self.bn(x)))
         21     return x
    
    AttributeError: 'MaskedLMOutput' object has no attribute 'view'

Sorry to bother, I met this error when I evaluate some models, and I didn't find a good method to fix it.
What does 'MaskedLMOutput'means?Could somebody tell me How to fix this please? Thank you.

(AttributeError: 'MaskedLMOutput' object has no attribute 'view')

from transformers import BertForMaskedLM

class BertPunc(nn.Module):  
    
    def __init__(self, segment_size, output_size, dropout):
        super(BertPunc, self).__init__()
        self.bert = BertForMaskedLM.from_pretrained('cl-tohoku/bert-base-japanese')
        self.bert_vocab_size = 32000
        self.bn = nn.BatchNorm1d(segment_size*self.bert_vocab_size)
        self.fc = nn.Linear(segment_size*self.bert_vocab_size, output_size)
        self.dropout = nn.Dropout(dropout)

    def forward(self, input):
        
        x = self.bert(input)             
        x = x.view(x.shape[0], -1)                   # wrong thing here
        x = self.fc(self.dropout(self.bn(x)))
        return x

I ran this in jupyter notebook:

    def predictions(data_loader):
        y_pred = []
        y_true = []
        for inputs, labels in tqdm(data_loader, total=len(data_loader)):
            with torch.no_grad():
                inputs, labels = inputs.cuda(), labels.cuda()
                output = bert_punc(inputs)
                y_pred += list(output.argmax(dim=1).cpu().data.numpy().flatten())
                y_true += list(labels.cpu().data.numpy().flatten())
        return y_pred, y_true

def evaluation(y_pred, y_test):
    precision, recall, f1, _ = metrics.precision_recall_fscore_support(
        y_test, y_pred, average=None, labels=[1, 2, 3])
    overall = metrics.precision_recall_fscore_support(
        y_test, y_pred, average='macro', labels=[1, 2, 3])
    result = pd.DataFrame(
        np.array([precision, recall, f1]), 
        columns=list(punctuation_enc.keys())[1:], 
        index=['Precision', 'Recall', 'F1']
    )
    result['OVERALL'] = overall[:3]
    return result


y_pred_test, y_true_test = predictions(data_loader_test)
eval_test = evaluation(y_pred_test, y_true_test)
eval_test

wrong here:

   ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    Input In [12], in <cell line: 1>()
    ----> 1 y_pred_test, y_true_test = predictions(data_loader_test)
          2 eval_test = evaluation(y_pred_test, y_true_test)
          3 eval_test
    
    Input In [10], in predictions(data_loader)
          5 with torch.no_grad():
          6     inputs, labels = inputs.cuda(), labels.cuda()
    ----> 7     output = bert_punc(inputs)
          8     y_pred += list(output.argmax(dim=1).cpu().data.numpy().flatten())
          9     y_true += list(labels.cpu().data.numpy().flatten())
    
    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 F:\BertPunc-master\model.py:19, in BertPunc.forward(self, input)
         16 def forward(self, input):
         18     x = self.bert(input)
    ---> 19     x = x.view(x.shape[0], -1)
         20     x = self.fc(self.dropout(self.bn(x)))
         21     return x
    
    AttributeError: 'MaskedLMOutput' object has no attribute 'view'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2025-02-13 20:14:56

You can refer to the documentation of MaskedLMOutput. Basically, it is an object holding the loss, logits, hidden_states and attentions. It is not a tensor so you are getting this error. I think you are interested in logits, i.e., score for each token before applying softmax. Then, in the forward function, you can just access to the logits tensor like this

x = self.bert(input).logits
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文