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

时间:2019-05-21 15:41:21

标签: pytorch

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

我在炬管下有_six.py,但它仍然显示模块'torch'没有属性'_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()
~/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'

2 个答案:

答案 0 :(得分:1)

我在macOS上也遇到了同样的问题,正如Mark提到的那样,在重新启动Mac之后它可以正常工作!

答案 1 :(得分:0)

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