I want to constrain the parameters of an intermediate layer in a neural network to prefer discrete values: -1, 0, or 1. The idea is to add a custom objective function that would increase the loss if the parameters take any other value. Note that, I want to constrain parameters of a particular layer, not all layers.
How can I implement this in pytorch? I want to add this custom loss to the total loss in the training loop, something like this:
custom_loss = constrain_parameters_to_be_discrete
loss = other_loss + custom_loss
May be using a Dirichlet prior might help, any pointer to this?

Extending upon @Shai answer and mixing it with this answer one could do it simpler via custom layer into which you could pass your specific layer.
First, the calculated derivative of
torch.abs(x**2 - torch.abs(x))taken fromWolframAlpha(check here) would be placed insideregularizefunction.Now the
Constrainerlayer:Usage is really simple (with your specified
weight_decayhyperparameter if you need more/less force on the params):Now you don't have to worry about different loss functions and can use your model normally.