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

分类目录:《深入浅出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

创建一个形状为shape、数据类型为dtype且值全为fill_value 的Tensor。

语法

paddle.full(shape, fill_value, dtype=None, name=None)

参数

  • shape:[tuple/list/Tensor] 要创建的Tensor的形状,shape的数据类型为int32int64
  • fill_value:[bool/float/int/Tensor] 用于初始化输出Tensor的常量数据的值。注意:该参数不可超过输出变量数据类型的表示范围。
  • dtype:[可选,np.dtype/str] 要创建的Tensor的数据类型,可以为boolfloat16float32float64int32int64。如果dtypeNone,那么数据类型为float32
  • name:[可选,str] 具体用法请参见Name,一般无需设置,默认值为None

返回值

Tensor,每个元素都是fill_value ,形状为 shape,数据类型为dtype

实例

import paddle

data1 = paddle.full(shape=[2,1], fill_value=0, dtype='int64')
#[[0]
# [0]]

# attr shape is a list which contains Tensor.
positive_2 = paddle.full([1], 2, "int32")
data3 = paddle.full(shape=[1, positive_2], dtype='float32', fill_value=1.5)
# [[1.5 1.5]]

# attr shape is a Tensor.
shape = paddle.full([2], 2, "int32")
data4 = paddle.full(shape=shape, dtype='bool', fill_value=True)
# [[True True]
#  [True True]]

# attr fill_value is a Tensor.
val = paddle.full([1], 2.0, "float32")
data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32')
# [[2.0]
#  [2.0]]

函数实现

def full(shape, fill_value, dtype=None, name=None):
    """
    Return a Tensor with the ``fill_value`` which size is same as ``shape``.
    Args:
        shape(list|tuple|Tensor): Shape of the Tensor to be created.
                The data type is ``int32`` or ``int64`` . If ``shape`` is a list or tuple,
                the elements of it should be integers or Tensors with shape [1].
                If ``shape`` is an Tensor, it should be an 1-D Tensor.
        fill_value(bool|float|int|Tensor): The constant value
            used to initialize the Tensor to be created. If ``fill_value`` is an Tensor, it must be an 1-D Tensor.
        dtype(np.dtype|str, optional): Data type of the output Tensor
            which can be float16, float32, float64, int32, int64, if dytpe is `None`, the data
            type of created Tensor is `float32`.
        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 ``shape``, ``fill_value`` and ``dtype``.
    Examples:
        .. code-block:: python
            import paddle
            data1 = paddle.full(shape=[2,1], fill_value=0, dtype='int64')
            #[[0]
            # [0]]
            # attr shape is a list which contains Tensor.
            positive_2 = paddle.full([1], 2, "int32")
            data3 = paddle.full(shape=[1, positive_2], dtype='float32', fill_value=1.5)
            # [[1.5 1.5]]
            # attr shape is a Tensor.
            shape = paddle.full([2], 2, "int32")
            data4 = paddle.full(shape=shape, dtype='bool', fill_value=True)
            # [[True True]
            #  [True True]]
            # attr fill_value is a Tensor.
            val = paddle.full([1], 2.0, "float32")
            data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32')
            # [[2.0]
            #  [2.0]]
    """

    if dtype is None:
        dtype = 'float32'

    return fill_constant(shape=shape, dtype=dtype, value=fill_value, name=name)

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

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

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年6月20日
下一篇 2023年6月20日

相关推荐