<s> from __future__ import absolute_import <EOL> from . import backend as K <EOL> class Regularizer ( object ) : <EOL> def set_param ( self , p ) : <EOL> self . p = p <EOL> def set_layer ( self , layer ) : <EOL> self . layer = layer <EOL> def __call__ ( self , loss ) : <EOL> return loss <EOL> def get_config ( self ) : <EOL> return { '<STR_LIT:name>' : self . __class__ . __name__ } <EOL> class WeightRegularizer ( Regularizer ) : <EOL> def __init__ ( self , l1 = <NUM_LIT:0.> , l2 = <NUM_LIT:0.> ) : <EOL> self . l1 = K . cast_to_floatx ( l1 ) <EOL> self . l2 = K . cast_to_floatx ( l2 ) <EOL> self . uses_learning_phase = True <EOL> def set_param ( self , p ) : <EOL> self . p = p <EOL> def __call__ ( self , loss ) : <EOL> if not hasattr ( self , '<STR_LIT:p>' ) : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> regularized_loss = loss + K . sum ( K . abs ( self . p ) ) * self . l1 <EOL> regularized_loss += K . sum ( K . square ( self . p ) ) * self . l2 <EOL> return K . in_train_phase ( regularized_loss , loss ) <EOL> def get_config ( self ) : <EOL> return { '<STR_LIT:name>' : self . __class__ . __name__ , <EOL> '<STR_LIT>' : self . l1 , <EOL> '<STR_LIT>' : self . l2 } <EOL> class ActivityRegularizer ( Regularizer ) : <EOL> def __init__ ( self , l1 = <NUM_LIT:0.> , l2 = <NUM_LIT:0.> ) : <EOL> self . l1 = K . cast_to_floatx ( l1 ) <EOL> self . l2 = K . cast_to_floatx ( l2 ) <EOL> self . uses_learning_phase = True <EOL> def set_layer ( self , layer ) : <EOL> self . layer = layer <EOL> def __call__ ( self , loss ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> output = self . layer . output <EOL> regularized_loss = loss + self . l1 * K . sum ( K . mean ( K . abs ( output ) , axis = <NUM_LIT:0> ) ) <EOL> regularized_loss += self . l2 * K . sum ( K . mean ( K . square ( output ) , axis = <NUM_LIT:0> ) ) <EOL> return K . in_train_phase ( regularized_loss , loss ) <EOL> def get_config ( self ) : <EOL> return { '<STR_LIT:name>' : self . __class__ . __name__ , <EOL> '<STR_LIT>' : self . l1 , <EOL> '<STR_LIT>' : self . l2 } <EOL> def l1 ( l = <NUM_LIT> ) : <EOL> return WeightRegularizer ( l1 = l ) <EOL> def l2 ( l = <NUM_LIT> ) : <EOL> return WeightRegularizer ( l2 = l ) <EOL> def l1l2 ( l1 = <NUM_LIT> , l2 = <NUM_LIT> ) : <EOL> return WeightRegularizer ( l1 = l1 , l2 = l2 ) <EOL> def activity_l1 ( l = <NUM_LIT> ) : <EOL> return ActivityRegularizer ( l1 = l ) <EOL> def activity_l2 ( l = <NUM_LIT> ) : <EOL> return ActivityRegularizer ( l2 = l ) <EOL> def activity_l1l2 ( l1 = <NUM_LIT> , l2 = <NUM_LIT> ) : <EOL> return ActivityRegularizer ( l1 = l1 , l2 = l2 ) <EOL> from . utils . generic_utils import get_from_module <EOL> def get ( identifier , kwargs = None ) : <EOL> return get_from_module ( identifier , globals ( ) , '<STR_LIT>' , <EOL> instantiate = True , kwargs = kwargs ) </s>
