Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/reshaping/zero_padding1d.py: 54%
24 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"""Keras zero-padding layer for 1D input."""
18import tensorflow.compat.v2 as tf
20from keras.src import backend
21from keras.src.engine.base_layer import Layer
22from keras.src.engine.input_spec import InputSpec
23from keras.src.utils import conv_utils
25# isort: off
26from tensorflow.python.util.tf_export import keras_export
29@keras_export("keras.layers.ZeroPadding1D")
30class ZeroPadding1D(Layer):
31 """Zero-padding layer for 1D input (e.g. temporal sequence).
33 Examples:
35 >>> input_shape = (2, 2, 3)
36 >>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
37 >>> print(x)
38 [[[ 0 1 2]
39 [ 3 4 5]]
40 [[ 6 7 8]
41 [ 9 10 11]]]
42 >>> y = tf.keras.layers.ZeroPadding1D(padding=2)(x)
43 >>> print(y)
44 tf.Tensor(
45 [[[ 0 0 0]
46 [ 0 0 0]
47 [ 0 1 2]
48 [ 3 4 5]
49 [ 0 0 0]
50 [ 0 0 0]]
51 [[ 0 0 0]
52 [ 0 0 0]
53 [ 6 7 8]
54 [ 9 10 11]
55 [ 0 0 0]
56 [ 0 0 0]]], shape=(2, 6, 3), dtype=int64)
58 Args:
59 padding: Int, or tuple of int (length 2), or dictionary.
60 - If int:
61 How many zeros to add at the beginning and end of
62 the padding dimension (axis 1).
63 - If tuple of int (length 2):
64 How many zeros to add at the beginning and the end of
65 the padding dimension (`(left_pad, right_pad)`).
67 Input shape:
68 3D tensor with shape `(batch_size, axis_to_pad, features)`
70 Output shape:
71 3D tensor with shape `(batch_size, padded_axis, features)`
72 """
74 def __init__(self, padding=1, **kwargs):
75 super().__init__(**kwargs)
76 self.padding = conv_utils.normalize_tuple(
77 padding, 2, "padding", allow_zero=True
78 )
79 self.input_spec = InputSpec(ndim=3)
81 def compute_output_shape(self, input_shape):
82 if input_shape[1] is not None:
83 length = input_shape[1] + self.padding[0] + self.padding[1]
84 else:
85 length = None
86 return tf.TensorShape([input_shape[0], length, input_shape[2]])
88 def call(self, inputs):
89 return backend.temporal_padding(inputs, padding=self.padding)
91 def get_config(self):
92 config = {"padding": self.padding}
93 base_config = super().get_config()
94 return dict(list(base_config.items()) + list(config.items()))