Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow_addons/layers/poincare.py: 46%

24 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

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"""Implementing PoincareNormalize layer.""" 

16 

17import tensorflow as tf 

18from typeguard import typechecked 

19from typing import Union, List 

20 

21 

22@tf.keras.utils.register_keras_serializable(package="Addons") 

23class PoincareNormalize(tf.keras.layers.Layer): 

24 """Project into the Poincare ball with `norm <= 1.0 - epsilon`. 

25 

26 See [Poincaré Embeddings for Learning Hierarchical Representations](https://arxiv.org/pdf/1705.08039.pdf), 

27 and [wiki](https://en.wikipedia.org/wiki/Poincare_ball_model). 

28 

29 For a 1-D tensor with `axis = 0`, computes 

30 

31 (x * (1 - epsilon)) / ||x|| if ||x|| > 1 - epsilon 

32 output = 

33 x otherwise 

34 

35 For `x` with more dimensions, independently normalizes each 1-D slice along 

36 dimension `axis`. 

37 

38 Args: 

39 axis: Axis along which to normalize. A scalar or a vector of integers. 

40 epsilon: A small deviation from the edge of the unit sphere for 

41 numerical stability. 

42 """ 

43 

44 @typechecked 

45 def __init__( 

46 self, axis: Union[None, int, List[int]] = 1, epsilon: float = 1e-5, **kwargs 

47 ): 

48 super().__init__(**kwargs) 

49 self.axis = axis 

50 self.epsilon = epsilon 

51 

52 def call(self, inputs): 

53 x = tf.convert_to_tensor(inputs) 

54 square_sum = tf.math.reduce_sum(tf.math.square(x), self.axis, keepdims=True) 

55 x_inv_norm = tf.math.rsqrt(square_sum) 

56 x_inv_norm = tf.math.minimum((1.0 - self.epsilon) * x_inv_norm, 1.0) 

57 outputs = tf.math.multiply(x, x_inv_norm) 

58 return outputs 

59 

60 def compute_output_shape(self, input_shape): 

61 return input_shape 

62 

63 def get_config(self): 

64 config = {"axis": self.axis, "epsilon": self.epsilon} 

65 base_config = super().get_config() 

66 return {**base_config, **config}