Skip to content

Commit 91f67b8

Browse files
Removed run_all_in_eager_and_graph_mode from pointcare_test. (#1336)
1 parent 8dc7ebc commit 91f67b8

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

tensorflow_addons/layers/poincare_test.py

+43-42
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,33 @@
1818

1919
import pytest
2020
import numpy as np
21-
import tensorflow as tf
2221

2322
from tensorflow_addons.layers.poincare import PoincareNormalize
2423
from tensorflow_addons.utils import test_utils
2524

2625

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)
3936

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)
4637

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)
4945

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)
6948

7049
outputs = test_utils.layer_test(
7150
PoincareNormalize,
@@ -74,8 +53,30 @@ def testPoincareNormalizeDimArray(self):
7453
expected_output=outputs_expected,
7554
)
7655
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
7980

8081

8182
if __name__ == "__main__":

0 commit comments

Comments
 (0)