未验证 提交 80f7f7ea 编写于 作者: S shiyutang 提交者: GitHub

Add Identity OP (#34420)

* test=develop

* update identity

* add unittest

* notest,test=mac_py3

* modify comment & testname

* test=document_fix

* update comment

* test=document_fix

* activate all of the CI
上级 9d54a531
# 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()
......@@ -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
......
......@@ -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
......
# 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"""
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册