From dcf2ffbfe3ff980834980f7d4b7bf1ef8ca5b9e1 Mon Sep 17 00:00:00 2001 From: zhupengyang Date: Wed, 22 Jul 2020 19:02:58 +0800 Subject: [PATCH 1/3] ones_like API: remove device, input -> x --- .../fluid/tests/unittests/test_ones_like.py | 80 +++++++++++++++++ python/paddle/tensor/creation.py | 85 +++++++------------ 2 files changed, 110 insertions(+), 55 deletions(-) create mode 100644 python/paddle/fluid/tests/unittests/test_ones_like.py diff --git a/python/paddle/fluid/tests/unittests/test_ones_like.py b/python/paddle/fluid/tests/unittests/test_ones_like.py new file mode 100644 index 0000000000000..4e3b3f3edc9f9 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_ones_like.py @@ -0,0 +1,80 @@ +# Copyright (c) 2018 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. + +from __future__ import print_function + +import unittest +import numpy as np +import paddle +import paddle.fluid as fluid +from paddle import ones_like +from paddle.fluid import core, Program, program_guard + + +class TestOnesLikeAPIError(unittest.TestCase): + def test_errors(self): + with program_guard(Program(), Program()): + x = paddle.data('x', [3, 4]) + self.assertRaises(TypeError, ones_like, x, 'int8') + + +class TestOnesLikeAPI(unittest.TestCase): + def test_api(self): + shape = [3, 4] + startup_program = Program() + train_program = Program() + with program_guard(train_program, startup_program): + x = paddle.data('X', shape) + + # 'bool', 'float32', 'float64', 'int32', 'int64' + out1 = ones_like(x) + out2 = ones_like(x, np.bool) + out3 = ones_like(x, 'float64') + out4 = ones_like(x, 'int32') + out5 = ones_like(x, 'int64') + + place = fluid.CUDAPlace(0) if core.is_compiled_with_cuda( + ) else fluid.CPUPlace() + exe = fluid.Executor(place) + outs = exe.run(train_program, + feed={'X': np.ones(shape).astype('float32')}, + fetch_list=[out1, out2, out3, out4, out5]) + + for i, dtype in enumerate( + [np.float32, np.bool, np.float64, np.int32, np.int64]): + self.assertEqual(outs[i].dtype, dtype) + self.assertEqual((outs[i] == np.ones(shape, dtype)).all(), True) + + +class TestOnesLikeImpeartive(unittest.TestCase): + def test_out(self): + shape = [3, 4] + place = fluid.CUDAPlace(0) if core.is_compiled_with_cuda( + ) else fluid.CPUPlace() + with paddle.imperative.guard(place): + x = paddle.imperative.to_variable(np.ones(shape)) + for dtype in [np.bool, np.float32, np.float64, np.int32, np.int64]: + out = ones_like(x, dtype) + self.assertEqual((out.numpy() == np.ones(shape, dtype)).all(), + True) + + out = paddle.tensor.ones_like(x) + self.assertEqual((out.numpy() == np.ones(shape, dtype)).all(), True) + + out = paddle.tensor.creation.ones_like(x) + self.assertEqual((out.numpy() == np.ones(shape, dtype)).all(), True) + + +if __name__ == "__main__": + unittest.main() diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index ed104b5f3e702..4e1fdd5c62d7e 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -164,74 +164,49 @@ def ones(shape, dtype=None, name=None): return fill_constant(value=1.0, shape=shape, dtype=dtype, name=name) -def ones_like(input, dtype=None, device=None, name=None): +def ones_like(x, dtype=None, name=None): """ :alias_main: paddle.ones_like - :alias: paddle.ones_like,paddle.tensor.ones_like,paddle.tensor.creation.ones_like + :alias: paddle.tensor.ones_like, paddle.tensor.creation.ones_like - This function creates a ones tensor which has identical shape and dtype - with `input`. + This OP returns a tensor filled with value 1, with the same shape of ``x``. + The data type of the output is the same as ``x``, or if ``dtype`` is not + None, it is the same as ``dtype``. Args: - input(Variable): The input tensor which specifies shape and dtype.The dtype of input can be - float32, float64, int32, int64. - dtype(np.dtype|core.VarDesc.VarType|str, optional): The data type can be set bool, float32, float64, int32, int64. - The default value is None, the dtype is the same as input. - device(str, optional): Which device to run the operator. The :attr:`device` must be - None, 'cpu', 'gpu'. If :attr:`device` is None, it will be choose the device that the user set in - the paddle program. Default value is None. - name(str, optional): The name of output variable, normally there is no need for user to set this this property. - Default value is None, the framework set the name of output variable. + x(Variable): The input tensor which specifies shape and dtype. The + dtype of ``x`` can be bool, float16, float32, float64, int32, int64. + dtype(np.dtype|core.VarDesc.VarType|str, optional): The data type of + the output tensor. The ``dtype`` supports bool, float16, float32, + float64, int32, int64. If ``dtype`` is None, the dtype is the same + as input. Default is None. + name(str, optional): The default value is None. Normally there is no + need for user to set this property. For more information, please + refer to :ref:`api_guide_Name`. + Returns: - out(Variable): The tensor variable storing the output. + Variable: A tensor filled with the value 1, with the same shape as ``x``. + The data type of the output is the same as ``x``, or if ``dtype`` is + not None, it is the same as ``dtype``. + + Raise: + TypeError: If ``dtype`` is not None and is not one of bool, float16, + float32, float64, int32 or int64. Examples: .. code-block:: python - import paddle - import paddle.fluid as fluid + import paddle + import numpy as np - x = fluid.data(name='x', dtype='float32', shape=[3]) - data = paddle.ones_like(x) # data=[1.0, 1.0, 1.0] - data1 = paddle.ones_like(input=x, device="gpu") data1=[1.0, 1.0. 1.0] + paddle.enable_imperative() - """ + x = paddle.imperative.to_variable(np.array([1,2,3], dtype='float32')) + out1 = paddle.ones_like(x) # [1., 1., 1.] + out2 = paddle.ones_like(x, dtype='int32') # [1, 1, 1] - helper = LayerHelper("zeros_like", **locals()) - - attrs = {"value": 1.0} - var_dtype = None - if dtype is not None: - check_dtype( - dtype, 'create data type', - ['bool', 'float16', 'float32', 'float64', 'int32', 'int64'], - 'zeros_like') - var_dtype = convert_np_dtype_to_dtype_(dtype) - attrs["dtype"] = var_dtype - else: - var_dtype = input.dtype - - out = helper.create_variable_for_type_inference(dtype=var_dtype) - - if device is not None: - if device not in ['cpu', 'gpu']: - raise ValueError( - "The value of 'device' in zeros_op must be cpu or gpu, but received %s." - % (device)) - with fluid.device_guard(device): - helper.append_op( - type='fill_any_like', - inputs={'X': [input]}, - attrs=attrs, - outputs={'Out': [out]}) - return out - helper.append_op( - type='fill_any_like', - inputs={'X': [input]}, - attrs=attrs, - outputs={'Out': [out]}) - out.stop_gradient = True - return out + """ + return full_like(x=x, fill_value=1, dtype=dtype, name=name) def zeros(shape, dtype=None, name=None): From b6ed94440171f30c9735dec26ebf69157edc888c Mon Sep 17 00:00:00 2001 From: zhupengyang Date: Fri, 24 Jul 2020 16:51:50 +0800 Subject: [PATCH 2/3] test=develop --- python/paddle/tensor/creation.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index ff632c6349e63..12b6545d422a3 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -169,29 +169,27 @@ def ones_like(x, dtype=None, name=None): :alias_main: paddle.ones_like :alias: paddle.tensor.ones_like, paddle.tensor.creation.ones_like - This OP returns a tensor filled with the value 1, with the same shape of ``x``. - The data type of the output is the same as ``x``, or if ``dtype`` is not - None, it is the same as ``dtype``. + This OP returns a Tensor filled with the value 0, with the same shape and + data type (use ``dtype`` if ``dtype`` is not None) as ``x``. Args: - x(Variable): The input tensor which specifies shape and dtype. The + x(Tensor): The input tensor which specifies shape and dtype. The dtype of ``x`` can be bool, float16, float32, float64, int32, int64. - dtype(np.dtype|core.VarDesc.VarType|str, optional): The data type of - the output tensor. The ``dtype`` supports bool, float16, float32, - float64, int32, int64. If ``dtype`` is None, the dtype is the same - as input. Default is None. + dtype(str|np.dtype|core.VarDesc.VarType, optional): The data type of the + output tensor. Supported data types: bool, float16, float32, float64, + int32, int64. If ``dtype`` is None, the data type is the same as ``x``. + Default is None. name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: - Variable: A tensor filled with the value 1, with the same shape as ``x``. - The data type of the output is the same as ``x``, or if ``dtype`` is - not None, it is the same as ``dtype``. + Tensor: A Tensor filled with the value 1, with the same shape and + data type (use ``dtype`` if ``dtype`` is not None) as ``x``. Raise: - TypeError: If ``dtype`` is not None and is not one of bool, float16, - float32, float64, int32 or int64. + TypeError: If ``dtype`` is not None and is not bool, float16, float32, + float64, int32 or int64. Examples: .. code-block:: python @@ -202,8 +200,8 @@ def ones_like(x, dtype=None, name=None): paddle.enable_imperative() x = paddle.imperative.to_variable(np.array([1,2,3], dtype='float32')) - out1 = paddle.ones_like(x) # [1., 1., 1.] - out2 = paddle.ones_like(x, dtype='int32') # [1, 1, 1] + out1 = paddle.zeros_like(x) # [1., 1., 1.] + out2 = paddle.zeros_like(x, dtype='int32') # [1, 1, 1] """ return full_like(x=x, fill_value=1, dtype=dtype, name=name) From f617f7d213c12418b80260424574a85d63bd5484 Mon Sep 17 00:00:00 2001 From: zhupengyang Date: Mon, 27 Jul 2020 09:53:44 +0800 Subject: [PATCH 3/3] test=develop --- python/paddle/tensor/creation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 12b6545d422a3..f15980b268934 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -103,7 +103,7 @@ def full_like(x, fill_value, dtype=None, name=None): helper = LayerHelper("full_like", **locals()) check_dtype(dtype, 'dtype', ['bool', 'float16', 'float32', 'float64', 'int32', 'int64'], - 'full_like') + 'full_like/zeros_like/ones_like') out = helper.create_variable_for_type_inference(dtype=dtype) helper.append_op( @@ -169,7 +169,7 @@ def ones_like(x, dtype=None, name=None): :alias_main: paddle.ones_like :alias: paddle.tensor.ones_like, paddle.tensor.creation.ones_like - This OP returns a Tensor filled with the value 0, with the same shape and + This OP returns a Tensor filled with the value 1, with the same shape and data type (use ``dtype`` if ``dtype`` is not None) as ``x``. Args: