Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/pooling/global_average_pooling1d.py: 52%

21 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"""Global average pooling 1D layer.""" 

16 

17 

18import tensorflow.compat.v2 as tf 

19 

20from keras.src import backend 

21from keras.src.layers.pooling.base_global_pooling1d import GlobalPooling1D 

22 

23# isort: off 

24from tensorflow.python.util.tf_export import keras_export 

25 

26 

27@keras_export( 

28 "keras.layers.GlobalAveragePooling1D", "keras.layers.GlobalAvgPool1D" 

29) 

30class GlobalAveragePooling1D(GlobalPooling1D): 

31 """Global average pooling operation for temporal data. 

32 

33 Examples: 

34 

35 >>> input_shape = (2, 3, 4) 

36 >>> x = tf.random.normal(input_shape) 

37 >>> y = tf.keras.layers.GlobalAveragePooling1D()(x) 

38 >>> print(y.shape) 

39 (2, 4) 

40 

41 Args: 

42 data_format: A string, 

43 one of `channels_last` (default) or `channels_first`. 

44 The ordering of the dimensions in the inputs. 

45 `channels_last` corresponds to inputs with shape 

46 `(batch, steps, features)` while `channels_first` 

47 corresponds to inputs with shape 

48 `(batch, features, steps)`. 

49 keepdims: A boolean, whether to keep the temporal dimension or not. 

50 If `keepdims` is `False` (default), the rank of the tensor is reduced 

51 for spatial dimensions. 

52 If `keepdims` is `True`, the temporal dimension are retained with 

53 length 1. 

54 The behavior is the same as for `tf.reduce_mean` or `np.mean`. 

55 

56 Call arguments: 

57 inputs: A 3D tensor. 

58 mask: Binary tensor of shape `(batch_size, steps)` indicating whether 

59 a given step should be masked (excluded from the average). 

60 

61 Input shape: 

62 - If `data_format='channels_last'`: 

63 3D tensor with shape: 

64 `(batch_size, steps, features)` 

65 - If `data_format='channels_first'`: 

66 3D tensor with shape: 

67 `(batch_size, features, steps)` 

68 

69 Output shape: 

70 - If `keepdims`=False: 

71 2D tensor with shape `(batch_size, features)`. 

72 - If `keepdims`=True: 

73 - If `data_format='channels_last'`: 

74 3D tensor with shape `(batch_size, 1, features)` 

75 - If `data_format='channels_first'`: 

76 3D tensor with shape `(batch_size, features, 1)` 

77 """ 

78 

79 def __init__(self, data_format="channels_last", **kwargs): 

80 super().__init__(data_format=data_format, **kwargs) 

81 self.supports_masking = True 

82 

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

84 steps_axis = 1 if self.data_format == "channels_last" else 2 

85 if mask is not None: 

86 mask = tf.cast(mask, inputs[0].dtype) 

87 mask = tf.expand_dims( 

88 mask, 2 if self.data_format == "channels_last" else 1 

89 ) 

90 inputs *= mask 

91 return backend.sum( 

92 inputs, axis=steps_axis, keepdims=self.keepdims 

93 ) / tf.reduce_sum(mask, axis=steps_axis, keepdims=self.keepdims) 

94 else: 

95 return backend.mean(inputs, axis=steps_axis, keepdims=self.keepdims) 

96 

97 def compute_mask(self, inputs, mask=None): 

98 return None 

99 

100 

101# Alias 

102 

103GlobalAvgPool1D = GlobalAveragePooling1D 

104