18
18
19
19
import pytest
20
20
import numpy as np
21
- import tensorflow as tf
22
21
23
22
from tensorflow_addons .layers .poincare import PoincareNormalize
24
23
from tensorflow_addons .utils import test_utils
25
24
26
25
27
- @test_utils .run_all_in_graph_and_eager_modes
28
- class PoincareNormalizeTest (tf .test .TestCase ):
29
- def _PoincareNormalize (self , x , dim , epsilon = 1e-5 ):
30
- if isinstance (dim , list ):
31
- norm = np .linalg .norm (x , axis = tuple (dim ))
32
- for d in dim :
33
- norm = np .expand_dims (norm , d )
34
- norm_x = ((1.0 - epsilon ) * x ) / norm
35
- else :
36
- norm = np .expand_dims (np .apply_along_axis (np .linalg .norm , dim , x ), dim )
37
- norm_x = ((1.0 - epsilon ) * x ) / norm
38
- return np .where (norm > 1.0 - epsilon , norm_x , x )
26
+ def _poincare_normalize (x , dim , epsilon = 1e-5 ):
27
+ if isinstance (dim , list ):
28
+ norm = np .linalg .norm (x , axis = tuple (dim ))
29
+ for d in dim :
30
+ norm = np .expand_dims (norm , d )
31
+ norm_x = ((1.0 - epsilon ) * x ) / norm
32
+ else :
33
+ norm = np .expand_dims (np .apply_along_axis (np .linalg .norm , dim , x ), dim )
34
+ norm_x = ((1.0 - epsilon ) * x ) / norm
35
+ return np .where (norm > 1.0 - epsilon , norm_x , x )
39
36
40
- def testPoincareNormalize (self ):
41
- x_shape = [20 , 7 , 3 ]
42
- epsilon = 1e-5
43
- tol = 1e-6
44
- np .random .seed (1 )
45
- inputs = np .random .random_sample (x_shape ).astype (np .float32 )
46
37
47
- for dim in range (len (x_shape )):
48
- outputs_expected = self ._PoincareNormalize (inputs , dim , epsilon )
38
+ @pytest .mark .usefixtures ("maybe_run_functions_eagerly" )
39
+ def test_poincare_normalize ():
40
+ x_shape = [20 , 7 , 3 ]
41
+ epsilon = 1e-5
42
+ tol = 1e-6
43
+ np .random .seed (1 )
44
+ inputs = np .random .random_sample (x_shape ).astype (np .float32 )
49
45
50
- outputs = test_utils .layer_test (
51
- PoincareNormalize ,
52
- kwargs = {"axis" : dim , "epsilon" : epsilon },
53
- input_data = inputs ,
54
- expected_output = outputs_expected ,
55
- )
56
- for y in outputs_expected , outputs :
57
- norm = np .linalg .norm (y , axis = dim )
58
- self .assertLessEqual (norm .max (), 1.0 - epsilon + tol )
59
-
60
- def testPoincareNormalizeDimArray (self ):
61
- x_shape = [20 , 7 , 3 ]
62
- epsilon = 1e-5
63
- tol = 1e-6
64
- np .random .seed (1 )
65
- inputs = np .random .random_sample (x_shape ).astype (np .float32 )
66
- dim = [1 , 2 ]
67
-
68
- outputs_expected = self ._PoincareNormalize (inputs , dim , epsilon )
46
+ for dim in range (len (x_shape )):
47
+ outputs_expected = _poincare_normalize (inputs , dim , epsilon )
69
48
70
49
outputs = test_utils .layer_test (
71
50
PoincareNormalize ,
@@ -74,8 +53,30 @@ def testPoincareNormalizeDimArray(self):
74
53
expected_output = outputs_expected ,
75
54
)
76
55
for y in outputs_expected , outputs :
77
- norm = np .linalg .norm (y , axis = tuple (dim ))
78
- self .assertLessEqual (norm .max (), 1.0 - epsilon + tol )
56
+ norm = np .linalg .norm (y , axis = dim )
57
+ assert norm .max () <= 1.0 - epsilon + tol
58
+
59
+
60
+ @pytest .mark .usefixtures ("maybe_run_functions_eagerly" )
61
+ def test_poincare_normalize_dim_array ():
62
+ x_shape = [20 , 7 , 3 ]
63
+ epsilon = 1e-5
64
+ tol = 1e-6
65
+ np .random .seed (1 )
66
+ inputs = np .random .random_sample (x_shape ).astype (np .float32 )
67
+ dim = [1 , 2 ]
68
+
69
+ outputs_expected = _poincare_normalize (inputs , dim , epsilon )
70
+
71
+ outputs = test_utils .layer_test (
72
+ PoincareNormalize ,
73
+ kwargs = {"axis" : dim , "epsilon" : epsilon },
74
+ input_data = inputs ,
75
+ expected_output = outputs_expected ,
76
+ )
77
+ for y in outputs_expected , outputs :
78
+ norm = np .linalg .norm (y , axis = tuple (dim ))
79
+ assert norm .max () <= 1.0 - epsilon + tol
79
80
80
81
81
82
if __name__ == "__main__" :
0 commit comments