AttributeError: 'Sequential' object has no attribute 'weight'


I am using a model from

However when I use
print(“weight[0,0] b4 average: %f” % model.classifier.weight[0,0].item())

I got the error.

model.classifier is defines as nn.Sequential, so you would have to index the module inside it, e.g.



pretrained_dict = torch.load(‘H:/workspace/pretrain/epoch_500_pretrain_resnet50.pth’)
model_dict = model.state_dict()
# 1. filter out unnecessary keys
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
# 2. overwrite entries in the existing state dict
# 3. load the new state dict
# copy braches 0 -> branches 1/2

I get an error: AttributeError: ‘Sequential’ object has no attribute ‘weight’

Im not sure whats wrong?
Here is an image of the model structure that might reveal the issue?

It seems that this module is a Sequential of Sequentials, try



AttributeError: ‘AdaptiveAvgPool2d’ object has no attribute ‘weight’

If I understand correctly, this adresses as follows:
model -> _modules:branches -> _modules: 0 -> _modules:1 = AAP2d

I tried to track down where the weights are in debugging:

But I still cant find how to address it correctly, when to use the number indexing, or the . indexing?
I tried for example:

model.branches[1][0].conv1.weight .copy_( model.branches[0][0].conv1.weight )

AttributeError: ‘Sequential’ object has no attribute ‘conv1’

1 Like

You don’t understand the key points.

  • Use number indexing for Sequential and ModuleList
  • Use key for ModuleDict
  • Use .module_name otherwise

For example, if I have a network like

import torch.nn as nn

class SubNet(nn.Module):
    def __init__(self):
        self.conv = nn.Conv2d(2, 2, 2)
        self.relu = nn.ReLU(inplace=True)
    def forward(self, x):
        return x

class Net(nn.Module):
    def __init__(self):
        self.module = nn.Sequential(
                nn.Conv2d(1, 1, 1),
            nn.Conv2d(3, 3, 3)
        self.conv = nn.Conv2d(4, 4, 4)
    def forward(self, x):
        return x

net = Net()

The output is

  (module): Sequential(
    (0): Sequential(
      (0): Conv2d(1, 1, kernel_size=(1, 1), stride=(1, 1))
      (1): SubNet(
        (conv): Conv2d(2, 2, kernel_size=(2, 2), stride=(1, 1))
        (relu): ReLU(inplace=True)
    (1): Conv2d(3, 3, kernel_size=(3, 3), stride=(1, 1))
  (conv): Conv2d(4, 4, kernel_size=(4, 4), stride=(1, 1))

Then we have
net.module[0][0].weight -> Conv2d(1, 1, 1)'s weight
net.module[0][1].conv.weight -> Conv2d(2, 2, 2)'s weight
net.module[1].weight -> Conv2d(3, 3, 3)'s weight
net.conv.weight -> Conv2d(4, 4, 4)'s weight

1 Like

Thanks you example was very informative.
I had problems understanding and seeing clearly from pycharm debug, but printing the net makes it much more clear.

What would we do without you.

1 Like