diff --git a/python/paddle/fluid/tests/unittests/test_identity_op.py b/python/paddle/fluid/tests/unittests/test_identity_op.py new file mode 100644 index 0000000000000000000000000000000000000000..5c2ff2138ee4042319cb97664456158fd0de0cc8 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_identity_op.py @@ -0,0 +1,53 @@ +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest +import numpy as np +import paddle.fluid as fluid +import paddle.fluid.core as core +import paddle + + +class TestIdentityAPI(unittest.TestCase): + def setUp(self): + self.shape = [4, 4] + self.x = np.random.random((4, 4)).astype(np.float32) + self.place = paddle.CPUPlace() + + def test_api_static(self): + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.fluid.data('X', self.shape) + id_layer = paddle.nn.Identity() + out = id_layer(x) + exe = paddle.static.Executor(self.place) + res = exe.run(feed={'X': self.x}, fetch_list=[out]) + + out_ref = self.x + for out in res: + self.assertEqual(np.allclose(out, out_ref, rtol=1e-08), True) + + def test_api_dygraph(self): + paddle.disable_static(self.place) + x_tensor = paddle.to_tensor(self.x) + id_layer = paddle.nn.Identity() + out = id_layer(x_tensor) + + out_ref = self.x + self.assertEqual(np.allclose(out.numpy(), out_ref, rtol=1e-08), True) + paddle.enable_static() + + +if __name__ == "__main__": + unittest.main() diff --git a/python/paddle/nn/__init__.py b/python/paddle/nn/__init__.py index 8f094877e74b6730ace0bd0222042a7aa2f60b48..394bac51ade3812c46845fde99ccf74feb2a6f84 100644 --- a/python/paddle/nn/__init__.py +++ b/python/paddle/nn/__init__.py @@ -55,6 +55,7 @@ from .layer.common import Pad3D # noqa: F401 from .layer.common import CosineSimilarity # noqa: F401 from .layer.common import Embedding # noqa: F401 from .layer.common import Linear # noqa: F401 +from .layer.common import Identity # noqa: F401 from .layer.common import Flatten # noqa: F401 from .layer.common import Upsample # noqa: F401 from .layer.common import UpsamplingNearest2D # noqa: F401 diff --git a/python/paddle/nn/layer/__init__.py b/python/paddle/nn/layer/__init__.py index 10c2b1e3056f15e2df3141ec2c1e7387eae3048d..1ffd992579ae298e41c5895bde72bf05f7dac7db 100644 --- a/python/paddle/nn/layer/__init__.py +++ b/python/paddle/nn/layer/__init__.py @@ -32,6 +32,7 @@ from .common import Pad3D # noqa: F401 from .common import CosineSimilarity # noqa: F401 from .common import Embedding # noqa: F401 from .common import Linear # noqa: F401 +from .common import Identity # noqa: F401 from .common import Flatten # noqa: F401 from .common import Upsample # noqa: F401 from .common import Dropout # noqa: F401 diff --git a/python/paddle/nn/layer/common.py b/python/paddle/nn/layer/common.py index 9aa8097befc98bfc6bb93f083411a0d4e534bbb5..b88dc4bfe9538d120d21acb909593936adb494e7 100644 --- a/python/paddle/nn/layer/common.py +++ b/python/paddle/nn/layer/common.py @@ -1,10 +1,10 @@ -# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, @@ -30,6 +30,49 @@ def _npairs(x, n): return x +class Identity(Layer): + r""" + + A placeholder identity operator that is argument-insensitive. For each input :math:`X` , + the output :math:`Out` is: + + .. math:: + + Out = X + + Parameters: + args: any argument (unused) + kwargs: any keyword argument (unused) + + Shape: + - input: Multi-dimentional tensor with shape :math:`[batch\_size, n1, n2, ...]` . + - output: Multi-dimentional tensor with shape :math:`[batch\_size, n1, n2, ...]` . + + Examples: + .. code-block:: python + + import paddle + + input_tensor = paddle.randn(shape=[3, 2]) + layer = paddle.nn.Identity() + out = layer(input_tensor) + # input_tensor: [[-0.32342386 -1.200079 ] + # [ 0.7979031 -0.90978354] + # [ 0.40597573 1.8095392 ]] + # out: [[-0.32342386 -1.200079 ] + # [ 0.7979031 -0.90978354] + # [ 0.40597573 1.8095392 ]] + + + """ + + def __init__(self, *args, **kwargs): + super(Identity, self).__init__() + + def forward(self, input): + return input + + class Linear(Layer): r"""