Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: module 'torch' has no attribute '_six'. Bert model in Pytorch

I tried to load pre-trained model by using BertModel class in pytorch.

I have _six.py under torch, but it still shows module 'torch' has no attribute '_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'
like image 212
Ruitong LIU Avatar asked May 21 '19 15:05

Ruitong LIU


2 Answers

In jupyter notebook simply restarting kernel works fine

like image 120
Marco Cerliani Avatar answered Nov 01 '22 07:11

Marco Cerliani


I also had the same issue on macOS, and as Mark mentioned, it works fine after restarting my mac!

like image 31
Jin Avatar answered Nov 01 '22 07:11

Jin