Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/pooling/average_pooling1d.py: 90%
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"""Average pooling 1D layer."""
18import functools
20from keras.src import backend
21from keras.src.layers.pooling.base_pooling1d import Pooling1D
23# isort: off
24from tensorflow.python.util.tf_export import keras_export
27@keras_export("keras.layers.AveragePooling1D", "keras.layers.AvgPool1D")
28class AveragePooling1D(Pooling1D):
29 """Average pooling for temporal data.
31 Downsamples the input representation by taking the average value over the
32 window defined by `pool_size`. The window is shifted by `strides`. The
33 resulting output when using "valid" padding option has a shape of:
34 `output_shape = (input_shape - pool_size + 1) / strides)`
36 The resulting output shape when using the "same" padding option is:
37 `output_shape = input_shape / strides`
39 For example, for strides=1 and padding="valid":
41 >>> x = tf.constant([1., 2., 3., 4., 5.])
42 >>> x = tf.reshape(x, [1, 5, 1])
43 >>> x
44 <tf.Tensor: shape=(1, 5, 1), dtype=float32, numpy=
45 array([[[1.],
46 [2.],
47 [3.],
48 [4.],
49 [5.]], dtype=float32)>
50 >>> avg_pool_1d = tf.keras.layers.AveragePooling1D(pool_size=2,
51 ... strides=1, padding='valid')
52 >>> avg_pool_1d(x)
53 <tf.Tensor: shape=(1, 4, 1), dtype=float32, numpy=
54 array([[[1.5],
55 [2.5],
56 [3.5],
57 [4.5]]], dtype=float32)>
59 For example, for strides=2 and padding="valid":
61 >>> x = tf.constant([1., 2., 3., 4., 5.])
62 >>> x = tf.reshape(x, [1, 5, 1])
63 >>> x
64 <tf.Tensor: shape=(1, 5, 1), dtype=float32, numpy=
65 array([[[1.],
66 [2.],
67 [3.],
68 [4.],
69 [5.]], dtype=float32)>
70 >>> avg_pool_1d = tf.keras.layers.AveragePooling1D(pool_size=2,
71 ... strides=2, padding='valid')
72 >>> avg_pool_1d(x)
73 <tf.Tensor: shape=(1, 2, 1), dtype=float32, numpy=
74 array([[[1.5],
75 [3.5]]], dtype=float32)>
77 For example, for strides=1 and padding="same":
79 >>> x = tf.constant([1., 2., 3., 4., 5.])
80 >>> x = tf.reshape(x, [1, 5, 1])
81 >>> x
82 <tf.Tensor: shape=(1, 5, 1), dtype=float32, numpy=
83 array([[[1.],
84 [2.],
85 [3.],
86 [4.],
87 [5.]], dtype=float32)>
88 >>> avg_pool_1d = tf.keras.layers.AveragePooling1D(pool_size=2,
89 ... strides=1, padding='same')
90 >>> avg_pool_1d(x)
91 <tf.Tensor: shape=(1, 5, 1), dtype=float32, numpy=
92 array([[[1.5],
93 [2.5],
94 [3.5],
95 [4.5],
96 [5.]]], dtype=float32)>
98 Args:
99 pool_size: Integer, size of the average pooling windows.
100 strides: Integer, or None. Factor by which to downscale.
101 E.g. 2 will halve the input.
102 If None, it will default to `pool_size`.
103 padding: One of `"valid"` or `"same"` (case-insensitive).
104 `"valid"` means no padding. `"same"` results in padding evenly to
105 the left/right or up/down of the input such that output has the same
106 height/width dimension as the input.
107 data_format: A string,
108 one of `channels_last` (default) or `channels_first`.
109 The ordering of the dimensions in the inputs.
110 `channels_last` corresponds to inputs with shape
111 `(batch, steps, features)` while `channels_first`
112 corresponds to inputs with shape
113 `(batch, features, steps)`.
115 Input shape:
116 - If `data_format='channels_last'`:
117 3D tensor with shape `(batch_size, steps, features)`.
118 - If `data_format='channels_first'`:
119 3D tensor with shape `(batch_size, features, steps)`.
121 Output shape:
122 - If `data_format='channels_last'`:
123 3D tensor with shape `(batch_size, downsampled_steps, features)`.
124 - If `data_format='channels_first'`:
125 3D tensor with shape `(batch_size, features, downsampled_steps)`.
126 """
128 def __init__(
129 self,
130 pool_size=2,
131 strides=None,
132 padding="valid",
133 data_format="channels_last",
134 **kwargs
135 ):
136 super().__init__(
137 functools.partial(backend.pool2d, pool_mode="avg"),
138 pool_size=pool_size,
139 strides=strides,
140 padding=padding,
141 data_format=data_format,
142 **kwargs
143 )
146# Alias
148AvgPool1D = AveragePooling1D