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

10 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"""Max pooling 1D layer.""" 

16 

17 

18import functools 

19 

20from keras.src import backend 

21from keras.src.layers.pooling.base_pooling1d import Pooling1D 

22 

23# isort: off 

24from tensorflow.python.util.tf_export import keras_export 

25 

26 

27@keras_export("keras.layers.MaxPooling1D", "keras.layers.MaxPool1D") 

28class MaxPooling1D(Pooling1D): 

29 """Max pooling operation for 1D temporal data. 

30 

31 Downsamples the input representation by taking the maximum value over a 

32 spatial window of size `pool_size`. The window is shifted by `strides`. The 

33 resulting output, when using the `"valid"` padding option, has a shape of: 

34 `output_shape = (input_shape - pool_size + 1) / strides)` 

35 

36 The resulting output shape when using the `"same"` padding option is: 

37 `output_shape = input_shape / strides` 

38 

39 For example, for `strides=1` and `padding="valid"`: 

40 

41 >>> x = tf.constant([1., 2., 3., 4., 5.]) 

42 >>> x = tf.reshape(x, [1, 5, 1]) 

43 >>> max_pool_1d = tf.keras.layers.MaxPooling1D(pool_size=2, 

44 ... strides=1, padding='valid') 

45 >>> max_pool_1d(x) 

46 <tf.Tensor: shape=(1, 4, 1), dtype=float32, numpy= 

47 array([[[2.], 

48 [3.], 

49 [4.], 

50 [5.]]], dtype=float32)> 

51 

52 For example, for `strides=2` and `padding="valid"`: 

53 

54 >>> x = tf.constant([1., 2., 3., 4., 5.]) 

55 >>> x = tf.reshape(x, [1, 5, 1]) 

56 >>> max_pool_1d = tf.keras.layers.MaxPooling1D(pool_size=2, 

57 ... strides=2, padding='valid') 

58 >>> max_pool_1d(x) 

59 <tf.Tensor: shape=(1, 2, 1), dtype=float32, numpy= 

60 array([[[2.], 

61 [4.]]], dtype=float32)> 

62 

63 For example, for `strides=1` and `padding="same"`: 

64 

65 >>> x = tf.constant([1., 2., 3., 4., 5.]) 

66 >>> x = tf.reshape(x, [1, 5, 1]) 

67 >>> max_pool_1d = tf.keras.layers.MaxPooling1D(pool_size=2, 

68 ... strides=1, padding='same') 

69 >>> max_pool_1d(x) 

70 <tf.Tensor: shape=(1, 5, 1), dtype=float32, numpy= 

71 array([[[2.], 

72 [3.], 

73 [4.], 

74 [5.], 

75 [5.]]], dtype=float32)> 

76 

77 Args: 

78 pool_size: Integer, size of the max pooling window. 

79 strides: Integer, or None. Specifies how much the pooling window moves 

80 for each pooling step. 

81 If None, it will default to `pool_size`. 

82 padding: One of `"valid"` or `"same"` (case-insensitive). 

83 `"valid"` means no padding. `"same"` results in padding evenly to 

84 the left/right or up/down of the input such that output has the same 

85 height/width dimension as the input. 

86 data_format: A string, 

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

88 The ordering of the dimensions in the inputs. 

89 `channels_last` corresponds to inputs with shape 

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

91 corresponds to inputs with shape 

92 `(batch, features, steps)`. 

93 

94 Input shape: 

95 - If `data_format='channels_last'`: 

96 3D tensor with shape `(batch_size, steps, features)`. 

97 - If `data_format='channels_first'`: 

98 3D tensor with shape `(batch_size, features, steps)`. 

99 

100 Output shape: 

101 - If `data_format='channels_last'`: 

102 3D tensor with shape `(batch_size, downsampled_steps, features)`. 

103 - If `data_format='channels_first'`: 

104 3D tensor with shape `(batch_size, features, downsampled_steps)`. 

105 """ 

106 

107 def __init__( 

108 self, 

109 pool_size=2, 

110 strides=None, 

111 padding="valid", 

112 data_format="channels_last", 

113 **kwargs 

114 ): 

115 

116 super().__init__( 

117 functools.partial(backend.pool2d, pool_mode="max"), 

118 pool_size=pool_size, 

119 strides=strides, 

120 padding=padding, 

121 data_format=data_format, 

122 **kwargs 

123 ) 

124 

125 

126# Alias 

127 

128MaxPool1D = MaxPooling1D 

129