<s> import numpy as np <EOL> from deepy . utils import global_rand <EOL> def get_fans ( shape ) : <EOL> fan_in = shape [ <NUM_LIT:0> ] if len ( shape ) == <NUM_LIT:2> else np . prod ( shape [ <NUM_LIT:1> : ] ) <EOL> fan_out = shape [ <NUM_LIT:1> ] if len ( shape ) == <NUM_LIT:2> else shape [ <NUM_LIT:0> ] <EOL> return fan_in , fan_out <EOL> class WeightInitializer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , seed = None ) : <EOL> if not seed : <EOL> self . rand = global_rand <EOL> else : <EOL> self . rand = np . random . RandomState ( seed ) <EOL> def sample ( self , shape ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class UniformInitializer ( WeightInitializer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , scale = None , svd = False , seed = None ) : <EOL> super ( UniformInitializer , self ) . __init__ ( seed ) <EOL> self . scale = scale <EOL> self . svd = svd <EOL> def sample ( self , shape ) : <EOL> if not self . scale : <EOL> scale = np . sqrt ( <NUM_LIT> / sum ( get_fans ( shape ) ) ) <EOL> else : <EOL> scale = self . scale <EOL> weight = self . rand . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , size = shape ) * scale <EOL> if self . svd : <EOL> norm = np . sqrt ( ( weight ** <NUM_LIT:2> ) . sum ( ) ) <EOL> ws = scale * weight / norm <EOL> _ , v , _ = np . linalg . svd ( ws ) <EOL> ws = scale * ws / v [ <NUM_LIT:0> ] <EOL> return weight <EOL> class GaussianInitializer ( WeightInitializer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mean = <NUM_LIT:0> , deviation = <NUM_LIT> , seed = None ) : <EOL> super ( GaussianInitializer , self ) . __init__ ( seed ) <EOL> self . mean = mean <EOL> self . deviation = deviation <EOL> def sample ( self , shape ) : <EOL> weight = self . rand . normal ( self . mean , self . deviation , size = shape ) <EOL> return weight <EOL> class IdentityInitializer ( WeightInitializer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , scale = <NUM_LIT:1> ) : <EOL> super ( IdentityInitializer , self ) . __init__ ( ) <EOL> self . scale = <NUM_LIT:1> <EOL> def sample ( self , shape ) : <EOL> assert len ( shape ) == <NUM_LIT:2> <EOL> return np . eye ( * shape ) * self . scale <EOL> class XavierGlorotInitializer ( WeightInitializer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , uniform = False , seed = None ) : <EOL> """<STR_LIT>""" <EOL> super ( XavierGlorotInitializer , self ) . __init__ ( seed ) <EOL> self . uniform = uniform <EOL> def sample ( self , shape ) : <EOL> scale = np . sqrt ( <NUM_LIT> / sum ( get_fans ( shape ) ) ) <EOL> if self . uniform : <EOL> return self . rand . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , size = shape ) * scale <EOL> else : <EOL> return self . rand . randn ( * shape ) * scale <EOL> class KaimingHeInitializer ( WeightInitializer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , uniform = False , seed = None ) : <EOL> """<STR_LIT>""" <EOL> super ( KaimingHeInitializer , self ) . __init__ ( seed ) <EOL> self . uniform = uniform <EOL> def sample ( self , shape ) : <EOL> fan_in , fan_out = get_fans ( shape ) <EOL> scale = np . sqrt ( <NUM_LIT> / fan_in ) <EOL> if self . uniform : <EOL> return self . rand . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , size = shape ) * scale <EOL> else : <EOL> return self . rand . randn ( * shape ) * scale <EOL> class OrthogonalInitializer ( WeightInitializer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , scale = <NUM_LIT> , seed = None ) : <EOL> """<STR_LIT>""" <EOL> super ( OrthogonalInitializer , self ) . __init__ ( seed ) <EOL> self . scale = scale <EOL> def sample ( self , shape ) : <EOL> flat_shape = ( shape [ <NUM_LIT:0> ] , np . prod ( shape [ <NUM_LIT:1> : ] ) ) <EOL> a = np . random . normal ( <NUM_LIT:0.0> , <NUM_LIT:1.0> , flat_shape ) <EOL> u , _ , v = np . linalg . svd ( a , full_matrices = False ) <EOL> q = u if u . shape == flat_shape else v <EOL> q = q . reshape ( shape ) <EOL> return self . scale * q [ : shape [ <NUM_LIT:0> ] , : shape [ <NUM_LIT:1> ] ] </s>
