Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/activation/softmax.py: 44%

32 statements  

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

1# Copyright 2015 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"""Softmax activation layer.""" 

16 

17 

18import tensorflow.compat.v2 as tf 

19 

20from keras.src import backend 

21from keras.src.engine.base_layer import Layer 

22from keras.src.utils import tf_utils 

23 

24# isort: off 

25from tensorflow.python.util.tf_export import keras_export 

26 

27 

28def _large_compatible_negative(tensor_type): 

29 """Large negative number as Tensor. 

30 

31 This function is necessary because the standard value for epsilon 

32 in this module (-1e9) cannot be represented using tf.float16 

33 

34 Args: 

35 tensor_type: a dtype to determine the type. 

36 

37 Returns: 

38 a large negative number. 

39 """ 

40 # In case of dtype=float16 (e.g., for mixed-precision), the largest 

41 # negative number (dtypes.float16.min) is divided by 2, in order to 

42 # avoid overflows when summing negative inputs. 

43 if tensor_type == tf.float16: 

44 return tf.float16.min / 2.0 

45 return -1e9 

46 

47 

48@keras_export("keras.layers.Softmax") 

49class Softmax(Layer): 

50 """Softmax activation function. 

51 

52 Example without mask: 

53 

54 >>> inp = np.asarray([1., 2., 1.]) 

55 >>> layer = tf.keras.layers.Softmax() 

56 >>> layer(inp).numpy() 

57 array([0.21194157, 0.5761169 , 0.21194157], dtype=float32) 

58 >>> mask = np.asarray([True, False, True], dtype=bool) 

59 >>> layer(inp, mask).numpy() 

60 array([0.5, 0. , 0.5], dtype=float32) 

61 

62 Input shape: 

63 Arbitrary. Use the keyword argument `input_shape` 

64 (tuple of integers, does not include the samples axis) 

65 when using this layer as the first layer in a model. 

66 

67 Output shape: 

68 Same shape as the input. 

69 

70 Args: 

71 axis: Integer, or list of Integers, axis along which the softmax 

72 normalization is applied. 

73 Call arguments: 

74 inputs: The inputs, or logits to the softmax layer. 

75 mask: A boolean mask of the same shape as `inputs`. The mask 

76 specifies 1 to keep and 0 to mask. Defaults to `None`. 

77 

78 

79 Returns: 

80 Softmaxed output with the same shape as `inputs`. 

81 """ 

82 

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

84 super().__init__(**kwargs) 

85 self.supports_masking = True 

86 self.axis = axis 

87 

88 def call(self, inputs, mask=None): 

89 if mask is not None: 

90 # Since mask is 1.0 for positions we want to keep and 0.0 for masked 

91 # positions, this operation will create a tensor which is 0.0 for 

92 # positions we want to attend and -1e.9 for masked positions. 

93 adder = (1.0 - tf.cast(mask, inputs.dtype)) * ( 

94 _large_compatible_negative(inputs.dtype) 

95 ) 

96 

97 # Since we are adding it to the raw scores before the softmax, this 

98 # is effectively the same as removing these entirely. 

99 inputs += adder 

100 if isinstance(self.axis, (tuple, list)): 

101 if len(self.axis) > 1: 

102 return tf.exp( 

103 inputs 

104 - tf.reduce_logsumexp(inputs, axis=self.axis, keepdims=True) 

105 ) 

106 else: 

107 return backend.softmax(inputs, axis=self.axis[0]) 

108 return backend.softmax(inputs, axis=self.axis) 

109 

110 def get_config(self): 

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

112 base_config = super().get_config() 

113 return dict(list(base_config.items()) + list(config.items())) 

114 

115 @tf_utils.shape_type_conversion 

116 def compute_output_shape(self, input_shape): 

117 return input_shape 

118