Course contentsShow
← Machine Learning and Deep Learning
Lesson 778 of 3,53819. PyTorch FundamentalsFree lesson

Tensor Attributes: Shape, Dtype, and Device

Inspect tensor properties including shape, data type (dtype), and device (CPU vs GPU) location.

Tensor Attributes: Shape, Dtype, and Device

What you'll learn: How to inspect the three fundamental properties that define every PyTorch tensor: its shape, data type, and computational device.

The Three Essential Attributes

Every tensor carries metadata that tells you exactly what it contains and where it lives. Think of these attributes like a package label: the shape tells you the dimensions (how big the box is), the dtype tells you what type of items are inside (integers, floats, etc.), and the device tells you the warehouse location (CPU memory or GPU memory).

Shape: The Dimensions

The shape attribute reveals the tensor's structure—how many elements exist along each dimension. A shape of (3, 4) means 3 rows and 4 columns. Access it with .shape:

import torch
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(x.shape)  # torch.Size([2, 3])

Dtype: The Data Type

The dtype attribute specifies the numerical precision: torch.float32 (32-bit floating point), torch.int64 (64-bit integer), etc. This affects memory usage and computation accuracy:

print(x.dtype)  # torch.int64 (default for integer data)

Device: CPU or GPU?

The device attribute tells you where the tensor resides—on CPU (cpu) or GPU (cuda:0, cuda:1, etc.). This is critical because tensors must be on the same device to operate together:

print(x.device)  # cpu

Key Takeaway: Every tensor has three core attributes you can inspect: .shape reveals its dimensions, .dtype shows its data type, and .device indicates whether it lives on CPU or GPU memory.