<s> import itertools <EOL> import numpy as np <EOL> import deeppy as dp <EOL> import deeppy . expr as ex <EOL> from deeppy . misc . test import ( eps , BPropableSource , approx_fprime , gradclose , <EOL> graph_funs ) <EOL> shapes = [ ( <NUM_LIT:1> , <NUM_LIT:4> ) , ( <NUM_LIT:1> , <NUM_LIT:7> ) , ( <NUM_LIT:2> , <NUM_LIT:6> ) , ( <NUM_LIT:3> , <NUM_LIT:3> ) , ( <NUM_LIT:3> , <NUM_LIT:4> ) , ( <NUM_LIT:5> , <NUM_LIT:1> ) ] <EOL> def test_unary ( ) : <EOL> ops = [ <EOL> ex . Absolute , ex . Negative , ex . Log , ex . Exp , ex . Tanh , <EOL> ] <EOL> confs = itertools . product ( shapes , ops ) <EOL> for shape , op in confs : <EOL> print ( '<STR_LIT>' % ( op . __name__ . ljust ( <NUM_LIT:20> ) , shape ) ) <EOL> src_array = np . random . normal ( size = shape ) . astype ( dp . float_ ) <EOL> if op is ex . Log : <EOL> src_array = np . fabs ( src_array ) + eps <EOL> src = BPropableSource ( src_array ) <EOL> sink = op ( ) ( src ) <EOL> f , f_grad = graph_funs ( src , sink ) <EOL> g_approx = approx_fprime ( src_array , f ) <EOL> g_true = f_grad ( src_array ) <EOL> assert gradclose ( g_true , g_approx ) <EOL> def test_binary ( ) : <EOL> ops = [ <EOL> ex . Add , ex . Subtract , ex . Multiply , ex . Divide , ex . Power , ex . Maximum , <EOL> ex . Minimum , <EOL> ] <EOL> confs = itertools . product ( shapes , ops ) <EOL> for shape , op in confs : <EOL> print ( '<STR_LIT>' % ( op . __name__ . ljust ( <NUM_LIT:20> ) , shape ) ) <EOL> lhs_array = np . random . normal ( size = shape ) . astype ( dp . float_ ) <EOL> rhs_array = np . random . normal ( size = shape ) . astype ( dp . float_ ) <EOL> if op is ex . Power : <EOL> lhs_array = np . fabs ( lhs_array ) <EOL> if op is ex . Divide : <EOL> rhs_array [ np . fabs ( rhs_array ) < eps ] = eps <EOL> rhs_array [ np . fabs ( rhs_array ) < eps ] = eps <EOL> lhs_src = BPropableSource ( lhs_array ) <EOL> rhs_src = BPropableSource ( rhs_array ) <EOL> sink = op ( ) ( lhs_src , rhs_src ) <EOL> for src , x0 in [ ( lhs_src , lhs_array ) , ( rhs_src , rhs_array ) ] : <EOL> f , f_grad = graph_funs ( src , sink ) <EOL> g_approx = approx_fprime ( x0 , f ) <EOL> g_true = f_grad ( x0 ) <EOL> assert gradclose ( g_true , g_approx ) <EOL> def test_expressions ( ) : <EOL> exprs = [ <EOL> lambda a , b , c : a + b + c , <EOL> lambda a , b , c : a - b - c , <EOL> lambda a , b , c : a * b * c , <EOL> lambda a , b , c : a / b / c , <EOL> lambda a , b , c : a + b - c , <EOL> lambda a , b , c : a - b + c , <EOL> lambda a , b , c : a + b + ( - c ) , <EOL> lambda a , b , c : a + b * a + c , <EOL> lambda a , b , c : a + b ** <NUM_LIT:2> + c , <EOL> lambda a , b , c : a + b * b + a * b + c , <EOL> lambda a , b , c : a + <NUM_LIT:2> * ( b + a ) + c , <EOL> lambda a , b , c : c / ( a + ( ( b / <NUM_LIT:1> ) * ( c + <NUM_LIT:1> ) ) + a * b * c ) , <EOL> ] <EOL> confs = itertools . product ( shapes , exprs ) <EOL> for shape , op in confs : <EOL> a_array = np . random . normal ( size = shape ) . astype ( dp . float_ ) <EOL> b_array = np . random . normal ( size = shape ) . astype ( dp . float_ ) <EOL> c_array = np . random . normal ( size = shape ) . astype ( dp . float_ ) <EOL> a_src = BPropableSource ( a_array ) <EOL> b_src = BPropableSource ( b_array ) <EOL> c_src = BPropableSource ( c_array ) <EOL> sink = op ( a_src , b_src , c_src ) <EOL> for src , x0 in [ ( a_src , a_array ) , ( b_src , b_array ) , ( c_src , c_array ) ] : <EOL> f , f_grad = graph_funs ( src , sink ) <EOL> g_approx = approx_fprime ( x0 , f ) <EOL> g_true = f_grad ( x0 ) <EOL> assert gradclose ( g_true , g_approx ) <EOL> def test_broadcast ( ) : <EOL> shapes = [ <EOL> [ ( <NUM_LIT:3> , <NUM_LIT:4> ) , ( <NUM_LIT:3> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:4> ) ] , <EOL> [ ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:4> ) , ( <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:4> ) ] , <EOL> [ ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:1> ) ] , <EOL> ] <EOL> exprs = [ <EOL> lambda a , b , c : a + b + c , <EOL> lambda a , b , c : a - b - c , <EOL> lambda a , b , c : a * b * c , <EOL> lambda a , b , c : a / b / c , <EOL> lambda a , b , c : a + b - c , <EOL> lambda a , b , c : a - b + c , <EOL> lambda a , b , c : a + b + ( - c ) , <EOL> lambda a , b , c : a + b * a + c , <EOL> lambda a , b , c : a + b ** <NUM_LIT:2> + c , <EOL> lambda a , b , c : a + b * b + a * b + c , <EOL> lambda a , b , c : a + <NUM_LIT:2> * ( b + a ) + c , <EOL> lambda a , b , c : c / ( a + ( ( b / <NUM_LIT:1> ) * ( c + <NUM_LIT:1> ) ) + a * b * c ) , <EOL> ] <EOL> confs = itertools . product ( shapes , exprs ) <EOL> for ( a_shape , b_shape , c_shape ) , op in confs : <EOL> a_array = np . random . normal ( size = a_shape ) . astype ( dp . float_ ) <EOL> b_array = np . random . normal ( size = b_shape ) . astype ( dp . float_ ) <EOL> c_array = np . random . normal ( size = c_shape ) . astype ( dp . float_ ) <EOL> a_src = BPropableSource ( a_array ) <EOL> b_src = BPropableSource ( b_array ) <EOL> c_src = BPropableSource ( c_array ) <EOL> sink = op ( a_src , b_src , c_src ) <EOL> for src , x0 in [ ( a_src , a_array ) , ( b_src , b_array ) , ( c_src , c_array ) ] : <EOL> f , f_grad = graph_funs ( src , sink ) <EOL> g_approx = approx_fprime ( x0 , f ) <EOL> g_true = f_grad ( x0 ) <EOL> assert gradclose ( g_true , g_approx ) </s>
