<s> from __future__ import absolute_import , print_function , division <EOL> import unittest <EOL> import numpy <EOL> from theano . compat import imap <EOL> import theano . tensor . inplace <EOL> from theano . tensor import basic as tensor <EOL> from theano import tensor as T <EOL> from theano import config <EOL> from theano . tests import unittest_tools as utt <EOL> from theano . tensor . nnet import ( sigmoid , sigmoid_inplace , <EOL> softplus , ultra_fast_sigmoid , hard_sigmoid ) <EOL> from theano . tensor . nnet . sigm import ( <EOL> compute_mul , is_1pexp , parse_mul_tree , perform_sigm_times_exp , <EOL> register_local_1msigmoid , simplify_mul , <EOL> ) <EOL> from theano . tensor . tests . test_basic import ( makeBroadcastTester , rand , <EOL> check_floatX , upcast_int8_nfunc , <EOL> _good_broadcast_unary_normal_no_complex ) <EOL> class T_sigmoid ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> utt . seed_rng ( ) <EOL> def test_elemwise ( self ) : <EOL> utt . verify_grad ( sigmoid , [ numpy . random . rand ( <NUM_LIT:3> , <NUM_LIT:4> ) ] ) <EOL> SigmoidTester = makeBroadcastTester ( <EOL> op = sigmoid , <EOL> expected = upcast_int8_nfunc ( lambda inputs : check_floatX ( <EOL> inputs , <NUM_LIT:1> / ( <NUM_LIT:1> + numpy . exp ( - inputs ) ) ) ) , <EOL> good = _good_broadcast_unary_normal_no_complex , <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> UltraFastSigmoidTester = makeBroadcastTester ( <EOL> op = ultra_fast_sigmoid , <EOL> expected = upcast_int8_nfunc ( lambda inputs : check_floatX ( <EOL> inputs , <NUM_LIT:1> / ( <NUM_LIT:1> + numpy . exp ( - inputs ) ) ) ) , <EOL> good = _good_broadcast_unary_normal_no_complex , <EOL> name = '<STR_LIT>' , <EOL> eps = <NUM_LIT> ) <EOL> HardSigmoidTester = makeBroadcastTester ( <EOL> op = hard_sigmoid , <EOL> expected = upcast_int8_nfunc ( lambda inputs : check_floatX ( <EOL> inputs , <NUM_LIT:1> / ( <NUM_LIT:1> + numpy . exp ( - inputs ) ) ) ) , <EOL> good = _good_broadcast_unary_normal_no_complex , <EOL> name = '<STR_LIT>' , <EOL> eps = <NUM_LIT> ) <EOL> SoftplusTester = makeBroadcastTester ( <EOL> op = softplus , <EOL> expected = upcast_int8_nfunc ( lambda inputs : check_floatX ( <EOL> inputs , numpy . log1p ( numpy . exp ( inputs ) ) ) ) , <EOL> good = dict ( _good_broadcast_unary_normal_no_complex , <EOL> int8 = [ numpy . arange ( - <NUM_LIT> , <NUM_LIT> , dtype = '<STR_LIT>' ) ] ) , <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> class T_softplus ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> utt . seed_rng ( ) <EOL> def test_elemwise ( self ) : <EOL> utt . verify_grad ( softplus , [ numpy . random . rand ( <NUM_LIT:3> , <NUM_LIT:4> ) ] ) <EOL> class T_sigmoid_opts ( unittest . TestCase ) : <EOL> def get_mode ( self , excluding = None ) : <EOL> """<STR_LIT>""" <EOL> if excluding is None : <EOL> excluding = [ ] <EOL> m = theano . config . mode <EOL> if m == '<STR_LIT>' : <EOL> mode = theano . compile . mode . get_mode ( '<STR_LIT>' ) <EOL> else : <EOL> mode = theano . compile . mode . get_default_mode ( ) <EOL> if excluding : <EOL> return mode . excluding ( * excluding ) <EOL> else : <EOL> return mode <EOL> def test_exp_over_1_plus_exp ( self ) : <EOL> m = self . get_mode ( excluding = [ '<STR_LIT>' ] ) <EOL> x = T . vector ( ) <EOL> data = numpy . random . rand ( <NUM_LIT> ) . astype ( config . floatX ) <EOL> backup = config . warn . identify_1pexp_bug <EOL> config . warn . identify_1pexp_bug = False <EOL> try : <EOL> f = theano . function ( [ x ] , T . exp ( x ) / ( <NUM_LIT:1> + T . exp ( x ) ) , mode = m ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] == [ sigmoid ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , T . exp ( x ) / ( <NUM_LIT:2> + T . exp ( x ) ) , mode = m ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , T . exp ( x ) / ( <NUM_LIT:1> - T . exp ( x ) ) , mode = m ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , T . exp ( x + <NUM_LIT:1> ) / ( <NUM_LIT:1> + T . exp ( x ) ) , mode = m ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , T . fill ( x , <NUM_LIT:1.0> ) / ( <NUM_LIT:1> + T . exp ( - x ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] == [ sigmoid ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , T . fill ( x , <NUM_LIT:1.0> ) / ( <NUM_LIT:2> + T . exp ( - x ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , T . fill ( x , <NUM_LIT:1.0> ) / ( <NUM_LIT:1> - T . exp ( - x ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , T . fill ( x , <NUM_LIT> ) / ( <NUM_LIT:1> + T . exp ( - x ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , T . fill ( x , - <NUM_LIT:1.0> ) / ( <NUM_LIT:1> + T . exp ( - x ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] == [ sigmoid , <EOL> theano . tensor . inplace . neg_inplace ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , T . fill ( x , - <NUM_LIT:1.0> ) / ( <NUM_LIT:1> - T . exp ( - x ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid , <EOL> theano . tensor . inplace . neg_inplace ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , T . fill ( x , - <NUM_LIT:1.0> ) / ( <NUM_LIT:2> + T . exp ( - x ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid , <EOL> theano . tensor . inplace . neg_inplace ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , T . fill ( x , - <NUM_LIT> ) / ( <NUM_LIT:1> + T . exp ( - x ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid , <EOL> theano . tensor . inplace . neg_inplace ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , ( T . fill ( x , - <NUM_LIT:1.0> ) * T . exp ( x ) ) / <EOL> ( ( <NUM_LIT:1> + T . exp ( x ) ) * ( <NUM_LIT:1> + T . exp ( - x ) ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] == [ sigmoid , <EOL> T . mul ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , ( T . fill ( x , - <NUM_LIT> ) * T . exp ( x ) ) / <EOL> ( ( <NUM_LIT:1> + T . exp ( x ) ) * ( <NUM_LIT:1> + T . exp ( - x ) ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid , <EOL> T . mul , theano . tensor . inplace . neg_inplace ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , ( T . fill ( x , - <NUM_LIT:1.0> ) * T . exp ( x ) ) / <EOL> ( ( <NUM_LIT:2> + T . exp ( x ) ) * ( <NUM_LIT:1> + T . exp ( - x ) ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid , <EOL> T . mul , theano . tensor . inplace . neg_inplace ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , ( T . fill ( x , - <NUM_LIT:1.0> ) * T . exp ( x ) ) / <EOL> ( ( <NUM_LIT:1> + T . exp ( x ) ) * ( <NUM_LIT:2> + T . exp ( - x ) ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid , <EOL> T . mul , theano . tensor . inplace . neg_inplace ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , ( T . fill ( x , - <NUM_LIT:1.0> ) * T . exp ( x ) ) / <EOL> ( ( <NUM_LIT:1> + T . exp ( x ) ) * ( <NUM_LIT:1> + T . exp ( x ) ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid , <EOL> T . mul , theano . tensor . inplace . neg_inplace ] <EOL> f ( data ) <EOL> f = theano . function ( [ x ] , ( T . fill ( x , - <NUM_LIT:1.0> ) * T . exp ( x ) ) / <EOL> ( ( <NUM_LIT:1> + T . exp ( x ) ) * ( <NUM_LIT:2> + T . exp ( - x ) ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] != [ sigmoid , <EOL> T . mul , theano . tensor . inplace . neg_inplace ] <EOL> f ( data ) <EOL> finally : <EOL> config . warn . identify_1pexp_bug = backup <EOL> def test_1msigmoid ( self ) : <EOL> if not register_local_1msigmoid : <EOL> return <EOL> m = self . get_mode ( ) <EOL> x = T . fmatrix ( ) <EOL> f = theano . function ( [ x ] , <NUM_LIT:1> - T . exp ( x ) / ( <NUM_LIT:1> + T . exp ( x ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] == [ <EOL> tensor . neg , sigmoid_inplace ] <EOL> f = theano . function ( [ x ] , <NUM_LIT:1> - T . fill ( x , <NUM_LIT:1.0> ) / ( <NUM_LIT:1> + T . exp ( - x ) ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> assert [ node . op for node in f . maker . fgraph . toposort ( ) ] == [ tensor . neg , <EOL> sigmoid_inplace ] <EOL> def test_local_sigm_times_exp ( self ) : <EOL> """<STR_LIT>""" <EOL> def match ( func , ops ) : <EOL> assert [ node . op for node in func . maker . fgraph . toposort ( ) ] == ops <EOL> m = self . get_mode ( excluding = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> x , y = tensor . vectors ( '<STR_LIT:x>' , '<STR_LIT:y>' ) <EOL> f = theano . function ( [ x ] , sigmoid ( - x ) * tensor . exp ( x ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> match ( f , [ sigmoid ] ) <EOL> f = theano . function ( [ x ] , sigmoid ( x ) * tensor . exp ( - x ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> match ( f , [ tensor . neg , sigmoid ] ) <EOL> f = theano . function ( [ x ] , - ( - ( - ( sigmoid ( x ) ) ) ) * tensor . exp ( - x ) , mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> match ( f , [ tensor . neg , sigmoid , tensor . neg ] ) <EOL> f = theano . function ( <EOL> [ x , y ] , <EOL> ( sigmoid ( x ) * sigmoid ( - y ) * - tensor . exp ( - x ) * <EOL> tensor . exp ( x * y ) * tensor . exp ( y ) ) , <EOL> mode = m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> match ( f , [ sigmoid , tensor . mul , tensor . neg , tensor . exp , sigmoid , <EOL> tensor . mul ] ) <EOL> def test_perform_sigm_times_exp ( self ) : <EOL> """<STR_LIT>""" <EOL> x , y , z , t = tensor . vectors ( '<STR_LIT:x>' , '<STR_LIT:y>' , '<STR_LIT:z>' , '<STR_LIT:t>' ) <EOL> exp = tensor . exp <EOL> def ok ( expr1 , expr2 ) : <EOL> trees = [ parse_mul_tree ( e ) for e in ( expr1 , expr2 ) ] <EOL> perform_sigm_times_exp ( trees [ <NUM_LIT:0> ] ) <EOL> trees [ <NUM_LIT:0> ] = simplify_mul ( trees [ <NUM_LIT:0> ] ) <EOL> good = theano . gof . graph . is_same_graph ( <EOL> compute_mul ( trees [ <NUM_LIT:0> ] ) , <EOL> compute_mul ( trees [ <NUM_LIT:1> ] ) ) <EOL> if not good : <EOL> print ( trees [ <NUM_LIT:0> ] ) <EOL> print ( trees [ <NUM_LIT:1> ] ) <EOL> print ( '<STR_LIT>' ) <EOL> theano . printing . debugprint ( compute_mul ( trees [ <NUM_LIT:0> ] ) ) <EOL> print ( '<STR_LIT>' ) <EOL> theano . printing . debugprint ( compute_mul ( trees [ <NUM_LIT:1> ] ) ) <EOL> assert good <EOL> ok ( sigmoid ( x ) * exp ( - x ) , sigmoid ( - x ) ) <EOL> ok ( - x * sigmoid ( x ) * ( y * ( - <NUM_LIT:1> * z ) * exp ( - x ) ) , <EOL> - x * sigmoid ( - x ) * ( y * ( - <NUM_LIT:1> * z ) ) ) <EOL> ok ( - sigmoid ( - x ) * <EOL> ( exp ( y ) * ( - exp ( - z ) * <NUM_LIT:3> * - exp ( x ) ) * <EOL> ( y * <NUM_LIT:2> * ( - sigmoid ( - y ) * ( z + t ) * exp ( z ) ) * sigmoid ( z ) ) ) * <EOL> - sigmoid ( x ) , <EOL> sigmoid ( x ) * <EOL> ( - sigmoid ( y ) * ( - sigmoid ( - z ) * <NUM_LIT:3> ) * ( y * <NUM_LIT:2> * ( ( z + t ) * exp ( z ) ) ) ) * <EOL> - sigmoid ( x ) ) <EOL> ok ( exp ( - x ) * - exp ( - x ) * ( - sigmoid ( x ) * - sigmoid ( x ) ) , <EOL> - sigmoid ( - x ) * sigmoid ( - x ) ) <EOL> ok ( - exp ( x ) * - sigmoid ( - x ) * - exp ( - x ) , <EOL> - sigmoid ( - x ) ) <EOL> def test_grad_log1msigm ( self ) : <EOL> x = tensor . matrix ( '<STR_LIT:x>' ) <EOL> lr = tensor . scalar ( '<STR_LIT>' ) <EOL> s = sigmoid ( x ) <EOL> l = T . log ( <NUM_LIT:1> - s ) <EOL> c = l . mean ( ) <EOL> ux = x - lr * theano . grad ( c , x ) <EOL> mode = self . get_mode ( ) <EOL> if not isinstance ( mode , theano . compile . DebugMode ) : <EOL> f = theano . function ( [ x , lr ] , ux , mode = mode ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> ux_v = f ( [ [ <NUM_LIT:50> ] ] , <NUM_LIT:0.1> ) <EOL> assert not numpy . isnan ( ux_v ) <EOL> def test_local_ultra_fast_sigmoid ( self ) : <EOL> x = tensor . matrix ( '<STR_LIT:x>' ) <EOL> s = sigmoid ( x ) <EOL> mode = self . get_mode ( '<STR_LIT>' ) <EOL> f = theano . function ( [ x ] , s , mode = mode ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> topo = f . maker . fgraph . toposort ( ) <EOL> assert len ( topo ) == <NUM_LIT:1> <EOL> assert topo [ <NUM_LIT:0> ] . op == sigmoid <EOL> mode = self . get_mode ( ) . including ( '<STR_LIT>' ) <EOL> f = theano . function ( [ x ] , s , mode = mode ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> topo = f . maker . fgraph . toposort ( ) <EOL> assert topo [ <NUM_LIT:0> ] . op == ultra_fast_sigmoid <EOL> assert len ( topo ) == <NUM_LIT:1> <EOL> ux_v = f ( [ [ - <NUM_LIT:50> , - <NUM_LIT:10> , - <NUM_LIT:4> , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:10> , <NUM_LIT:50> ] ] ) <EOL> def test_local_hard_sigmoid ( self ) : <EOL> x = tensor . matrix ( '<STR_LIT:x>' ) <EOL> s = sigmoid ( x ) <EOL> mode = self . get_mode ( '<STR_LIT>' ) <EOL> f = theano . function ( [ x ] , s , mode = mode ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> topo = f . maker . fgraph . toposort ( ) <EOL> assert topo [ <NUM_LIT:0> ] . op == sigmoid <EOL> assert len ( topo ) == <NUM_LIT:1> <EOL> mode = self . get_mode ( ) . including ( '<STR_LIT>' ) <EOL> f = theano . function ( [ x ] , s , mode = mode ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> topo = f . maker . fgraph . toposort ( ) <EOL> assert len ( topo ) > <NUM_LIT:1> <EOL> assert not any ( [ n . op == sigmoid for n in topo ] ) <EOL> ux_v = f ( [ [ - <NUM_LIT:50> , - <NUM_LIT:10> , - <NUM_LIT:4> , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:10> , <NUM_LIT:50> ] ] ) <EOL> class T_softplus_opts ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> if theano . config . mode == '<STR_LIT>' : <EOL> m = theano . compile . mode . get_mode ( '<STR_LIT>' ) . excluding ( <EOL> '<STR_LIT>' ) <EOL> else : <EOL> m = theano . compile . mode . get_default_mode ( ) . excluding ( <EOL> '<STR_LIT>' ) <EOL> self . m = m <EOL> utt . seed_rng ( ) <EOL> def test_logsigm_to_softplus ( self ) : <EOL> x = T . vector ( ) <EOL> out = T . log ( sigmoid ( x ) ) <EOL> f = theano . function ( [ x ] , out , mode = self . m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> topo = f . maker . fgraph . toposort ( ) <EOL> assert len ( topo ) == <NUM_LIT:3> <EOL> assert isinstance ( topo [ <NUM_LIT:0> ] . op . scalar_op , theano . scalar . Neg ) <EOL> assert isinstance ( topo [ <NUM_LIT:1> ] . op . scalar_op , <EOL> theano . tensor . nnet . sigm . ScalarSoftplus ) <EOL> assert isinstance ( topo [ <NUM_LIT:2> ] . op . scalar_op , theano . scalar . Neg ) <EOL> f ( numpy . random . rand ( <NUM_LIT> ) . astype ( config . floatX ) ) <EOL> def test_log1msigm_to_softplus ( self ) : <EOL> x = T . matrix ( ) <EOL> out = T . log ( <NUM_LIT:1> - sigmoid ( x ) ) <EOL> f = theano . function ( [ x ] , out , mode = self . m ) <EOL> topo = f . maker . fgraph . toposort ( ) <EOL> assert len ( topo ) == <NUM_LIT:2> <EOL> assert isinstance ( topo [ <NUM_LIT:0> ] . op . scalar_op , <EOL> theano . tensor . nnet . sigm . ScalarSoftplus ) <EOL> assert isinstance ( topo [ <NUM_LIT:1> ] . op . scalar_op , theano . scalar . Neg ) <EOL> f ( numpy . random . rand ( <NUM_LIT> , <NUM_LIT:11> ) . astype ( config . floatX ) ) <EOL> out = T . log ( <NUM_LIT:1> - T . flatten ( sigmoid ( x ) ) ) <EOL> f = theano . function ( [ x ] , out , mode = self . m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> topo = f . maker . fgraph . toposort ( ) <EOL> assert len ( topo ) == <NUM_LIT:3> <EOL> assert tensor . is_flat ( topo [ <NUM_LIT:0> ] . outputs [ <NUM_LIT:0> ] ) <EOL> assert isinstance ( topo [ <NUM_LIT:1> ] . op . scalar_op , <EOL> theano . tensor . nnet . sigm . ScalarSoftplus ) <EOL> assert isinstance ( topo [ <NUM_LIT:2> ] . op . scalar_op , theano . scalar . Neg ) <EOL> f ( numpy . random . rand ( <NUM_LIT> , <NUM_LIT:11> ) . astype ( config . floatX ) ) <EOL> out = T . log ( <NUM_LIT:1> - sigmoid ( x ) . reshape ( [ x . size ] ) ) <EOL> f = theano . function ( [ x ] , out , mode = self . m ) <EOL> topo = f . maker . fgraph . toposort ( ) <EOL> assert any ( isinstance ( node . op , T . Reshape ) for node in topo ) <EOL> assert any ( isinstance ( getattr ( node . op , '<STR_LIT>' , None ) , <EOL> theano . tensor . nnet . sigm . ScalarSoftplus ) <EOL> for node in topo ) <EOL> f ( numpy . random . rand ( <NUM_LIT> , <NUM_LIT:11> ) . astype ( config . floatX ) ) <EOL> def test_log1pexp_to_softplus ( self ) : <EOL> m = theano . config . mode <EOL> if m == '<STR_LIT>' : <EOL> m = '<STR_LIT>' <EOL> x = T . vector ( ) <EOL> out = T . log ( <NUM_LIT:1> + T . exp ( x ) ) <EOL> f = theano . function ( [ x ] , out , mode = self . m ) <EOL> assert hasattr ( f . maker . fgraph . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' ) <EOL> topo = f . maker . fgraph . toposort ( ) <EOL> assert len ( topo ) == <NUM_LIT:1> <EOL> assert isinstance ( topo [ <NUM_LIT:0> ] . op . scalar_op , <EOL> theano . tensor . nnet . sigm . ScalarSoftplus ) <EOL> f ( numpy . random . rand ( <NUM_LIT> ) . astype ( config . floatX ) ) <EOL> class T_sigmoid_utils ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_compute_mul ( self ) : <EOL> x , y , z = tensor . vectors ( '<STR_LIT:x>' , '<STR_LIT:y>' , '<STR_LIT:z>' ) <EOL> tree = ( x * y ) * - z <EOL> mul_tree = parse_mul_tree ( tree ) <EOL> assert parse_mul_tree ( compute_mul ( mul_tree ) ) == mul_tree <EOL> assert theano . gof . graph . is_same_graph ( <EOL> compute_mul ( parse_mul_tree ( tree ) ) , tree ) <EOL> def test_parse_mul_tree ( self ) : <EOL> x , y , z = tensor . vectors ( '<STR_LIT:x>' , '<STR_LIT:y>' , '<STR_LIT:z>' ) <EOL> assert parse_mul_tree ( x * y ) == [ False , [ [ False , x ] , [ False , y ] ] ] <EOL> assert parse_mul_tree ( - ( x * y ) ) == [ True , [ [ False , x ] , [ False , y ] ] ] <EOL> assert parse_mul_tree ( - x * y ) == [ False , [ [ True , x ] , [ False , y ] ] ] <EOL> assert parse_mul_tree ( - x ) == [ True , x ] <EOL> assert parse_mul_tree ( ( x * y ) * - z ) == [ <EOL> False , [ [ False , [ [ False , x ] , [ False , y ] ] ] , [ True , z ] ] ] <EOL> def test_is_1pexp ( self ) : <EOL> backup = config . warn . identify_1pexp_bug <EOL> config . warn . identify_1pexp_bug = False <EOL> try : <EOL> x = tensor . vector ( '<STR_LIT:x>' ) <EOL> exp = tensor . exp <EOL> assert is_1pexp ( <NUM_LIT:1> + exp ( x ) ) == ( False , x ) <EOL> assert is_1pexp ( exp ( x ) + <NUM_LIT:1> ) == ( False , x ) <EOL> for neg , exp_arg in imap ( is_1pexp , [ ( <NUM_LIT:1> + exp ( - x ) ) , ( exp ( - x ) + <NUM_LIT:1> ) ] ) : <EOL> assert not neg and theano . gof . graph . is_same_graph ( exp_arg , - x ) <EOL> assert is_1pexp ( <NUM_LIT:1> - exp ( x ) ) is None <EOL> assert is_1pexp ( <NUM_LIT:2> + exp ( x ) ) is None <EOL> assert is_1pexp ( exp ( x ) + <NUM_LIT:2> ) is None <EOL> assert is_1pexp ( exp ( x ) - <NUM_LIT:1> ) is None <EOL> assert is_1pexp ( - <NUM_LIT:1> + exp ( x ) ) is None <EOL> assert is_1pexp ( <NUM_LIT:1> + <NUM_LIT:2> * exp ( x ) ) is None <EOL> finally : <EOL> config . warn . identify_1pexp_bug = backup </s>
