have you seen this 580?
there are two ways to handle backprop, doing it by hand or using the autograd package (and also a third way which is using both of them, by defining backward)
if you are using the autograd, and your modules are composed of standard operations, you can simply define your loss without the L2 regularizer and in the optimizer define the regularizer
class custom(nn.Module):
 def __init__(self):
  super().__init__()
 def forward(self, x):
  return x
net = custom()
optimizer = optim.SGD(net.parameters(), lr=0.01, weight_decay=0.01)
criterion = nn.MSELoss()
for batch in batches:
 optimizer.zero_grad()
 y = net(batch['x'])
 loss = criterion(y, batch['y'])
 loss.backward()
 optimizer.step()