Keras has an option to force the weights of the learned model to be positive:
tf.keras.constraints.NonNeg()
But I couldn't find the equivalent of this in pytorch, does anyone know how can I force my linear model's weights to be all positives?
Tried asking this on other forums but the answers were not helpful.
Let's say I have a very simple linear model as shown below, how should I change it?
class Classifier(nn.Module):
def __init__(self,input , n_classes):
super(Classifier, self).__init__()
self.classify = nn.Linear( input , n_classes)
def forward(self, h ):
final = self.classify(h)
return final
I want to do exactly what the NonNeg() does but in pytorch, don't want to change what its doing.
This is the implementation of NonNeg in keras:
class NonNeg(Constraint):
"""Constrains the weights to be non-negative.
"""
def __call__(self, w):
w *= K.cast(K.greater_equal(w, 0.), K.floatx())
return w