<s> """<STR_LIT>""" <EOL> __authors__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __credits__ = [ "<STR_LIT>" ] <EOL> __license__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> import numpy as np <EOL> import theano <EOL> from theano . printing import Print <EOL> from theano import tensor as T <EOL> from theano . gof . op import get_debug_values <EOL> def softmax_numpy ( x ) : <EOL> """<STR_LIT>""" <EOL> stable_x = ( x . T - x . max ( axis = <NUM_LIT:1> ) ) . T <EOL> numer = np . exp ( stable_x ) <EOL> return ( numer . T / numer . sum ( axis = <NUM_LIT:1> ) ) . T <EOL> def pseudoinverse_softmax_numpy ( x ) : <EOL> """<STR_LIT>""" <EOL> rval = np . log ( x ) <EOL> rval -= rval . mean ( ) <EOL> return rval <EOL> def sigmoid_numpy ( x ) : <EOL> """<STR_LIT>""" <EOL> assert not isinstance ( x , theano . gof . Variable ) <EOL> return <NUM_LIT:1.> / ( <NUM_LIT:1.> + np . exp ( - x ) ) <EOL> def inverse_sigmoid_numpy ( x ) : <EOL> """<STR_LIT>""" <EOL> return np . log ( x / ( <NUM_LIT:1.> - x ) ) <EOL> def arg_of_softmax ( Y_hat ) : <EOL> """<STR_LIT>""" <EOL> assert hasattr ( Y_hat , '<STR_LIT>' ) <EOL> owner = Y_hat . owner <EOL> assert owner is not None <EOL> op = owner . op <EOL> if isinstance ( op , Print ) : <EOL> assert len ( owner . inputs ) == <NUM_LIT:1> <EOL> Y_hat , = owner . inputs <EOL> owner = Y_hat . owner <EOL> op = owner . op <EOL> if not isinstance ( op , T . nnet . Softmax ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" + str ( op ) + <EOL> "<STR_LIT>" + str ( type ( op ) ) ) <EOL> z , = owner . inputs <EOL> assert z . ndim == <NUM_LIT:2> <EOL> return z <EOL> def arg_of_sigmoid ( Y_hat ) : <EOL> """<STR_LIT>""" <EOL> assert hasattr ( Y_hat , '<STR_LIT>' ) <EOL> owner = Y_hat . owner <EOL> assert owner is not None <EOL> op = owner . op <EOL> if isinstance ( op , Print ) : <EOL> assert len ( owner . inputs ) == <NUM_LIT:1> <EOL> Y_hat , = owner . inputs <EOL> owner = Y_hat . owner <EOL> op = owner . op <EOL> success = False <EOL> if isinstance ( op , T . Elemwise ) : <EOL> if isinstance ( op . scalar_op , T . nnet . sigm . ScalarSigmoid ) : <EOL> success = True <EOL> if not success : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" + str ( op ) + <EOL> "<STR_LIT>" + str ( type ( op ) ) ) <EOL> z , = owner . inputs <EOL> assert z . ndim == <NUM_LIT:2> <EOL> return z <EOL> def kl ( Y , Y_hat , batch_axis ) : <EOL> """<STR_LIT>""" <EOL> assert hasattr ( Y_hat , '<STR_LIT>' ) <EOL> assert batch_axis is not None <EOL> owner = Y_hat . owner <EOL> assert owner is not None <EOL> op = owner . op <EOL> if not hasattr ( op , '<STR_LIT>' ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" + str ( op ) + "<STR_LIT>" + str ( type ( op ) ) ) <EOL> assert isinstance ( op . scalar_op , T . nnet . sigm . ScalarSigmoid ) <EOL> for Yv in get_debug_values ( Y ) : <EOL> if not ( Yv . min ( ) >= <NUM_LIT:0.0> and Yv . max ( ) <= <NUM_LIT:1.0> ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> + "<STR_LIT>" ) <EOL> z , = owner . inputs <EOL> term_1 = Y * T . nnet . softplus ( - z ) <EOL> term_2 = ( <NUM_LIT:1> - Y ) * T . nnet . softplus ( z ) <EOL> total = term_1 + term_2 <EOL> naxes = total . ndim <EOL> axes_to_reduce = list ( range ( naxes ) ) <EOL> del axes_to_reduce [ batch_axis ] <EOL> ave = total . mean ( axis = axes_to_reduce ) <EOL> return ave <EOL> def elemwise_kl ( Y , Y_hat ) : <EOL> """<STR_LIT>""" <EOL> assert hasattr ( Y_hat , '<STR_LIT>' ) <EOL> owner = Y_hat . owner <EOL> assert owner is not None <EOL> op = owner . op <EOL> if not hasattr ( op , '<STR_LIT>' ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" + str ( op ) + "<STR_LIT>" + str ( type ( op ) ) ) <EOL> assert isinstance ( op . scalar_op , T . nnet . sigm . ScalarSigmoid ) <EOL> for Yv in get_debug_values ( Y ) : <EOL> if not ( Yv . min ( ) >= <NUM_LIT:0.0> and Yv . max ( ) <= <NUM_LIT:1.0> ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> + "<STR_LIT>" ) <EOL> z , = owner . inputs <EOL> term_1 = Y * T . nnet . softplus ( - z ) <EOL> term_2 = ( <NUM_LIT:1> - Y ) * T . nnet . softplus ( z ) <EOL> total = term_1 + term_2 <EOL> return total <EOL> def softmax_ratio ( numer , denom ) : <EOL> """<STR_LIT>""" <EOL> numer_Z = arg_of_softmax ( numer ) <EOL> denom_Z = arg_of_softmax ( denom ) <EOL> numer_Z -= numer_Z . max ( axis = <NUM_LIT:1> ) . dimshuffle ( <NUM_LIT:0> , '<STR_LIT:x>' ) <EOL> denom_Z -= denom_Z . min ( axis = <NUM_LIT:1> ) . dimshuffle ( <NUM_LIT:0> , '<STR_LIT:x>' ) <EOL> new_num = T . exp ( numer_Z - denom_Z ) * ( T . exp ( denom_Z ) . sum ( <EOL> axis = <NUM_LIT:1> ) . dimshuffle ( <NUM_LIT:0> , '<STR_LIT:x>' ) ) <EOL> new_den = ( T . exp ( numer_Z ) . sum ( axis = <NUM_LIT:1> ) . dimshuffle ( <NUM_LIT:0> , '<STR_LIT:x>' ) ) <EOL> return new_num / new_den <EOL> def compute_precision ( tp , fp ) : <EOL> """<STR_LIT>""" <EOL> precision = tp / T . maximum ( <NUM_LIT:1.> , tp + fp ) <EOL> return precision <EOL> def compute_recall ( y , tp ) : <EOL> """<STR_LIT>""" <EOL> recall = tp / T . maximum ( <NUM_LIT:1.> , y . sum ( ) ) <EOL> return recall <EOL> def compute_f1 ( precision , recall ) : <EOL> """<STR_LIT>""" <EOL> f1 = ( <NUM_LIT> * precision * recall / <EOL> T . maximum ( <NUM_LIT:1> , precision + recall ) ) <EOL> return f1 </s>
