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

**URL:** <https://discuss.pytorch.org/t/attributeerror-sequential-object-has-no-attribute-weight/53855>\
**Category:** Uncategorized\
**Created:** [August 20, 2019, 5:10pm UTC](https://discuss.pytorch.org/t/attributeerror-sequential-object-has-no-attribute-weight/53855 "2019-08-20T17:10:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ammalikwaterloo](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/74df32/32.png) [@ammalikwaterloo](https://discuss.pytorch.org/u/ammalikwaterloo)\
**Post date:** [August 20, 2019, 5:10pm UTC](https://discuss.pytorch.org/t/attributeerror-sequential-object-has-no-attribute-weight/53855/1 "2019-08-20T17:10:12Z")

</div>

Hello,

I am using a model from

> <https://github.com/pytorch/vision/blob/master/torchvision/models/vgg.py>

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

I got the error.  
Thanks,

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [August 20, 2019, 5:32pm UTC](https://discuss.pytorch.org/t/attributeerror-sequential-object-has-no-attribute-weight/53855/2 "2019-08-20T17:32:53Z")

</div>

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

```python
model.classifier[0].weight

```

---

<div class="post-metadata">

**Author:** ![TO\_Kocsis](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/58f4c7/32.png) [@TO\_Kocsis](https://discuss.pytorch.org/u/TO_Kocsis)\
**Post date:** [January 4, 2020, 12:00pm UTC](https://discuss.pytorch.org/t/attributeerror-sequential-object-has-no-attribute-weight/53855/3 "2020-01-04T12:00:38Z")

</div>

Using:

> #load  
> 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  
> model\_dict.update(pretrained\_dict)  
> # 3. load the new state dict  
> model.load\_state\_dict(model\_dict)  
> # copy braches 0 → branches 1/2  
> model.branches[1].weight.copy\_(model.branches[0].weight)

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?

 ![image](https://discuss.pytorch.org/uploads/default/original/3X/1/6/164e395f6e2e3c415bbb6ceaf93acac744688e04.png)

---

<div class="post-metadata">

**Author:** ![Eta\_C](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eta_c/32/17667_2.png) [@Eta\_C](https://discuss.pytorch.org/u/Eta_C)\
**Post date:** [January 4, 2020, 12:09pm UTC](https://discuss.pytorch.org/t/attributeerror-sequential-object-has-no-attribute-weight/53855/4 "2020-01-04T12:09:50Z")

</div>

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

```python
model.branches[0][1].weight

```

---

<div class="post-metadata">

**Author:** ![TO\_Kocsis](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/58f4c7/32.png) [@TO\_Kocsis](https://discuss.pytorch.org/u/TO_Kocsis)\
**Post date:** [January 4, 2020, 1:49pm UTC](https://discuss.pytorch.org/t/attributeerror-sequential-object-has-no-attribute-weight/53855/5 "2020-01-04T13:49:28Z")

</div>

> model.branches[0][1].weight.copy\_(model.branches[0][0].weight)

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

 ![image](https://discuss.pytorch.org/uploads/default/original/3X/8/b/8b5e59edbca5b82bceacda1e484299b0f679aef6.png)

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:

 ![image](https://discuss.pytorch.org/uploads/default/original/3X/6/5/65da576aa7e05471cb8ff141a1ef75258cb2b227.png)

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’

---

<div class="post-metadata">

**Author:** ![Eta\_C](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eta_c/32/17667_2.png) [@Eta\_C](https://discuss.pytorch.org/u/Eta_C)\
**Post date:** [January 5, 2020, 6:09am UTC](https://discuss.pytorch.org/t/attributeerror-sequential-object-has-no-attribute-weight/53855/6 "2020-01-05T06:09:28Z")

</div>

> [@TO\_Kocsis](#):
>
> But I still cant find how to address it correctly, when to use the number indexing, or the . indexing?  
> I tried for example:

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

```python
import torch.nn as nn

class SubNet(nn.Module):
    def __init__ (self):
        super(). __init__ ()
        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):
        super(). __init__ ()
        self.module = nn.Sequential(
            nn.Sequential(
                nn.Conv2d(1, 1, 1),
                SubNet(),
            ),
            nn.Conv2d(3, 3, 3)
        )
        self.conv = nn.Conv2d(4, 4, 4)
    def forward(self, x):
        return x

net = Net()
print(net)

```

The output is

```auto
Net(
  (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

---

<div class="post-metadata">

**Author:** ![TO\_Kocsis](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/58f4c7/32.png) [@TO\_Kocsis](https://discuss.pytorch.org/u/TO_Kocsis)\
**Post date:** [January 6, 2020, 11:36am UTC](https://discuss.pytorch.org/t/attributeerror-sequential-object-has-no-attribute-weight/53855/7 "2020-01-06T11:36:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kamil\_Adamczewski](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kamil_adamczewski/32/12077_2.png) [@Kamil\_Adamczewski](https://discuss.pytorch.org/u/Kamil_Adamczewski)\
**Post date:** [April 2, 2020, 3:41pm UTC](https://discuss.pytorch.org/t/attributeerror-sequential-object-has-no-attribute-weight/53855/8 "2020-04-02T15:41:48Z")

</div>

What would we do without you.

---

<div class="post-metadata">

**Author:** ![Fathima](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/f6c823/32.png) [@Fathima](https://discuss.pytorch.org/u/Fathima)\
**Post date:** [July 5, 2022, 8:37am UTC](https://discuss.pytorch.org/t/attributeerror-sequential-object-has-no-attribute-weight/53855/9 "2022-07-05T08:37:15Z")

</div>

I am trying to initialise the weight but I get this error and I followed the thread but I don’t understand how I could fix in my code. I have 2 con layers and 2 fc layers

this is my architecture

```auto
#original code 
class ConvNet(nn.Module):
    def __init__ (self):
        super(ConvNet, self). __init__ ()
        
        #refer the article ... 
        self.conv1 = nn.Sequential(
            nn.Conv1d(1, 32, kernel_size=2, stride=1),
            nn.Dropout(0.5),
            nn.ReLU())
        
        self.conv2 = nn.Sequential(
            nn.Conv1d(32,32, kernel_size=2, stride=1),
            nn.Dropout(0.5),
            nn.ReLU(),
            nn.MaxPool1d(2,stride=3))
       
        self.conv3 = nn.Sequential(
            nn.Conv1d(32, 32, kernel_size=2, stride=1),
            nn.Dropout(0.5),
            nn.ReLU())
        
        
        #fully connected layers
        self.fc1 = nn.Linear(32*47,32)
        #self.fc2 = nn.Linear(32,1)
        self.fc2 = nn.Linear(32,1)
        #self.activation = nn.Softmax()
        #self.activation = nn.Sigmoid()
        
         # Initialization
        nn.init.zeros_(self.conv1.weight)
        nn.init.zeros_(self.conv2.bias)
    
    
    def forward(self, x):
        # input x : 
        #expected conv1d input = minibatch_size * num_channel * width
        batch_size=x.size(0)
        y = self.conv1(x.view(batch_size,1,-1))
        y = self.conv2(y)
        y = self.conv3(y)
        
        #print(y.size())
        batch_size= y.size(0)
        y = y.flatten(start_dim=1)
        #print(y.size())
        y = self.fc1(y.view(y.size(0), -1))
        #y = self.fc1(y.view(batch_size,1,-1))
        y = self.fc2(y.view(batch_size,1,-1))

        return y  

```

```auto

```

````auto
AttributeError Traceback (most recent call last)
<ipython-input-66-063f0bb327dd> in <module>
      1 #check the output size
----> 2 Model = ConvNet()
      3 X = (torch.randn(32, 1, 145))
      4 output=Model(X)
      5 #print(output.size())

<ipython-input-65-154d3f618194> in __init__ (self)
     30 
     31 # Initialization
---> 32 nn.init.zeros_(self.conv1.weight)
     33 nn.init.zeros_(self.conv2.bias)
     34 

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in __getattr__ (self, name)
   1183 if name in modules:
   1184 return modules[name]
-> 1185 raise AttributeError("'{}' object has no attribute '{}'".format(
   1186 type(self). __name__ , name))
   1187 

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

````

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [July 5, 2022, 8:39am UTC](https://discuss.pytorch.org/t/attributeerror-sequential-object-has-no-attribute-weight/53855/10 "2022-07-05T08:39:03Z")

</div>

This line of code:

```python
nn.init.zeros_(self.conv1.weight)

```

won’t work, since `self.conv1` is an `nn.Sequential` block and thus does not have any parameters.  
You would need to index the actual conv layer via `self.conv1[0]` and it should work:

```python
nn.init.zeros_(self.conv1[0].weight)

```
