The important bit here being the .clamp. For spectral norm, there is a utility in torch.nn.utils.
In IPython, you can get the forward of Conv2d using ?? nn.Conv2d to ensure feature parity (e.g. I left out the padding_mode).
and replace my conv2D defined in the init by ConstrainedConv2D ? and if so how do I pass the the desired parameters to ConstrainedConv2D (input channel, outputChannel, kernel size …)
Wouldn’t this constrain the weights to be between -1.0 and 1.0? In Keras the max_norm constraint is on the norm itself and weights are adjusted proportionally. Something along the following lines
If a hidden unit’s weight vector’s L2 norm L ever gets bigger than a certain max value c , multiply the weight vector by c/L . Enforce it immediately after each weight vector update or after every X gradient update.
This constraint is another form of regularization. While L2 penalizes high weights using the loss function, “max norm” acts directly on the weights. L2 exerts a constant pressure to move the weights near zero which could throw away useful information when the loss function doesn’t provide incentive for the weights to remain far from zero. On the other hand, “max norm” never drives the weights to near zero. As long as the norm is less than the constraint value, the constraint has no effect.
Since the OP accepted the given answer, the weight constraint probably solved their problem. But Just for future references, I think max_norm is different than clamping the weight directly. Let me know if I missed something here.