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 API paddle.neg() and paddle.lgamma(), along with some unittests for paddle.neg(). #33248

Merged
merged 10 commits into from
Jun 9, 2021
2 changes: 2 additions & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
from .tensor.math import prod # noqa: F401
from .tensor.math import broadcast_shape # noqa: F401
from .tensor.math import conj # noqa: F401
from .tensor.math import neg # noqa: F401

from .tensor.random import multinomial # noqa: F401
from .tensor.random import standard_normal # noqa: F401
Expand Down Expand Up @@ -422,6 +423,7 @@
'prod',
'broadcast_shape',
'conj',
'neg',
'square',
'divide',
'ceil',
Expand Down
91 changes: 91 additions & 0 deletions python/paddle/fluid/tests/unittests/test_neg_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 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


class TestNegOp(unittest.TestCase):
def setUp(self):
self.init_dtype_type()
self.input = np.random.random((32, 8)).astype(self.dtype)

def init_dtype_type(self):
self.dtype = np.float64

def run_imperative(self):
input = paddle.to_tensor(self.input)
dy_result = paddle.neg(input)
expected_result = np.negative(self.input)
self.assertTrue(np.allclose(dy_result.numpy(), expected_result))

def run_static(self, use_gpu=False):
input = paddle.fluid.data(name='input', shape=[32, 8], dtype=self.dtype)
result = paddle.neg(input)

place = paddle.CUDAPlace(0) if use_gpu else paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
st_result = exe.run(feed={"input": self.input}, fetch_list=[result])
expected_result = np.negative(self.input)
self.assertTrue(np.allclose(st_result[0], expected_result))

def test_cpu(self):
paddle.disable_static(place=paddle.CPUPlace())
self.run_imperative()
paddle.enable_static()

with paddle.static.program_guard(paddle.static.Program()):
self.run_static()

def test_gpu(self):
if not paddle.fluid.core.is_compiled_with_cuda():
return

paddle.disable_static(place=paddle.CUDAPlace(0))
self.run_imperative()
paddle.enable_static()

with paddle.static.program_guard(paddle.static.Program()):
self.run_static(use_gpu=True)


class TestNegOpFp32(TestNegOp):
def init_dtype_type(self):
self.dtype = np.float32


class TestNegOpInt64(TestNegOp):
def init_dtype_type(self):
self.dtype = np.int64


class TestNegOpInt32(TestNegOp):
def init_dtype_type(self):
self.dtype = np.int32


class TestNegOpInt16(TestNegOp):
def init_dtype_type(self):
self.dtype = np.int16


class TestNegOpInt8(TestNegOp):
def init_dtype_type(self):
self.dtype = np.int8


if __name__ == "__main__":
unittest.main()
24 changes: 24 additions & 0 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -2280,3 +2280,27 @@ def conj(x, name=None):

helper.append_op(type='conj', inputs={'X': x}, outputs={'Out': [out]})
return out

def neg(x, name=None):
"""
This function computes the negative of the Tensor elementwisely.

Args:
x (Tensor): Input of neg operator, an N-D Tensor, with data type float32, float64, int8, int16, int32, or int64.
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
out (Tensor): The negative of input. The shape and data type are the same with input.

Examples:
.. code-block:: python

import paddle

x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
out = paddle.neg(x)
print(out)
# [0.4 0.2 -0.1 -0.3]
"""

return layers.scale(x, scale=-1.0, bias=0.0, bias_after_scale=True, act=None, name=name)