<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> __authors__ = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> __contact__ = "<STR_LIT>" <EOL> import numpy <EOL> import random <EOL> import string <EOL> import copy as pycopy <EOL> import theano <EOL> import theano . tensor as TT <EOL> def print_time ( secs ) : <EOL> if secs < <NUM_LIT> : <EOL> return '<STR_LIT>' % secs <EOL> elif secs <= <NUM_LIT> * <NUM_LIT> : <EOL> return '<STR_LIT>' % ( secs / <NUM_LIT> ) <EOL> else : <EOL> return '<STR_LIT>' % ( secs / <NUM_LIT> ) <EOL> def print_mem ( context = None ) : <EOL> if theano . sandbox . cuda . cuda_enabled : <EOL> rvals = theano . sandbox . cuda . cuda_ndarray . cuda_ndarray . mem_info ( ) <EOL> available = float ( rvals [ <NUM_LIT:0> ] ) / <NUM_LIT> / <NUM_LIT> <EOL> total = float ( rvals [ <NUM_LIT:1> ] ) / <NUM_LIT> / <NUM_LIT> <EOL> if context == None : <EOL> print ( '<STR_LIT>' % <EOL> ( total - available , available , total ) ) <EOL> else : <EOL> info = str ( context ) <EOL> print ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % <EOL> ( total - available , available , total , info ) ) <EOL> def const ( value ) : <EOL> return TT . constant ( numpy . asarray ( value , dtype = theano . config . floatX ) ) <EOL> def as_floatX ( variable ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( variable , float ) : <EOL> return numpy . cast [ theano . config . floatX ] ( variable ) <EOL> if isinstance ( variable , numpy . ndarray ) : <EOL> return numpy . cast [ theano . config . floatX ] ( variable ) <EOL> return theano . tensor . cast ( variable , theano . config . floatX ) <EOL> def copy ( x ) : <EOL> new_x = pycopy . copy ( x ) <EOL> new_x . params = [ x for x in new_x . params ] <EOL> new_x . params_grad_scale = [ x for x in new_x . params_grad_scale ] <EOL> new_x . noise_params = [ x for x in new_x . noise_params ] <EOL> new_x . noise_params_shape_fn = [ x for x in new_x . noise_params_shape_fn ] <EOL> new_x . updates = [ x for x in new_x . updates ] <EOL> new_x . additional_gradients = [ x for x in new_x . additional_gradients ] <EOL> new_x . inputs = [ x for x in new_x . inputs ] <EOL> new_x . schedules = [ x for x in new_x . schedules ] <EOL> new_x . properties = [ x for x in new_x . properties ] <EOL> return new_x <EOL> def softmax ( x ) : <EOL> if x . ndim == <NUM_LIT:2> : <EOL> e = TT . exp ( x ) <EOL> return e / TT . sum ( e , axis = <NUM_LIT:1> ) . dimshuffle ( <NUM_LIT:0> , '<STR_LIT:x>' ) <EOL> else : <EOL> e = TT . exp ( x ) <EOL> return e / TT . sum ( e ) <EOL> def sample_weights ( sizeX , sizeY , sparsity , scale , rng ) : <EOL> """<STR_LIT>""" <EOL> sizeX = int ( sizeX ) <EOL> sizeY = int ( sizeY ) <EOL> sparsity = numpy . minimum ( sizeY , sparsity ) <EOL> values = numpy . zeros ( ( sizeX , sizeY ) , dtype = theano . config . floatX ) <EOL> for dx in xrange ( sizeX ) : <EOL> perm = rng . permutation ( sizeY ) <EOL> new_vals = rng . uniform ( low = - scale , high = scale , size = ( sparsity , ) ) <EOL> vals_norm = numpy . sqrt ( ( new_vals ** <NUM_LIT:2> ) . sum ( ) ) <EOL> new_vals = scale * new_vals / vals_norm <EOL> values [ dx , perm [ : sparsity ] ] = new_vals <EOL> _ , v , _ = numpy . linalg . svd ( values ) <EOL> values = scale * values / v [ <NUM_LIT:0> ] <EOL> return values . astype ( theano . config . floatX ) <EOL> def sample_weights_classic ( sizeX , sizeY , sparsity , scale , rng ) : <EOL> sizeX = int ( sizeX ) <EOL> sizeY = int ( sizeY ) <EOL> sparsity = numpy . minimum ( sizeY , sparsity ) <EOL> values = numpy . zeros ( ( sizeX , sizeY ) , dtype = theano . config . floatX ) <EOL> for dx in xrange ( sizeX ) : <EOL> perm = rng . permutation ( sizeY ) <EOL> new_vals = rng . normal ( loc = <NUM_LIT:0> , scale = scale , size = ( sparsity , ) ) <EOL> values [ dx , perm [ : sparsity ] ] = new_vals <EOL> return values . astype ( theano . config . floatX ) <EOL> def sample_weights_orth ( sizeX , sizeY , sparsity , scale , rng ) : <EOL> sizeX = int ( sizeX ) <EOL> sizeY = int ( sizeY ) <EOL> assert sizeX == sizeY , '<STR_LIT>' <EOL> if sparsity < <NUM_LIT:0> : <EOL> sparsity = sizeY <EOL> else : <EOL> sparsity = numpy . minimum ( sizeY , sparsity ) <EOL> values = numpy . zeros ( ( sizeX , sizeY ) , dtype = theano . config . floatX ) <EOL> for dx in xrange ( sizeX ) : <EOL> perm = rng . permutation ( sizeY ) <EOL> new_vals = rng . normal ( loc = <NUM_LIT:0> , scale = scale , size = ( sparsity , ) ) <EOL> values [ dx , perm [ : sparsity ] ] = new_vals <EOL> u , s , v = numpy . linalg . svd ( values ) <EOL> values = u * scale <EOL> return values . astype ( theano . config . floatX ) <EOL> def init_bias ( size , scale , rng ) : <EOL> return numpy . ones ( ( size , ) , dtype = theano . config . floatX ) * scale <EOL> def id_generator ( size = <NUM_LIT:5> , chars = string . ascii_uppercase + string . digits ) : <EOL> return '<STR_LIT>' . join ( random . choice ( chars ) for i in xrange ( size ) ) <EOL> def constant_shape ( shape ) : <EOL> return lambda * args , ** kwargs : shape <EOL> def binVec2Int ( binVec ) : <EOL> add = lambda x , y : x + y <EOL> return reduce ( add , <EOL> [ int ( x ) * <NUM_LIT:2> ** y <EOL> for x , y in zip ( <EOL> list ( binVec ) , range ( len ( binVec ) - <NUM_LIT:1> , - <NUM_LIT:1> , <EOL> - <NUM_LIT:1> ) ) ] ) <EOL> def Int2binVec ( val , nbits = <NUM_LIT:10> ) : <EOL> strVal = '<STR_LIT>' . format ( val ) <EOL> value = numpy . zeros ( ( nbits , ) , dtype = theano . config . floatX ) <EOL> if theano . config . floatX == '<STR_LIT>' : <EOL> value [ : len ( strVal ) ] = [ numpy . float32 ( x ) for x in strVal [ : : - <NUM_LIT:1> ] ] <EOL> else : <EOL> value [ : len ( strVal ) ] = [ numpy . float64 ( x ) for x in strVal [ : : - <NUM_LIT:1> ] ] <EOL> return value <EOL> def dot ( inp , matrix ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:int>' in inp . dtype and inp . ndim == <NUM_LIT:2> : <EOL> return matrix [ inp . flatten ( ) ] <EOL> elif '<STR_LIT:int>' in inp . dtype : <EOL> return matrix [ inp ] <EOL> elif '<STR_LIT:float>' in inp . dtype and inp . ndim == <NUM_LIT:3> : <EOL> shape0 = inp . shape [ <NUM_LIT:0> ] <EOL> shape1 = inp . shape [ <NUM_LIT:1> ] <EOL> shape2 = inp . shape [ <NUM_LIT:2> ] <EOL> return TT . dot ( inp . reshape ( ( shape0 * shape1 , shape2 ) ) , matrix ) <EOL> else : <EOL> return TT . dot ( inp , matrix ) </s>
