Skip to content

Commit efa0007

Browse files
author
Pei Yang
authored
[cherry-pick] refine and fix full_like en api (#24137)
* merge latest. test=develop * move full_like to fluid.layers
1 parent 9cbf601 commit efa0007

File tree

5 files changed

+100
-110
lines changed

5 files changed

+100
-110
lines changed

python/paddle/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
# from .tensor.creation import eye #DEFINE_ALIAS
6060
from .tensor.creation import full #DEFINE_ALIAS
6161
# from .tensor.creation import linspace #DEFINE_ALIAS
62-
from .tensor.creation import full_like #DEFINE_ALIAS
62+
# from .tensor.creation import full_like #DEFINE_ALIAS
6363
# from .tensor.creation import triu #DEFINE_ALIAS
6464
# from .tensor.creation import tril #DEFINE_ALIAS
6565
from .tensor.creation import meshgrid #DEFINE_ALIAS

python/paddle/fluid/layers/tensor.py

Lines changed: 95 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,37 @@
3030
import warnings
3131

3232
__all__ = [
33-
'create_tensor', 'create_parameter', 'create_global_var', 'cast',
34-
'tensor_array_to_tensor', 'concat', 'sums', 'assign',
35-
'fill_constant_batch_size_like', 'fill_constant', 'argmin', 'argmax',
36-
'argsort', 'ones', 'zeros', 'reverse', 'has_inf', 'has_nan', 'isfinite',
37-
'range', 'linspace', 'zeros_like', 'ones_like', 'diag', 'eye', 'kron',
38-
'full_like', 'arange', 'full', 'tril', 'triu'
33+
'create_tensor',
34+
'create_parameter',
35+
'create_global_var',
36+
'cast',
37+
'tensor_array_to_tensor',
38+
'concat',
39+
'sums',
40+
'assign',
41+
'fill_constant_batch_size_like',
42+
'fill_constant',
43+
'argmin',
44+
'argmax',
45+
'argsort',
46+
'ones',
47+
'zeros',
48+
'reverse',
49+
'has_inf',
50+
'has_nan',
51+
'isfinite',
52+
'range',
53+
'linspace',
54+
'full_like',
55+
'zeros_like',
56+
'ones_like',
57+
'diag',
58+
'eye',
59+
'kron',
60+
'arange',
61+
'full',
62+
'tril',
63+
'triu',
3964
]
4065

4166

@@ -1371,6 +1396,70 @@ def linspace(start, stop, num, dtype):
13711396
return out
13721397

13731398

1399+
def full_like(input,
1400+
fill_value,
1401+
out=None,
1402+
dtype=None,
1403+
device=None,
1404+
stop_gradient=True,
1405+
name=None):
1406+
"""
1407+
**full_like**
1408+
This function creates a tensor filled with `fill_value` which has identical shape and dtype
1409+
with `input`.
1410+
1411+
Args:
1412+
input(Variable): The input tensor which specifies shape and data type. The data type can be bool, float16, float32, float64, int32, int64.
1413+
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.
1414+
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.
1415+
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.
1416+
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.
1417+
stop_gradient(bool, optional): Indicating if we stop gradient from current(out) Variable. Default value is True.
1418+
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`
1419+
1420+
Returns:
1421+
out(Variable): The Tensor variable storing the output.
1422+
1423+
Examples:
1424+
.. code-block:: python
1425+
1426+
import paddle
1427+
import paddle.fluid as fluid
1428+
import numpy as np
1429+
input = fluid.data(name='input', dtype='float32', shape=[2, 3])
1430+
output = fluid.layers.full_like(input, 2.0)
1431+
exe = fluid.Executor(fluid.CPUPlace())
1432+
exe.run(fluid.default_startup_program())
1433+
img=np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
1434+
res = exe.run(fluid.default_main_program(), feed={'input':img}, fetch_list=[output])
1435+
print(res) # [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)]
1436+
"""
1437+
helper = LayerHelper("full_like", **locals())
1438+
1439+
var_dtype = None
1440+
if dtype is None:
1441+
var_dtype = input.dtype
1442+
else:
1443+
check_dtype(
1444+
dtype, 'dtype',
1445+
['bool', 'float16', 'float32', 'float64', 'int32', 'int64'],
1446+
'full_like')
1447+
var_dtype = convert_np_dtype_to_dtype_(dtype)
1448+
1449+
if out is None:
1450+
out = helper.create_variable_for_type_inference(dtype=dtype)
1451+
1452+
helper.append_op(
1453+
type='fill_any_like',
1454+
inputs={'X': [input]},
1455+
attrs={'value': fill_value,
1456+
"dtype": var_dtype},
1457+
outputs={'Out': [out]})
1458+
out.stop_gradient = stop_gradient
1459+
1460+
return out
1461+
1462+
13741463
def zeros_like(x, out=None):
13751464
"""
13761465
This OP creates a zeros tensor which has identical shape and dtype
@@ -1567,56 +1656,6 @@ def ones_like(x, out=None):
15671656
return out
15681657

15691658

1570-
def full_like(input,
1571-
fill_value,
1572-
out=None,
1573-
dtype=None,
1574-
device=None,
1575-
stop_gradient=True,
1576-
name=None):
1577-
"""
1578-
**full_like**
1579-
This function creates a tensor filled with `fill_value` which has identical shape and dtype
1580-
with `input`.
1581-
Args:
1582-
input(Variable): The input tensor which specifies shape and dtype.
1583-
fill_value: The value to fill the tensor with. Data type can be bool, float32, float64, int32, int64. Default value is 0.
1584-
out(Variable): The output tensor.
1585-
Returns:
1586-
out(Variable): The tensor variable storing the output.
1587-
Examples:
1588-
.. code-block:: python
1589-
import paddle.fluid as fluid
1590-
import numpy as np
1591-
1592-
input = fluid.data(name='input', dtype='float32', shape=[2, 3])
1593-
output = fluid.layers.full_like(input, 2.0)
1594-
exe = fluid.Executor(fluid.CPUPlace())
1595-
exe.run(fluid.default_startup_program())
1596-
img=np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
1597-
res = exe.run(fluid.default_main_program(), feed={'input':img}, fetch_list=[output])
1598-
print(res) # [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)]
1599-
"""
1600-
helper = LayerHelper("full_like", **locals())
1601-
1602-
if dtype is None:
1603-
dtype = 'float32'
1604-
1605-
check_dtype(dtype, 'dtype',
1606-
['bool', 'float16', 'float32', 'int32', 'int64'], 'full_like')
1607-
1608-
if out is None:
1609-
out = helper.create_variable_for_type_inference(dtype=dtype)
1610-
helper.append_op(
1611-
type='fill_any_like',
1612-
inputs={'X': [input]},
1613-
attrs={'value': fill_value},
1614-
outputs={'Out': [out]})
1615-
out.stop_gradient = stop_gradient
1616-
1617-
return out
1618-
1619-
16201659
def arange(start, end, step=1, dtype=None, name=None):
16211660
"""
16221661
Return evenly spaced values within a given interval.

python/paddle/fluid/tests/unittests/test_fill_any_like_op.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ def test_attr_tensor_API(self):
107107
fill_value = 2.0
108108
input = fluid.data(name='input', dtype='float32', shape=[2, 3])
109109
output = fluid.layers.full_like(input, fill_value)
110+
output_dtype = fluid.layers.full_like(
111+
input, fill_value, dtype='float32')
110112

111113
place = fluid.CPUPlace()
112114
if fluid.core.is_compiled_with_cuda():

python/paddle/tensor/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
# from .creation import eye #DEFINE_ALIAS
3737
from .creation import full # DEFINE_ALIAS
3838
# from .creation import linspace #DEFINE_ALIAS
39-
from .creation import full_like #DEFINE_ALIAS
39+
# from .creation import full_like #DEFINE_ALIAS
4040
from .creation import triu #DEFINE_ALIAS
4141
from .creation import tril #DEFINE_ALIAS
4242
from .creation import meshgrid #DEFINE_ALIAS

python/paddle/tensor/creation.py

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,64 +40,13 @@
4040
'arrange',
4141
'eye',
4242
'full',
43-
'full_like',
43+
#'full_like',
4444
'triu',
4545
'tril',
4646
'meshgrid',
4747
]
4848

