Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/pooling/global_max_pooling1d.py: 80%
10 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« 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 max pooling 1D layer."""
18from keras.src import backend
19from keras.src.layers.pooling.base_global_pooling1d import GlobalPooling1D
21# isort: off
22from tensorflow.python.util.tf_export import keras_export
25@keras_export("keras.layers.GlobalMaxPooling1D", "keras.layers.GlobalMaxPool1D")
26class GlobalMaxPooling1D(GlobalPooling1D):
27 """Global max pooling operation for 1D temporal data.
29 Downsamples the input representation by taking the maximum value over
30 the time dimension.
32 For example:
34 >>> x = tf.constant([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]])
35 >>> x = tf.reshape(x, [3, 3, 1])
36 >>> x
37 <tf.Tensor: shape=(3, 3, 1), dtype=float32, numpy=
38 array([[[1.], [2.], [3.]],
39 [[4.], [5.], [6.]],
40 [[7.], [8.], [9.]]], dtype=float32)>
41 >>> max_pool_1d = tf.keras.layers.GlobalMaxPooling1D()
42 >>> max_pool_1d(x)
43 <tf.Tensor: shape=(3, 1), dtype=float32, numpy=
44 array([[3.],
45 [6.],
46 [9.], dtype=float32)>
48 Args:
49 data_format: A string,
50 one of `channels_last` (default) or `channels_first`.
51 The ordering of the dimensions in the inputs.
52 `channels_last` corresponds to inputs with shape
53 `(batch, steps, features)` while `channels_first`
54 corresponds to inputs with shape
55 `(batch, features, steps)`.
56 keepdims: A boolean, whether to keep the temporal dimension or not.
57 If `keepdims` is `False` (default), the rank of the tensor is reduced
58 for spatial dimensions.
59 If `keepdims` is `True`, the temporal dimension are retained with
60 length 1.
61 The behavior is the same as for `tf.reduce_max` or `np.max`.
63 Input shape:
64 - If `data_format='channels_last'`:
65 3D tensor with shape:
66 `(batch_size, steps, features)`
67 - If `data_format='channels_first'`:
68 3D tensor with shape:
69 `(batch_size, features, steps)`
71 Output shape:
72 - If `keepdims`=False:
73 2D tensor with shape `(batch_size, features)`.
74 - If `keepdims`=True:
75 - If `data_format='channels_last'`:
76 3D tensor with shape `(batch_size, 1, features)`
77 - If `data_format='channels_first'`:
78 3D tensor with shape `(batch_size, features, 1)`
79 """
81 def call(self, inputs):
82 steps_axis = 1 if self.data_format == "channels_last" else 2
83 return backend.max(inputs, axis=steps_axis, keepdims=self.keepdims)
86# Alias
88GlobalMaxPool1D = GlobalMaxPooling1D