|
| 1 | +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Tests for PoincareNormalize layer.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import numpy as np |
| 22 | + |
| 23 | +from tensorflow.python.keras import testing_utils |
| 24 | +from tensorflow.python.platform import test |
| 25 | +from tensorflow_addons.layers.python.poincare import PoincareNormalize |
| 26 | + |
| 27 | + |
| 28 | +class PoincareNormalizeTest(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. - epsilon) * x) / norm |
| 35 | + else: |
| 36 | + norm = np.expand_dims( |
| 37 | + np.apply_along_axis(np.linalg.norm, dim, x), dim) |
| 38 | + norm_x = ((1. - epsilon) * x) / norm |
| 39 | + return np.where(norm > 1.0 - epsilon, norm_x, x) |
| 40 | + |
| 41 | + def testPoincareNormalize(self): |
| 42 | + x_shape = [20, 7, 3] |
| 43 | + epsilon = 1e-5 |
| 44 | + tol = 1e-6 |
| 45 | + np.random.seed(1) |
| 46 | + inputs = np.random.random_sample(x_shape).astype(np.float32) |
| 47 | + |
| 48 | + for dim in range(len(x_shape)): |
| 49 | + outputs_expected = self._PoincareNormalize(inputs, dim, epsilon) |
| 50 | + |
| 51 | + outputs = testing_utils.layer_test( |
| 52 | + PoincareNormalize, |
| 53 | + kwargs={ |
| 54 | + 'axis': dim, |
| 55 | + 'epsilon': epsilon |
| 56 | + }, |
| 57 | + input_data=inputs, |
| 58 | + expected_output=outputs_expected) |
| 59 | + for y in outputs_expected, outputs: |
| 60 | + norm = np.linalg.norm(y, axis=dim) |
| 61 | + self.assertLessEqual(norm.max(), 1. - epsilon + tol) |
| 62 | + |
| 63 | + def testPoincareNormalizeDimArray(self): |
| 64 | + x_shape = [20, 7, 3] |
| 65 | + epsilon = 1e-5 |
| 66 | + tol = 1e-6 |
| 67 | + np.random.seed(1) |
| 68 | + inputs = np.random.random_sample(x_shape).astype(np.float32) |
| 69 | + dim = [1, 2] |
| 70 | + |
| 71 | + outputs_expected = self._PoincareNormalize(inputs, dim, epsilon) |
| 72 | + |
| 73 | + outputs = testing_utils.layer_test( |
| 74 | + PoincareNormalize, |
| 75 | + kwargs={ |
| 76 | + 'axis': dim, |
| 77 | + 'epsilon': epsilon |
| 78 | + }, |
| 79 | + input_data=inputs, |
| 80 | + expected_output=outputs_expected) |
| 81 | + for y in outputs_expected, outputs: |
| 82 | + norm = np.linalg.norm(y, axis=tuple(dim)) |
| 83 | + self.assertLessEqual(norm.max(), 1. - epsilon + tol) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == '__main__': |
| 87 | + test.main() |
0 commit comments