diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index de701013f83485ecc05cca7cef30a5fdf127cb07..6673a145b134cd208a8f072504c5ef3a1b4ed8c3 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -59,7 +59,7 @@ from .tensor.creation import arange #DEFINE_ALIAS # from .tensor.creation import eye #DEFINE_ALIAS from .tensor.creation import full #DEFINE_ALIAS # from .tensor.creation import linspace #DEFINE_ALIAS -from .tensor.creation import full_like #DEFINE_ALIAS +# from .tensor.creation import full_like #DEFINE_ALIAS # from .tensor.creation import triu #DEFINE_ALIAS # from .tensor.creation import tril #DEFINE_ALIAS from .tensor.creation import meshgrid #DEFINE_ALIAS diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index 6ddd4dc04a8cb5824be8e27b0195ad32917e6f9e..05432e0aedf14b80efadac85a450ab1514078da2 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -30,12 +30,37 @@ import numpy import warnings __all__ = [ - 'create_tensor', 'create_parameter', 'create_global_var', 'cast', - 'tensor_array_to_tensor', 'concat', 'sums', 'assign', - 'fill_constant_batch_size_like', 'fill_constant', 'argmin', 'argmax', - 'argsort', 'ones', 'zeros', 'reverse', 'has_inf', 'has_nan', 'isfinite', - 'range', 'linspace', 'zeros_like', 'ones_like', 'diag', 'eye', 'kron', - 'full_like', 'arange', 'full', 'tril', 'triu' + 'create_tensor', + 'create_parameter', + 'create_global_var', + 'cast', + 'tensor_array_to_tensor', + 'concat', + 'sums', + 'assign', + 'fill_constant_batch_size_like', + 'fill_constant', + 'argmin', + 'argmax', + 'argsort', + 'ones', + 'zeros', + 'reverse', + 'has_inf', + 'has_nan', + 'isfinite', + 'range', + 'linspace', + 'full_like', + 'zeros_like', + 'ones_like', + 'diag', + 'eye', + 'kron', + 'arange', + 'full', + 'tril', + 'triu', ] @@ -1371,6 +1396,70 @@ def linspace(start, stop, num, dtype): return out +def full_like(input, + fill_value, + out=None, + dtype=None, + device=None, + stop_gradient=True, + name=None): + """ + **full_like** + This function creates a tensor filled with `fill_value` which has identical shape and dtype + with `input`. + + Args: + input(Variable): The input tensor which specifies shape and data type. The data type can be bool, float16, float32, float64, int32, int64. + fill_value(bool|float|int): The value to fill the tensor with. Default value is 0. Note: this value shouldn't exceed the range of the output data type. + 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. Default value is None. + dtype(np.dtype|core.VarDesc.VarType|str, optional): The data type of output. The default value is None, which means the output data type is the same as input. + device (string, optional): Which device to run the operator. The :attr:`device` must be None, 'cpu', 'gpu'. If :attr:`device` is None, it will be the device that the user set in the paddle program. Default value is None. + stop_gradient(bool, optional): Indicating if we stop gradient from current(out) Variable. Default value is True. + 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. + + Examples: + .. code-block:: python + + import paddle + import paddle.fluid as fluid + import numpy as np + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = fluid.layers.full_like(input, 2.0) + exe = fluid.Executor(fluid.CPUPlace()) + exe.run(fluid.default_startup_program()) + img=np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + res = exe.run(fluid.default_main_program(), feed={'input':img}, fetch_list=[output]) + print(res) # [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)] + """ + helper = LayerHelper("full_like", **locals()) + + var_dtype = None + if dtype is None: + var_dtype = input.dtype + else: + check_dtype( + dtype, 'dtype', + ['bool', 'float16', 'float32', 'float64', 'int32', 'int64'], + 'full_like') + var_dtype = convert_np_dtype_to_dtype_(dtype) + + if out is None: + out = helper.create_variable_for_type_inference(dtype=dtype) + + helper.append_op( + type='fill_any_like', + inputs={'X': [input]}, + attrs={'value': fill_value, + "dtype": var_dtype}, + outputs={'Out': [out]}) + out.stop_gradient = stop_gradient + + return out + + def zeros_like(x, out=None): """ This OP creates a zeros tensor which has identical shape and dtype @@ -1567,56 +1656,6 @@ def ones_like(x, out=None): return out -def full_like(input, - fill_value, - out=None, - dtype=None, - device=None, - stop_gradient=True, - name=None): - """ - **full_like** - This function creates a tensor filled with `fill_value` which has identical shape and dtype - with `input`. - Args: - input(Variable): The input tensor which specifies shape and dtype. - fill_value: The value to fill the tensor with. Data type can be bool, float32, float64, int32, int64. Default value is 0. - out(Variable): The output tensor. - Returns: - out(Variable): The tensor variable storing the output. - Examples: - .. code-block:: python - import paddle.fluid as fluid - import numpy as np - - input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = fluid.layers.full_like(input, 2.0) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) - img=np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) - res = exe.run(fluid.default_main_program(), feed={'input':img}, fetch_list=[output]) - print(res) # [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)] - """ - helper = LayerHelper("full_like", **locals()) - - if dtype is None: - dtype = 'float32' - - check_dtype(dtype, 'dtype', - ['bool', 'float16', 'float32', 'int32', 'int64'], 'full_like') - - if out is None: - out = helper.create_variable_for_type_inference(dtype=dtype) - helper.append_op( - type='fill_any_like', - inputs={'X': [input]}, - attrs={'value': fill_value}, - outputs={'Out': [out]}) - out.stop_gradient = stop_gradient - - return out - - def arange(start, end, step=1, dtype=None, name=None): """ Return evenly spaced values within a given interval. diff --git a/python/paddle/fluid/tests/unittests/test_fill_any_like_op.py b/python/paddle/fluid/tests/unittests/test_fill_any_like_op.py index 777d38cb25e20feef75e31509724183f362652a1..368132b7d042b8a4ae275d60ed22ef83ec8e43ce 100644 --- a/python/paddle/fluid/tests/unittests/test_fill_any_like_op.py +++ b/python/paddle/fluid/tests/unittests/test_fill_any_like_op.py @@ -107,6 +107,8 @@ class TestFillAnyLikeOp_attr_out(unittest.TestCase): fill_value = 2.0 input = fluid.data(name='input', dtype='float32', shape=[2, 3]) output = fluid.layers.full_like(input, fill_value) + output_dtype = fluid.layers.full_like( + input, fill_value, dtype='float32') place = fluid.CPUPlace() if fluid.core.is_compiled_with_cuda(): diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 42fbfbad948c9fd627fda71578d41ae52d56dc65..acca14f396de55fb57ed4ca0a6c855acc26ae1b1 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -36,7 +36,7 @@ from .creation import arange #DEFINE_ALIAS # from .creation import eye #DEFINE_ALIAS from .creation import full # DEFINE_ALIAS # from .creation import linspace #DEFINE_ALIAS -from .creation import full_like #DEFINE_ALIAS +# from .creation import full_like #DEFINE_ALIAS from .creation import triu #DEFINE_ALIAS from .creation import tril #DEFINE_ALIAS from .creation import meshgrid #DEFINE_ALIAS diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index bc979fdbc142b91953933c1b5a496fa50ad73edb..68d71539653d5a9b59fec721e7c4cbffe37fdafa 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -40,64 +40,13 @@ __all__ = [ 'arrange', 'eye', 'full', - 'full_like', + #'full_like', 'triu', 'tril', 'meshgrid', ] -def full_like(input, - fill_value, - out=None, - dtype=None, - device=None, - stop_gradient=True, - name=None): - """ - **full_like** - This function creates a tensor filled with `fill_value` which has identical shape and dtype - with `input`. - Args: - input(Variable): The input tensor which specifies shape and dtype. - fill_value: The value to fill the tensor with. Data type can be bool, float32, float64, int32, int64. Default value is 0. - out(Variable): The output tensor. - Returns: - out(Variable): The tensor variable storing the output. - Examples: - .. code-block:: python - import paddle - import paddle.fluid as fluid - import numpy as np - - input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.full_like(input, 2.0) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) - img=np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) - res = exe.run(fluid.default_main_program(), feed={'input':img}, fetch_list=[output]) - print(res) # [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)] - """ - helper = LayerHelper("full_like", **locals()) - - if dtype is None: - dtype = 'float32' - - check_dtype(dtype, 'dtype', - ['bool', 'float16', 'float32', 'int32', 'int64'], 'full_like') - - if out is None: - out = helper.create_variable_for_type_inference(dtype=dtype) - helper.append_op( - type='fill_any_like', - inputs={'X': [input]}, - attrs={'value': fill_value}, - outputs={'Out': [out]}) - out.stop_gradient = stop_gradient - - return out - - def linspace(start, stop, num, dtype, out=None, device=None, name=None): """ This OP return fixed number of evenly spaced values within a given interval.