深入浅出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)
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

von Neumann

您的赞赏是我创作最大的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值