分类目录:《深入浅出PaddlePaddle函数》总目录
相关文章:
· 深入浅出PaddlePaddle函数——paddle.Tensor
· 深入浅出PaddlePaddle函数——paddle.ones
· 深入浅出PaddlePaddle函数——paddle.zeros
· 深入浅出PaddlePaddle函数——paddle.full
· 深入浅出PaddlePaddle函数——paddle.ones_like
· 深入浅出PaddlePaddle函数——paddle.zeros_like
· 深入浅出PaddlePaddle函数——paddle.full_like
返回一个和输入参数
x
具有相同形状的数值都为
fill_value
的Tensor,数据类型为
dtype
或者和
x
相同,如果
dtype
为
None
,则输出Tensor的数据类型与
x
相同。
语法
paddle.full_like(x, fill_value, dtype=None, name=None)
参数
x
:[Tensor
] 输入的Tensor,数据类型可以是bool
、float16l
、float32l
、float64l
、int32l
、int64
。fill_value
:[bool
/float
/int
/Tensor
] 用于初始化输出Tensor的常量数据的值。注意:该参数不可超过输出变量数据类型的表示范围。dtype
:[可选,np.dtype
/str
] 要创建的Tensor的数据类型,可以为bool
、float16
、float32
、float64
、int32
或int64
。如果dtype
为None
,那么数据类型为float32
。name
:[可选,str
] 具体用法请参见Name
,一般无需设置,默认值为None
。
返回值
和
x
具有相同形状的数值都为
fill_value
的Tensor,数据类型为
dtype
或者和
x
相同。
实例
import paddle
import paddle
input = paddle.full(shape=[2,3], fill_value=0.0, dtype='float32', name='input')
output = paddle.full_like(input,2.0)
# [[2.2.2.]
# [2.2.2.]]
函数实现
def full_like(x, fill_value, dtype=None, name=None):"""
This function creates a tensor filled with ``fill_value`` which has identical shape of ``x`` and ``dtype``.
If the ``dtype`` is None, the data type of Tensor is same with ``x``.
Args:x(Tensor): 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. Note: this value shouldn't exceed the range of the output data type.dtype(np.dtype|str, optional): The data type of output. The data type can be one
of bool, float16, float32, float64, int32, int64. The default value is None, which means the output
data type is the same as input.name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
Returns:
Tensor: Tensor which is created according to ``x``, ``fill_value`` and ``dtype``.
Examples:.. code-block:: python
import paddle
input = paddle.full(shape=[2,3], fill_value=0.0, dtype='float32', name='input')
output = paddle.full_like(input,2.0)
# [[2.2.2.]
# [2.2.2.]]"""
if dtype is None:
dtype = x.dtype
else:if not isinstance(dtype, core.VarDesc.VarType):
dtype =convert_np_dtype_to_dtype_(dtype)ifin_dygraph_mode():return _C_ops.full_like(x, fill_value, dtype, x.place)if_in_legacy_dygraph():return _legacy_C_ops.fill_any_like(
x,'value', fill_value,'dtype', dtype
)
helper =LayerHelper("full_like",**locals())check_variable_and_dtype(
x,'x',['bool','float16','float32','float64','int16','int32','int64'],'full_like',)check_dtype(
dtype,'dtype',['bool','float16','float32','float64','int16','int32','int64'],'full_like/zeros_like/ones_like',)
out = helper.create_variable_for_type_inference(dtype=dtype)
helper.append_op(
type='fill_any_like',
inputs={'X':[x]},
attrs={'value': fill_value,"dtype": dtype},
outputs={'Out':[out]},)
out.stop_gradient = True
return out
版权归原作者 von Neumann 所有, 如有侵权,请联系我们删除。