Skip to content

Commit 80f7f7e

Browse files
authored
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
1 parent 9d54a53 commit 80f7f7e

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) 2021 PaddlePaddle 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+
import unittest
16+
import numpy as np
17+
import paddle.fluid as fluid
18+
import paddle.fluid.core as core
19+
import paddle
20+
21+
22+
class TestIdentityAPI(unittest.TestCase):
23+
def setUp(self):
24+
self.shape = [4, 4]
25+
self.x = np.random.random((4, 4)).astype(np.float32)
26+
self.place = paddle.CPUPlace()
27+
28+
def test_api_static(self):
29+
paddle.enable_static()
30+
with paddle.static.program_guard(paddle.static.Program()):
31+
x = paddle.fluid.data('X', self.shape)
32+
id_layer = paddle.nn.Identity()
33+
out = id_layer(x)
34+
exe = paddle.static.Executor(self.place)
35+
res = exe.run(feed={'X': self.x}, fetch_list=[out])
36+
37+
out_ref = self.x
38+
for out in res:
39+
self.assertEqual(np.allclose(out, out_ref, rtol=1e-08), True)
40+
41+
def test_api_dygraph(self):
42+
paddle.disable_static(self.place)
43+
x_tensor = paddle.to_tensor(self.x)
44+
id_layer = paddle.nn.Identity()
45+
out = id_layer(x_tensor)
46+
47+
out_ref = self.x
48+
self.assertEqual(np.allclose(out.numpy(), out_ref, rtol=1e-08), True)
49+
paddle.enable_static()
50+
51+
52+
if __name__ == "__main__":
53+
unittest.main()

python/paddle/nn/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from .layer.common import CosineSimilarity # noqa: F401
5656
from .layer.common import Embedding # noqa: F401
5757
from .layer.common import Linear # noqa: F401
58+
from .layer.common import Identity # noqa: F401
5859
from .layer.common import Flatten # noqa: F401
5960
from .layer.common import Upsample # noqa: F401
6061
from .layer.common import UpsamplingNearest2D # noqa: F401

python/paddle/nn/layer/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from .common import CosineSimilarity # noqa: F401
3333
from .common import Embedding # noqa: F401
3434
from .common import Linear # noqa: F401
35+
from .common import Identity # noqa: F401
3536
from .common import Flatten # noqa: F401
3637
from .common import Upsample # noqa: F401
3738
from .common import Dropout # noqa: F401

python/paddle/nn/layer/common.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
1+
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -30,6 +30,49 @@ def _npairs(x, n):
3030
return x
3131

3232

33+
class Identity(Layer):
34+
r"""
35+
36+
A placeholder identity operator that is argument-insensitive. For each input :math:`X` ,
37+
the output :math:`Out` is:
38+
39+
.. math::
40+
41+
Out = X
42+
43+
Parameters:
44+
args: any argument (unused)
45+
kwargs: any keyword argument (unused)
46+
47+
Shape:
48+
- input: Multi-dimentional tensor with shape :math:`[batch\_size, n1, n2, ...]` .
49+
- output: Multi-dimentional tensor with shape :math:`[batch\_size, n1, n2, ...]` .
50+
51+
Examples:
52+
.. code-block:: python
53+
54+
import paddle
55+
56+
input_tensor = paddle.randn(shape=[3, 2])
57+
layer = paddle.nn.Identity()
58+
out = layer(input_tensor)
59+
# input_tensor: [[-0.32342386 -1.200079 ]
60+
# [ 0.7979031 -0.90978354]
61+
# [ 0.40597573 1.8095392 ]]
62+
# out: [[-0.32342386 -1.200079 ]
63+
# [ 0.7979031 -0.90978354]
64+
# [ 0.40597573 1.8095392 ]]
65+
66+
67+
"""
68+
69+
def __init__(self, *args, **kwargs):
70+
super(Identity, self).__init__()
71+
72+
def forward(self, input):
73+
return input
74+
75+
3376
class Linear(Layer):
3477
r"""
3578

0 commit comments

Comments
 (0)