<s> '''<STR_LIT>''' <EOL> import itertools as itt <EOL> import numpy as np <EOL> from neon import NervanaObject <EOL> from neon . backends import gen_backend <EOL> from neon . transforms import ( CrossEntropyBinary , CrossEntropyMulti , SumSquared , <EOL> MeanSquared , Misclassification , PrecisionRecall , <EOL> SmoothL1Loss ) <EOL> def pytest_generate_tests ( metafunc ) : <EOL> if '<STR_LIT>' in metafunc . fixturenames : <EOL> fargs = [ ] <EOL> if metafunc . config . option . all : <EOL> shape1_rng = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] <EOL> shape2_rng = [ <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:10> , <NUM_LIT:20> ] <EOL> mag_rng = [ <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:10> , <NUM_LIT:20> ] <EOL> else : <EOL> shape1_rng = [ <NUM_LIT:3> ] <EOL> shape2_rng = [ <NUM_LIT:5> ] <EOL> mag_rng = [ <NUM_LIT:10> ] <EOL> fargs = itt . product ( shape1_rng , shape2_rng , mag_rng ) <EOL> metafunc . parametrize ( '<STR_LIT>' , fargs ) <EOL> def compare_tensors ( func , y , t , outputs , deriv = False , tol = <NUM_LIT:0.> ) : <EOL> be = NervanaObject . be <EOL> temp = be . empty ( outputs . shape ) <EOL> dtypeu = np . float32 <EOL> if deriv is True : <EOL> temp [ : ] = func . bprop ( be . array ( dtypeu ( y ) ) , be . array ( dtypeu ( t ) ) ) <EOL> else : <EOL> temp [ : ] = func ( be . array ( dtypeu ( y ) ) , be . array ( dtypeu ( t ) ) ) <EOL> cond = np . sum ( np . abs ( temp . get ( ) - outputs ) <= tol ) <EOL> assert cond == np . prod ( outputs . shape ) <EOL> """<STR_LIT>""" <EOL> def test_cross_entropy_binary ( backend_default ) : <EOL> outputs = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> targets = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> eps = np . exp ( - <NUM_LIT:50> ) <EOL> expected_log = np . log ( np . maximum ( outputs , eps ) ) <EOL> expected_mlog = np . log ( np . maximum ( <NUM_LIT:1> - outputs , eps ) ) <EOL> expected_result = np . sum ( ( - targets * expected_log ) - ( <NUM_LIT:1> - targets ) * expected_mlog , <EOL> keepdims = True ) <EOL> compare_tensors ( CrossEntropyBinary ( ) , <EOL> outputs , targets , expected_result , tol = <NUM_LIT> ) <EOL> def test_cross_entropy_binary_limits ( backend_default ) : <EOL> outputs = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> targets = np . array ( ( [ <NUM_LIT:0.5> , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT> ] ) ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> eps = np . exp ( - <NUM_LIT:50> ) <EOL> expected_log = np . log ( np . maximum ( outputs , eps ) ) <EOL> expected_mlog = np . log ( np . maximum ( <NUM_LIT:1> - outputs , eps ) ) <EOL> expected_result = np . sum ( ( - targets * expected_log ) - ( <NUM_LIT:1> - targets ) * expected_mlog , <EOL> keepdims = True ) <EOL> compare_tensors ( CrossEntropyBinary ( ) , <EOL> outputs , targets , expected_result , tol = <NUM_LIT> ) <EOL> def test_cross_entropy_binary_derivative ( backend_default ) : <EOL> outputs = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> targets = np . array ( ( [ <NUM_LIT:0.5> , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT> ] ) ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> expected_result = ( ( outputs - targets ) / outputs . shape [ <NUM_LIT:1> ] ) <EOL> compare_tensors ( <EOL> CrossEntropyBinary ( ) , outputs , targets , expected_result , deriv = True , <EOL> tol = <NUM_LIT> ) <EOL> """<STR_LIT>""" <EOL> def test_cross_entropy_multi ( backend_default ) : <EOL> outputs = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> targets = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> eps = np . exp ( - <NUM_LIT:50> ) <EOL> expected_log = np . log ( np . maximum ( outputs , eps ) ) <EOL> expected_result = np . sum ( - targets * expected_log , axis = <NUM_LIT:0> , keepdims = True ) <EOL> compare_tensors ( CrossEntropyMulti ( ) , <EOL> outputs , targets , expected_result , tol = <NUM_LIT> ) <EOL> def test_cross_entropy_multi_limits ( backend_default ) : <EOL> outputs = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> targets = np . array ( ( [ <NUM_LIT:0.5> , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT> ] ) ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> eps = np . exp ( - <NUM_LIT:50> ) <EOL> expected_log = np . log ( np . maximum ( outputs , eps ) ) <EOL> expected_result = np . sum ( - targets * expected_log , axis = <NUM_LIT:0> , keepdims = True ) <EOL> compare_tensors ( CrossEntropyMulti ( ) , <EOL> outputs , targets , expected_result , tol = <NUM_LIT> ) <EOL> def test_cross_entropy_multi_derivative ( backend_default ) : <EOL> outputs = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> targets = np . array ( ( [ <NUM_LIT:0.5> , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT> ] ) ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> expected_result = ( ( outputs - targets ) / outputs . shape [ <NUM_LIT:1> ] ) <EOL> compare_tensors ( CrossEntropyMulti ( ) , outputs , targets , expected_result , <EOL> deriv = True , tol = <NUM_LIT> ) <EOL> """<STR_LIT>""" <EOL> def test_sum_squared ( backend_default ) : <EOL> outputs = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> targets = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> expected_result = np . sum ( ( outputs - targets ) ** <NUM_LIT:2> , axis = <NUM_LIT:0> , keepdims = True ) / <NUM_LIT> <EOL> compare_tensors ( SumSquared ( ) , outputs , targets , expected_result , tol = <NUM_LIT> ) <EOL> def test_sum_squared_limits ( backend_default ) : <EOL> outputs = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> targets = np . array ( ( [ <NUM_LIT:0.5> , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT> ] ) ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> expected_result = np . sum ( ( outputs - targets ) ** <NUM_LIT:2> , axis = <NUM_LIT:0> , keepdims = True ) / <NUM_LIT> <EOL> compare_tensors ( SumSquared ( ) , outputs , targets , expected_result , tol = <NUM_LIT> ) <EOL> def test_sum_squared_derivative ( backend_default ) : <EOL> outputs = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> targets = np . array ( ( [ <NUM_LIT:0.5> , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT> ] ) ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> expected_result = ( outputs - targets ) / outputs . shape [ <NUM_LIT:1> ] <EOL> compare_tensors ( SumSquared ( ) , outputs , <EOL> targets , expected_result , deriv = True , tol = <NUM_LIT> ) <EOL> """<STR_LIT>""" <EOL> def test_mean_squared ( backend_default ) : <EOL> outputs = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> targets = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> expected_result = np . mean ( ( outputs - targets ) ** <NUM_LIT:2> , axis = <NUM_LIT:0> , keepdims = True ) / <NUM_LIT> <EOL> compare_tensors ( MeanSquared ( ) , outputs , targets , expected_result , tol = <NUM_LIT> ) <EOL> def test_mean_squared_limits ( backend_default ) : <EOL> outputs = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> targets = np . array ( ( [ <NUM_LIT:0.5> , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT> ] ) ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> expected_result = np . mean ( ( outputs - targets ) ** <NUM_LIT:2> , axis = <NUM_LIT:0> , keepdims = True ) / <NUM_LIT> <EOL> compare_tensors ( MeanSquared ( ) , outputs , targets , expected_result , tol = <NUM_LIT> ) <EOL> def test_mean_squared_derivative ( backend_default ) : <EOL> outputs = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> targets = np . array ( ( [ <NUM_LIT:0.5> , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT> ] ) ) . reshape ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) <EOL> expected_result = ( outputs - targets ) / outputs . shape [ <NUM_LIT:1> ] / outputs . shape [ <NUM_LIT:0> ] <EOL> compare_tensors ( MeanSquared ( ) , outputs , <EOL> targets , expected_result , deriv = True , tol = <NUM_LIT> ) <EOL> """<STR_LIT>""" <EOL> def compare_metric ( func , y , t , outputs , deriv = False , tol = <NUM_LIT:0.> ) : <EOL> be = NervanaObject . be <EOL> dtypeu = np . float32 <EOL> temp = func ( be . array ( dtypeu ( y ) ) , be . array ( dtypeu ( t ) ) ) <EOL> cond = np . sum ( np . abs ( temp - outputs ) <= tol ) <EOL> assert cond == np . prod ( outputs . shape ) <EOL> def test_misclassification ( backend_default ) : <EOL> NervanaObject . be . bsz = <NUM_LIT:3> <EOL> outputs = np . array ( <EOL> [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> targets = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ] ) <EOL> expected_result = np . ones ( ( <NUM_LIT:1> , <NUM_LIT:1> ) ) / <NUM_LIT> <EOL> compare_metric ( Misclassification ( ) , <EOL> outputs , targets , expected_result , tol = <NUM_LIT> ) <EOL> """<STR_LIT>""" <EOL> def test_precision_recall ( backend_default ) : <EOL> be = NervanaObject . be <EOL> be . bsz = <NUM_LIT:4> <EOL> preds = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> targets = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ] ) <EOL> expected_result = np . array ( [ <NUM_LIT:1> + <NUM_LIT:1> + <NUM_LIT:0> , <NUM_LIT:1> + <NUM_LIT:0.5> + <NUM_LIT:0> ] ) / <NUM_LIT> <EOL> compare_metric ( PrecisionRecall ( <NUM_LIT:3> ) , preds , targets , expected_result , tol = <NUM_LIT> ) <EOL> def test_precision_recall_binarize ( backend_default ) : <EOL> be = NervanaObject . be <EOL> be . bsz = <NUM_LIT:4> <EOL> preds = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ] ] ) <EOL> targets = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ] ) <EOL> expected_result = np . array ( [ <NUM_LIT:1> + <NUM_LIT:1> + <NUM_LIT:0> , <NUM_LIT:1> + <NUM_LIT:0.5> + <NUM_LIT:0> ] ) / <NUM_LIT> <EOL> compare_metric ( PrecisionRecall ( <NUM_LIT:3> , binarize = True ) , preds , targets , <EOL> expected_result , tol = <NUM_LIT> ) <EOL> """<STR_LIT>""" <EOL> def test_smoothL1_random ( backend_default , fargs ) : <EOL> s1 , s2 , m = fargs <EOL> shape = ( s1 , s2 ) <EOL> magnitude = m <EOL> outputs = ( np . random . random ( shape ) - <NUM_LIT:0.5> ) * magnitude <EOL> targets = np . random . random ( shape ) <EOL> x = outputs - targets <EOL> expected_result = np . zeros ( shape ) <EOL> I1 , J1 = np . where ( abs ( x ) < <NUM_LIT:1> ) <EOL> I2 , J2 = np . where ( abs ( x ) >= <NUM_LIT:1> ) <EOL> expected_result [ I1 , J1 ] = <NUM_LIT:0.5> * x [ I1 , J1 ] ** <NUM_LIT:2> <EOL> expected_result [ I2 , J2 ] = abs ( x [ I2 , J2 ] ) - <NUM_LIT:0.5> <EOL> expected_result = np . sum ( expected_result , axis = <NUM_LIT:0> , keepdims = True ) <EOL> compare_tensors ( SmoothL1Loss ( ) , outputs , <EOL> targets , expected_result , <EOL> deriv = False , tol = <NUM_LIT> ) <EOL> def test_smoothL1_zeros ( backend_default , fargs ) : <EOL> s1 , s2 , m = fargs <EOL> shape = ( s1 , s2 ) <EOL> outputs = np . zeros ( shape ) <EOL> targets = np . zeros ( shape ) <EOL> x = outputs - targets <EOL> expected_result = np . zeros ( shape ) <EOL> I1 , J1 = np . where ( abs ( x ) < <NUM_LIT:1> ) <EOL> I2 , J2 = np . where ( abs ( x ) >= <NUM_LIT:1> ) <EOL> expected_result [ I1 , J1 ] = <NUM_LIT:0.5> * x [ I1 , J1 ] ** <NUM_LIT:2> <EOL> expected_result [ I2 , J2 ] = abs ( x [ I2 , J2 ] ) - <NUM_LIT:0.5> <EOL> expected_result = np . sum ( expected_result , axis = <NUM_LIT:0> , keepdims = True ) <EOL> compare_tensors ( SmoothL1Loss ( ) , outputs , <EOL> targets , expected_result , <EOL> deriv = False , tol = <NUM_LIT> ) <EOL> def test_smoothL1_ones ( backend_default , fargs ) : <EOL> s1 , s2 , m = fargs <EOL> shape = ( s1 , s2 ) <EOL> outputs = np . ones ( shape ) * m <EOL> targets = np . ones ( shape ) * m <EOL> x = outputs - targets <EOL> expected_result = np . zeros ( shape ) <EOL> I1 , J1 = np . where ( abs ( x ) < <NUM_LIT:1> ) <EOL> I2 , J2 = np . where ( abs ( x ) >= <NUM_LIT:1> ) <EOL> expected_result [ I1 , J1 ] = <NUM_LIT:0.5> * x [ I1 , J1 ] ** <NUM_LIT:2> <EOL> expected_result [ I2 , J2 ] = abs ( x [ I2 , J2 ] ) - <NUM_LIT:0.5> <EOL> expected_result = np . sum ( expected_result , axis = <NUM_LIT:0> , keepdims = True ) <EOL> compare_tensors ( SmoothL1Loss ( ) , outputs , <EOL> targets , expected_result , <EOL> deriv = False , tol = <NUM_LIT> ) <EOL> def test_smoothL1_random_derivative ( backend_default , fargs ) : <EOL> s1 , s2 , m = fargs <EOL> shape = ( s1 , s2 ) <EOL> magnitude = m <EOL> outputs = ( np . random . random ( shape ) - <NUM_LIT:0.5> ) * magnitude <EOL> targets = np . random . random ( shape ) <EOL> x = outputs - targets <EOL> expected_result = np . zeros ( shape ) <EOL> I1 , J1 = np . where ( abs ( x ) < <NUM_LIT:1> ) <EOL> I2 , J2 = np . where ( abs ( x ) >= <NUM_LIT:1> ) <EOL> expected_result [ I1 , J1 ] = x [ I1 , J1 ] <EOL> expected_result [ I2 , J2 ] = np . sign ( x [ I2 , J2 ] ) <EOL> compare_tensors ( SmoothL1Loss ( ) , outputs , <EOL> targets , expected_result , <EOL> deriv = True , tol = <NUM_LIT> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> be = gen_backend ( backend = '<STR_LIT>' , batch_size = <NUM_LIT:50> ) <EOL> fargs = ( <NUM_LIT:4> , <NUM_LIT:10> , <NUM_LIT:20> ) <EOL> test_smoothL1_random ( be , fargs ) <EOL> test_smoothL1_random_derivative ( be , fargs ) </s>