4949

50-
def full_like(input,
51-
fill_value,
52-
out=None,
53-
dtype=None,
54-
device=None,
55-
stop_gradient=True,
56-
name=None):
57-
"""
58-
**full_like**
59-
This function creates a tensor filled with `fill_value` which has identical shape and dtype
60-
with `input`.
61-
Args:
62-
input(Variable): The input tensor which specifies shape and dtype.
63-
fill_value: The value to fill the tensor with. Data type can be bool, float32, float64, int32, int64. Default value is 0.
64-
out(Variable): The output tensor.
65-
Returns:
66-
out(Variable): The tensor variable storing the output.
67-
Examples:
68-
.. code-block:: python
69-
import paddle
70-
import paddle.fluid as fluid
71-
import numpy as np
72-
73-
input = fluid.data(name='input', dtype='float32', shape=[2, 3])
74-
output = paddle.full_like(input, 2.0)
75-
exe = fluid.Executor(fluid.CPUPlace())
76-
exe.run(fluid.default_startup_program())
77-
img=np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
78-
res = exe.run(fluid.default_main_program(), feed={'input':img}, fetch_list=[output])
79-
print(res) # [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)]
80-
"""
81-
helper = LayerHelper("full_like", **locals())
82-
83-
if dtype is None:
84-
dtype = 'float32'
85-
86-
check_dtype(dtype, 'dtype',
87-
['bool', 'float16', 'float32', 'int32', 'int64'], 'full_like')
88-
89-
if out is None:
90-
out = helper.create_variable_for_type_inference(dtype=dtype)
91-
helper.append_op(
92-
type='fill_any_like',
93-
inputs={'X': [input]},
94-
attrs={'value': fill_value},
95-
outputs={'Out': [out]})
96-
out.stop_gradient = stop_gradient
97-
98-
return out
99-
100-
10150
def linspace(start, stop, num, dtype, out=None, device=None, name=None):
10251
"""
10352
This OP return fixed number of evenly spaced values within a given interval.

0 commit comments

Comments
 (0)