Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Identity OP #34420

Merged
merged 10 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions python/paddle/fluid/tests/unittests/test_identity_op.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions python/paddle/nn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
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
Expand Down
1 change: 1 addition & 0 deletions python/paddle/nn/layer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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
Expand Down
48 changes: 47 additions & 1 deletion python/paddle/nn/layer/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 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.
Expand Down Expand Up @@ -30,6 +30,52 @@ def _npairs(x, n):
return x


class Identity(Layer):
r"""

A placeholder identity operator that is argument-insensitive. For each input :math:`X` ,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只支持一个输入,一个输出吧。
这里用for each,感觉是支持多个输入的。

the output :math:`Out` is:

.. math::

Out = X

Parameters:
Any parameters which is not used

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
layer = paddle.nn.Identity(54)
shiyutang marked this conversation as resolved.
Show resolved Hide resolved
input_tensor = paddle.to_tensor(paddle.randn(shape=[3, 2]))
shiyutang marked this conversation as resolved.
Show resolved Hide resolved
input_tensor.stop_gradient=False
input_tensor = input_tensor+1
input_tensor.register_hook(lambda grad: print('input grad', grad))
print('input_tensor.grad', input_tensor.grad)
out = m(input_tensor)
shiyutang marked this conversation as resolved.
Show resolved Hide resolved
# 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 ]]
out.backward()
print(out.shape, paddle.sum(input_tensor), paddle.sum(out))

"""

def __init__(self, *args, **kwargs):
super(Identity, self).__init__()

def forward(self, input):
return input


class Linear(Layer):
r"""

Expand Down