dev-resources.site
for different kinds of informations.
Device conversion with to() and from_numpy() and numpy() in PyTorch
Published at
7/16/2024
Categories
python
pytorch
deviceconversion
numpy
Author
hyperkai
Author
8 person written this
hyperkai
open
*My post explains how to create and acceess a tensor.
to() can do device conversion as shown below:
*Memos:
-
to()
can be used with a tensor but not with torch. - The 1st argument with a tensor is
device
(Optional-Defalut:None
-Type:str
,int
or device()): *Memos:- If it's
None
, the device of a tensor is not converted. -
cpu
,cuda
,ipu
,xpu
,mkldnn
,opengl
,opencl
,ideep
,hip
,ve
,fpga
,ort
,xla
,lazy
,vulkan
,mps
,meta
,hpu
,mtia
orprivateuseone
can be set todevice
. - Setting
0
todevice
usescuda
(GPU). *The number must be zero or positive. - My post explains device().
- If it's
- A copied tensor can be created.
import torch
cpu_tensor = torch.tensor([0, 1, 2])
cpu_tensor.device
# device(type='cpu')
cpu_tensor.to().device
# device(type='cpu')
gpu_tensor = cpu_tensor.to(device='cuda:0')
gpu_tensor = cpu_tensor.to(device='cuda')
gpu_tensor = cpu_tensor.to(device=0)
gpu_tensor = cpu_tensor.to(device=torch.device(device='cuda:0'))
gpu_tensor = cpu_tensor.to(device=torch.device(device='cuda'))
gpu_tensor = cpu_tensor.to(device=torch.device(device=0))
gpu_tensor = cpu_tensor.to(device=torch.device(type='cuda', index=0))
gpu_tensor = cpu_tensor.to(device=torch.device(type='cuda'))
gpu_tensor.device
# device(type='cuda', index=0)
gpu_tensor.to().device
# device(type='cuda', index=0)
cpu_tensor = gpu_tensor.to(device='cpu')
cpu_tensor.device
# device(type='cpu')
cuda() and cpu() can change the device of a tensor to GPU(CUDA) and CPU respectively as shwon below:
*Memos:
-
cuda()
orcpu()
can be used with a tensor but not withtorch
. - For
cuda()
, the 1st argument with a tensor isdevice
(Optional-Default:None
-Type:str
,int
or device()). *Memos: - A copied tensor is created.
import torch
cpu_tensor = torch.tensor([0, 1, 2])
cpu_tensor.device
# device(type='cpu')
gpu_tensor = cpu_tensor.cuda()
gpu_tensor = cpu_tensor.cuda(device='cuda:0')
gpu_tensor = cpu_tensor.cuda(device='cuda')
gpu_tensor = cpu_tensor.cuda(device=0)
gpu_tensor = cpu_tensor.cuda(device=torch.device(device='cuda:0'))
gpu_tensor = cpu_tensor.cuda(device=torch.device(device='cuda'))
gpu_tensor = cpu_tensor.cuda(device=torch.device(device=0))
gpu_tensor = cpu_tensor.cuda(device=torch.device(type='cuda', index=0))
gpu_tensor = cpu_tensor.cuda(device=torch.device(type='cuda'))
gpu_tensor.device
# device(type='cuda', index=0)
cpu_tensor = gpu_tensor.cpu()
cpu_tensor.device
# device(type='cpu')
from_numpy() can convert a NumPy array to a PyTorch tensor as shown below:
*Memos:
-
from_numpy()
can be used withtorch
but not with a tensor. - The 1st argument with
torch
(Required-Type:ndarray
). *There is no keyword argument. - The type of a NumPy array is also inherited to a PyTorch tensor.
import torch
my_array = np.array([0., 1., 2.])
my_array.dtype
# dtype('float64')
my_tensor = torch.from_numpy(my_array)
my_tensor
# tensor([0., 1., 2.], dtype=torch.float64)
numpy() can convert a PyTorch tensor to a NumPy array as shown below:
*Memos:
-
numpy()
can be used with a tensor but not withtorch
. - There is
force
argument with a tensor(Optional-Default:False
-Type:bool
). *Memos:- If it's
True
, a GPU(CUDA) PyTorch tensor can be converted to a NumPy array which may be a copy. -
force=
must be used.
- If it's
- The type of a PyTorch tensor is also inherited to a NumPy array.
import torch
my_tensor = torch.tensor([0., 1., 2.])
my_tensor.dtype
# torch.float32
my_array = my_tensor.numpy()
my_array
# array([0., 1., 2.], dtype=float32)
my_tensor = torch.tensor([0., 1., 2.], device='cuda:0')
my_tensor.numpy(force=True)
# array([0., 1., 2.], dtype=float32)
my_tensor = torch.tensor([0., 1., 2.], device='cuda:0')
my_tensor.numpy()
my_tensor.numpy(force=False)
# Error
numpy Article's
30 articles in total
Basics of Python in 1 minute
read article
Previewing a .npy file
read article
Python NumPy Tutorial for Beginners: Learn Array Creation, Indexing, and More
read article
A Visual Guide to Affine Transformations: Translation, Scaling, Rotation, and Shear
read article
NumPy for Machine Learning & Deep Learning
read article
Investigating the performance of np.einsum
read article
The Unreasonable Usefulness of numpy's einsum
read article
ML Zoomcamp Week 1
read article
Python Data Wrangling and Data Quality
read article
Build Your Own AI Language Model with Python and NumPy
read article
Streamline Your NumPy File Conversions with npyConverter
read article
Streamline Plots: NumPy to Jupyter, No Loops
read article
PYTHON 101: INTRODUCTION TO PYTHON FOR DATA ANALYTICS
read article
How to Install NumPy in PyCharm on a Mac
read article
NumPy for the Curious Beginner
read article
NumPy: The Superhero Library Python Deserves (But Maybe Didn't Know It Needed)
read article
Transforming Simplicity: Adapting Linear Regression to Capture Complex Non-Linear Phenomena with NumPy
read article
A Beginner's Guide to Python Libraries
read article
fix: A module that was compiled using NumPy 1.x cannot be run in NumPy 2.0.0 as it may crash
read article
NumPy Asarray Function: A Comprehensive Guide
read article
Device conversion with to() and from_numpy() and numpy() in PyTorch
currently reading
Master Linear Regression with NumPy: Step-by-Step Guide to Building and Optimizing Your First Model!
read article
A Comprehensive Guide to NumPy with Python ๐๐ฒ
read article
Creating Line Plots with Object-Oriented API and Subplot Function in Python
read article
Numpy Isnumeric Function: Mastering Numeric String Validation
read article
Element-Wise Numerical Operations in NumPy: A Practical Guide with Examples
read article
NumPy for Beginners: Why You Should Rely on Numpy Arrays More
read article
NumPy's Argmax? How it Finds Max Elements from Arrays
read article
5 Exciting NumPy Challenges to Boost Your Programming Skills! ๐
read article
Array Manipulation: A Deep Dive into Insertions and Deletions
read article
Featured ones: