<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import numpy <EOL> import theano <EOL> from theano import tensor <EOL> T = tensor <EOL> from pylearn2 . utils . rng import make_np_rng <EOL> theano . config . warn . sum_div_dimshuffle_bug = False <EOL> if <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' ) <EOL> RandomStreams = tensor . shared_randomstreams . RandomStreams <EOL> else : <EOL> import theano . sandbox . rng_mrg <EOL> RandomStreams = theano . sandbox . rng_mrg . MRG_RandomStreams <EOL> from pylearn2 . expr . activations import rescaled_softmax <EOL> class Corruptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , corruption_level , rng = <NUM_LIT> ) : <EOL> rng = make_np_rng ( rng , which_method = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> seed = int ( rng . randint ( <NUM_LIT:2> ** <NUM_LIT:30> ) ) <EOL> self . s_rng = RandomStreams ( seed ) <EOL> self . corruption_level = corruption_level <EOL> def __call__ ( self , inputs ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( inputs , tensor . Variable ) : <EOL> return self . _corrupt ( inputs ) <EOL> else : <EOL> return [ self . _corrupt ( inp ) for inp in inputs ] <EOL> def _corrupt ( self , x ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def corruption_free_energy ( self , corrupted_X , X ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class DummyCorruptor ( Corruptor ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , inputs ) : <EOL> """<STR_LIT>""" <EOL> return inputs <EOL> class BinomialCorruptor ( Corruptor ) : <EOL> """<STR_LIT>""" <EOL> def _corrupt ( self , x ) : <EOL> """<STR_LIT>""" <EOL> return self . s_rng . binomial ( <EOL> size = x . shape , <EOL> n = <NUM_LIT:1> , <EOL> p = <NUM_LIT:1> - self . corruption_level , <EOL> dtype = theano . config . floatX <EOL> ) * x <EOL> class DropoutCorruptor ( BinomialCorruptor ) : <EOL> """<STR_LIT>""" <EOL> def _corrupt ( self , x ) : <EOL> """<STR_LIT>""" <EOL> if self . corruption_level < <NUM_LIT> : <EOL> return x <EOL> dropped = super ( DropoutCorruptor , self ) . _corrupt ( x ) <EOL> return <NUM_LIT:1.0> / ( <NUM_LIT:1.0> - self . corruption_level ) * dropped <EOL> class GaussianCorruptor ( Corruptor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stdev , rng = <NUM_LIT> ) : <EOL> super ( GaussianCorruptor , self ) . __init__ ( corruption_level = stdev , <EOL> rng = rng ) <EOL> def _corrupt ( self , x ) : <EOL> """<STR_LIT>""" <EOL> noise = self . s_rng . normal ( <EOL> size = x . shape , <EOL> avg = <NUM_LIT:0.> , <EOL> std = self . corruption_level , <EOL> dtype = theano . config . floatX <EOL> ) <EOL> return noise + x <EOL> def corruption_free_energy ( self , corrupted_X , X ) : <EOL> """<STR_LIT>""" <EOL> axis = range ( <NUM_LIT:1> , len ( X . type . broadcastable ) ) <EOL> rval = ( T . sum ( T . sqr ( corrupted_X - X ) , axis = axis ) / <EOL> ( <NUM_LIT> * ( self . corruption_level ** <NUM_LIT> ) ) ) <EOL> assert len ( rval . type . broadcastable ) == <NUM_LIT:1> <EOL> return rval <EOL> class SaltPepperCorruptor ( Corruptor ) : <EOL> """<STR_LIT>""" <EOL> def _corrupt ( self , x ) : <EOL> """<STR_LIT>""" <EOL> a = self . s_rng . binomial ( <EOL> size = x . shape , <EOL> p = ( <NUM_LIT:1> - self . corruption_level ) , <EOL> dtype = theano . config . floatX <EOL> ) <EOL> b = self . s_rng . binomial ( <EOL> size = x . shape , <EOL> p = <NUM_LIT:0.5> , <EOL> dtype = theano . config . floatX <EOL> ) <EOL> c = T . eq ( a , <NUM_LIT:0> ) * b <EOL> return x * a + c <EOL> class OneHotCorruptor ( Corruptor ) : <EOL> """<STR_LIT>""" <EOL> def _corrupt ( self , x ) : <EOL> """<STR_LIT>""" <EOL> num_examples = x . shape [ <NUM_LIT:0> ] <EOL> num_classes = x . shape [ <NUM_LIT:1> ] <EOL> keep_mask = T . addbroadcast ( <EOL> self . s_rng . binomial ( <EOL> size = ( num_examples , <NUM_LIT:1> ) , <EOL> p = <NUM_LIT:1> - self . corruption_level , <EOL> dtype = '<STR_LIT>' <EOL> ) , <EOL> <NUM_LIT:1> <EOL> ) <EOL> pvals = T . alloc ( <NUM_LIT:1.0> / num_classes , num_classes ) <EOL> one_hot = self . s_rng . multinomial ( size = ( num_examples , ) , pvals = pvals ) <EOL> return keep_mask * x + ( <NUM_LIT:1> - keep_mask ) * one_hot <EOL> class SmoothOneHotCorruptor ( Corruptor ) : <EOL> """<STR_LIT>""" <EOL> def _corrupt ( self , x ) : <EOL> """<STR_LIT>""" <EOL> noise = self . s_rng . normal ( <EOL> size = x . shape , <EOL> avg = <NUM_LIT:0.> , <EOL> std = self . corruption_level , <EOL> dtype = theano . config . floatX <EOL> ) <EOL> return rescaled_softmax ( x + noise ) <EOL> class BinomialSampler ( Corruptor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( BinomialSampler , self ) . __init__ ( <NUM_LIT:0> , * args , ** kwargs ) <EOL> def _corrupt ( self , x ) : <EOL> """<STR_LIT>""" <EOL> return self . s_rng . binomial ( size = x . shape , p = x , <EOL> dtype = theano . config . floatX ) <EOL> class MultinomialSampler ( Corruptor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( MultinomialSampler , self ) . __init__ ( <NUM_LIT:0> , * args , ** kwargs ) <EOL> def _corrupt ( self , x ) : <EOL> """<STR_LIT>""" <EOL> normalized = x / x . sum ( axis = <NUM_LIT:1> , keepdims = True ) <EOL> return self . s_rng . multinomial ( pvals = normalized , dtype = theano . config . floatX ) <EOL> class ComposedCorruptor ( Corruptor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * corruptors ) : <EOL> assert len ( corruptors ) >= <NUM_LIT:1> <EOL> self . _corruptors = corruptors <EOL> def _corrupt ( self , x ) : <EOL> """<STR_LIT>""" <EOL> result = x <EOL> for c in reversed ( self . _corruptors ) : <EOL> result = c ( result ) <EOL> return result <EOL> def get ( str ) : <EOL> """<STR_LIT>""" <EOL> obj = globals ( ) [ str ] <EOL> if issubclass ( obj , Corruptor ) : <EOL> return obj <EOL> else : <EOL> raise NameError ( str ) </s>
