PyTorch torch.numel 函数

约 283 字 更新于 2026-11-18 5iyo.com

PyTorch torch.numel 函数


torch.numel 是 PyTorch 中用于计算张量中元素总数的函数。

函数定义

torch.numel(input)

使用示例

实例

import torch

x = torch.randn(3, 4, 5)
print("元素总数:", torch.numel(x))

y = torch.tensor([1, 2, 3, 4, 5])
print("元素总数:", torch.numel(y))

输出结果为:

元素总数: 60
元素总数: 5