PyTorch Tensor Types

Every tensor in PyTorch stores numbers of a specific type — called its data type or dtype. Choosing the right dtype affects your model's accuracy, memory usage, and training speed. This page explains each type, when to use it, and how to convert between them.

Why Data Types Matter

Imagine you are measuring the temperature outside. You could write it as a whole number (23) or as a decimal (23.47). Whole numbers take less space but lose precision. Decimals are more accurate but cost more memory. Tensor dtypes work the same way — you trade off between precision and memory depending on what the task needs.

The Main PyTorch Data Types

PyTorch dtype         Python type     Bits   Use case
─────────────────────────────────────────────────────────────
torch.float32         float           32     Default for models
torch.float64         float           64     High-precision math
torch.float16         float           16     Fast GPU training
torch.int32           int             32     Integer counting
torch.int64           int             64     Large integers, indices
torch.bool            bool            8      True/False masks
torch.uint8           int             8      Image pixel values (0–255)

float32 – The Default for Neural Networks

When you create a tensor from decimal numbers, PyTorch uses float32 by default. This 32-bit floating point type gives a good balance between precision and speed. Almost all model weights, activations, and predictions use float32.

import torch

t = torch.tensor([1.5, 2.7, 3.9])
print(t.dtype)   # torch.float32

int64 – The Default for Integers

When you create a tensor from whole numbers, PyTorch uses int64 by default. Integer tensors are commonly used for class labels in classification tasks — for example, label 0 means "cat," label 1 means "dog," label 2 means "bird."

labels = torch.tensor([0, 1, 2, 1, 0])
print(labels.dtype)   # torch.int64

float16 – For Fast GPU Training

Half-precision (float16) uses half the memory of float32 and runs faster on modern GPUs. This lets you fit larger batches of data into GPU memory, which speeds up training. The trade-off is slightly lower numerical precision.

t32 = torch.tensor([1.0, 2.0])          # float32
t16 = t32.to(torch.float16)             # convert to float16
print(t16.dtype)   # torch.float16

bool – For Masks and Conditions

Boolean tensors store only True or False. They appear frequently when you want to filter elements of a tensor — for example, keeping only values greater than a threshold.

t = torch.tensor([1.0, -2.0, 3.0, -4.0])
mask = t > 0
print(mask)         # tensor([ True, False,  True, False])
print(t[mask])      # tensor([1., 3.])  ← only positive values

uint8 – For Image Pixel Data

Images stored as raw pixel values use integers from 0 to 255. The uint8 type stores exactly this range using 8 bits per value. When you load an image with libraries like PIL or OpenCV, it arrives as uint8. You then convert to float32 and normalize before feeding it into a model.

import torch

# Simulating a tiny 2x2 grayscale image with uint8 values
img = torch.tensor([[128, 255], [0, 64]], dtype=torch.uint8)
print(img.dtype)   # torch.uint8

# Convert to float32 and normalize to [0, 1]
img_float = img.float() / 255.0
print(img_float)

Diagram: Image Preprocessing Pipeline

Raw Image File
     │
     ▼
Load as uint8 tensor  [0 → 255 integer values]
     │
     ▼
Convert to float32    [still 0 → 255, now decimals]
     │
     ▼
Normalize             [divide by 255 → values 0.0 → 1.0]
     │
     ▼
Feed into Model       [model expects float32 in range 0–1]

Converting Between Types

Use .to(dtype) or shortcut methods like .float(), .long(), and .bool() to convert tensors between types.

t = torch.tensor([1, 2, 3])      # int64
print(t.dtype)                   # torch.int64

t_float = t.float()              # → float32
t_long  = t.long()               # → int64 (already, no change)
t_half  = t.half()               # → float16
t_bool  = t.bool()               # → bool (0=False, non-zero=True)

print(t_float.dtype)   # torch.float32
print(t_bool)          # tensor([True, True, True])

Type Mismatch Errors

One of the most common beginner mistakes in PyTorch is mixing incompatible tensor types. For example, you cannot add a float32 tensor to an int64 tensor without converting one of them first.

a = torch.tensor([1.0, 2.0])   # float32
b = torch.tensor([1, 2])       # int64

# This raises a RuntimeError:
# print(a + b)

# Fix: convert b to float
print(a + b.float())           # tensor([2., 4.])

Checking Type Before Operations

Before performing operations on tensors, always confirm their types match. A simple check saves confusing error messages later.

def safe_add(a, b):
    print(f"a: {a.dtype}, b: {b.dtype}")
    b = b.to(a.dtype)   # convert b to match a's type
    return a + b

Summary

PyTorch tensors store numbers as a specific data type. float32 is the default for model weights and activations. int64 is the default for integer tensors and class labels. float16 speeds up GPU training. uint8 stores raw image pixels. bool stores True/False masks for filtering. Convert between types using .to(dtype) or shortcut methods like .float() and .long(). Always match tensor types before performing math operations to avoid runtime errors.

Leave a Comment

Your email address will not be published. Required fields are marked *