I tried that earlier, but the problem is that i want to use differnet nChannels and dilation factor, but if I make modules inside __init__ then  it will be initialized with same initial value of nChannels and dilation factor, which I don’t want. Can you suggest any solution.
class netG(nn.Module):
  def __init__(self,channel_rate=64,drop_rate=0.0):
    super(netG, self).__init__()
    self.channel_rate = channel_rate
    self.nChannels = 4*self.channel_rate
    self.drop_rate = drop_rate
    self.head = nn.Conv2d(3,4*self.channel_rate,3,1,1)
    self.dilation_factor = 1
  
    self.dense_block = nn.Sequential(
      nn.BatchNorm2d(self.nChannels),
      nn.LeakyReLU(0.2), 
      nn.Conv2d(self.nChannels,4*self.channel_rate,1,1),
      nn.BatchNorm2d(4*self.channel_rate),
      nn.Conv2d(4*self.channel_rate,self.channel_rate,3,1,self.dilation_factor,self.dilation_factor),
      nn.BatchNorm2d(self.channel_rate),
      nn.Dropout2d(self.drop_rate)
    )

    self.tail = nn.Sequential(
      nn.LeakyReLU(0.2),
      nn.Conv2d(self.nChannels,4*self.channel_rate,1,1),
      nn.BatchNorm2d(4*self.channel_rate),
      nn.Dropout(self.drop_rate)
    )
    
    self.last_layer = nn.Sequential(
      nn.Conv2d(self.nChannels,self.channel_rate,3,1,1),
      nn.PReLU(),
      nn.Conv2d(self.channel_rate,3,3,1,1),
      nn.Tanh()
    )

  def forward(self,x):
    x = self.head(x)
    x1 = x
    self.nChannels = x.size(1)
    self.dilation_factor = 1
    d1 = self.dense_block(x)
    x = torch.cat((x,d1),1)
    self.dilation_factor = 1
    self.nChannels = x.size(1)
    d2 = self.dense_block(x)
    x = torch.cat((x,d2),1)
    self.dilation_factor = 2
    self.nChannels  = x.size(1)
    d4 = self.dense_block(x)
    x = torch.cat((x,d4),1)
    self.dilation_factor = 1
    self.nChannels  = x.size(1)
    d5 = self.dense_block(x)
    x = torch.cat((x,d5),1)
    self.dilation_factor = 3
    self.nChannels  = x.size(1)
    d6 = self.dense_block(x)
    x = torch.cat((x,d6),1)
    self.dilation_factor = 1
    self.nChannels  = x.size(1)
    d7 = self.dense_block(x)
    x = torch.cat((x,d7),1)
    self.dilation_factor = 2
    self.nChannels  = x.size(1)
    d8 = self.dense_block(x)
    x = torch.cat((x,d8),1)
    self.dilation_factor = 1
    self.nChannels  = x.size(1)
    d9 = self.dense_block(x)
    x = torch.cat((x,d9),1)
    self.dilation_factor = 1
    self.nChannels  = x.size(1)
    d10 = self.dense_block(x)
    x = self.tail(x)
    x = torch.cat((x,x1),1)
    self.nChannels  = x.size(1)
    x = self.last_layer(x)
    return x