Moved over from other project, slight adjustments & improvements.
Pretty straightforward usage, simply modify you training loop / loss calculation as follows:
model = # define your model
########################
reg = WeightRegularization(model, p=2, weight_decay=0.01) # register model to the regularizer
########################
output = model(...)
loss = # calculate your loss
########################
loss += reg.penalty() # Just add the additional penalty to the loss
########################
# Proceed as usual
loss.backward()
Moved over from other project, slight adjustments & improvements.
Pretty straightforward usage, simply modify you training loop / loss calculation as follows: