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

分类目录:《深入浅出PaddlePaddle函数》总目录
相关文章:
· 深入浅出TensorFlow2函数——tf.size
· 深入浅出Pytorch函数——torch.numel
· 深入浅出PaddlePaddle函数——paddle.numel


语法
paddle.numel(x)
参数
  • x:[Tensor] 输入Tensor,数据类型为int32int64float16float32float64int32int64
  • name :[可选。str] 具体用法请参见Name,一般无需设置,默认值为None
返回值

在静态图模式下,返回一个长度为1并且元素值为输入x元素个数的Tensor;在动态图模式下,返回一个标量数值。

实例
import paddle

x = paddle.full(shape=[4, 5, 7], fill_value=0, dtype='int32')
numel = paddle.numel(x) # 140
函数实现
def numel(x, name=None):
    """
    Returns the number of elements for a tensor, which is a int64 Tensor with shape [1] in static mode
    or a scalar value in imperative mode.
    Args:
        x (Tensor): The input Tensor, it's data type can be bool, float16, float32, float64, int32, int64.
        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.
    Returns:
        Tensor: The number of elements for the input Tensor.
    Examples:
        .. code-block:: python
            import paddle
            x = paddle.full(shape=[4, 5, 7], fill_value=0, dtype='int32')
            numel = paddle.numel(x) # 140
    """
    if in_dygraph_mode():
        return _C_ops.size(x)
    elif _in_legacy_dygraph():
        return _legacy_C_ops.size(x)

    if not isinstance(x, Variable):
        raise TypeError("x must be a Tensor in numel")
    helper = LayerHelper('numel', **locals())
    out = helper.create_variable_for_type_inference(
        dtype=core.VarDesc.VarType.INT64
    )
    helper.append_op(type='size', inputs={'Input': x}, outputs={'Out': out})
    return out
  • 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、付费专栏及课程。

余额充值