TUTORIALAI & dataFETCHED

PyTorch torch.numel 函数

PyTorch torch.numel 函数 Pytorch torch 参考手册 torch.numel 是 PyTorch 中用于计算张量中元素总数的函数。 函数定义 torch.numel(input) 使用示例 实例 [mycode4 type='python'] import torch x = torch.randn(3, 4, 5) print('元素总数:',..

Updated 7/16/2026

Tutorial detail pages default to noindex.

PyTorch torch.numel 函数


Pytorch torch 参考手册 Pytorch torch 参考手册

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

Pytorch torch 参考手册 Pytorch torch 参考手册