Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/reshaping/up_sampling1d.py: 52%
23 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 upsampling layer for 1D inputs."""
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
24# isort: off
25from tensorflow.python.util.tf_export import keras_export
28@keras_export("keras.layers.UpSampling1D")
29class UpSampling1D(Layer):
30 """Upsampling layer for 1D inputs.
32 Repeats each temporal step `size` times along the time axis.
34 Examples:
36 >>> input_shape = (2, 2, 3)
37 >>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
38 >>> print(x)
39 [[[ 0 1 2]
40 [ 3 4 5]]
41 [[ 6 7 8]
42 [ 9 10 11]]]
43 >>> y = tf.keras.layers.UpSampling1D(size=2)(x)
44 >>> print(y)
45 tf.Tensor(
46 [[[ 0 1 2]
47 [ 0 1 2]
48 [ 3 4 5]
49 [ 3 4 5]]
50 [[ 6 7 8]
51 [ 6 7 8]
52 [ 9 10 11]
53 [ 9 10 11]]], shape=(2, 4, 3), dtype=int64)
55 Args:
56 size: Integer. Upsampling factor.
58 Input shape:
59 3D tensor with shape: `(batch_size, steps, features)`.
61 Output shape:
62 3D tensor with shape: `(batch_size, upsampled_steps, features)`.
63 """
65 def __init__(self, size=2, **kwargs):
66 super().__init__(**kwargs)
67 self.size = int(size)
68 self.input_spec = InputSpec(ndim=3)
70 def compute_output_shape(self, input_shape):
71 input_shape = tf.TensorShape(input_shape).as_list()
72 size = (
73 self.size * input_shape[1] if input_shape[1] is not None else None
74 )
75 return tf.TensorShape([input_shape[0], size, input_shape[2]])
77 def call(self, inputs):
78 output = backend.repeat_elements(inputs, self.size, axis=1)
79 return output
81 def get_config(self):
82 config = {"size": self.size}
83 base_config = super().get_config()
84 return dict(list(base_config.items()) + list(config.items()))