Merge branch 'main' of github.com:keras-team/keras-core
This commit is contained in:
parent
5b7e5f88b2
commit
2a3ef732f6
@ -111,6 +111,7 @@ from keras_core.layers.reshaping.reshape import Reshape
|
|||||||
from keras_core.layers.reshaping.up_sampling1d import UpSampling1D
|
from keras_core.layers.reshaping.up_sampling1d import UpSampling1D
|
||||||
from keras_core.layers.reshaping.up_sampling2d import UpSampling2D
|
from keras_core.layers.reshaping.up_sampling2d import UpSampling2D
|
||||||
from keras_core.layers.reshaping.up_sampling3d import UpSampling3D
|
from keras_core.layers.reshaping.up_sampling3d import UpSampling3D
|
||||||
|
from keras_core.layers.reshaping.zero_padding1d import ZeroPadding1D
|
||||||
from keras_core.layers.reshaping.zero_padding2d import ZeroPadding2D
|
from keras_core.layers.reshaping.zero_padding2d import ZeroPadding2D
|
||||||
from keras_core.layers.reshaping.zero_padding3d import ZeroPadding3D
|
from keras_core.layers.reshaping.zero_padding3d import ZeroPadding3D
|
||||||
from keras_core.layers.rnn.bidirectional import Bidirectional
|
from keras_core.layers.rnn.bidirectional import Bidirectional
|
||||||
|
@ -26,10 +26,12 @@ class Cropping1D(Layer):
|
|||||||
[[8 9]]]
|
[[8 9]]]
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
cropping: Integer or tuple of integers of length 2.
|
cropping: Int, or tuple of int (length 2), or dictionary.
|
||||||
How many units should be trimmed off at the beginning and end of
|
- If int: how many units should be trimmed off at the beginning and
|
||||||
the cropping dimension (axis 1).
|
end of the cropping dimension (axis 1).
|
||||||
If a single int is provided, the same value will be used for both.
|
- If tuple of 2 ints: how many units should be trimmed off at the
|
||||||
|
beginning and end of the cropping dimension
|
||||||
|
(`(left_crop, right_crop)`).
|
||||||
|
|
||||||
Input shape:
|
Input shape:
|
||||||
3D tensor with shape `(batch_size, axis_to_crop, features)`
|
3D tensor with shape `(batch_size, axis_to_crop, features)`
|
||||||
|
69
keras_core/layers/reshaping/zero_padding1d.py
Normal file
69
keras_core/layers/reshaping/zero_padding1d.py
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
from keras_core import operations as ops
|
||||||
|
from keras_core.api_export import keras_core_export
|
||||||
|
from keras_core.layers.input_spec import InputSpec
|
||||||
|
from keras_core.layers.layer import Layer
|
||||||
|
|
||||||
|
|
||||||
|
@keras_core_export("keras_core.layers.ZeroPadding1D")
|
||||||
|
class ZeroPadding1D(Layer):
|
||||||
|
"""Zero-padding layer for 1D input (e.g. temporal sequence).
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
>>> input_shape = (2, 2, 3)
|
||||||
|
>>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
|
||||||
|
>>> x
|
||||||
|
[[[ 0 1 2]
|
||||||
|
[ 3 4 5]]
|
||||||
|
[[ 6 7 8]
|
||||||
|
[ 9 10 11]]]
|
||||||
|
>>> y = keras_core.layers.ZeroPadding1D(padding=2)(x)
|
||||||
|
>>> y
|
||||||
|
[[[ 0 0 0]
|
||||||
|
[ 0 0 0]
|
||||||
|
[ 0 1 2]
|
||||||
|
[ 3 4 5]
|
||||||
|
[ 0 0 0]
|
||||||
|
[ 0 0 0]]
|
||||||
|
[[ 0 0 0]
|
||||||
|
[ 0 0 0]
|
||||||
|
[ 6 7 8]
|
||||||
|
[ 9 10 11]
|
||||||
|
[ 0 0 0]
|
||||||
|
[ 0 0 0]]]
|
||||||
|
|
||||||
|
Args:
|
||||||
|
padding: Int, or tuple of int (length 2), or dictionary.
|
||||||
|
- If int: how many zeros to add at the beginning and end of
|
||||||
|
the padding dimension (axis 1).
|
||||||
|
- If tuple of 2 ints: how many zeros to add at the beginning and the
|
||||||
|
end of the padding dimension (`(left_pad, right_pad)`).
|
||||||
|
|
||||||
|
Input shape:
|
||||||
|
3D tensor with shape `(batch_size, axis_to_pad, features)`
|
||||||
|
|
||||||
|
Output shape:
|
||||||
|
3D tensor with shape `(batch_size, padded_axis, features)`
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, padding=1, name=None, dtype=None):
|
||||||
|
super().__init__(name=name, dtype=dtype)
|
||||||
|
if isinstance(padding, int):
|
||||||
|
padding = (padding, padding)
|
||||||
|
self.padding = padding
|
||||||
|
self.input_spec = InputSpec(ndim=3)
|
||||||
|
|
||||||
|
def compute_output_shape(self, input_shape):
|
||||||
|
output_shape = list(input_shape)
|
||||||
|
if input_shape[1] is not None:
|
||||||
|
input_shape[1] += self.padding[0] + self.padding[1]
|
||||||
|
return tuple(output_shape)
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
all_dims_padding = ((0, 0), self.padding, (0, 0))
|
||||||
|
return ops.pad(inputs, all_dims_padding)
|
||||||
|
|
||||||
|
def get_config(self):
|
||||||
|
config = {"padding": self.padding}
|
||||||
|
base_config = super().get_config()
|
||||||
|
return {**base_config, **config}
|
35
keras_core/layers/reshaping/zero_padding1d_test.py
Normal file
35
keras_core/layers/reshaping/zero_padding1d_test.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import numpy as np
|
||||||
|
import pytest
|
||||||
|
from absl.testing import parameterized
|
||||||
|
|
||||||
|
from keras_core import backend
|
||||||
|
from keras_core import layers
|
||||||
|
from keras_core import testing
|
||||||
|
|
||||||
|
|
||||||
|
class ZeroPadding1DTest(testing.TestCase, parameterized.TestCase):
|
||||||
|
def test_zero_padding_1d(self):
|
||||||
|
inputs = np.random.rand(1, 2, 3)
|
||||||
|
outputs = layers.ZeroPadding1D(padding=(1, 2))(inputs)
|
||||||
|
|
||||||
|
for index in [0, -1, -2]:
|
||||||
|
self.assertAllClose(outputs[:, index, :], 0.0)
|
||||||
|
self.assertAllClose(outputs[:, 1:-2, :], inputs)
|
||||||
|
|
||||||
|
@parameterized.named_parameters(("one_tuple", (2, 2)), ("one_int", 2))
|
||||||
|
def test_zero_padding_1d_with_same_padding(self, padding):
|
||||||
|
inputs = np.random.rand(1, 2, 3)
|
||||||
|
outputs = layers.ZeroPadding1D(padding=padding)(inputs)
|
||||||
|
|
||||||
|
for index in [0, 1, -1, -2]:
|
||||||
|
self.assertAllClose(outputs[:, index, :], 0.0)
|
||||||
|
self.assertAllClose(outputs[:, 2:-2, :], inputs)
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
not backend.DYNAMIC_SHAPES_OK,
|
||||||
|
reason="Backend does not support dynamic shapes",
|
||||||
|
)
|
||||||
|
def test_zero_padding_1d_with_dynamic_spatial_dim(self):
|
||||||
|
input_layer = layers.Input(batch_shape=(1, None, 3))
|
||||||
|
padded = layers.ZeroPadding1D((1, 2))(input_layer)
|
||||||
|
self.assertEqual(padded.shape, (1, None, 3))
|
@ -67,8 +67,8 @@ class ZeroPadding2D(Layer):
|
|||||||
`(batch_size, channels, padded_height, padded_width)`
|
`(batch_size, channels, padded_height, padded_width)`
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, padding=(1, 1), data_format=None, **kwargs):
|
def __init__(self, padding=(1, 1), data_format=None, name=None, dtype=None):
|
||||||
super().__init__(**kwargs)
|
super().__init__(name=name, dtype=dtype)
|
||||||
self.data_format = backend.standardize_data_format(data_format)
|
self.data_format = backend.standardize_data_format(data_format)
|
||||||
if isinstance(padding, int):
|
if isinstance(padding, int):
|
||||||
self.padding = ((padding, padding), (padding, padding))
|
self.padding = ((padding, padding), (padding, padding))
|
||||||
|
Loading…
Reference in New Issue
Block a user