AttributeError: module 'torch.nn.init' has no attribute 'kaiming_normal_'


When I run this cell in lesson1.ipynb on my windows PC,

data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz))
learn = ConvLearner.pretrained(arch, data, precompute=True), 2)

I get this error.

AttributeError Traceback (most recent call last)
1 arch=resnet34
2 data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz))
----> 3 learn = ConvLearner.pretrained(arch, data, precompute=True)
4, 2)

~\Anaconda3\envs\fastai\lib\site-packages\fastai\ in pretrained(cls, f, data, ps, xtra_fc, xtra_cut, custom_head, precompute, pretrained, **kwargs)
111 pretrained=True, **kwargs):
112 models = ConvnetBuilder(f, data.c, data.is_multi, data.is_reg,
–> 113 ps=ps, xtra_fc=xtra_fc, xtra_cut=xtra_cut, custom_head=custom_head, pretrained=pretrained)
114 return cls(data, models, precompute, **kwargs)

~\Anaconda3\envs\fastai\lib\site-packages\fastai\ in init(self, f, c, is_multi, is_reg, ps, xtra_fc, xtra_cut, custom_head, pretrained)
38 else: cut,self.lr_cut = 0,0
39 cut-=xtra_cut
—> 40 layers = cut_model(f(pretrained), cut)
41 = model_features[f] if f in model_features else (num_features(layers)*2)
42 if not custom_head: layers += [AdaptiveConcatPool2d(), Flatten()]

~\Anaconda3\envs\fastai\lib\site-packages\torchvision\models\ in resnet34(pretrained, **kwargs)
171 pretrained (bool): If True, returns a model pre-trained on ImageNet
172 “”"
–> 173 model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
174 if pretrained:
175 model.load_state_dict(model_zoo.load_url(model_urls[‘resnet34’]))

~\Anaconda3\envs\fastai\lib\site-packages\torchvision\models\ in init(self, block, layers, num_classes)
113 for m in self.modules():
114 if isinstance(m, nn.Conv2d):
–> 115 nn.init.kaiming_normal_(m.weight, mode=‘fan_out’, nonlinearity=‘relu’)
116 elif isinstance(m, nn.BatchNorm2d):
117 nn.init.constant_(m.weight, 1)

AttributeError: module ‘torch.nn.init’ has no attribute ‘kaiming_normal_’

What is the cause?


Are you running python 2 or 3?


Yes, I run this in python3.

(Adedoja Adedamola) #4

HI, Please do you have a fix yet?

I am having the same error coming up on my PC.


I believe this error is caused by the wrong version of torch. Try upgrading pytorch >=.4 ?

see here:

and here:


I also had this problem and was able to resolve it by running the following commands in the fastai directory.

conda install -c anaconda mkl
conda install -c pytorch pytorch torchvision


It works well.
Thank you very match!


I solved this by this reply.

(Adedoja Adedamola) #9

Thanks. I just updated my pytorch to >=0.4 and it worked.

(Adedoja Adedamola) #10


(fareeha saeed) #12

for me
conda install -c pytorch pytorch torchvision
wasnt working
gave me the following error

PackagesNotFoundError: The following packages are not available from current channels:

  • torchvision

so instead this series if commands worked for me
conda install -c anaconda mkl
conda install -c pytorch pytorch-cpu


I’d be cautious of updating to pytorch v 0.4, since it appears that the fastai library uses pytorch < 0.4. Updating to 0.4 caused other errors for me. Instead, try pip install torchvision==0.1.9. It appears that the fastai library uses pytorch < 0.4, but torchvision >= 1.9, which may be causing conflicts. I’m going to try and bring this up in github in the fastai library.