AttributeError: 'str' 对象在 pytorch 中没有属性 'dim' [英] AttributeError: 'str' object has no attribute 'dim' in pytorch

查看:111
本文介绍了AttributeError: 'str' 对象在 pytorch 中没有属性 'dim'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当将模型预测发送到模型中时,我在PyTorch中收到以下错误输出.有人知道发生了什么事吗?

I got the following error output in the PyTorch when sent model predictions into the model. Does anyone know what's going on?

以下是我创建的体系结构模型,在错误输出中,它显示了x = self.fc1(cls_hs)行中存在的问题.

Following are the architecture model that I created, in the error output, it shows the issue exists in the x = self.fc1(cls_hs) line.

class BERT_Arch(nn.Module):

    def __init__(self, bert):
      
      super(BERT_Arch, self).__init__()

      self.bert = bert 
      
      # dropout layer
      self.dropout = nn.Dropout(0.1)
      
      # relu activation function
      self.relu =  nn.ReLU()

      # dense layer 1
      self.fc1 = nn.Linear(768,512)
      
      # dense layer 2 (Output layer)
      self.fc2 = nn.Linear(512,2)

      #softmax activation function
      self.softmax = nn.LogSoftmax(dim=1)

    #define the forward pass
    def forward(self, sent_id, mask):

      #pass the inputs to the model  
      _, cls_hs = self.bert(sent_id, attention_mask=mask)
      print(mask)
      print(type(mask))
      
      x = self.fc1(cls_hs)

      x = self.relu(x)

      x = self.dropout(x)

      # output layer
      x = self.fc2(x)
      
      # apply softmax activation
      x = self.softmax(x)

      return x

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1686         if any([type(t) is not Tensor for t in tens_ops]) and has_torch_function(tens_ops):
   1687             return handle_torch_function(linear, tens_ops, input, weight, bias=bias)
-> 1688     if input == 2 and bias is not None:
   1689         print(input)
   1690         # fused op is marginally faster
AttributeError: 'str' object has no attribute 'dim'

推荐答案

如果您使用的是Translator == 3.0.0,一切都应该正常工作!

translators == 4.0.0中有一些更新

There were some updates in transformers==4.0.0

要获取变压器== 3.0.0,可以使用以下命令:

To get transformers==3.0.0, following command can be used:

!pip install transformers==3.0.0

这篇关于AttributeError: 'str' 对象在 pytorch 中没有属性 'dim'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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