深入浅出PaddlePaddle函数——paddle.full_like

分类目录:《深入浅出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相同,如果dtypeNone,则输出Tensor的数据类型与x相同。

语法

paddle.full_like(x, fill_value, dtype=None, name=None)

参数

  • x:[Tensor] 输入的Tensor,数据类型可以是boolfloat16lfloat32lfloat64lint32lint64
  • fill_value:[bool/float/int/Tensor] 用于初始化输出Tensor的常量数据的值。注意:该参数不可超过输出变量数据类型的表示范围。
  • dtype:[可选,np.dtype/str] 要创建的Tensor的数据类型,可以为boolfloat16float32float64int32int64。如果dtypeNone,那么数据类型为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)

    if in_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

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年6月12日
下一篇 2023年6月12日

相关推荐