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

Type Casting and Conversions

Convert between data types using .to(), .float(), .int(), .long() and convert to/from NumPy arrays.

Type Casting and Conversions

What you'll learn: How to convert PyTorch tensors between different data types and exchange data with NumPy arrays.

The Core Idea

Just like you might convert a string "42" to an integer 42 in Python, tensors often need their data types changed. Maybe your model expects float32 but your data loaded as int64, or you need to integrate with NumPy-based visualization tools. PyTorch provides simple methods to handle these conversions smoothly.

Think of type casting like exchanging currency: the value stays conceptually the same, but its representation changes to work in a different system.

Converting Between Tensor Types

The .to() method is your Swiss Army knife for type conversion:

import torch

x = torch.tensor([1, 2, 3])  # defaults to int64
x_float = x.to(torch.float32)  # now float32

For common conversions, PyTorch offers shortcuts:

y = x.float()  # convert to float32
z = y.int()    # convert to int32
w = y.long()   # convert to int64

NumPy Integration

Converting between PyTorch tensors and NumPy arrays is seamless:

import numpy as np

# Tensor to NumPy
tensor = torch.tensor([1.0, 2.0, 3.0])
array = tensor.numpy()

# NumPy to Tensor
new_tensor = torch.from_numpy(array)

Warning: These conversions share memory when possible—modifying one affects the other!

Practical Use

Type casting matters when:

  • Loading data (images are often uint8, models need float32)
  • Mixing operations (can't multiply int tensors with float weights)
  • Saving memory (float16 uses half the space of float32)
  • Interfacing with libraries that expect specific types

Key Takeaway: Use .to(), .float(), .int(), .long() to convert tensor data types, and .numpy()/torch.from_numpy() to bridge PyTorch and NumPy seamlessly.