0


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

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


语法

  1. paddle.numel(x)

参数

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

返回值

在静态图模式下,返回一个长度为1并且元素值为输入

  1. x

元素个数的Tensor;在动态图模式下,返回一个标量数值。

实例

  1. import paddle
  2. x = paddle.full(shape=[4,5,7], fill_value=0, dtype='int32')
  3. numel = paddle.numel(x) # 140

函数实现

  1. def numel(x, name=None):"""
  2. Returns the number of elements for a tensor, which is a int64 Tensor with shape [1] in static mode
  3. or a scalar value in imperative mode.
  4. 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).
  5. For more information, please refer to :ref:`api_guide_Name`.
  6. Returns:
  7. Tensor: The number of elements for the input Tensor.
  8. Examples:.. code-block:: python
  9. import paddle
  10. x = paddle.full(shape=[4,5,7], fill_value=0, dtype='int32')
  11. numel = paddle.numel(x) # 140"""
  12. ifin_dygraph_mode():return _C_ops.size(x)
  13. elif _in_legacy_dygraph():return _legacy_C_ops.size(x)if not isinstance(x, Variable):
  14. raise TypeError("x must be a Tensor in numel")
  15. helper =LayerHelper('numel',**locals())
  16. out = helper.create_variable_for_type_inference(
  17. dtype=core.VarDesc.VarType.INT64
  18. )
  19. helper.append_op(type='size', inputs={'Input': x}, outputs={'Out': out})return out

本文转载自: https://blog.csdn.net/hy592070616/article/details/129429271
版权归原作者 von Neumann 所有, 如有侵权,请联系我们删除。

“深入浅出PaddlePaddle函数——paddle.numel”的评论:

还没有评论