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

How to Get the Shape of a Tensor as a List of Integers in PyTorch

Home How to Get the Shape of a Tensor as a List of Integers in PyTorch
Shape of Tensor as a List in PyTorch
  • Written by krunallathiya21
  • May 7, 2025
  • 0 Com
PyTorch

PyTorch provides a .shape attribute (or .size() method) that returns a size object for the specific tensor, and then you can convert it to a list using the list() constructor to get the shape of a tensor as a list of integers.

Get the Shape of a Tensor as a List of Integers in PyTorch

Basically, you can either use a list(tensor.shape) or a list(tensor.size()) expression. PyTorch shapes are always concrete at runtime.

import torch

tensor = torch.randn(2, 3)

shape_list_as_shape = list(tensor.shape)
print(shape_list_as_shape)
# Output: [2, 3]

shape_list_as_size = list(tensor.size())
print(shape_list_as_size)
# Output: [2, 3]
We created a random tensor with normalized values using a torch.randn() method.

For a clearer, readable, and more efficient approach that works for any PyTorch tensor, use list(tensor.shape) expression. The .size() method is an old-school way.

Named Tensors (Advanced)

The named tensors have dimension names, but the shape itself is still a tuple of integers. While initializing a tensor, you can assign names to the tensor, and it becomes a named tensor.

Named tensors and all their associated APIs are an experimental feature and subject to change.
import torch

named_tensor = torch.randn(2, 3, names=("batch", "channel"))

shape_list = list(named_tensor.shape)
print(shape_list)
# Output: [2, 3]

names = named_tensor.names
print(names)
# Output: ('batch', 'channel')

You can see that the named tensor is helpful in user readability, but it does not affect shape extraction.

Scalar tensors

Get the Shape of a Scalar Tensor

The scalar tensor has an empty shape as it is a 0-dimensional tensor, and it has a rank of 0. If you convert the shape into a list, it will be an empty list ([ ]).

import torch

scalar = torch.tensor(5)

scalar_shape = list(scalar.shape)

print(scalar_shape)
# Output: []

To represent a scalar as a 1D tensor (shape [1]), explicitly reshape it.

import torch

scalar = torch.tensor(5)

# Convert the scalar to a 1D tensor
scalar_reshaped = scalar.unsqueeze(0)

shape_list = list(scalar_reshaped.shape)

print(shape_list)
# Output: [1]

And we get a list with one element as a shape because the tensor is now 1D.

Zero-Dim Tensors

If any dimension becomes 0, it is a zero-dimensional tensor, and they are valid, but it contains no data.

import torch

empty_tensor = torch.randn(0, 3)

shape_list = list(empty_tensor.shape)

print(shape_list)
# Output: [0, 3]
If you want to check if a tensor is empty, always use the tensor.numel() == 0 expression.

When Tensor is on the GPU

If you are working on an Nvidia GPU, it does not matter because .shape attribute or .size() method works the same as CPU.

import torch

gpu_tensor = torch.randn(4, 4).cuda()

shape_list = list(gpu_tensor.shape)

print(shape_list)
# Output: [4, 4]

size_list = list(gpu_tensor.size())

print(size_list)
# Output: [4, 4]

Using Unpacking: [*tensor.shape] or [*tensor.size()]

You can unpack the torch.Size object into a list directly using * operator.

import torch

tensor = torch.randn(3, 3)

shape_list = [*tensor.shape]

size_list = [*tensor.size()]

print(shape_list)
# Output: [3, 3]

print(size_list)
# Output: [3, 3]

It’s highly compact and clean for one-liners.

Optional: Utility Function

To get the shape as a list of ints, you can create a custom utility function that accepts a tensor and returns a list. The main advantage is that you can call it from wherever you like and get the desired output.

import torch


def tensor_shape_list(t):
    return list(t.shape)


# Usage
tensor = torch.rand(2, 2, 2)

print(tensor_shape_list(tensor))
# Output: [2, 2, 2]
That’s all!
Post Views: 1
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