<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function , division <EOL> import logging <EOL> from nose . plugins . skip import SkipTest <EOL> import numpy <EOL> from six . moves import StringIO <EOL> import theano <EOL> import theano . tensor as tensor <EOL> from theano . printing import min_informative_str , debugprint <EOL> def test_pydotprint_cond_highlight ( ) : <EOL> """<STR_LIT>""" <EOL> if not theano . printing . pydot_imported : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> x = tensor . dvector ( ) <EOL> f = theano . function ( [ x ] , x * <NUM_LIT:2> ) <EOL> f ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> s = StringIO ( ) <EOL> new_handler = logging . StreamHandler ( s ) <EOL> new_handler . setLevel ( logging . DEBUG ) <EOL> orig_handler = theano . logging_default_handler <EOL> theano . theano_logger . removeHandler ( orig_handler ) <EOL> theano . theano_logger . addHandler ( new_handler ) <EOL> try : <EOL> theano . printing . pydotprint ( f , cond_highlight = True , <EOL> print_output_file = False ) <EOL> finally : <EOL> theano . theano_logger . addHandler ( orig_handler ) <EOL> theano . theano_logger . removeHandler ( new_handler ) <EOL> assert ( s . getvalue ( ) == '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def test_pydotprint_return_image ( ) : <EOL> if not theano . printing . pydot_imported : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> x = tensor . dvector ( ) <EOL> ret = theano . printing . pydotprint ( x * <NUM_LIT:2> , return_image = True ) <EOL> assert isinstance ( ret , ( str , bytes ) ) <EOL> def test_pydotprint_variables ( ) : <EOL> """<STR_LIT>""" <EOL> if not theano . printing . pydot_imported : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> x = tensor . dvector ( ) <EOL> s = StringIO ( ) <EOL> new_handler = logging . StreamHandler ( s ) <EOL> new_handler . setLevel ( logging . DEBUG ) <EOL> orig_handler = theano . logging_default_handler <EOL> theano . theano_logger . removeHandler ( orig_handler ) <EOL> theano . theano_logger . addHandler ( new_handler ) <EOL> try : <EOL> theano . printing . pydotprint ( x * <NUM_LIT:2> ) <EOL> if not theano . printing . pd . __name__ == "<STR_LIT>" : <EOL> theano . printing . pydotprint_variables ( x * <NUM_LIT:2> ) <EOL> finally : <EOL> theano . theano_logger . addHandler ( orig_handler ) <EOL> theano . theano_logger . removeHandler ( new_handler ) <EOL> def test_pydotprint_long_name ( ) : <EOL> """<STR_LIT>""" <EOL> if not theano . printing . pydot_imported : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> x = tensor . dvector ( ) <EOL> mode = theano . compile . mode . get_default_mode ( ) . excluding ( "<STR_LIT>" ) <EOL> f = theano . function ( [ x ] , [ x * <NUM_LIT:2> , x + x ] , mode = mode ) <EOL> f ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> theano . printing . pydotprint ( f , max_label_size = <NUM_LIT:5> , <EOL> print_output_file = False ) <EOL> theano . printing . pydotprint ( [ x * <NUM_LIT:2> , x + x ] , <EOL> max_label_size = <NUM_LIT:5> , <EOL> print_output_file = False ) <EOL> def test_pydotprint_profile ( ) : <EOL> """<STR_LIT>""" <EOL> if not theano . printing . pydot_imported : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> A = tensor . matrix ( ) <EOL> f = theano . function ( [ A ] , A + <NUM_LIT:1> , mode = '<STR_LIT>' ) <EOL> theano . printing . pydotprint ( f , print_output_file = False ) <EOL> def test_min_informative_str ( ) : <EOL> """<STR_LIT>""" <EOL> A = tensor . matrix ( name = '<STR_LIT:A>' ) <EOL> B = tensor . matrix ( name = '<STR_LIT:B>' ) <EOL> C = A + B <EOL> C . name = '<STR_LIT:C>' <EOL> D = tensor . matrix ( name = '<STR_LIT:D>' ) <EOL> E = tensor . matrix ( name = '<STR_LIT:E>' ) <EOL> F = D + E <EOL> G = C + F <EOL> mis = min_informative_str ( G ) . replace ( "<STR_LIT:\t>" , "<STR_LIT:U+0020>" ) <EOL> reference = """<STR_LIT>""" <EOL> if mis != reference : <EOL> print ( '<STR_LIT>' + mis + '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' + reference + '<STR_LIT>' ) <EOL> assert mis == reference <EOL> def test_debugprint ( ) : <EOL> A = tensor . matrix ( name = '<STR_LIT:A>' ) <EOL> B = tensor . matrix ( name = '<STR_LIT:B>' ) <EOL> C = A + B <EOL> C . name = '<STR_LIT:C>' <EOL> D = tensor . matrix ( name = '<STR_LIT:D>' ) <EOL> E = tensor . matrix ( name = '<STR_LIT:E>' ) <EOL> F = D + E <EOL> G = C + F <EOL> mode = theano . compile . get_default_mode ( ) . including ( '<STR_LIT>' ) <EOL> g = theano . function ( [ A , B , D , E ] , G , mode = mode ) <EOL> debugprint ( G ) <EOL> s = StringIO ( ) <EOL> debugprint ( G , file = s , ids = '<STR_LIT:int>' ) <EOL> s = s . getvalue ( ) <EOL> reference = '<STR_LIT:\n>' . join ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) + '<STR_LIT:\n>' <EOL> if s != reference : <EOL> print ( '<STR_LIT>' + s + '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' + reference + '<STR_LIT>' ) <EOL> assert s == reference <EOL> s = StringIO ( ) <EOL> debugprint ( G , file = s , ids = '<STR_LIT>' ) <EOL> s = s . getvalue ( ) <EOL> reference = "<STR_LIT:\n>" . join ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) + '<STR_LIT:\n>' <EOL> if s != reference : <EOL> print ( '<STR_LIT>' + s + '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' + reference + '<STR_LIT>' ) <EOL> assert s == reference <EOL> s = StringIO ( ) <EOL> debugprint ( G , file = s , ids = '<STR_LIT>' , stop_on_name = True ) <EOL> s = s . getvalue ( ) <EOL> reference = '<STR_LIT:\n>' . join ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) + '<STR_LIT:\n>' <EOL> if s != reference : <EOL> print ( '<STR_LIT>' + s + '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' + reference + '<STR_LIT>' ) <EOL> assert s == reference <EOL> s = StringIO ( ) <EOL> debugprint ( G , file = s , ids = '<STR_LIT>' ) <EOL> s = s . getvalue ( ) <EOL> reference = '<STR_LIT:\n>' . join ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) + '<STR_LIT:\n>' <EOL> if s != reference : <EOL> print ( '<STR_LIT>' + s + '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' + reference + '<STR_LIT>' ) <EOL> assert s == reference <EOL> s = StringIO ( ) <EOL> debugprint ( g , file = s , ids = '<STR_LIT>' , print_storage = True ) <EOL> s = s . getvalue ( ) <EOL> reference = '<STR_LIT:\n>' . join ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) + '<STR_LIT:\n>' <EOL> if s != reference : <EOL> print ( '<STR_LIT>' + s + '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' + reference + '<STR_LIT>' ) <EOL> assert s == reference <EOL> def test_scan_debugprint1 ( ) : <EOL> k = tensor . iscalar ( "<STR_LIT:k>" ) <EOL> A = tensor . dvector ( "<STR_LIT:A>" ) <EOL> result , updates = theano . scan ( fn = lambda prior_result , A : prior_result * A , <EOL> outputs_info = tensor . ones_like ( A ) , <EOL> non_sequences = A , <EOL> n_steps = k ) <EOL> final_result = result [ - <NUM_LIT:1> ] <EOL> output_str = theano . printing . debugprint ( final_result , file = '<STR_LIT:str>' ) <EOL> lines = [ ] <EOL> for line in output_str . split ( '<STR_LIT:\n>' ) : <EOL> lines += [ line ] <EOL> expected_output = """<STR_LIT>""" <EOL> for truth , out in zip ( expected_output . split ( "<STR_LIT:\n>" ) , lines ) : <EOL> assert truth . strip ( ) == out . strip ( ) <EOL> def test_scan_debugprint2 ( ) : <EOL> coefficients = theano . tensor . vector ( "<STR_LIT>" ) <EOL> x = tensor . scalar ( "<STR_LIT:x>" ) <EOL> max_coefficients_supported = <NUM_LIT> <EOL> components , updates = theano . scan ( fn = lambda coefficient , power , <EOL> free_variable : <EOL> coefficient * ( free_variable ** power ) , <EOL> outputs_info = None , <EOL> sequences = [ <EOL> coefficients , <EOL> theano . tensor . arange ( <EOL> max_coefficients_supported ) ] , <EOL> non_sequences = x ) <EOL> polynomial = components . sum ( ) <EOL> output_str = theano . printing . debugprint ( polynomial , file = '<STR_LIT:str>' ) <EOL> lines = [ ] <EOL> for line in output_str . split ( '<STR_LIT:\n>' ) : <EOL> lines += [ line ] <EOL> expected_output = """<STR_LIT>""" <EOL> for truth , out in zip ( expected_output . split ( "<STR_LIT:\n>" ) , lines ) : <EOL> assert truth . strip ( ) == out . strip ( ) <EOL> def test_scan_debugprint3 ( ) : <EOL> coefficients = theano . tensor . dvector ( "<STR_LIT>" ) <EOL> max_coefficients_supported = <NUM_LIT:10> <EOL> k = tensor . iscalar ( "<STR_LIT:k>" ) <EOL> A = tensor . dvector ( "<STR_LIT:A>" ) <EOL> def compute_A_k ( A , k ) : <EOL> result , updates = theano . scan ( fn = lambda prior_result , <EOL> A : prior_result * A , <EOL> outputs_info = tensor . ones_like ( A ) , <EOL> non_sequences = A , <EOL> n_steps = k ) <EOL> A_k = result [ - <NUM_LIT:1> ] <EOL> return A_k <EOL> components , updates = theano . scan ( fn = lambda coefficient , <EOL> power , some_A , some_k : <EOL> coefficient * <EOL> ( compute_A_k ( some_A , some_k ) ** power ) , <EOL> outputs_info = None , <EOL> sequences = [ <EOL> coefficients , <EOL> theano . tensor . arange ( <EOL> max_coefficients_supported ) ] , <EOL> non_sequences = [ A , k ] ) <EOL> polynomial = components . sum ( ) <EOL> final_result = polynomial <EOL> output_str = theano . printing . debugprint ( final_result , file = '<STR_LIT:str>' ) <EOL> lines = [ ] <EOL> for line in output_str . split ( '<STR_LIT:\n>' ) : <EOL> lines += [ line ] <EOL> expected_output = """<STR_LIT>""" <EOL> for truth , out in zip ( expected_output . split ( "<STR_LIT:\n>" ) , lines ) : <EOL> assert truth . strip ( ) == out . strip ( ) <EOL> def test_scan_debugprint4 ( ) : <EOL> def fn ( a_m2 , a_m1 , b_m2 , b_m1 ) : <EOL> return a_m1 + a_m2 , b_m1 + b_m2 <EOL> a0 = theano . shared ( numpy . arange ( <NUM_LIT:2> , dtype = '<STR_LIT>' ) ) <EOL> b0 = theano . shared ( numpy . arange ( <NUM_LIT:2> , dtype = '<STR_LIT>' ) ) <EOL> ( a , b ) , _ = theano . scan ( <EOL> fn , outputs_info = [ { '<STR_LIT>' : a0 , '<STR_LIT>' : [ - <NUM_LIT:2> , - <NUM_LIT:1> ] } , <EOL> { '<STR_LIT>' : b0 , '<STR_LIT>' : [ - <NUM_LIT:2> , - <NUM_LIT:1> ] } ] , <EOL> n_steps = <NUM_LIT:5> ) <EOL> final_result = a + b <EOL> output_str = theano . printing . debugprint ( final_result , file = '<STR_LIT:str>' ) <EOL> lines = [ ] <EOL> for line in output_str . split ( '<STR_LIT:\n>' ) : <EOL> lines += [ line ] <EOL> expected_output = """<STR_LIT>""" <EOL> for truth , out in zip ( expected_output . split ( "<STR_LIT:\n>" ) , lines ) : <EOL> assert truth . strip ( ) == out . strip ( ) <EOL> def test_scan_debugprint5 ( ) : <EOL> k = tensor . iscalar ( "<STR_LIT:k>" ) <EOL> A = tensor . dvector ( "<STR_LIT:A>" ) <EOL> result , updates = theano . scan ( fn = lambda prior_result , A : prior_result * A , <EOL> outputs_info = tensor . ones_like ( A ) , <EOL> non_sequences = A , <EOL> n_steps = k ) <EOL> final_result = tensor . grad ( result [ - <NUM_LIT:1> ] . sum ( ) , A ) <EOL> output_str = theano . printing . debugprint ( final_result , file = '<STR_LIT:str>' ) <EOL> lines = [ ] <EOL> for line in output_str . split ( '<STR_LIT:\n>' ) : <EOL> lines += [ line ] <EOL> expected_output = """<STR_LIT>""" <EOL> for truth , out in zip ( expected_output . split ( "<STR_LIT:\n>" ) , lines ) : <EOL> assert truth . strip ( ) == out . strip ( ) <EOL> def test_printing_scan ( ) : <EOL> if not theano . printing . pydot_imported : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> def f_pow2 ( x_tm1 ) : <EOL> return <NUM_LIT:2> * x_tm1 <EOL> state = theano . tensor . scalar ( '<STR_LIT:state>' ) <EOL> n_steps = theano . tensor . iscalar ( '<STR_LIT>' ) <EOL> output , updates = theano . scan ( f_pow2 , <EOL> [ ] , <EOL> state , <EOL> [ ] , <EOL> n_steps = n_steps , <EOL> truncate_gradient = - <NUM_LIT:1> , <EOL> go_backwards = False ) <EOL> f = theano . function ( [ state , n_steps ] , <EOL> output , <EOL> updates = updates , <EOL> allow_input_downcast = True ) <EOL> theano . printing . pydotprint ( output , scan_graphs = True ) <EOL> theano . printing . pydotprint ( f , scan_graphs = True ) </s>
