分类目录:《深入浅出PaddlePaddle函数》总目录
相关文章:
· 深入浅出TensorFlow2函数——tf.size
· 深入浅出Pytorch函数——torch.numel
· 深入浅出PaddlePaddle函数——paddle.numel
语法
paddle.numel(x)
参数
x
:[Tensor
] 输入Tensor,数据类型为int32
、int64
、float16
、float32
、float64
、int32
、int64
。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
更多推荐
深入浅出PaddlePaddle函数——paddle.numel
发布评论