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

18 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 

16import tensorflow as tf 

17from tensorflow_addons.activations.sparsemax import sparsemax 

18from typeguard import typechecked 

19 

20 

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

22class Sparsemax(tf.keras.layers.Layer): 

23 """Sparsemax activation function. 

24 

25 The output shape is the same as the input shape. 

26 

27 See [From Softmax to Sparsemax: A Sparse Model of Attention and Multi-Label Classification](https://arxiv.org/abs/1602.02068). 

28 

29 Args: 

30 axis: Integer, axis along which the sparsemax normalization is applied. 

31 """ 

32 

33 @typechecked 

34 def __init__(self, axis: int = -1, **kwargs): 

35 super().__init__(**kwargs) 

36 self.supports_masking = True 

37 self.axis = axis 

38 

39 def call(self, inputs): 

40 return sparsemax(inputs, axis=self.axis) 

41 

42 def get_config(self): 

43 config = {"axis": self.axis} 

44 base_config = super().get_config() 

45 return {**base_config, **config} 

46 

47 def compute_output_shape(self, input_shape): 

48 return input_shape