python - 属性错误 : 'str' object has no attribute 'dim' in pytorch

标签 python python-3.x tensorflow machine-learning bert-language-model

将模型预测发送到模型中时,我在 PyTorch 中得到以下错误输出。有谁知道发生了什么?
以下是我创建的架构模型,在错误输出中,它显示 x = self.fc1(cls_hs) 行中存在问题。

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'

最佳答案

如果您使用transformers==3.0.0,一切都应该正常!
变压器==4.0.0有一些更新
要获得transformers==3.0.0,可以使用以下命令:

!pip install transformers==3.0.0

关于python - 属性错误 : 'str' object has no attribute 'dim' in pytorch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65079318/

相关文章:

tensorflow - Keras 和 Tensorflow 的 NVIDIA GPU 使用率低

python - 我的 PostgreSQL docker 镜像数据库在 Django 上不存在

python - 如何在 python 中创建 3x3 数独 block 列表

python - 在 Azure 上的 Python WebJob 中导入 Numpy

python - 使用八进制数时 token 无效

python - 如何在批量学习中为 tf-agents 定义正确的形状

python - 如何合并数值和嵌入序列模型以处理 RNN 中的类别

python - 将 Pandas 中的一列转换为一个长字符串 (Python 3)

python - 是否可以在虚拟开发环境中使用 matplotlib 打开图像?

python - 在给定的时间间隔内循环或包装颜色图