From e5205ba34c5cd40d8e2eb7bfa0688490356341fd Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Wed, 8 Jul 2020 07:20:30 +0000 Subject: [PATCH 01/18] refine the ones Op test=develop --- python/paddle/tensor/creation.py | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 8208629781bb0..be21c8e2a60af 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -210,7 +210,7 @@ def linspace(start, stop, num, dtype, out=None, device=None, name=None): return out -def ones(shape, dtype=None, out=None, device=None): +def ones(shape, dtype=None, name=None): """ :alias_main: paddle.ones :alias: paddle.ones,paddle.tensor.ones,paddle.tensor.creation.ones @@ -219,14 +219,10 @@ def ones(shape, dtype=None, out=None, device=None): Args: shape(tuple|list): Shape of output tensor. - dtype(np.dtype|core.VarDesc.VarType|str): Data type of output tensor, it supports - bool, float16, float32, float64, int32 and int64. - out(Variable, optional): Optional output which can be any created - Variable that meets the requirements to store the result of operation. - if out is None, a new Varibale will be create to store the result. - 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 False. + dtype(np.dtype|core.VarDesc.VarType|str, optional): Data type of output tensor, it supports + bool, float16, float32, float64, int32 and int64. Default: if None, the data type is 'float32'. + 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. Returns: Variable: A tensor of data type :attr:`dtype` with shape :attr:`shape` and all elements set to 1. @@ -235,21 +231,14 @@ def ones(shape, dtype=None, out=None, device=None): .. code-block:: python import paddle + + paddle.enable_imperative() data = paddle.ones(shape=[3, 2], dtype='float32') # [[1., 1.], [1., 1.], [1., 1.]] data = paddle.ones(shape=[2, 2], dtype='float32', device='cpu') # [[1., 1.], [1., 1.]] """ - check_dtype(dtype, 'create data type', - ['bool', 'float16', 'float32', 'float64', 'int32', 'int64'], - 'zeros') - - 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): - return fill_constant(value=1.0, shape=shape, dtype=dtype, out=out) - return fill_constant(value=1.0, shape=shape, dtype=dtype, out=out) + if dtype is None: + dtype = 'float32' + return fill_constant(value=1.0, shape=shape, dtype=dtype, name=name) def ones_like(input, dtype=None, device=None, name=None): From dfcac0639f417381fa6e2ee418917ab879408200 Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Mon, 13 Jul 2020 06:39:50 +0000 Subject: [PATCH 02/18] refine the ones for API 2.0 test=develop --- .../fluid/tests/unittests/test_ones_op.py | 47 +++++++++++-------- python/paddle/tensor/creation.py | 2 +- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_ones_op.py b/python/paddle/fluid/tests/unittests/test_ones_op.py index 6061bfcff442e..49621911ec9d5 100644 --- a/python/paddle/fluid/tests/unittests/test_ones_op.py +++ b/python/paddle/fluid/tests/unittests/test_ones_op.py @@ -26,27 +26,36 @@ class ApiOnesTest(unittest.TestCase): - def test_out(self): - with fluid.program_guard(fluid.Program()): + def test_paddle_ones(self): + with paddle.program_guard(paddle.Program()): ones = paddle.ones(shape=[10], dtype="float64") - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = paddle.CPUPlace() + exe = paddle.Executor(place) result, = exe.run(fetch_list=[ones]) expected_result = np.ones(10, dtype="float64") self.assertEqual((result == expected_result).all(), True) - with fluid.program_guard(fluid.Program()): + with paddle.program_guard(paddle.Program()): + ones = paddle.ones(shape=[10], dtype="float64") + place = paddle.CUDAPlace(0) + exe = paddle.Executor(place) + result, = exe.run(fetch_list=[ones]) + expected_result = np.ones(10, dtype="float64") + self.assertEqual((result == expected_result).all(), True) + + with paddle.program_guard(paddle.Program()): ones = paddle.ones(shape=[10], dtype="int64") - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = paddle.CPUPlace() + exe = paddle.Executor(place) result, = exe.run(fetch_list=[ones]) expected_result = np.ones(10, dtype="int64") self.assertEqual((result == expected_result).all(), True) - with fluid.program_guard(fluid.Program()): - ones = paddle.ones(shape=[10], dtype="int64", device="cpu") - place = fluid.CPUPlace() - exe = fluid.Executor(place) + def test_fluid_ones(self): + with paddle.program_guard(paddle.Program()): + ones = fluid.layers.ones(shape=[10], dtype="int64") + place = paddle.CPUPlace() + exe = paddle.Executor(place) result, = exe.run(fetch_list=[ones]) expected_result = np.ones(10, dtype="int64") self.assertEqual((result == expected_result).all(), True) @@ -55,25 +64,25 @@ def test_out(self): class ApiOnesZerosError(unittest.TestCase): def test_errors(self): def test_error1(): - with fluid.program_guard(fluid.Program()): - ones = paddle.ones(shape=10, dtype="int64", device="opu") + with paddle.program_guard(paddle.Program()): + ones = paddle.ones(shape=10, dtype="int64") - self.assertRaises(ValueError, test_error1) + self.assertRaises(TypeError, test_error1) def test_error2(): - with fluid.program_guard(fluid.Program()): - ones = paddle.ones(shape=10, dtype="int64", device="opu") + with paddle.program_guard(paddle.Program()): + ones = paddle.ones(shape=10) - self.assertRaises(ValueError, test_error2) + self.assertRaises(TypeError, test_error2) def test_error3(): - with fluid.program_guard(fluid.Program()): + with paddle.program_guard(paddle.Program()): ones = fluid.layers.ones(shape=10, dtype="int64") self.assertRaises(TypeError, test_error3) def test_error4(): - with fluid.program_guard(fluid.Program()): + with paddle.program_guard(paddle.Program()): ones = fluid.layers.ones(shape=[10], dtype="int8") self.assertRaises(TypeError, test_error4) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 032d503286256..7f2786a49ca6e 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -135,7 +135,7 @@ def ones(shape, dtype=None, name=None): paddle.enable_imperative() data = paddle.ones(shape=[3, 2], dtype='float32') # [[1., 1.], [1., 1.], [1., 1.]] - data = paddle.ones(shape=[2, 2], dtype='float32', device='cpu') # [[1., 1.], [1., 1.]] + data = paddle.ones(shape=[2, 2], dtype='int32', name="ones") # [[1, 1], [1, 1]] """ if dtype is None: dtype = 'float32' From 4c731d63c3e65e7715ff09f353216cf33896dfa7 Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Tue, 14 Jul 2020 02:59:11 +0000 Subject: [PATCH 03/18] refine the unittest test=develop --- .../tests/unittests/test_fill_constant_op.py | 20 ----------- .../fluid/tests/unittests/test_zeros_op.py | 35 ++++++++++++++----- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py index 0bd3516e48d2c..2ca3729306e1b 100644 --- a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py +++ b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py @@ -83,26 +83,6 @@ def test_check_output(self): self.check_output() -class TestFillConstantOp5(unittest.TestCase): - def test_errors(self): - with program_guard(Program()): - out_np = np.zeros(shape=(1), dtype='float32') - out = paddle.zeros(shape=[1], dtype="float32") - place = fluid.CPUPlace() - exe = fluid.Executor(place) - result = exe.run(fetch_list=[out]) - self.assertEqual((result == out_np).all(), True) - with program_guard(Program()): - data = fluid.data(name="X", shape=[1], dtype="float32") - out = paddle.ones(shape=[1], out=data, dtype="float32") - place = fluid.CPUPlace() - exe = fluid.Executor(place) - result = exe.run(feed={"X": np.array( - [0.1], dtype="float32")}, - fetch_list=[data, out]) - self.assertEqual(result[0], result[1]) - - class TestFillConstantOpWithSelectedRows(unittest.TestCase): def check_with_place(self, place): scope = core.Scope() diff --git a/python/paddle/fluid/tests/unittests/test_zeros_op.py b/python/paddle/fluid/tests/unittests/test_zeros_op.py index b7f7d93418342..0cf51a87cf6b8 100644 --- a/python/paddle/fluid/tests/unittests/test_zeros_op.py +++ b/python/paddle/fluid/tests/unittests/test_zeros_op.py @@ -36,26 +36,43 @@ def test_errors(self): class ApiZerosTest(unittest.TestCase): def test_out(self): - with paddle.program_guard(fluid.Program()): + with program_guard(Program()): zeros = paddle.zeros(shape=[10], dtype="float64") - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = paddle.CPUPlace() + exe = paddle.Executor(place) result, = exe.run(fetch_list=[zeros]) expected_result = np.zeros(10, dtype="float64") self.assertEqual((result == expected_result).all(), True) - with paddle.program_guard(fluid.Program()): + with paddle.program_guard(Program()): zeros = paddle.zeros(shape=[10], dtype="int64") - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = paddle.CPUPlace() + exe = paddle.Executor(place) result, = exe.run(fetch_list=[zeros]) expected_result = np.zeros(10, dtype="int64") self.assertEqual((result == expected_result).all(), True) - with paddle.program_guard(fluid.Program()): + with program_guard(Program()): zeros = paddle.zeros(shape=[10], dtype="int64") - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = paddle.CPUPlace() + exe = paddle.Executor(place) + result, = exe.run(fetch_list=[zeros]) + expected_result = np.zeros(10, dtype="int64") + self.assertEqual((result == expected_result).all(), True) + + with program_guard(Program()): + out_np = np.zeros(shape=(1), dtype='float32') + out = paddle.zeros(shape=[1], dtype="float32") + place = paddle.CPUPlace() + exe = paddle.Executor(place) + result = exe.run(fetch_list=[out]) + self.assertEqual((result == out_np).all(), True) + + def test_fluid_out(self): + with program_guard(Program()): + zeros = fluid.layers.zeros(shape=[10], dtype="int64") + place = paddle.CPUPlace() + exe = paddle.Executor(place) result, = exe.run(fetch_list=[zeros]) expected_result = np.zeros(10, dtype="int64") self.assertEqual((result == expected_result).all(), True) From c763da3058567c9038302d1588adf22b942dc0ad Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Tue, 14 Jul 2020 03:03:00 +0000 Subject: [PATCH 04/18] fix the testcase test=develop --- python/paddle/fluid/tests/unittests/test_ones_op.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_ones_op.py b/python/paddle/fluid/tests/unittests/test_ones_op.py index 49621911ec9d5..94a23b32aa8ea 100644 --- a/python/paddle/fluid/tests/unittests/test_ones_op.py +++ b/python/paddle/fluid/tests/unittests/test_ones_op.py @@ -37,7 +37,7 @@ def test_paddle_ones(self): with paddle.program_guard(paddle.Program()): ones = paddle.ones(shape=[10], dtype="float64") - place = paddle.CUDAPlace(0) + place = paddle.CPUPlace() exe = paddle.Executor(place) result, = exe.run(fetch_list=[ones]) expected_result = np.ones(10, dtype="float64") From df4f583b5c0bcaf431ddcf2b69a04add0870fc9b Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Tue, 14 Jul 2020 12:26:44 +0000 Subject: [PATCH 05/18] refine the doc test=develop --- python/paddle/tensor/creation.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 7f2786a49ca6e..840fb5dc93e7e 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -119,12 +119,11 @@ def ones(shape, dtype=None, name=None): The OP creates a tensor of specified :attr:`shape` and :attr:`dtype`, and fills it with 1. Args: - shape(tuple|list): Shape of output tensor. + shape(tuple|list|Variable): Shape of output tensor. dtype(np.dtype|core.VarDesc.VarType|str, optional): Data type of output tensor, it supports bool, float16, float32, float64, int32 and int64. Default: if None, the data type is 'float32'. - 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. - + 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 of data type :attr:`dtype` with shape :attr:`shape` and all elements set to 1. @@ -134,8 +133,10 @@ def ones(shape, dtype=None, name=None): import paddle paddle.enable_imperative() - data = paddle.ones(shape=[3, 2], dtype='float32') # [[1., 1.], [1., 1.], [1., 1.]] - data = paddle.ones(shape=[2, 2], dtype='int32', name="ones") # [[1, 1], [1, 1]] + data1 = paddle.ones(shape=[3, 2]) # [[1., 1.], [1., 1.], [1., 1.]] + data2 = paddle.ones(shape=[2, 2], dtype='int32') # [[1, 1], [1, 1]] + shape = paddle.fill_constant(shape=[2], dtype='int32', value=2) + data3 = paddle.ones(shape=shape, dtype='int32') # [[1, 1], [1, 1]] """ if dtype is None: dtype = 'float32' From f32c58dd030daf850b189d7cccf4e862773b6b33 Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Wed, 15 Jul 2020 04:16:05 +0000 Subject: [PATCH 06/18] fix the doc test=develop --- python/paddle/tensor/creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 840fb5dc93e7e..534c9b202484c 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -119,7 +119,7 @@ def ones(shape, dtype=None, name=None): The OP creates a tensor of specified :attr:`shape` and :attr:`dtype`, and fills it with 1. Args: - shape(tuple|list|Variable): Shape of output tensor. + shape(tuple|list|Variable): Shape of output tensor, the data type of shape is int32 or int64. dtype(np.dtype|core.VarDesc.VarType|str, optional): Data type of output tensor, it supports bool, float16, float32, float64, int32 and int64. Default: if None, the data type is 'float32'. 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` From ca9dd0acf3aebc2b13e0b68d16a2b9853dd73ded Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Wed, 15 Jul 2020 08:17:41 +0000 Subject: [PATCH 07/18] refine the doc test=develop --- python/paddle/tensor/creation.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 534c9b202484c..426926b5b561a 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -119,7 +119,7 @@ def ones(shape, dtype=None, name=None): The OP creates a tensor of specified :attr:`shape` and :attr:`dtype`, and fills it with 1. Args: - shape(tuple|list|Variable): Shape of output tensor, the data type of shape is int32 or int64. + shape(tuple|list|Variable): Shape of output tensor, the data type of shape is int31 or int64. dtype(np.dtype|core.VarDesc.VarType|str, optional): Data type of output tensor, it supports bool, float16, float32, float64, int32 and int64. Default: if None, the data type is 'float32'. 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` @@ -133,10 +133,17 @@ def ones(shape, dtype=None, name=None): import paddle paddle.enable_imperative() - data1 = paddle.ones(shape=[3, 2]) # [[1., 1.], [1., 1.], [1., 1.]] - data2 = paddle.ones(shape=[2, 2], dtype='int32') # [[1, 1], [1, 1]] + data1 = paddle.ones(shape=[3, 2]) + # [[1. 1.] + # [1. 1.] + # [1. 1.]] + data2 = paddle.ones(shape=[2, 2], dtype='int32') + # [[1 1] + # [1 1]] shape = paddle.fill_constant(shape=[2], dtype='int32', value=2) - data3 = paddle.ones(shape=shape, dtype='int32') # [[1, 1], [1, 1]] + data3 = paddle.ones(shape=shape, dtype='int32') + # [[1 1] + # [1 1]] """ if dtype is None: dtype = 'float32' From a8531c81fbdbd043fd81ddb77de9c6c9dd593e67 Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Wed, 15 Jul 2020 09:04:08 +0000 Subject: [PATCH 08/18] fix the doc test=develop --- python/paddle/tensor/creation.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 5ab913db488ba..813e6fb9a5a1f 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -83,7 +83,8 @@ def full_like(x, fill_value, dtype=None, name=None): paddle.enable_imperative() # Now we are in imperative mode input = paddle.full(shape=[2, 3], fill_value=0.0, dtype='float32', name='input') output = paddle.full_like(input, 2.0) - #output result : [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)] + # [[2. 2. 2.] + # [2. 2. 2.]] """ if dtype is None: @@ -387,19 +388,26 @@ def full(shape, fill_value, dtype=None, name=None): import paddle paddle.enable_imperative() # Now we are in imperative mode - data1 = paddle.full(shape=[2,1], fill_value=0, dtype='int64') # data1=[[0],[0]] + data1 = paddle.full(shape=[2,1], fill_value=0, dtype='int64') + #[[0] + # [0]] # attr shape is a list which contains Variable Tensor. positive_2 = paddle.fill_constant([1], "int32", 2) - data3 = paddle.full(shape=[1, positive_2], dtype='float32', fill_value=1.5) # data3=[1.5, 1.5] + data3 = paddle.full(shape=[1, positive_2], dtype='float32', fill_value=1.5) + # [1.5 1.5] # attr shape is an Variable Tensor. - shape = paddle.fill_constant([2], "int32", 2) # shape=[2,2] - data4 = paddle.full(shape=shape, dtype='bool', fill_value=True) # data4=[[True,True],[True,True]] + shape = paddle.fill_constant([2], "int32", 2) + data4 = paddle.full(shape=shape, dtype='bool', fill_value=True) + # [[True True] + # [True True]] # attr value is an Variable Tensor. - val = paddle.fill_constant([1], "float32", 2.0) # val=[2.0] - data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32') #data5=[[2.0],[2.0]] + val = paddle.fill_constant([1], "float32", 2.0) + data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32') i + # [[2.0] + # [2.0]] """ helper = LayerHelper("full", **locals()) From ab1cbda95ca1758b9c586a1718ce35a4e582b0ca Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Wed, 15 Jul 2020 09:07:35 +0000 Subject: [PATCH 09/18] fix the doc test=develop --- python/paddle/fluid/layers/tensor.py | 2 +- python/paddle/tensor/creation.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index dedee1fdfd403..8438b3bdd00ac 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -665,7 +665,7 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None): # attr shape is a list which contains Variable Tensor. positive_2 = fluid.layers.fill_constant([1], "int32", 2) - data3 = fluid.layers.fill_constant(shape=[1, positive_2], dtype='float32', value=1.5) # data3=[1.5, 1.5] + data3 = fluid.layers.fill_constant(shape=[1, positive_2], dtype='float32', value=1.5) # data3=[[1.5, 1.5]] # attr shape is an Variable Tensor. shape = fluid.layers.fill_constant([2], "int32", 2) # shape=[2,2] diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 813e6fb9a5a1f..83e03fae1b5f8 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -393,9 +393,9 @@ def full(shape, fill_value, dtype=None, name=None): # [0]] # attr shape is a list which contains Variable Tensor. - positive_2 = paddle.fill_constant([1], "int32", 2) + positive_3 = paddle.fill_constant([1], "int32", 2) data3 = paddle.full(shape=[1, positive_2], dtype='float32', fill_value=1.5) - # [1.5 1.5] + # [[1.5 1.5]] # attr shape is an Variable Tensor. shape = paddle.fill_constant([2], "int32", 2) From df069fe811a46517feb64ad4f523514900fb9776 Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Wed, 15 Jul 2020 09:12:58 +0000 Subject: [PATCH 10/18] fix typo test=develop --- python/paddle/tensor/creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 83e03fae1b5f8..faa829fee51d0 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -120,7 +120,7 @@ def ones(shape, dtype=None, name=None): The OP creates a tensor of specified :attr:`shape` and :attr:`dtype`, and fills it with 1. Args: - shape(tuple|list|Variable): Shape of output tensor, the data type of shape is int31 or int64. + shape(tuple|list|Variable): Shape of output tensor, the data type of shape is int32 or int64. dtype(np.dtype|core.VarDesc.VarType|str, optional): Data type of output tensor, it supports bool, float16, float32, float64, int32 and int64. Default: if None, the data type is 'float32'. 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` From 712ed062cbaea1ceae24735f5441e33e7b67bd8c Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Fri, 17 Jul 2020 02:48:24 +0000 Subject: [PATCH 11/18] refine the doc test=develop --- python/paddle/fluid/layers/tensor.py | 9 ++++++++- python/paddle/tensor/creation.py | 11 ++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index 8438b3bdd00ac..658a40b08b17d 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -650,9 +650,10 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None): Returns: Variable: Tensor which is created according to shape and dtype. - Raise: + Raises: TypeError: The dtype must be one of bool, float16, float32, float64, int32 and int64 and the data type of out Tensor must be the same as the dtype. + TypeError: The shape must be one of list, tuple and Variable. Examples: .. code-block:: python @@ -1411,6 +1412,12 @@ def linspace(start, stop, num, dtype=None, name=None): the data shape of this tensor is :math:`[num]` . If the :attr:`num` is set 1, the output tensor just has \ the value with input :attr:`start`. + Raises: + TypeError: The dtype must be one of float32 and float64. + TypeError: The dtype of `start` and `stop` must be one of float32 and float64. + TypeError: The dtype of `num` must be one of int32 and int64. + + Examples: .. code-block:: python diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index faa829fee51d0..d72a7a09fe7d0 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -74,6 +74,10 @@ def full_like(x, fill_value, dtype=None, name=None): Returns: out(Variable): The Tensor variable storing the output. + Raises: + TypeError: The dtype must be one of bool, float16, float32, float64, int32, int64 and None + and the data type of out Tensor must be the same as the dtype. + Examples: .. code-block:: python @@ -128,6 +132,11 @@ def ones(shape, dtype=None, name=None): Returns: Variable: A tensor of data type :attr:`dtype` with shape :attr:`shape` and all elements set to 1. + Raises: + TypeError: The dtype must be one of bool, float16, float32, float64, int32, int64 and None + and the data type of out Tensor must be the same as the dtype. + TypeError: The `shape` must be one of list, tuple and Variable. + Examples: .. code-block:: python @@ -380,7 +389,7 @@ def full(shape, fill_value, dtype=None, name=None): Raises: TypeError: The `dtype` must be one of None, bool, float16, float32, float64, int32 and int64. - TypeError: The `shape` must be one of Variable, list tuple. + TypeError: The `shape` must be one of Variable, list and tuple. Examples: .. code-block:: python From fc5948b8cdacf7467f515f2501d5d8956439b87f Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Fri, 17 Jul 2020 03:52:07 +0000 Subject: [PATCH 12/18] refine the doc test=develop --- python/paddle/tensor/creation.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index d72a7a09fe7d0..bee9cd07b55d5 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -75,8 +75,7 @@ def full_like(x, fill_value, dtype=None, name=None): out(Variable): The Tensor variable storing the output. Raises: - TypeError: The dtype must be one of bool, float16, float32, float64, int32, int64 and None - and the data type of out Tensor must be the same as the dtype. + TypeError: The dtype must be one of bool, float16, float32, float64, int32, int64 and None. Examples: .. code-block:: python @@ -103,7 +102,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/zeros_like') + 'full_like') out = helper.create_variable_for_type_inference(dtype=dtype) helper.append_op( From 8a82c339d2c2309f44a1275011cbd31f0af180bc Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Fri, 17 Jul 2020 04:14:15 +0000 Subject: [PATCH 13/18] refine the code test=develop --- python/paddle/tensor/creation.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index bee9cd07b55d5..e1b5db8d95d61 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -418,8 +418,6 @@ def full(shape, fill_value, dtype=None, name=None): # [2.0]] """ - helper = LayerHelper("full", **locals()) - if dtype is None: dtype = 'float32' From ff6f58135ac13ff806533f326f311fa7c3a9f21d Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Fri, 17 Jul 2020 05:17:34 +0000 Subject: [PATCH 14/18] fix typo test=develop --- python/paddle/tensor/creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index e1b5db8d95d61..8ec14d2f5dc44 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -413,7 +413,7 @@ def full(shape, fill_value, dtype=None, name=None): # attr value is an Variable Tensor. val = paddle.fill_constant([1], "float32", 2.0) - data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32') i + data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32') # [[2.0] # [2.0]] """ From 71c47b30001f2a9480d5f5b2803c7c6c36fde1ee Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Fri, 17 Jul 2020 05:34:05 +0000 Subject: [PATCH 15/18] fix typo test=develop --- python/paddle/tensor/creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 8ec14d2f5dc44..c86ee8d2eb534 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -401,7 +401,7 @@ def full(shape, fill_value, dtype=None, name=None): # [0]] # attr shape is a list which contains Variable Tensor. - positive_3 = paddle.fill_constant([1], "int32", 2) + positive_2 = paddle.fill_constant([1], "int32", 2) data3 = paddle.full(shape=[1, positive_2], dtype='float32', fill_value=1.5) # [[1.5 1.5]] From 605038fed21496292fd7a89420083f3e7516dc39 Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Sun, 19 Jul 2020 05:22:58 +0000 Subject: [PATCH 16/18] fix the doc test=develop --- python/paddle/tensor/creation.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index c86ee8d2eb534..9198237437d91 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -142,13 +142,18 @@ def ones(shape, dtype=None, name=None): import paddle paddle.enable_imperative() + + #default dtype for ones OP data1 = paddle.ones(shape=[3, 2]) # [[1. 1.] # [1. 1.] # [1. 1.]] + data2 = paddle.ones(shape=[2, 2], dtype='int32') # [[1 1] # [1 1]] + + #shape is a Variable shape = paddle.fill_constant(shape=[2], dtype='int32', value=2) data3 = paddle.ones(shape=shape, dtype='int32') # [[1 1] From 216fc3f0bcc3a934d240fe17661d46d5cd33c8a9 Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Sun, 19 Jul 2020 13:47:40 +0000 Subject: [PATCH 17/18] refine the doc test=develop --- python/paddle/tensor/creation.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 9198237437d91..4cc3fa1984d78 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -139,8 +139,7 @@ def ones(shape, dtype=None, name=None): Examples: .. code-block:: python - import paddle - + import paddle paddle.enable_imperative() #default dtype for ones OP From b054ec494d4bfe73187a37a03ca3e8ca6be474b9 Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Sun, 19 Jul 2020 14:08:02 +0000 Subject: [PATCH 18/18] refine the doc test=develop --- python/paddle/tensor/creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 4cc3fa1984d78..a672d15fba25d 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -415,7 +415,7 @@ def full(shape, fill_value, dtype=None, name=None): # [[True True] # [True True]] - # attr value is an Variable Tensor. + # attr fill_value is an Variable Tensor. val = paddle.fill_constant([1], "float32", 2.0) data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32') # [[2.0]