How to access the metadata of a tensor in PyTorch?

In PyTorch, tensor metadata includes essential information like size, shape, data type, and device location. The most commonly accessed metadata are the tensor's dimensions and total number of elements.

Key Metadata Properties

PyTorch tensors provide several ways to access metadata:

  • .size() − Returns the dimensions as a torch.Size object
  • .shape − Returns the same dimensions as .size()
  • torch.numel() − Returns the total number of elements
  • .dtype − Returns the data type
  • .device − Returns the device (CPU/GPU)

Example 1: 2D Tensor Metadata

import torch

# Create a 4x3 tensor
T = torch.tensor([[1,2,3],[2,1,3],[2,3,5],[5,6,4]])
print("Tensor T:")
print(T)

# Access size and shape
print("\nSize using .size():", T.size())
print("Shape using .shape:", T.shape)

# Get total number of elements
print("Number of elements:", torch.numel(T))

# Additional metadata
print("Data type:", T.dtype)
print("Device:", T.device)
Tensor T:
tensor([[1, 2, 3],
        [2, 1, 3],
        [2, 3, 5],
        [5, 6, 4]])

Size using .size(): torch.Size([4, 3])
Shape using .shape: torch.Size([4, 3])
Number of elements: 12
Data type: torch.int64
Device: cpu

Example 2: 3D Tensor Metadata

import torch

# Create a 3D tensor with random values
T = torch.randn(2, 3, 4)
print("3D Tensor shape:", T.shape)
print("Total elements:", torch.numel(T))

# Access specific dimensions
print("Dimension 0 (batch):", T.size(0))
print("Dimension 1 (height):", T.size(1)) 
print("Dimension 2 (width):", T.size(2))

# Check if tensor is on GPU (if available)
print("Device:", T.device)
print("Data type:", T.dtype)
3D Tensor shape: torch.Size([2, 3, 4])
Total elements: 24
Dimension 0 (batch): 2
Dimension 1 (height): 3
Dimension 2 (width): 4
Device: cpu
Data type: torch.float32

Comparison of Methods

Method Returns Use Case
.size() torch.Size object Can access specific dimensions
.shape torch.Size object More readable, NumPy-like syntax
torch.numel() Integer Memory usage calculations

Conclusion

Use .shape for readable dimension access, .size(dim) for specific dimensions, and torch.numel() for element counting. These metadata properties are essential for tensor operations and debugging.

Updated on: 2026-03-26T18:39:21+05:30

934 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements