@ -79,7 +79,7 @@ class TestMeshgridOp3(unittest.TestCase):
out_2 = np . broadcast_to ( out_2 , [ 100 , 200 ] )
out_2 = np . broadcast_to ( out_2 , [ 100 , 200 ] )
exe = fluid . Executor ( place = fluid . CPUPlace ( ) )
exe = fluid . Executor ( place = fluid . CPUPlace ( ) )
grid_x , grid_y = paddle . tensor . meshgrid ( [ x , y ] )
grid_x , grid_y = paddle . tensor . meshgrid ( x , y )
res_1 , res_2 = exe . run ( fluid . default_main_program ( ) ,
res_1 , res_2 = exe . run ( fluid . default_main_program ( ) ,
feed = { ' x ' : input_1 ,
feed = { ' x ' : input_1 ,
' y ' : input_2 } ,
' y ' : input_2 } ,
@ -90,21 +90,72 @@ class TestMeshgridOp3(unittest.TestCase):
class TestMeshgridOp4 ( unittest . TestCase ) :
class TestMeshgridOp4 ( unittest . TestCase ) :
def test_errors ( self ) :
def test_list_input ( self ) :
with program_guard ( Program ( ) , Program ( ) ) :
x = fluid . data ( shape = [ 100 ] , dtype = ' int32 ' , name = ' x ' )
y = fluid . data ( shape = [ 200 ] , dtype = ' int32 ' , name = ' y ' )
def test_input_type ( ) :
input_1 = np . random . randint ( 0 , 100 , [ 100 , ] ) . astype ( ' int32 ' )
x = fluid . data ( shape = [ 200 ] , dtype = ' float32 ' , name = ' x2 ' )
input_2 = np . random . randint ( 0 , 100 , [ 200 , ] ) . astype ( ' int32 ' )
paddle . tensor . meshgrid ( x )
self . assertRaises ( TypeError , test_input_type )
out_1 = np . reshape ( input_1 , [ 100 , 1 ] )
out_1 = np . broadcast_to ( out_1 , [ 100 , 200 ] )
out_2 = np . reshape ( input_2 , [ 1 , 200 ] )
out_2 = np . broadcast_to ( out_2 , [ 100 , 200 ] )
exe = fluid . Executor ( place = fluid . CPUPlace ( ) )
grid_x , grid_y = paddle . tensor . meshgrid ( [ x , y ] )
res_1 , res_2 = exe . run ( fluid . default_main_program ( ) ,
feed = { ' x ' : input_1 ,
' y ' : input_2 } ,
fetch_list = [ grid_x , grid_y ] )
assert np . array_equal ( res_1 , out_1 )
assert np . array_equal ( res_2 , out_2 )
class TestMeshgridOp5 ( unittest . TestCase ) :
class TestMeshgridOp5 ( unittest . TestCase ) :
def test_tuple_input ( self ) :
x = fluid . data ( shape = [ 100 ] , dtype = ' int32 ' , name = ' x ' )
y = fluid . data ( shape = [ 200 ] , dtype = ' int32 ' , name = ' y ' )
input_1 = np . random . randint ( 0 , 100 , [ 100 , ] ) . astype ( ' int32 ' )
input_2 = np . random . randint ( 0 , 100 , [ 200 , ] ) . astype ( ' int32 ' )
out_1 = np . reshape ( input_1 , [ 100 , 1 ] )
out_1 = np . broadcast_to ( out_1 , [ 100 , 200 ] )
out_2 = np . reshape ( input_2 , [ 1 , 200 ] )
out_2 = np . broadcast_to ( out_2 , [ 100 , 200 ] )
exe = fluid . Executor ( place = fluid . CPUPlace ( ) )
grid_x , grid_y = paddle . tensor . meshgrid ( ( x , y ) )
res_1 , res_2 = exe . run ( fluid . default_main_program ( ) ,
feed = { ' x ' : input_1 ,
' y ' : input_2 } ,
fetch_list = [ grid_x , grid_y ] )
assert np . array_equal ( res_1 , out_1 )
assert np . array_equal ( res_2 , out_2 )
class TestMeshgridOp6 ( unittest . TestCase ) :
def test_api_with_dygraph ( self ) :
def test_api_with_dygraph ( self ) :
input_3 = np . random . randint ( 0 , 100 , [ 100 , ] ) . astype ( ' int32 ' )
input_3 = np . random . randint ( 0 , 100 , [ 100 , ] ) . astype ( ' int32 ' )
input_4 = np . random . randint ( 0 , 100 , [ 200 , ] ) . astype ( ' int32 ' )
input_4 = np . random . randint ( 0 , 100 , [ 200 , ] ) . astype ( ' int32 ' )
with fluid . dygraph . guard ( ) :
tensor_3 = fluid . dygraph . to_variable ( input_3 )
tensor_4 = fluid . dygraph . to_variable ( input_4 )
res_3 , res_4 = paddle . tensor . meshgrid ( tensor_3 , tensor_4 )
assert np . array_equal ( res_3 . shape , [ 100 , 200 ] )
assert np . array_equal ( res_4 . shape , [ 100 , 200 ] )
class TestMeshgridOp7 ( unittest . TestCase ) :
def test_api_with_dygraph_list_input ( self ) :
input_3 = np . random . randint ( 0 , 100 , [ 100 , ] ) . astype ( ' int32 ' )
input_4 = np . random . randint ( 0 , 100 , [ 200 , ] ) . astype ( ' int32 ' )
with fluid . dygraph . guard ( ) :
with fluid . dygraph . guard ( ) :
tensor_3 = fluid . dygraph . to_variable ( input_3 )
tensor_3 = fluid . dygraph . to_variable ( input_3 )
tensor_4 = fluid . dygraph . to_variable ( input_4 )
tensor_4 = fluid . dygraph . to_variable ( input_4 )
@ -114,5 +165,19 @@ class TestMeshgridOp5(unittest.TestCase):
assert np . array_equal ( res_4 . shape , [ 100 , 200 ] )
assert np . array_equal ( res_4 . shape , [ 100 , 200 ] )
class TestMeshgridOp7 ( unittest . TestCase ) :
def test_api_with_dygraph_tuple_input ( self ) :
input_3 = np . random . randint ( 0 , 100 , [ 100 , ] ) . astype ( ' int32 ' )
input_4 = np . random . randint ( 0 , 100 , [ 200 , ] ) . astype ( ' int32 ' )
with fluid . dygraph . guard ( ) :
tensor_3 = fluid . dygraph . to_variable ( input_3 )
tensor_4 = fluid . dygraph . to_variable ( input_4 )
res_3 , res_4 = paddle . tensor . meshgrid ( ( tensor_3 , tensor_4 ) )
assert np . array_equal ( res_3 . shape , [ 100 , 200 ] )
assert np . array_equal ( res_4 . shape , [ 100 , 200 ] )
if __name__ == ' __main__ ' :
if __name__ == ' __main__ ' :
unittest . main ( )
unittest . main ( )