Last active
August 24, 2018 16:54
-
-
Save HemersonTacon/c60deabb25d6826e09545e3e7096d49e to your computer and use it in GitHub Desktop.
Generic function to apply kernel regularization to any keras model layer that supports it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.regularizers import l1_l2 | |
def set_kernel_reg(model, lambdal1 = 0, lambdal2 = 0): | |
""" | |
Apply kernel regularization to keras model | |
Args: | |
model: Instance of `Model` not compiled yet | |
lambda1 (float): L1 regularization factor | |
lambda2 (float): L2 regularization factor | |
Returns: | |
Return the same model but with kernel_regularizer configured | |
""" | |
for layer in model.layers: | |
if hasattr(layer, 'kernel_regularizer'): | |
layer.kernel_regularizer = l1_l2(l1 = lambdal1, l2 = lambdal2) | |
return model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment