AttributeError:模块“ torch”没有属性“ _six”。Pytorch中的Bert模型

Rui*_*LIU 6 pytorch

我试图通过在pytorch中使用BertModel类加载预训练的模型。

我在手电筒下有_six.py,但它仍然显示模块“手电筒”没有属性“ _six”

import torch
from pytorch_pretrained_bert import BertTokenizer, BertModel, BertForMaskedLM
# Load pre-trained model (weights)
model = BertModel.from_pretrained('bert-base-uncased')
model.eval()
Run Code Online (Sandbox Code Playgroud)
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in __setattr__(self, name, value)
    551                                     .format(torch.typename(value), name))
    552                 modules[name] = value
--> 553             else:
    554                 buffers = self.__dict__.get('_buffers')
    555                 if buffers is not None and name in buffers:

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in register_parameter(self, name, param)
    140             raise KeyError("parameter name can't be empty string \"\"")
    141         elif hasattr(self, name) and name not in self._parameters:
--> 142             raise KeyError("attribute '{}' already exists".format(name))
    143 
    144         if param is None:

AttributeError: module 'torch' has no attribute '_six'
Run Code Online (Sandbox Code Playgroud)

Mar*_*ani 9

在 jupyter notebook 中,只需重新启动内核即可正常工作


use*_*366 9

当我尝试加载 CIFAR10 数据集时出现此错误。简单的重启并不能解决问题!我发现对于某些(conda)环境它工作得很好。

更新 torch 和 torchvision 有助于:

pip install --upgrade torch torchvision
Run Code Online (Sandbox Code Playgroud)


小智 1

我在 macOS 上也遇到了同样的问题,正如 Mark 提到的,重新启动我的 Mac 后一切正常!