Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/pooling/global_max_pooling2d.py: 73%
11 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 2D layer."""
18from keras.src import backend
19from keras.src.layers.pooling.base_global_pooling2d import GlobalPooling2D
21# isort: off
22from tensorflow.python.util.tf_export import keras_export
25@keras_export("keras.layers.GlobalMaxPooling2D", "keras.layers.GlobalMaxPool2D")
26class GlobalMaxPooling2D(GlobalPooling2D):
27 """Global max pooling operation for spatial data.
29 Examples:
31 >>> input_shape = (2, 4, 5, 3)
32 >>> x = tf.random.normal(input_shape)
33 >>> y = tf.keras.layers.GlobalMaxPooling2D()(x)
34 >>> print(y.shape)
35 (2, 3)
37 Args:
38 data_format: A string,
39 one of `channels_last` (default) or `channels_first`.
40 The ordering of the dimensions in the inputs.
41 `channels_last` corresponds to inputs with shape
42 `(batch, height, width, channels)` while `channels_first`
43 corresponds to inputs with shape
44 `(batch, channels, height, width)`.
45 It defaults to the `image_data_format` value found in your
46 Keras config file at `~/.keras/keras.json`.
47 If you never set it, then it will be "channels_last".
48 keepdims: A boolean, whether to keep the spatial dimensions or not.
49 If `keepdims` is `False` (default), the rank of the tensor is reduced
50 for spatial dimensions.
51 If `keepdims` is `True`, the spatial dimensions are retained with
52 length 1.
53 The behavior is the same as for `tf.reduce_max` or `np.max`.
55 Input shape:
56 - If `data_format='channels_last'`:
57 4D tensor with shape `(batch_size, rows, cols, channels)`.
58 - If `data_format='channels_first'`:
59 4D tensor with shape `(batch_size, channels, rows, cols)`.
61 Output shape:
62 - If `keepdims`=False:
63 2D tensor with shape `(batch_size, channels)`.
64 - If `keepdims`=True:
65 - If `data_format='channels_last'`:
66 4D tensor with shape `(batch_size, 1, 1, channels)`
67 - If `data_format='channels_first'`:
68 4D tensor with shape `(batch_size, channels, 1, 1)`
69 """
71 def call(self, inputs):
72 if self.data_format == "channels_last":
73 return backend.max(inputs, axis=[1, 2], keepdims=self.keepdims)
74 else:
75 return backend.max(inputs, axis=[2, 3], keepdims=self.keepdims)
78# Alias
80GlobalMaxPool2D = GlobalMaxPooling2D