Add Sequential model to Sequential

Hi I am new to Pyttorch and may be the question be simple,
I want to create Sequential model that have multiple Sequential segment or slice so that is my code

class Net(nn.Module):
      def __init__(self):
            super(Net, self).__init__()
             #input 3 32 32
            self.slic1.add_module('conv1', nn.Conv2d(3, 6, 7,padding=3))
            self.layers = nn.Sequential()
           self.layers.add_module('pool1',nn.MaxPool2d(2, 2))
           self.layers.add_module('conv2', nn.Conv2d(6, 16 ,5,padding=2))
           self.layers.add_module('pool2',nn.MaxPool2d(2, 2))

           self.layers.add_module('lin1',nn.Linear(16 * 8 * 8, 500))
          self.layers.add_module('lin3',nn.Linear(500, 10))
     def forward(self, x):
          x_out = self.layers(x)
         return x_out

But conv layer at self.slic1 TWO time added to main model !!! when execute
net = Net()
weightlist HAVE TWO time conv1 layer (layer defined in self.slic1) parameter,
what Is wrong ? and how I can create Sequential model with multiple segment


This is because you have slic1 both in self. and self.layers.
If you only want it in self.layers, you should not create it as an attribute of self:

slic1.add_module('conv1', nn.Conv2d(3, 6, 7,padding=3))
self.layers = nn.Sequential()
1 Like

Thank you :grinning:,
yes my problem solved but I want to access model slice from outside and attach to them
specific loss function in addition to model loss function, slice is not accessible from outside in this way

You can still access slice from model.layers.slic1.
If you want to save a nn.Module into another one, without it being detected as a child module, you can “hide” it by putting it into a list/dict: self.slic1 = [slic1,] and access it as self.slic1[0].