<s> import h5py <EOL> import numpy as np <EOL> from numbers import Number <EOL> rng = np . random . RandomState ( seed = <NUM_LIT> ) <EOL> class Constant ( object ) : <EOL> def __init__ ( self , nrows , ncols , val = <NUM_LIT:0.0> ) : <EOL> self . shape = ( nrows , ncols ) <EOL> self . val = val <EOL> def __call__ ( self ) : <EOL> c = np . empty ( self . shape , dtype = np . float32 , order = '<STR_LIT:F>' ) <EOL> c . fill ( self . val ) <EOL> return c <EOL> class Orthogonal ( object ) : <EOL> def __init__ ( self , nrows , ncols , gain = <NUM_LIT:1.0> ) : <EOL> self . shape = ( nrows , ncols ) <EOL> self . gain = gain <EOL> def __call__ ( self ) : <EOL> a = rng . normal ( <NUM_LIT:0.0> , <NUM_LIT:1.0> , self . shape ) <EOL> u , _ , v = np . linalg . svd ( a , full_matrices = False ) <EOL> a = u if u . shape == self . shape else v <EOL> return np . asfortranarray ( self . gain * a , np . float32 ) <EOL> class StackedInitializer ( object ) : <EOL> def __init__ ( self , initializer , n , axis ) : <EOL> self . initializer = initializer <EOL> self . n = n <EOL> self . axis = axis <EOL> def __call__ ( self ) : <EOL> a = [ self . initializer ( ) for _ in xrange ( self . n ) ] <EOL> if self . axis == <NUM_LIT:0> : <EOL> a = np . vstack ( a ) <EOL> elif self . axis == <NUM_LIT:1> : <EOL> a = np . hstack ( a ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if a . dtype != np . float32 and not np . isfortran ( a ) : <EOL> return np . asfortranarray ( a , np . float32 ) <EOL> else : <EOL> return a <EOL> class Xavier ( object ) : <EOL> def __init__ ( self , nrows , ncols ) : <EOL> self . shape = ( nrows , ncols ) <EOL> def __call__ ( self ) : <EOL> amp = np . sqrt ( <NUM_LIT> / ( self . shape [ <NUM_LIT:0> ] + self . shape [ <NUM_LIT:1> ] ) ) <EOL> a = rng . uniform ( - amp , amp , self . shape ) <EOL> return np . asfortranarray ( a , np . float32 ) <EOL> class Uniform ( object ) : <EOL> def __init__ ( self , nrows , ncols , init_range = <NUM_LIT:0.1> ) : <EOL> self . shape = ( nrows , ncols ) <EOL> self . init_range = init_range <EOL> def __call__ ( self ) : <EOL> if isinstance ( self . init_range , Number ) : <EOL> init_range = ( - self . init_range , self . init_range ) <EOL> else : <EOL> init_range = self . init_range <EOL> a = rng . uniform ( low = init_range [ <NUM_LIT:0> ] , high = init_range [ <NUM_LIT:1> ] , size = self . shape ) <EOL> return np . asfortranarray ( a , np . float32 ) <EOL> class H5pyInitializer ( object ) : <EOL> def __init__ ( self , path , key ) : <EOL> with h5py . File ( path , '<STR_LIT:r>' ) as f : <EOL> matrix = f [ key ] [ ... ] <EOL> self . matrix = matrix . astype ( np . float32 ) <EOL> def __call__ ( self ) : <EOL> return np . copy ( self . matrix ) </s>
