Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute
menu
close

Need Help? Talk to an Expert

+91 8000107255
Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute

Need Help? Talk to an Expert

+91 8000107255

torch.numel(): Calculating Total Elements in a Tensor

Home torch.numel(): Calculating Total Elements in a Tensor
Counting Number of Elements in a PyTorch Tensor
  • Written by krunallathiya21
  • May 8, 2025
  • 0 Com
PyTorch

PyTorch torch.numel() method calculates the total number of elements in the tensor, regardless of its shape.

torch.numel() method

The above figure shows that we calculated the total number of elements in a 2×3 tensor, which is 2×3 = 6.

It works with any shape or dimensions (e.g., 1D, 2D, 3D, or other multidimensional) and does not track gradients, as it is a metadata operation.

The numel() method returns 0 for empty tensors (e.g., torch.tensor([])), and scalars (0D tensors) return 1.

An alias for this method is called torch.nelement().

Syntax

torch.numel(input_tensor)

Parameters

Argument Description
input_tensor (Tensor) It is an input tensor whose elements you want to count.

Scalar Tensor

Applying numel() on a scalar (0D) tensor  

Since the scalar tensor contains only one element, this method returns 1.

import torch

scalar = torch.tensor(21.0)

count = torch.numel(scalar)

print(count)

# Output: 1

1D Tensor (Vector)

Applying numel() on a vector (1D) tensor

If your input is a one-dimensional tensor, numel() returns the length of the 1D tensor.

import torch

vector = torch.tensor([19, 21, 18, 48])

count = torch.numel(vector)

print(count)

# Output: 4

The shape of the input tensor is (4, ), which means it is a 1D tensor, and it returns the number of elements.

2D Tensor (Matrix)

What about 2D tensors? Well, for a 2D tensor, it returns the product of the dimensions.

import torch

matrix = torch.tensor([[1, 2, 3], [4, 5, 6]])

counting = torch.numel(matrix)

print(counting)
# Output: 6

print(matrix.shape)
# Output: torch.Size([2, 3])

The .numel() returns 2×3 = 6, the total elements.

Higher-Dimensional Tensor

If the input is a 3D or higher-dimensional tensor, it calculates the product of all dimensions.

We can generate a random 3D tensor using torch.randn() method.

import torch

tensor_3d = torch.randn(2, 3, 4)

counting_elements = torch.numel(tensor_3d)

print(counting_elements)
# Output: 24

print(tensor_3d.shape)
# Output: torch.Size([2, 3, 4])

As the input tensor’s shape is (2, 3, 4), the numel() returns 2 * 3 * 4 = 24.

Empty Tensor

import torch

empty_tensor = torch.tensor([])

counting = torch.numel(empty_tensor)

print(counting)

# Output: 0

Validating tensor reshape

You can use the torch.view() method with the .numel() to ensure a reshape operation is valid (same number of elements).

import torch

tensor = torch.randn(2, 3, 4)

count_elements = torch.numel(tensor)

reshaped_tensor = tensor.view(4, 6)  # Reshape to (4, 6)

reshaped_numel = torch.numel(reshaped_tensor)

print(f"Original numel: {count_elements}")  
# Output: 24

print(f"Reshaped numel: {reshaped_numel}")  
# Output: 24

After reshaping, using the numel() method, we can confirm that both have the same elements.

Post Views: 6
LEAVE A COMMENT Cancel reply
Please Enter Your Comments *

krunallathiya21

All Categories
  • JavaScript
  • Python
  • PyTorch
site logo

Address:  TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

sprintchasetechnologies@gmail.com

(+91) 8000107255.

ABOUT US
  • About
  • Team Members
  • Testimonials
  • Contact

Copyright by @SprintChase  All Rights Reserved

  • PRIVACY
  • TERMS & CONDITIONS