24
24
import itertools
25
25
from numpy .testing import assert_allclose , assert_array_equal
26
26
from mxnet .test_utils import *
27
- from mxnet .base import py_str
27
+ from mxnet .base import py_str , MXNetError
28
28
from common import setup_module , with_seed
29
29
import unittest
30
30
@@ -3485,24 +3485,23 @@ def test_reverse():
3485
3485
@with_seed ()
3486
3486
def test_tile ():
3487
3487
def test_normal_case ():
3488
- ndim_max = 3 # max number of dims of the ndarray
3489
- size_max = 10 # max number of elements in each dim
3490
- length_max = 3 # max length of reps
3491
- rep_max = 10 # max number of tiling in each dim
3492
- for ndim in range (ndim_max , ndim_max + 1 ):
3493
- shape = ()
3494
- for i in range (0 , ndim ):
3495
- shape += (np .random .randint (1 , size_max + 1 ), )
3488
+ ndim_min = 1
3489
+ ndim_max = 5 # max number of dims of the ndarray
3490
+ size_max = 10 # max number of elements in each dim
3491
+ length_max = 3 # max length of reps
3492
+ rep_max = 10 # max number of tiling in each dim
3493
+ for ndim in range (ndim_min , ndim_max + 1 ):
3494
+ shape = []
3495
+ for i in range (1 , ndim + 1 ):
3496
+ shape .append (np .random .randint (1 , size_max + 1 ))
3497
+ shape = tuple (shape )
3496
3498
a = np .random .randint (0 , 100 , shape )
3497
- a = np .asarray (a , dtype = np .int32 )
3498
- if ndim == 0 :
3499
- a = np .array ([])
3500
- b = mx .nd .array (a , ctx = default_context (), dtype = a .dtype )
3499
+ b = mx .nd .array (a , dtype = a .dtype )
3501
3500
3502
- reps_len = np .random .randint (0 , length_max + 1 )
3501
+ reps_len = np .random .randint (1 , length_max + 1 )
3503
3502
reps_tuple = ()
3504
3503
for i in range (1 , reps_len ):
3505
- reps_tuple += (np .random .randint (0 , rep_max ), )
3504
+ reps_tuple += (np .random .randint (1 , rep_max ), )
3506
3505
reps_array = np .asarray (reps_tuple )
3507
3506
3508
3507
a_tiled = np .tile (a , reps_array )
@@ -3526,14 +3525,6 @@ def test_empty_reps():
3526
3525
b_tiled = mx .nd .tile (b , ()).asnumpy ()
3527
3526
assert same (a_tiled , b_tiled )
3528
3527
3529
- def test_zero_reps ():
3530
- a = np .array ([[2 , 3 , 4 ], [5 , 6 , 7 ]], dtype = np .int32 )
3531
- b = mx .nd .array (a , ctx = default_context (), dtype = a .dtype )
3532
- reps = (2 , 0 , 4 , 5 )
3533
- a_tiled = np .tile (a , reps )
3534
- b_tiled = mx .nd .tile (b , reps ).asnumpy ()
3535
- assert same (a_tiled , b_tiled )
3536
-
3537
3528
def test_tile_backward ():
3538
3529
data = mx .sym .Variable ('data' )
3539
3530
n1 = 2
@@ -3570,12 +3561,17 @@ def test_tile_numeric_gradient():
3570
3561
test = mx .sym .tile (data , reps = reps )
3571
3562
check_numeric_gradient (test , [data_tmp ], numeric_eps = 1e-2 , rtol = 1e-2 )
3572
3563
3564
+ def test_invalid_reps ():
3565
+ data = mx .nd .arange (16 ).reshape ((4 , 4 ))
3566
+ assert_exception (mx .nd .tile , MXNetError , data , (1 , 2 , - 3 ))
3567
+ assert_exception (mx .nd .tile , MXNetError , data , (1 , 0 , 3 ))
3568
+
3573
3569
test_normal_case ()
3574
3570
test_empty_tensor ()
3575
3571
test_empty_reps ()
3576
- test_zero_reps ()
3577
3572
test_tile_backward ()
3578
3573
test_tile_numeric_gradient ()
3574
+ test_invalid_reps ()
3579
3575
3580
3576
3581
3577
@with_seed ()
0 commit comments