Ruitong LIU
Ruitong LIU

Reputation: 221

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'

Upvotes: 22

Views: 31058

Answers (6)

Coder
Coder

Reputation: 1121

Works for me

From: https://discuss.pytorch.org/t/attributeerror-module-torch-has-no-attribute-utils/176733/7

Downgrading to torchvision 0.14.0 or upgrading torchvision to 0.15.1 both resolve this error

Upvotes: 0

Yasine Deghaies
Yasine Deghaies

Reputation: 52

I faced the same issue under Apple M1 using conda and I solved it after creating a new conda environment with the latest version of python and then installing torch and torchvision again.

Upvotes: 0

Feidu Akmel
Feidu Akmel

Reputation: 29

I solved this error by commenting the following lines of code in vision.py

if isinstance(root, torch._six.string_classes):
            root = os.path.expanduser(root)
        self.root = root

Upvotes: 2

user1953366
user1953366

Reputation: 1611

I was getting this erro when I was trying to load CIFAR10 dataset. Simple restart didn't solved it! I found that for some (conda) environment it was working fine.

Updating torch and torchvision helped:

pip install --upgrade torch torchvision

Upvotes: 12

Marco Cerliani
Marco Cerliani

Reputation: 22031

In jupyter notebook/lab simply restarting the kernel works fine

Upvotes: 10

fitfall
fitfall

Reputation: 27

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

Upvotes: 1

Related Questions