title: How to get weights for custom loss function in pytorch?
body: <p>I have a model in pytorch and would like to add L1 regularization insde the loss_function. But I don't want to pass the weights to the loss_function() - is there a better way of doing this? For details see the loss_function() below. </p>
  
  <pre><code>class AutoEncoder(nn.Module):
      def __init__(self, inp_size, hid_size):
          super(AutoEncoder, self).__init__(
  
          self.lambd = 1. 
  
          # Encoder
          self.e1 = nn.Linear(inp_size, hid_size)
  
          # Decoder
          self.d1 = nn.Linear(hid_size, inp_size)
          self.sigmoid = nn.Sigmoid()    
  
          pass
  
     def forward(self,x):
         encode = self.e1(x)
         decode = self.sigmoid(self.d1(encode))
         return decode
  
     def loss_function(self, recon_x, x):
         l2_loss = nn.MSELoss()
  
         # Here I would like to compute the L1 regularization of the weight parameters
         loss = l2_loss(recon_x, x) + self.lambd(l1_loss(self.e1) + l1_loss(self.e2))
         return loss
  </code></pre>