<s> from __future__ import division , print_function , unicode_literals <EOL> import numpy as np <EOL> from brainstorm . handlers . base_handler import Handler <EOL> class DebugArray ( object ) : <EOL> def __init__ ( self , arr ) : <EOL> assert arr is not None <EOL> self . shape = arr . shape <EOL> self . array = arr <EOL> self . size = self . array . size <EOL> def __getitem__ ( self , item ) : <EOL> if isinstance ( item , ( int , slice ) ) : <EOL> item = tuple ( [ item ] ) <EOL> assert isinstance ( item , tuple ) <EOL> for i in item : <EOL> assert isinstance ( i , ( int , slice ) ) <EOL> if isinstance ( i , slice ) : <EOL> assert i . step is None <EOL> return DebugArray ( arr = self . array . __getitem__ ( item ) ) <EOL> def reshape ( self , new_shape ) : <EOL> if isinstance ( new_shape , ( tuple , list ) ) : <EOL> assert all ( [ t >= <NUM_LIT:0> for t in tuple ( new_shape ) ] ) <EOL> else : <EOL> assert isinstance ( new_shape , int ) <EOL> assert new_shape >= <NUM_LIT:0> <EOL> return DebugArray ( arr = self . array . reshape ( new_shape ) ) <EOL> def _check_for_inf ( handler , arg , name ) : <EOL> if isinstance ( arg , ( int , float ) ) and not np . isfinite ( arg ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> . format ( name ) ) <EOL> if isinstance ( arg , DebugArray ) and not handler . is_fully_finite ( arg ) : <EOL> raise ValueError ( '<STR_LIT>' . format ( name ) ) <EOL> def check_for_inf_or_nan ( f ) : <EOL> def checked_f ( * args , ** kwargs ) : <EOL> result = f ( * args , ** kwargs ) <EOL> handler = args [ <NUM_LIT:0> ] <EOL> for i , arg in enumerate ( args , start = <NUM_LIT:1> ) : <EOL> _check_for_inf ( handler , arg , '<STR_LIT>' . format ( i ) ) <EOL> for n , v in kwargs . items ( ) : <EOL> _check_for_inf ( handler , v , n ) <EOL> return result <EOL> return checked_f <EOL> class DebugHandler ( Handler ) : <EOL> __undescribed__ = { '<STR_LIT>' , '<STR_LIT>' } <EOL> def __init__ ( self , handler ) : <EOL> super ( DebugHandler , self ) . __init__ ( ) <EOL> self . handler = handler <EOL> self . EMPTY = DebugArray ( arr = handler . EMPTY ) <EOL> self . array_type = DebugArray <EOL> def __init_from_description__ ( self , description ) : <EOL> self . __init__ ( self . handler ) <EOL> def allocate ( self , shape ) : <EOL> assert_is_shape ( shape ) <EOL> return DebugArray ( self . handler . allocate ( shape ) ) <EOL> def ones ( self , shape ) : <EOL> assert_is_shape ( shape ) <EOL> return DebugArray ( self . handler . ones ( shape ) ) <EOL> def zeros ( self , shape ) : <EOL> assert_is_shape ( shape ) <EOL> return DebugArray ( self . handler . zeros ( shape ) ) <EOL> @ check_for_inf_or_nan <EOL> def copy_to ( self , src , dest ) : <EOL> assert_debug_arrays ( dest , src ) <EOL> assert_shapes_equal ( dest , src ) <EOL> assert dest . size == src . size , "<STR_LIT>" . format ( dest . size , src . size ) <EOL> self . handler . copy_to ( src . array , dest . array ) <EOL> @ check_for_inf_or_nan <EOL> def copy_to_if ( self , src , dest , cond ) : <EOL> assert_debug_arrays ( src , dest , cond ) <EOL> assert_shapes_equal ( src , dest , cond ) <EOL> self . handler . copy_to_if ( src . array , dest . array , cond . array ) <EOL> @ check_for_inf_or_nan <EOL> def create_from_numpy ( self , arr ) : <EOL> assert isinstance ( arr , np . ndarray ) <EOL> return DebugArray ( self . handler . create_from_numpy ( arr ) ) <EOL> @ check_for_inf_or_nan <EOL> def fill ( self , mem , val ) : <EOL> assert_debug_arrays ( mem ) <EOL> assert_is_scalar ( val ) <EOL> self . handler . fill ( mem . array , val ) <EOL> @ check_for_inf_or_nan <EOL> def fill_if ( self , mem , val , cond ) : <EOL> assert_is_scalar ( val ) <EOL> assert_debug_arrays ( mem , cond ) <EOL> assert_shapes_equal ( mem , cond ) <EOL> self . handler . fill_if ( mem . array , val , cond . array ) <EOL> @ check_for_inf_or_nan <EOL> def get_numpy_copy ( self , mem ) : <EOL> assert_debug_arrays ( mem ) <EOL> return self . handler . get_numpy_copy ( mem . array ) <EOL> @ check_for_inf_or_nan <EOL> def set_from_numpy ( self , mem , arr ) : <EOL> assert_debug_arrays ( mem ) <EOL> assert isinstance ( arr , np . ndarray ) <EOL> assert mem . shape == arr . shape , "<STR_LIT>" . format ( mem . shape , arr . shape ) <EOL> self . handler . set_from_numpy ( mem . array , arr ) <EOL> def is_fully_finite ( self , a ) : <EOL> return self . handler . is_fully_finite ( a . array ) <EOL> @ check_for_inf_or_nan <EOL> def abs_t ( self , a , out ) : <EOL> assert_debug_arrays ( a , out ) <EOL> assert_shapes_equal ( a , out ) <EOL> self . handler . abs_t ( a . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def add_into_if ( self , a , out , cond ) : <EOL> assert_debug_arrays ( a , out , cond ) <EOL> assert_shapes_equal ( a , out , cond ) <EOL> self . handler . add_into_if ( a . array , out . array , cond . array ) <EOL> @ check_for_inf_or_nan <EOL> def add_mv ( self , m , v , out ) : <EOL> assert_debug_arrays ( m , v , out ) <EOL> assert_shapes_equal ( m , out ) <EOL> assert len ( m . shape ) == <NUM_LIT:2> , "<STR_LIT>" . format ( m . shape ) <EOL> assert v . shape == ( m . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) or v . shape == ( <NUM_LIT:1> , m . shape [ <NUM_LIT:1> ] ) , "<STR_LIT>" . format ( v . shape ) <EOL> self . handler . add_mv ( m . array , v . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def add_st ( self , s , t , out ) : <EOL> assert_debug_arrays ( t , out ) <EOL> assert_is_scalar ( s ) <EOL> assert_shapes_equal ( t , out ) <EOL> self . handler . add_st ( s , t . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def add_tt ( self , a , b , out ) : <EOL> assert_debug_arrays ( a , b , out ) <EOL> assert_shapes_equal ( a , b , out ) <EOL> self . handler . add_tt ( a . array , b . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def avgpool2d_backward_batch ( self , inputs , window , outputs , padding , <EOL> stride , in_deltas , out_deltas ) : <EOL> assert_debug_arrays ( inputs , outputs , in_deltas , out_deltas ) <EOL> assert_is_shape ( window ) <EOL> assert len ( window ) == <NUM_LIT:2> , "<STR_LIT>" . format ( window ) <EOL> assert_is_shape ( stride ) <EOL> assert len ( stride ) == <NUM_LIT:2> , "<STR_LIT>" . format ( stride ) <EOL> assert isinstance ( padding , int ) and <NUM_LIT:0> <= padding , "<STR_LIT>" . format ( padding ) <EOL> assert_shapes_equal ( inputs , in_deltas ) <EOL> assert_shapes_equal ( outputs , out_deltas ) <EOL> self . handler . avgpool2d_backward_batch ( inputs . array , window , <EOL> outputs . array , padding , stride , <EOL> in_deltas . array , <EOL> out_deltas . array ) <EOL> @ check_for_inf_or_nan <EOL> def avgpool2d_forward_batch ( self , inputs , window , outputs , padding , <EOL> stride ) : <EOL> assert_debug_arrays ( inputs , outputs ) <EOL> assert_is_shape ( window ) <EOL> assert len ( window ) == <NUM_LIT:2> , "<STR_LIT>" . format ( window ) <EOL> assert_is_shape ( stride ) <EOL> assert len ( stride ) == <NUM_LIT:2> , "<STR_LIT>" . format ( stride ) <EOL> assert isinstance ( padding , int ) and <NUM_LIT:0> <= padding , "<STR_LIT>" . format ( padding ) <EOL> self . handler . avgpool2d_forward_batch ( inputs . array , window , <EOL> outputs . array , padding , stride ) <EOL> @ check_for_inf_or_nan <EOL> def binarize_v ( self , v , out ) : <EOL> assert_debug_arrays ( v , out ) <EOL> assert len ( v . shape ) == len ( out . shape ) == <NUM_LIT:2> <EOL> assert v . shape == ( out . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> assert self . handler . get_numpy_copy ( v . array ) . min ( ) >= <NUM_LIT:0> <EOL> assert int ( self . handler . get_numpy_copy ( v . array ) . max ( ) ) < out . shape [ <NUM_LIT:1> ] <EOL> self . handler . binarize_v ( v . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def broadcast_t ( self , a , axis , out ) : <EOL> assert_debug_arrays ( a , out ) <EOL> assert ( isinstance ( axis , int ) and <NUM_LIT:0> <= axis < len ( out . shape ) ) , "<STR_LIT>" . format ( axis ) <EOL> assert a . shape [ axis ] == <NUM_LIT:1> <EOL> assert a . shape == out . shape [ : axis ] + ( <NUM_LIT:1> , ) + out . shape [ axis + <NUM_LIT:1> : ] <EOL> self . handler . broadcast_t ( a . array , axis , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def clip_t ( self , a , a_min , a_max , out ) : <EOL> assert_debug_arrays ( a , out ) <EOL> assert_is_scalar ( a_min ) <EOL> assert_is_scalar ( a_max ) <EOL> assert_shapes_equal ( a , out ) <EOL> assert a_min <= a_max , "<STR_LIT>" . format ( a_min , a_max ) <EOL> self . handler . clip_t ( a . array , a_min , a_max , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def conv2d_backward_batch ( self , inputs , weights , padding , stride , <EOL> in_deltas , out_deltas , weight_deltas , <EOL> bias_deltas ) : <EOL> assert_debug_arrays ( inputs , weights , in_deltas , out_deltas , <EOL> weight_deltas , bias_deltas ) <EOL> assert isinstance ( padding , int ) and <NUM_LIT:0> <= padding , "<STR_LIT>" . format ( padding ) <EOL> assert_is_shape ( stride ) <EOL> assert len ( stride ) == <NUM_LIT:2> , "<STR_LIT>" . format ( stride ) <EOL> self . handler . conv2d_backward_batch ( inputs . array , weights . array , <EOL> padding , stride , in_deltas . array , <EOL> out_deltas . array , <EOL> weight_deltas . array , <EOL> bias_deltas . array ) <EOL> @ check_for_inf_or_nan <EOL> def conv2d_forward_batch ( self , inputs , weights , bias , outputs , <EOL> padding , stride ) : <EOL> assert_debug_arrays ( inputs , weights , bias , outputs ) <EOL> assert isinstance ( padding , int ) and <NUM_LIT:0> <= padding , "<STR_LIT>" . format ( padding ) <EOL> assert_is_shape ( stride ) <EOL> assert len ( stride ) == <NUM_LIT:2> , "<STR_LIT>" . format ( stride ) <EOL> self . handler . conv2d_forward_batch ( inputs . array , weights . array , <EOL> bias . array , outputs . array , <EOL> padding , stride ) <EOL> @ check_for_inf_or_nan <EOL> def dot_add_mm ( self , a , b , out , transa = False , transb = False ) : <EOL> assert_debug_arrays ( a , b , out ) <EOL> assert len ( a . shape ) == <NUM_LIT:2> , "<STR_LIT>" . format ( a . shape ) <EOL> assert len ( b . shape ) == <NUM_LIT:2> , "<STR_LIT>" . format ( b . shape ) <EOL> assert len ( out . shape ) == <NUM_LIT:2> , "<STR_LIT>" . format ( out . shape ) <EOL> assert transa in [ True , False ] <EOL> assert transb in [ True , False ] <EOL> a1 , a2 = a . shape <EOL> b1 , b2 = b . shape <EOL> if transa : <EOL> a1 , a2 = a2 , a1 <EOL> if transb : <EOL> b1 , b2 = b2 , b1 <EOL> assert a2 == b1 , "<STR_LIT>" . format ( a2 , b1 , transa , transb ) <EOL> assert out . shape == ( a1 , b2 ) , "<STR_LIT>" . format ( out . shape , ( a1 , b2 ) ) <EOL> self . handler . dot_add_mm ( a . array , b . array , out . array , transa , transb ) <EOL> @ check_for_inf_or_nan <EOL> def dot_mm ( self , a , b , out , transa = False , transb = False ) : <EOL> assert_debug_arrays ( a , b , out ) <EOL> assert len ( a . shape ) == <NUM_LIT:2> , "<STR_LIT>" . format ( a . shape ) <EOL> assert len ( b . shape ) == <NUM_LIT:2> , "<STR_LIT>" . format ( b . shape ) <EOL> assert len ( out . shape ) == <NUM_LIT:2> , "<STR_LIT>" . format ( out . shape ) <EOL> assert transa in [ True , False ] <EOL> assert transb in [ True , False ] <EOL> a1 , a2 = a . shape <EOL> b1 , b2 = b . shape <EOL> if transa : <EOL> a1 , a2 = a2 , a1 <EOL> if transb : <EOL> b1 , b2 = b2 , b1 <EOL> assert a2 == b1 , "<STR_LIT>" . format ( a2 , b1 , transa , transb ) <EOL> assert out . shape == ( a1 , b2 ) , "<STR_LIT>" . format ( out . shape , ( a1 , b2 ) ) <EOL> self . handler . dot_mm ( a . array , b . array , out . array , transa , transb ) <EOL> @ check_for_inf_or_nan <EOL> def divide_mv ( self , m , v , out ) : <EOL> assert_debug_arrays ( m , v , out ) <EOL> assert_shapes_equal ( m , out ) <EOL> assert len ( m . shape ) == <NUM_LIT:2> , "<STR_LIT>" . format ( m . shape ) <EOL> assert v . shape == ( m . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) or v . shape == ( <NUM_LIT:1> , m . shape [ <NUM_LIT:1> ] ) , "<STR_LIT>" . format ( v . shape ) <EOL> self . handler . divide_mv ( m . array , v . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def divide_tt ( self , a , b , out ) : <EOL> assert_debug_arrays ( a , b , out ) <EOL> assert_shapes_equal ( a , b , out ) <EOL> self . handler . divide_tt ( a . array , b . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def fill_gaussian ( self , mean , std , out ) : <EOL> assert_debug_arrays ( out ) <EOL> assert std >= <NUM_LIT:0.0> <EOL> self . handler . fill_gaussian ( mean , std , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def generate_probability_mask ( self , mask , probability ) : <EOL> assert_debug_arrays ( mask ) <EOL> assert_is_scalar ( probability ) <EOL> assert <NUM_LIT:0.0> <= probability <= <NUM_LIT:1.0> , "<STR_LIT:{}>" . format ( probability ) <EOL> self . handler . generate_probability_mask ( mask . array , probability ) <EOL> @ check_for_inf_or_nan <EOL> def index_m_by_v ( self , m , v , out ) : <EOL> assert_debug_arrays ( m , v , out ) <EOL> assert_shapes_equal ( v , out ) <EOL> assert len ( m . shape ) == len ( v . shape ) == <NUM_LIT:2> <EOL> assert v . shape == ( m . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> self . handler . index_m_by_v ( m . array , v . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def log_t ( self , a , out ) : <EOL> assert_debug_arrays ( a , out ) <EOL> assert_shapes_equal ( a , out ) <EOL> self . handler . log_t ( a . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def maxpool2d_backward_batch ( self , inputs , window , outputs , padding , <EOL> stride , argmax , in_deltas , out_deltas ) : <EOL> assert_debug_arrays ( inputs , outputs , argmax , in_deltas , out_deltas ) <EOL> assert_is_shape ( window ) <EOL> assert len ( window ) == <NUM_LIT:2> , "<STR_LIT>" . format ( window ) <EOL> assert_is_shape ( stride ) <EOL> assert len ( stride ) == <NUM_LIT:2> , "<STR_LIT>" . format ( stride ) <EOL> assert isinstance ( padding , int ) and <NUM_LIT:0> <= padding , "<STR_LIT>" . format ( padding ) <EOL> assert_shapes_equal ( inputs , in_deltas ) <EOL> assert_shapes_equal ( outputs , out_deltas ) <EOL> self . handler . maxpool2d_backward_batch ( inputs . array , window , <EOL> outputs . array , <EOL> padding , stride , argmax . array , <EOL> in_deltas . array , <EOL> out_deltas . array ) <EOL> @ check_for_inf_or_nan <EOL> def maxpool2d_forward_batch ( self , inputs , window , outputs , padding , <EOL> stride , argmax ) : <EOL> assert_debug_arrays ( inputs , outputs , argmax ) <EOL> assert_is_shape ( window ) <EOL> assert len ( window ) == <NUM_LIT:2> , "<STR_LIT>" . format ( window ) <EOL> assert_is_shape ( stride ) <EOL> assert len ( stride ) == <NUM_LIT:2> , "<STR_LIT>" . format ( stride ) <EOL> assert isinstance ( padding , int ) and <NUM_LIT:0> <= padding , "<STR_LIT>" . format ( padding ) <EOL> self . handler . maxpool2d_forward_batch ( inputs . array , window , <EOL> outputs . array , padding , stride , <EOL> argmax . array ) <EOL> @ check_for_inf_or_nan <EOL> def merge_tt ( self , a , b , out ) : <EOL> assert ( a . shape [ - <NUM_LIT:1> ] + b . shape [ - <NUM_LIT:1> ] == out . shape [ - <NUM_LIT:1> ] ) <EOL> assert_debug_arrays ( a , b , out ) <EOL> self . handler . merge_tt ( a . array , b . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def modulo_tt ( self , a , b , out ) : <EOL> assert_debug_arrays ( a , b , out ) <EOL> assert_shapes_equal ( a , b , out ) <EOL> self . handler . modulo_tt ( a . array , b . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def mult_add_st ( self , s , t , out ) : <EOL> assert_debug_arrays ( t , out ) <EOL> assert_is_scalar ( s ) <EOL> assert_shapes_equal ( t , out ) <EOL> self . handler . mult_add_st ( s , t . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def mult_add_tt ( self , a , b , out ) : <EOL> assert_debug_arrays ( a , b , out ) <EOL> assert_shapes_equal ( a , b , out ) <EOL> self . handler . mult_add_tt ( a . array , b . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def mult_mv ( self , m , v , out ) : <EOL> assert_debug_arrays ( m , v , out ) <EOL> assert_shapes_equal ( m , out ) <EOL> assert len ( m . shape ) == <NUM_LIT:2> , "<STR_LIT>" . format ( m . shape ) <EOL> assert v . shape in [ ( m . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) , ( <NUM_LIT:1> , m . shape [ <NUM_LIT:1> ] ) , m . shape ] , "<STR_LIT>" . format ( v . shape , m . shape ) <EOL> self . handler . mult_mv ( m . array , v . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def mult_add_mv ( self , m , v , out ) : <EOL> assert_debug_arrays ( m , v , out ) <EOL> assert_shapes_equal ( m , out ) <EOL> assert len ( m . shape ) == <NUM_LIT:2> , "<STR_LIT>" . format ( m . shape ) <EOL> assert v . shape == ( m . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) or v . shape == ( <NUM_LIT:1> , m . shape [ <NUM_LIT:1> ] ) , "<STR_LIT>" . format ( v . shape ) <EOL> self . handler . mult_add_mv ( m . array , v . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def mult_st ( self , s , t , out ) : <EOL> assert_debug_arrays ( t , out ) <EOL> assert_is_scalar ( s ) <EOL> assert_shapes_equal ( t , out ) <EOL> self . handler . mult_st ( s , t . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def mult_tt ( self , a , b , out ) : <EOL> assert_debug_arrays ( a , b , out ) <EOL> assert_shapes_equal ( a , b , out ) <EOL> self . handler . mult_tt ( a . array , b . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def sign_t ( self , a , out ) : <EOL> assert_debug_arrays ( a , out ) <EOL> assert_shapes_equal ( a , out ) <EOL> self . handler . sign_t ( a . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def split_add_tt ( self , x , out_a , out_b ) : <EOL> assert ( out_a . shape [ - <NUM_LIT:1> ] + out_b . shape [ - <NUM_LIT:1> ] == x . shape [ - <NUM_LIT:1> ] ) <EOL> assert_debug_arrays ( out_a , out_b , x ) <EOL> self . handler . split_add_tt ( x . array , out_a . array , out_b . array ) <EOL> @ check_for_inf_or_nan <EOL> def sqrt_t ( self , a , out ) : <EOL> assert_debug_arrays ( a , out ) <EOL> assert_shapes_equal ( a , out ) <EOL> self . handler . sqrt_t ( a . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def subtract_mv ( self , m , v , out ) : <EOL> assert_debug_arrays ( m , v , out ) <EOL> assert_shapes_equal ( m , out ) <EOL> assert len ( m . shape ) == <NUM_LIT:2> , "<STR_LIT>" . format ( m . shape ) <EOL> assert v . shape == ( m . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) or v . shape == ( <NUM_LIT:1> , m . shape [ <NUM_LIT:1> ] ) , "<STR_LIT>" . format ( v . shape ) <EOL> self . handler . subtract_mv ( m . array , v . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def subtract_tt ( self , a , b , out ) : <EOL> assert_debug_arrays ( a , b , out ) <EOL> assert_shapes_equal ( a , b , out ) <EOL> self . handler . subtract_tt ( a . array , b . array , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def sum_t ( self , a , axis , out ) : <EOL> assert_debug_arrays ( a , out ) <EOL> dims = len ( a . shape ) <EOL> assert axis is None or ( isinstance ( axis , int ) and <NUM_LIT:0> <= axis < dims ) , "<STR_LIT>" . format ( axis ) <EOL> self . handler . sum_t ( a . array , axis , out . array ) <EOL> @ check_for_inf_or_nan <EOL> def sigmoid ( self , x , y ) : <EOL> assert_debug_arrays ( x , y ) <EOL> assert_shapes_equal ( x , y ) <EOL> self . handler . sigmoid ( x . array , y . array ) <EOL> @ check_for_inf_or_nan <EOL> def sigmoid_deriv ( self , x , y , dy , dx ) : <EOL> assert_debug_arrays ( y , dy , dx ) <EOL> assert_shapes_equal ( y , dy , dx ) <EOL> if x is not None : <EOL> assert_debug_arrays ( x ) <EOL> assert_shapes_equal ( x , y ) <EOL> x = x . array <EOL> self . handler . sigmoid_deriv ( x , y . array , dy . array , dx . array ) <EOL> @ check_for_inf_or_nan <EOL> def tanh ( self , x , y ) : <EOL> assert_debug_arrays ( x , y ) <EOL> assert_shapes_equal ( x , y ) <EOL> self . handler . tanh ( x . array , y . array ) <EOL> @ check_for_inf_or_nan <EOL> def tanh_deriv ( self , x , y , dy , dx ) : <EOL> assert_debug_arrays ( y , dy , dx ) <EOL> assert_shapes_equal ( y , dy , dx ) <EOL> if x is not None : <EOL> assert_debug_arrays ( x ) <EOL> assert_shapes_equal ( x , y ) <EOL> x = x . array <EOL> self . handler . tanh_deriv ( x , y . array , dy . array , dx . array ) <EOL> @ check_for_inf_or_nan <EOL> def rel ( self , x , y ) : <EOL> assert_debug_arrays ( x , y ) <EOL> assert_shapes_equal ( x , y ) <EOL> self . handler . rel ( x . array , y . array ) <EOL> @ check_for_inf_or_nan <EOL> def rel_deriv ( self , x , y , dy , dx ) : <EOL> assert_debug_arrays ( y , dy , dx ) <EOL> assert_shapes_equal ( y , dy , dx ) <EOL> if x is not None : <EOL> assert_debug_arrays ( x ) <EOL> assert_shapes_equal ( x , y ) <EOL> x = x . array <EOL> self . handler . rel_deriv ( x , y . array , dy . array , dx . array ) <EOL> @ check_for_inf_or_nan <EOL> def el ( self , x , y ) : <EOL> assert_debug_arrays ( x , y ) <EOL> assert_shapes_equal ( x , y ) <EOL> self . handler . el ( x . array , y . array ) <EOL> @ check_for_inf_or_nan <EOL> def el_deriv ( self , x , y , dy , dx ) : <EOL> assert_debug_arrays ( y , dy , dx ) <EOL> assert_shapes_equal ( y , dy , dx ) <EOL> if x is not None : <EOL> assert_debug_arrays ( x ) <EOL> assert_shapes_equal ( x , y ) <EOL> x = x . array <EOL> self . handler . el_deriv ( x , y . array , dy . array , dx . array ) <EOL> @ check_for_inf_or_nan <EOL> def softmax_m ( self , m , out ) : <EOL> assert_debug_arrays ( m , out ) <EOL> assert_shapes_equal ( m , out ) <EOL> assert len ( m . shape ) == <NUM_LIT:2> , "<STR_LIT>" . format ( m . shape ) <EOL> self . handler . softmax_m ( m . array , out . array ) <EOL> def assert_is_shape ( shape ) : <EOL> assert isinstance ( shape , tuple ) , type ( shape ) <EOL> for i in shape : <EOL> assert isinstance ( i , int ) , "<STR_LIT>" . format ( i , type ( i ) ) <EOL> assert <NUM_LIT:0> <= i , "<STR_LIT>" . format ( i ) <EOL> def assert_debug_arrays ( * arrays ) : <EOL> for i , arr in enumerate ( arrays ) : <EOL> assert isinstance ( arr , DebugArray ) , "<STR_LIT>" . format ( i , type ( arr ) ) <EOL> def assert_shapes_equal ( ref_shape , * shapes ) : <EOL> if isinstance ( ref_shape , DebugArray ) : <EOL> ref_shape = ref_shape . shape <EOL> assert_is_shape ( ref_shape ) <EOL> for i , shape in enumerate ( shapes , start = <NUM_LIT:1> ) : <EOL> if isinstance ( shape , DebugArray ) : <EOL> shape = shape . shape <EOL> assert_is_shape ( shape ) <EOL> assert shape == ref_shape , "<STR_LIT>" . format ( shape , i , <EOL> ref_shape ) <EOL> def assert_is_scalar ( s ) : <EOL> assert isinstance ( s , ( int , float ) ) , "<STR_LIT>" . format ( s , type ( s ) ) </s>
