<s> import theano <EOL> from theano import tensor as T <EOL> from theano . sandbox . rng_mrg import MRG_RandomStreams as RandomStreams <EOL> from theano . tensor . signal import pool <EOL> from theano . tensor . nnet import conv3d2d <EOL> import inspect <EOL> import numpy as np <EOL> from . common import _FLOATX , _EPSILON <EOL> theano . config . floatX = _FLOATX <EOL> _LEARNING_PHASE = T . scalar ( dtype = '<STR_LIT>' , name = '<STR_LIT>' ) <EOL> def learning_phase ( ) : <EOL> return _LEARNING_PHASE <EOL> def variable ( value , dtype = _FLOATX , name = None ) : <EOL> '''<STR_LIT>''' <EOL> value = np . asarray ( value , dtype = dtype ) <EOL> return theano . shared ( value = value , name = name , strict = False ) <EOL> def placeholder ( shape = None , ndim = None , dtype = _FLOATX , name = None ) : <EOL> '''<STR_LIT>''' <EOL> if shape is None and ndim is None : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if shape is not None : <EOL> ndim = len ( shape ) <EOL> else : <EOL> shape = tuple ( [ None for _ in range ( ndim ) ] ) <EOL> broadcast = ( False , ) * ndim <EOL> x = T . TensorType ( dtype , broadcast ) ( name ) <EOL> x . _keras_shape = shape <EOL> x . _uses_learning_phase = False <EOL> return x <EOL> def shape ( x ) : <EOL> '''<STR_LIT>''' <EOL> return x . shape <EOL> def ndim ( x ) : <EOL> return x . ndim <EOL> def dtype ( x ) : <EOL> return x . dtype <EOL> def eval ( x ) : <EOL> '''<STR_LIT>''' <EOL> return x . eval ( ) <EOL> def zeros ( shape , dtype = _FLOATX , name = None ) : <EOL> '''<STR_LIT>''' <EOL> return variable ( np . zeros ( shape ) , dtype , name ) <EOL> def ones ( shape , dtype = _FLOATX , name = None ) : <EOL> '''<STR_LIT>''' <EOL> return variable ( np . ones ( shape ) , dtype , name ) <EOL> def eye ( size , dtype = _FLOATX , name = None ) : <EOL> '''<STR_LIT>''' <EOL> return variable ( np . eye ( size ) , dtype , name ) <EOL> def ones_like ( x ) : <EOL> return T . ones_like ( x ) <EOL> def zeros_like ( x ) : <EOL> return T . zeros_like ( x ) <EOL> def count_params ( x ) : <EOL> '''<STR_LIT>''' <EOL> return np . prod ( x . shape . eval ( ) ) <EOL> def cast ( x , dtype ) : <EOL> return T . cast ( x , dtype ) <EOL> '''<STR_LIT>''' <EOL> def dot ( x , y ) : <EOL> return T . dot ( x , y ) <EOL> def batch_dot ( x , y , axes = None ) : <EOL> '''<STR_LIT>''' <EOL> if type ( axes ) == int : <EOL> axes = ( axes , axes ) <EOL> if axes is None : <EOL> axes = [ x . ndim - <NUM_LIT:1> , y . ndim - <NUM_LIT:2> ] <EOL> out = T . batched_tensordot ( x , y , axes = axes ) <EOL> if ndim ( out ) == <NUM_LIT:1> : <EOL> out = expand_dims ( out , <NUM_LIT:1> ) <EOL> return out <EOL> def transpose ( x ) : <EOL> return T . transpose ( x ) <EOL> def gather ( reference , indices ) : <EOL> '''<STR_LIT>''' <EOL> return reference [ indices ] <EOL> def max ( x , axis = None , keepdims = False ) : <EOL> return T . max ( x , axis = axis , keepdims = keepdims ) <EOL> def min ( x , axis = None , keepdims = False ) : <EOL> return T . min ( x , axis = axis , keepdims = keepdims ) <EOL> def sum ( x , axis = None , keepdims = False ) : <EOL> '''<STR_LIT>''' <EOL> return T . sum ( x , axis = axis , keepdims = keepdims ) <EOL> def prod ( x , axis = None , keepdims = False ) : <EOL> '''<STR_LIT>''' <EOL> return T . prod ( x , axis = axis , keepdims = keepdims ) <EOL> def mean ( x , axis = None , keepdims = False ) : <EOL> dtype = None <EOL> if '<STR_LIT:int>' in x . dtype : <EOL> dtype = _FLOATX <EOL> return T . mean ( x , axis = axis , keepdims = keepdims , dtype = dtype ) <EOL> def std ( x , axis = None , keepdims = False ) : <EOL> return T . std ( x , axis = axis , keepdims = keepdims ) <EOL> def any ( x , axis = None , keepdims = False ) : <EOL> '''<STR_LIT>''' <EOL> return T . any ( x , axis = axis , keepdims = keepdims ) <EOL> def argmax ( x , axis = - <NUM_LIT:1> ) : <EOL> return T . argmax ( x , axis = axis , keepdims = False ) <EOL> def argmin ( x , axis = - <NUM_LIT:1> ) : <EOL> return T . argmin ( x , axis = axis , keepdims = False ) <EOL> def square ( x ) : <EOL> return T . sqr ( x ) <EOL> def abs ( x ) : <EOL> return T . abs_ ( x ) <EOL> def sqrt ( x ) : <EOL> x = T . clip ( x , <NUM_LIT:0.> , np . inf ) <EOL> return T . sqrt ( x ) <EOL> def exp ( x ) : <EOL> return T . exp ( x ) <EOL> def log ( x ) : <EOL> return T . log ( x ) <EOL> def round ( x ) : <EOL> return T . round ( x ) <EOL> def sign ( x ) : <EOL> return T . sgn ( x ) <EOL> def pow ( x , a ) : <EOL> return T . pow ( x , a ) <EOL> def clip ( x , min_value , max_value ) : <EOL> if max_value < min_value : <EOL> max_value = min_value <EOL> return T . clip ( x , min_value , max_value ) <EOL> def equal ( x , y ) : <EOL> return T . eq ( x , y ) <EOL> def not_equal ( x , y ) : <EOL> return T . neq ( x , y ) <EOL> def maximum ( x , y ) : <EOL> return T . maximum ( x , y ) <EOL> def minimum ( x , y ) : <EOL> return T . minimum ( x , y ) <EOL> def sin ( x ) : <EOL> return T . sin ( x ) <EOL> def cos ( x ) : <EOL> return T . cos ( x ) <EOL> def concatenate ( tensors , axis = - <NUM_LIT:1> ) : <EOL> return T . concatenate ( tensors , axis = axis ) <EOL> def reshape ( x , shape ) : <EOL> return T . reshape ( x , shape ) <EOL> def permute_dimensions ( x , pattern ) : <EOL> '''<STR_LIT>''' <EOL> pattern = tuple ( pattern ) <EOL> return x . dimshuffle ( pattern ) <EOL> def repeat_elements ( x , rep , axis ) : <EOL> '''<STR_LIT>''' <EOL> return T . repeat ( x , rep , axis = axis ) <EOL> def resize_images ( X , height_factor , width_factor , dim_ordering ) : <EOL> '''<STR_LIT>''' <EOL> if dim_ordering == '<STR_LIT>' : <EOL> output = repeat_elements ( X , height_factor , axis = <NUM_LIT:2> ) <EOL> output = repeat_elements ( output , width_factor , axis = <NUM_LIT:3> ) <EOL> return output <EOL> elif dim_ordering == '<STR_LIT>' : <EOL> output = repeat_elements ( X , height_factor , axis = <NUM_LIT:1> ) <EOL> output = repeat_elements ( output , width_factor , axis = <NUM_LIT:2> ) <EOL> return output <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + dim_ordering ) <EOL> def resize_volumes ( X , depth_factor , height_factor , width_factor , dim_ordering ) : <EOL> '''<STR_LIT>''' <EOL> if dim_ordering == '<STR_LIT>' : <EOL> output = repeat_elements ( X , depth_factor , axis = <NUM_LIT:2> ) <EOL> output = repeat_elements ( output , height_factor , axis = <NUM_LIT:3> ) <EOL> output = repeat_elements ( output , width_factor , axis = <NUM_LIT:4> ) <EOL> return output <EOL> elif dim_ordering == '<STR_LIT>' : <EOL> output = repeat_elements ( X , depth_factor , axis = <NUM_LIT:1> ) <EOL> output = repeat_elements ( output , height_factor , axis = <NUM_LIT:2> ) <EOL> output = repeat_elements ( output , width_factor , axis = <NUM_LIT:3> ) <EOL> return output <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + dim_ordering ) <EOL> def repeat ( x , n ) : <EOL> '''<STR_LIT>''' <EOL> assert x . ndim == <NUM_LIT:2> <EOL> x = x . dimshuffle ( ( <NUM_LIT:0> , '<STR_LIT:x>' , <NUM_LIT:1> ) ) <EOL> return T . extra_ops . repeat ( x , n , axis = <NUM_LIT:1> ) <EOL> def tile ( x , n ) : <EOL> return T . tile ( x , n ) <EOL> def flatten ( x ) : <EOL> return T . flatten ( x ) <EOL> def batch_flatten ( x ) : <EOL> '''<STR_LIT>''' <EOL> x = T . reshape ( x , ( x . shape [ <NUM_LIT:0> ] , T . prod ( x . shape ) // x . shape [ <NUM_LIT:0> ] ) ) <EOL> return x <EOL> def expand_dims ( x , dim = - <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> pattern = [ i for i in range ( x . type . ndim ) ] <EOL> if dim < <NUM_LIT:0> : <EOL> if x . type . ndim == <NUM_LIT:0> : <EOL> dim = <NUM_LIT:0> <EOL> else : <EOL> dim = dim % x . type . ndim + <NUM_LIT:1> <EOL> pattern . insert ( dim , '<STR_LIT:x>' ) <EOL> return x . dimshuffle ( pattern ) <EOL> def squeeze ( x , axis ) : <EOL> '''<STR_LIT>''' <EOL> x = T . addbroadcast ( x , axis ) <EOL> return T . squeeze ( x ) <EOL> def temporal_padding ( x , padding = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> input_shape = x . shape <EOL> output_shape = ( input_shape [ <NUM_LIT:0> ] , <EOL> input_shape [ <NUM_LIT:1> ] + <NUM_LIT:2> * padding , <EOL> input_shape [ <NUM_LIT:2> ] ) <EOL> output = T . zeros ( output_shape ) <EOL> return T . set_subtensor ( output [ : , padding : x . shape [ <NUM_LIT:1> ] + padding , : ] , x ) <EOL> def spatial_2d_padding ( x , padding = ( <NUM_LIT:1> , <NUM_LIT:1> ) , dim_ordering = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> input_shape = x . shape <EOL> if dim_ordering == '<STR_LIT>' : <EOL> output_shape = ( input_shape [ <NUM_LIT:0> ] , <EOL> input_shape [ <NUM_LIT:1> ] , <EOL> input_shape [ <NUM_LIT:2> ] + <NUM_LIT:2> * padding [ <NUM_LIT:0> ] , <EOL> input_shape [ <NUM_LIT:3> ] + <NUM_LIT:2> * padding [ <NUM_LIT:1> ] ) <EOL> output = T . zeros ( output_shape ) <EOL> indices = ( slice ( None ) , <EOL> slice ( None ) , <EOL> slice ( padding [ <NUM_LIT:0> ] , input_shape [ <NUM_LIT:2> ] + padding [ <NUM_LIT:0> ] ) , <EOL> slice ( padding [ <NUM_LIT:1> ] , input_shape [ <NUM_LIT:3> ] + padding [ <NUM_LIT:1> ] ) ) <EOL> elif dim_ordering == '<STR_LIT>' : <EOL> output_shape = ( input_shape [ <NUM_LIT:0> ] , <EOL> input_shape [ <NUM_LIT:1> ] + <NUM_LIT:2> * padding [ <NUM_LIT:0> ] , <EOL> input_shape [ <NUM_LIT:2> ] + <NUM_LIT:2> * padding [ <NUM_LIT:1> ] , <EOL> input_shape [ <NUM_LIT:3> ] ) <EOL> output = T . zeros ( output_shape ) <EOL> indices = ( slice ( None ) , <EOL> slice ( padding [ <NUM_LIT:0> ] , input_shape [ <NUM_LIT:1> ] + padding [ <NUM_LIT:0> ] ) , <EOL> slice ( padding [ <NUM_LIT:1> ] , input_shape [ <NUM_LIT:2> ] + padding [ <NUM_LIT:1> ] ) , <EOL> slice ( None ) ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + dim_ordering ) <EOL> return T . set_subtensor ( output [ indices ] , x ) <EOL> def spatial_3d_padding ( x , padding = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) , dim_ordering = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> input_shape = x . shape <EOL> if dim_ordering == '<STR_LIT>' : <EOL> output_shape = ( input_shape [ <NUM_LIT:0> ] , <EOL> input_shape [ <NUM_LIT:1> ] , <EOL> input_shape [ <NUM_LIT:2> ] + <NUM_LIT:2> * padding [ <NUM_LIT:0> ] , <EOL> input_shape [ <NUM_LIT:3> ] + <NUM_LIT:2> * padding [ <NUM_LIT:1> ] , <EOL> input_shape [ <NUM_LIT:4> ] + <NUM_LIT:2> * padding [ <NUM_LIT:2> ] ) <EOL> output = T . zeros ( output_shape ) <EOL> indices = ( slice ( None ) , <EOL> slice ( None ) , <EOL> slice ( padding [ <NUM_LIT:0> ] , input_shape [ <NUM_LIT:2> ] + padding [ <NUM_LIT:0> ] ) , <EOL> slice ( padding [ <NUM_LIT:1> ] , input_shape [ <NUM_LIT:3> ] + padding [ <NUM_LIT:1> ] ) , <EOL> slice ( padding [ <NUM_LIT:2> ] , input_shape [ <NUM_LIT:4> ] + padding [ <NUM_LIT:2> ] ) ) <EOL> elif dim_ordering == '<STR_LIT>' : <EOL> output_shape = ( input_shape [ <NUM_LIT:0> ] , <EOL> input_shape [ <NUM_LIT:1> ] + <NUM_LIT:2> * padding [ <NUM_LIT:0> ] , <EOL> input_shape [ <NUM_LIT:2> ] + <NUM_LIT:2> * padding [ <NUM_LIT:1> ] , <EOL> input_shape [ <NUM_LIT:3> ] + <NUM_LIT:2> * padding [ <NUM_LIT:2> ] , <EOL> input_shape [ <NUM_LIT:4> ] ) <EOL> output = T . zeros ( output_shape ) <EOL> indices = ( slice ( None ) , <EOL> slice ( padding [ <NUM_LIT:0> ] , input_shape [ <NUM_LIT:1> ] + padding [ <NUM_LIT:0> ] ) , <EOL> slice ( padding [ <NUM_LIT:1> ] , input_shape [ <NUM_LIT:2> ] + padding [ <NUM_LIT:1> ] ) , <EOL> slice ( padding [ <NUM_LIT:2> ] , input_shape [ <NUM_LIT:3> ] + padding [ <NUM_LIT:2> ] ) , <EOL> slice ( None ) ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + dim_ordering ) <EOL> return T . set_subtensor ( output [ indices ] , x ) <EOL> def pack ( x ) : <EOL> return T . stack ( * x ) <EOL> def get_value ( x ) : <EOL> if not hasattr ( x , '<STR_LIT>' ) : <EOL> raise Exception ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> return x . get_value ( ) <EOL> def set_value ( x , value ) : <EOL> x . set_value ( np . asarray ( value , dtype = x . dtype ) ) <EOL> def batch_set_value ( tuples ) : <EOL> for x , value in tuples : <EOL> x . set_value ( np . asarray ( value , dtype = x . dtype ) ) <EOL> class Function ( object ) : <EOL> def __init__ ( self , inputs , outputs , updates = [ ] , ** kwargs ) : <EOL> self . function = theano . function ( inputs , outputs , updates = updates , <EOL> allow_input_downcast = True , <EOL> on_unused_input = '<STR_LIT>' , <EOL> ** kwargs ) <EOL> def __call__ ( self , inputs ) : <EOL> assert type ( inputs ) in { list , tuple } <EOL> return self . function ( * inputs ) <EOL> def function ( inputs , outputs , updates = [ ] , ** kwargs ) : <EOL> if len ( kwargs ) > <NUM_LIT:0> : <EOL> function_args = inspect . getargspec ( theano . function ) [ <NUM_LIT:0> ] <EOL> for key in kwargs . keys ( ) : <EOL> if key not in function_args : <EOL> msg = "<STR_LIT>" % key <EOL> raise ValueError ( msg ) <EOL> return Function ( inputs , outputs , updates = updates , ** kwargs ) <EOL> def gradients ( loss , variables ) : <EOL> return T . grad ( loss , variables ) <EOL> def rnn ( step_function , inputs , initial_states , <EOL> go_backwards = False , mask = None , constants = None , <EOL> unroll = False , input_length = None ) : <EOL> '''<STR_LIT>''' <EOL> ndim = inputs . ndim <EOL> assert ndim >= <NUM_LIT:3> , '<STR_LIT>' <EOL> if unroll : <EOL> if input_length is None : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> axes = [ <NUM_LIT:1> , <NUM_LIT:0> ] + list ( range ( <NUM_LIT:2> , ndim ) ) <EOL> inputs = inputs . dimshuffle ( axes ) <EOL> if constants is None : <EOL> constants = [ ] <EOL> if mask is not None : <EOL> if mask . ndim == ndim - <NUM_LIT:1> : <EOL> mask = expand_dims ( mask ) <EOL> assert mask . ndim == ndim <EOL> mask = mask . dimshuffle ( axes ) <EOL> if unroll : <EOL> indices = list ( range ( input_length ) ) <EOL> if go_backwards : <EOL> indices = indices [ : : - <NUM_LIT:1> ] <EOL> successive_outputs = [ ] <EOL> successive_states = [ ] <EOL> states = initial_states <EOL> for i in indices : <EOL> output , new_states = step_function ( inputs [ i ] , states + constants ) <EOL> if len ( successive_outputs ) == <NUM_LIT:0> : <EOL> prev_output = zeros_like ( output ) <EOL> else : <EOL> prev_output = successive_outputs [ - <NUM_LIT:1> ] <EOL> output = T . switch ( mask [ i ] , output , prev_output ) <EOL> kept_states = [ ] <EOL> for state , new_state in zip ( states , new_states ) : <EOL> kept_states . append ( T . switch ( mask [ i ] , new_state , state ) ) <EOL> states = kept_states <EOL> successive_outputs . append ( output ) <EOL> successive_states . append ( states ) <EOL> outputs = T . stack ( * successive_outputs ) <EOL> states = [ ] <EOL> for i in range ( len ( successive_states [ - <NUM_LIT:1> ] ) ) : <EOL> states . append ( T . stack ( * [ states_at_step [ i ] for states_at_step in successive_states ] ) ) <EOL> else : <EOL> initial_output = step_function ( inputs [ <NUM_LIT:0> ] , initial_states + constants ) [ <NUM_LIT:0> ] * <NUM_LIT:0> <EOL> initial_output = T . unbroadcast ( initial_output , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> def _step ( input , mask , output_tm1 , * states ) : <EOL> output , new_states = step_function ( input , states ) <EOL> output = T . switch ( mask , output , output_tm1 ) <EOL> return_states = [ ] <EOL> for state , new_state in zip ( states , new_states ) : <EOL> return_states . append ( T . switch ( mask , new_state , state ) ) <EOL> return [ output ] + return_states <EOL> results , _ = theano . scan ( <EOL> _step , <EOL> sequences = [ inputs , mask ] , <EOL> outputs_info = [ initial_output ] + initial_states , <EOL> non_sequences = constants , <EOL> go_backwards = go_backwards ) <EOL> if type ( results ) is list : <EOL> outputs = results [ <NUM_LIT:0> ] <EOL> states = results [ <NUM_LIT:1> : ] <EOL> else : <EOL> outputs = results <EOL> states = [ ] <EOL> else : <EOL> if unroll : <EOL> indices = list ( range ( input_length ) ) <EOL> if go_backwards : <EOL> indices = indices [ : : - <NUM_LIT:1> ] <EOL> successive_outputs = [ ] <EOL> successive_states = [ ] <EOL> states = initial_states <EOL> for i in indices : <EOL> output , states = step_function ( inputs [ i ] , states + constants ) <EOL> successive_outputs . append ( output ) <EOL> successive_states . append ( states ) <EOL> outputs = T . stack ( * successive_outputs ) <EOL> states = [ ] <EOL> for i in range ( len ( successive_states [ - <NUM_LIT:1> ] ) ) : <EOL> states . append ( T . stack ( * [ states_at_step [ i ] for states_at_step in successive_states ] ) ) <EOL> else : <EOL> def _step ( input , * states ) : <EOL> output , new_states = step_function ( input , states ) <EOL> return [ output ] + new_states <EOL> results , _ = theano . scan ( <EOL> _step , <EOL> sequences = inputs , <EOL> outputs_info = [ None ] + initial_states , <EOL> non_sequences = constants , <EOL> go_backwards = go_backwards ) <EOL> if type ( results ) is list : <EOL> outputs = results [ <NUM_LIT:0> ] <EOL> states = results [ <NUM_LIT:1> : ] <EOL> else : <EOL> outputs = results <EOL> states = [ ] <EOL> outputs = T . squeeze ( outputs ) <EOL> last_output = outputs [ - <NUM_LIT:1> ] <EOL> axes = [ <NUM_LIT:1> , <NUM_LIT:0> ] + list ( range ( <NUM_LIT:2> , outputs . ndim ) ) <EOL> outputs = outputs . dimshuffle ( axes ) <EOL> states = [ T . squeeze ( state [ - <NUM_LIT:1> ] ) for state in states ] <EOL> return last_output , outputs , states <EOL> def switch ( condition , then_expression , else_expression ) : <EOL> '''<STR_LIT>''' <EOL> return T . switch ( condition , then_expression , else_expression ) <EOL> def in_train_phase ( x , alt ) : <EOL> x = T . switch ( _LEARNING_PHASE , x , alt ) <EOL> x . _uses_learning_phase = True <EOL> return x <EOL> def in_test_phase ( x , alt ) : <EOL> x = T . switch ( _LEARNING_PHASE , alt , x ) <EOL> x . _uses_learning_phase = True <EOL> return x <EOL> def relu ( x , alpha = <NUM_LIT:0.> , max_value = None ) : <EOL> assert hasattr ( T . nnet , '<STR_LIT:relu>' ) , ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> x = T . nnet . relu ( x , alpha ) <EOL> if max_value is not None : <EOL> x = T . minimum ( x , max_value ) <EOL> return x <EOL> def softmax ( x ) : <EOL> return T . nnet . softmax ( x ) <EOL> def softplus ( x ) : <EOL> return T . nnet . softplus ( x ) <EOL> def categorical_crossentropy ( output , target , from_logits = False ) : <EOL> if from_logits : <EOL> output = T . nnet . softmax ( output ) <EOL> else : <EOL> output /= output . sum ( axis = - <NUM_LIT:1> , keepdims = True ) <EOL> output = T . clip ( output , _EPSILON , <NUM_LIT:1.0> - _EPSILON ) <EOL> return T . nnet . categorical_crossentropy ( output , target ) <EOL> def sparse_categorical_crossentropy ( output , target , from_logits = False ) : <EOL> target = T . cast ( T . flatten ( target ) , '<STR_LIT>' ) <EOL> target = T . extra_ops . to_one_hot ( target , nb_class = output . shape [ - <NUM_LIT:1> ] ) <EOL> target = reshape ( target , shape ( output ) ) <EOL> return categorical_crossentropy ( output , target , from_logits ) <EOL> def binary_crossentropy ( output , target , from_logits = False ) : <EOL> if from_logits : <EOL> output = T . nnet . sigmoid ( output ) <EOL> output = T . clip ( output , _EPSILON , <NUM_LIT:1.0> - _EPSILON ) <EOL> return T . nnet . binary_crossentropy ( output , target ) <EOL> def sigmoid ( x ) : <EOL> return T . nnet . sigmoid ( x ) <EOL> def hard_sigmoid ( x ) : <EOL> return T . nnet . hard_sigmoid ( x ) <EOL> def tanh ( x ) : <EOL> return T . tanh ( x ) <EOL> def dropout ( x , level , seed = None ) : <EOL> if level < <NUM_LIT:0.> or level >= <NUM_LIT:1> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if seed is None : <EOL> seed = np . random . randint ( <NUM_LIT> ) <EOL> rng = RandomStreams ( seed = seed ) <EOL> retain_prob = <NUM_LIT:1.> - level <EOL> x *= rng . binomial ( x . shape , p = retain_prob , dtype = x . dtype ) <EOL> x /= retain_prob <EOL> return x <EOL> def l2_normalize ( x , axis ) : <EOL> norm = T . sqrt ( T . sum ( T . square ( x ) , axis = axis , keepdims = True ) ) <EOL> return x / norm <EOL> def conv2d ( x , kernel , strides = ( <NUM_LIT:1> , <NUM_LIT:1> ) , border_mode = '<STR_LIT>' , dim_ordering = '<STR_LIT>' , <EOL> image_shape = None , filter_shape = None ) : <EOL> '''<STR_LIT>''' <EOL> if dim_ordering not in { '<STR_LIT>' , '<STR_LIT>' } : <EOL> raise Exception ( '<STR_LIT>' + str ( dim_ordering ) ) <EOL> if dim_ordering == '<STR_LIT>' : <EOL> x = x . dimshuffle ( ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> kernel = kernel . dimshuffle ( ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> if image_shape : <EOL> image_shape = ( image_shape [ <NUM_LIT:0> ] , image_shape [ <NUM_LIT:3> ] , <EOL> image_shape [ <NUM_LIT:1> ] , image_shape [ <NUM_LIT:2> ] ) <EOL> if filter_shape : <EOL> filter_shape = ( filter_shape [ <NUM_LIT:3> ] , filter_shape [ <NUM_LIT:2> ] , <EOL> filter_shape [ <NUM_LIT:0> ] , filter_shape [ <NUM_LIT:1> ] ) <EOL> if border_mode == '<STR_LIT>' : <EOL> th_border_mode = '<STR_LIT>' <EOL> np_kernel = kernel . eval ( ) <EOL> elif border_mode == '<STR_LIT>' : <EOL> th_border_mode = '<STR_LIT>' <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + str ( border_mode ) ) <EOL> def int_or_none ( value ) : <EOL> try : <EOL> return int ( value ) <EOL> except TypeError : <EOL> return None <EOL> if image_shape is not None : <EOL> image_shape = tuple ( int_or_none ( v ) for v in image_shape ) <EOL> if filter_shape is not None : <EOL> filter_shape = tuple ( int_or_none ( v ) for v in filter_shape ) <EOL> conv_out = T . nnet . conv2d ( x , kernel , <EOL> border_mode = th_border_mode , <EOL> subsample = strides , <EOL> input_shape = image_shape , <EOL> filter_shape = filter_shape ) <EOL> if border_mode == '<STR_LIT>' : <EOL> if np_kernel . shape [ <NUM_LIT:2> ] % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> conv_out = conv_out [ : , : , : ( x . shape [ <NUM_LIT:2> ] + strides [ <NUM_LIT:0> ] - <NUM_LIT:1> ) // strides [ <NUM_LIT:0> ] , : ] <EOL> if np_kernel . shape [ <NUM_LIT:3> ] % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> conv_out = conv_out [ : , : , : , : ( x . shape [ <NUM_LIT:3> ] + strides [ <NUM_LIT:1> ] - <NUM_LIT:1> ) // strides [ <NUM_LIT:1> ] ] <EOL> if dim_ordering == '<STR_LIT>' : <EOL> conv_out = conv_out . dimshuffle ( ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ) ) <EOL> return conv_out <EOL> def conv3d ( x , kernel , strides = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> border_mode = '<STR_LIT>' , dim_ordering = '<STR_LIT>' , <EOL> volume_shape = None , filter_shape = None ) : <EOL> '''<STR_LIT>''' <EOL> if dim_ordering not in { '<STR_LIT>' , '<STR_LIT>' } : <EOL> raise Exception ( '<STR_LIT>' + str ( dim_ordering ) ) <EOL> if border_mode not in { '<STR_LIT>' , '<STR_LIT>' } : <EOL> raise Exception ( '<STR_LIT>' + str ( border_mode ) ) <EOL> if dim_ordering == '<STR_LIT>' : <EOL> x = x . dimshuffle ( ( <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> kernel = kernel . dimshuffle ( ( <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> if volume_shape : <EOL> volume_shape = ( volume_shape [ <NUM_LIT:0> ] , volume_shape [ <NUM_LIT:4> ] , <EOL> volume_shape [ <NUM_LIT:1> ] , volume_shape [ <NUM_LIT:2> ] , volume_shape [ <NUM_LIT:3> ] ) <EOL> if filter_shape : <EOL> filter_shape = ( filter_shape [ <NUM_LIT:4> ] , filter_shape [ <NUM_LIT:3> ] , <EOL> filter_shape [ <NUM_LIT:0> ] , filter_shape [ <NUM_LIT:1> ] , filter_shape [ <NUM_LIT:2> ] ) <EOL> if border_mode == '<STR_LIT>' : <EOL> assert ( strides == ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> pad_dim1 = ( kernel . shape [ <NUM_LIT:2> ] - <NUM_LIT:1> ) <EOL> pad_dim2 = ( kernel . shape [ <NUM_LIT:3> ] - <NUM_LIT:1> ) <EOL> pad_dim3 = ( kernel . shape [ <NUM_LIT:4> ] - <NUM_LIT:1> ) <EOL> output_shape = ( x . shape [ <NUM_LIT:0> ] , x . shape [ <NUM_LIT:1> ] , <EOL> x . shape [ <NUM_LIT:2> ] + pad_dim1 , <EOL> x . shape [ <NUM_LIT:3> ] + pad_dim2 , <EOL> x . shape [ <NUM_LIT:4> ] + pad_dim3 ) <EOL> output = T . zeros ( output_shape ) <EOL> indices = ( slice ( None ) , slice ( None ) , <EOL> slice ( pad_dim1 // <NUM_LIT:2> , x . shape [ <NUM_LIT:2> ] + pad_dim1 // <NUM_LIT:2> ) , <EOL> slice ( pad_dim2 // <NUM_LIT:2> , x . shape [ <NUM_LIT:3> ] + pad_dim2 // <NUM_LIT:2> ) , <EOL> slice ( pad_dim3 // <NUM_LIT:2> , x . shape [ <NUM_LIT:4> ] + pad_dim3 // <NUM_LIT:2> ) ) <EOL> x = T . set_subtensor ( output [ indices ] , x ) <EOL> border_mode = '<STR_LIT>' <EOL> border_mode_3d = ( border_mode , border_mode , border_mode ) <EOL> conv_out = conv3d2d . conv3d ( signals = x . dimshuffle ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> ) , <EOL> filters = kernel . dimshuffle ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> ) , <EOL> border_mode = border_mode_3d ) <EOL> conv_out = conv_out . dimshuffle ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> if strides != ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) : <EOL> conv_out = conv_out [ : , : , : : strides [ <NUM_LIT:0> ] , : : strides [ <NUM_LIT:1> ] , : : strides [ <NUM_LIT:2> ] ] <EOL> if dim_ordering == '<STR_LIT>' : <EOL> conv_out = conv_out . dimshuffle ( ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> return conv_out <EOL> def pool2d ( x , pool_size , strides = ( <NUM_LIT:1> , <NUM_LIT:1> ) , border_mode = '<STR_LIT>' , <EOL> dim_ordering = '<STR_LIT>' , pool_mode = '<STR_LIT>' ) : <EOL> if border_mode == '<STR_LIT>' : <EOL> w_pad = pool_size [ <NUM_LIT:0> ] - <NUM_LIT:2> if pool_size [ <NUM_LIT:0> ] % <NUM_LIT:2> == <NUM_LIT:1> else pool_size [ <NUM_LIT:0> ] - <NUM_LIT:1> <EOL> h_pad = pool_size [ <NUM_LIT:1> ] - <NUM_LIT:2> if pool_size [ <NUM_LIT:1> ] % <NUM_LIT:2> == <NUM_LIT:1> else pool_size [ <NUM_LIT:1> ] - <NUM_LIT:1> <EOL> padding = ( w_pad , h_pad ) <EOL> elif border_mode == '<STR_LIT>' : <EOL> padding = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + str ( border_mode ) ) <EOL> if dim_ordering not in { '<STR_LIT>' , '<STR_LIT>' } : <EOL> raise Exception ( '<STR_LIT>' + str ( dim_ordering ) ) <EOL> if dim_ordering == '<STR_LIT>' : <EOL> x = x . dimshuffle ( ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> if pool_mode == '<STR_LIT>' : <EOL> pool_out = pool . pool_2d ( x , ds = pool_size , st = strides , <EOL> ignore_border = True , <EOL> padding = padding , <EOL> mode = '<STR_LIT>' ) <EOL> elif pool_mode == '<STR_LIT>' : <EOL> pool_out = pool . pool_2d ( x , ds = pool_size , st = strides , <EOL> ignore_border = True , <EOL> padding = padding , <EOL> mode = '<STR_LIT>' ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + str ( pool_mode ) ) <EOL> if border_mode == '<STR_LIT>' : <EOL> expected_width = ( x . shape [ <NUM_LIT:2> ] + strides [ <NUM_LIT:0> ] - <NUM_LIT:1> ) // strides [ <NUM_LIT:0> ] <EOL> expected_height = ( x . shape [ <NUM_LIT:3> ] + strides [ <NUM_LIT:1> ] - <NUM_LIT:1> ) // strides [ <NUM_LIT:1> ] <EOL> pool_out = pool_out [ : , : , <EOL> : expected_width , <EOL> : expected_height ] <EOL> if dim_ordering == '<STR_LIT>' : <EOL> pool_out = pool_out . dimshuffle ( ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ) ) <EOL> return pool_out <EOL> def pool3d ( x , pool_size , strides = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) , border_mode = '<STR_LIT>' , <EOL> dim_ordering = '<STR_LIT>' , pool_mode = '<STR_LIT>' ) : <EOL> if border_mode == '<STR_LIT>' : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> elif border_mode == '<STR_LIT>' : <EOL> ignore_border = True <EOL> padding = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + str ( border_mode ) ) <EOL> if dim_ordering not in { '<STR_LIT>' , '<STR_LIT>' } : <EOL> raise Exception ( '<STR_LIT>' + str ( dim_ordering ) ) <EOL> if dim_ordering == '<STR_LIT>' : <EOL> x = x . dimshuffle ( ( <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> if pool_mode == '<STR_LIT>' : <EOL> output = pool . pool_2d ( input = x . dimshuffle ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:2> ) , <EOL> ds = ( pool_size [ <NUM_LIT:1> ] , pool_size [ <NUM_LIT:0> ] ) , <EOL> st = ( strides [ <NUM_LIT:1> ] , strides [ <NUM_LIT:0> ] ) , <EOL> ignore_border = ignore_border , <EOL> padding = padding , <EOL> mode = '<STR_LIT>' ) <EOL> pool_out = pool . pool_2d ( input = output . dimshuffle ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:2> ) , <EOL> ds = ( <NUM_LIT:1> , pool_size [ <NUM_LIT:2> ] ) , <EOL> st = ( <NUM_LIT:1> , strides [ <NUM_LIT:2> ] ) , <EOL> ignore_border = ignore_border , <EOL> padding = padding , <EOL> mode = '<STR_LIT>' ) <EOL> elif pool_mode == '<STR_LIT>' : <EOL> output = pool . pool_2d ( input = x . dimshuffle ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:2> ) , <EOL> ds = ( pool_size [ <NUM_LIT:1> ] , pool_size [ <NUM_LIT:0> ] ) , <EOL> st = ( strides [ <NUM_LIT:1> ] , strides [ <NUM_LIT:0> ] ) , <EOL> ignore_border = ignore_border , <EOL> padding = padding , <EOL> mode = '<STR_LIT>' ) <EOL> pool_out = pool . pool_2d ( input = output . dimshuffle ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:2> ) , <EOL> ds = ( <NUM_LIT:1> , pool_size [ <NUM_LIT:2> ] ) , <EOL> st = ( <NUM_LIT:1> , strides [ <NUM_LIT:2> ] ) , <EOL> ignore_border = ignore_border , <EOL> padding = padding , <EOL> mode = '<STR_LIT>' ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + str ( pool_mode ) ) <EOL> if dim_ordering == '<STR_LIT>' : <EOL> pool_out = pool_out . dimshuffle ( ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> return pool_out <EOL> def random_normal ( shape , mean = <NUM_LIT:0.0> , std = <NUM_LIT:1.0> , dtype = _FLOATX , seed = None ) : <EOL> if seed is None : <EOL> seed = np . random . randint ( <NUM_LIT> ) <EOL> rng = RandomStreams ( seed = seed ) <EOL> return rng . normal ( size = shape , avg = mean , std = std , dtype = dtype ) <EOL> def random_uniform ( shape , low = <NUM_LIT:0.0> , high = <NUM_LIT:1.0> , dtype = _FLOATX , seed = None ) : <EOL> if seed is None : <EOL> seed = np . random . randint ( <NUM_LIT> ) <EOL> rng = RandomStreams ( seed = seed ) <EOL> return rng . uniform ( shape , low = low , high = high , dtype = dtype ) <EOL> def random_binomial ( shape , p = <NUM_LIT:0.0> , dtype = _FLOATX , seed = None ) : <EOL> if seed is None : <EOL> seed = np . random . randint ( <NUM_LIT> ) <EOL> rng = RandomStreams ( seed = seed ) <EOL> return rng . binomial ( shape , p = p , dtype = dtype ) </s>
