Is there no way to do it with an OrderedDict class? If we have the constructor iterate over a number that is given as an argument and append layers to the ordereddict class, it seems to work without changing the class code. So the first time we may train a network with 3 layers and the second time train a network with 4 layers. Here’s my piece of code:
def init(self, blocklist, num_classes=1, stride = [1,2,2], block=BasicBlock):
"""
_make_block creates a residual block like those used in ResNet.
LastUpdatedOrderedDict is a subclass of OrderedDict that stores items in the order the keys were last added
"""
    #blocklist specifies the number of layers in each block

    self.inplanes = 64
    super(ModNet, self).__init__()

    #This structure has the advantage of being able to set the number of layers as a hyperparameter. The hyperparameters in this case are lists of same length which equals the number of blocks we want to use and contain how many layers each block has and the stride movement
    layers = LastUpdatedOrderedDict([
            ('conv1', nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
                               bias=False)),
            ('bn1', nn.BatchNorm2d(64)),
            ('relu1', nn.ReLU(inplace=True)),
            ('maxpool', nn.MaxPool2d(kernel_size=3, stride=2, padding=1)),
            ("layer1", self._make_block(block, 64, blocklist[0], stride=stride[0]))
            ])
    p = 128
    for i in range(1,len(blocklist)):
        layer = self._make_block(block, p, blocklist[i], stride=stride[i])
        layers.__setitem__("layer{}".format(i+1), layer) #__setitem__ appends a new layer to the dict
        p = 2*p

    self.stacked_layers = nn.Sequential(layers)
    self.avgpool = nn.AvgPool2d(14, stride=1)
    self.fc = nn.Linear(256 * block.expansion, num_classes)

However my issue is. I want to load a trained model and then add 1 layer to it and do a sort of transfer learning that way. Is this not possible in pytorch?