Record the mutual conversion between the three most commonly used python objects: ndarray conversion of numpy, cupy and pytorch
1. Interchange between numpy and cupy
import numpy as np
import cupy as cp
A = np.zeros((4,4))
B = cp.asarray(A) # numpy -> cupy
C = cp.asnumpy(B) # cupy -> numpy
print(type(A), type(B), type(C))
output:
<class 'numpy.ndarray'> <class 'cupy._core.core.ndarray'> <class 'numpy.ndarray'>
2. Interchange between numpy and pytorch
import torch
import numpy as np
A = np.zeros((4,4))
B = torch.tensor(A) # numpy -> pytorch 方法1
C = torch.from_numpy(A) # numpy -> pytorch 方法2
D = B.numpy() # pytorch -> numpy
print(type(A), type(B), type(C), type(D))
output:
<class 'numpy.ndarray'> <class 'torch.Tensor'> <class 'torch.Tensor'> <class 'numpy.ndarray'>
To convert a tensor of size 1 to a Python scalar, we can call item
a function or Python's built-in function.
a = torch.tensor([3.5])
a, a.item(), float(a), int(a)
output:
(tensor([3.5000]), 3.5, 3.5, 3)
3. Interchange between cupy and pytorch
import cupy as cp
from torch.utils.dlpack import to_dlpack,from_dlpack
from cupy import fromDlpack
A = cp.zeros((4,4))
B = from_dlpack(A.toDlpack()) # cupy -> pytorch
C = fromDlpack(to_dlpack(B)) # pytorch -> cupy
print(type(A), type(B), type(C))
output:
<class 'cupy._core.core.ndarray'> <class 'torch.Tensor'> <class 'cupy._core.core.ndarray'>
other
The default data type of numpy and cupy is float64, and the default data type of pytorch is float32
import torch
import numpy as np
import cupy as cp
A = np.zeros((4,4))
B = torch.zeros((4,4))
C = cp.zeros((4,4))
A.dtype, B.dtype, C.dtype
output:
(dtype('float64'), torch.float32, dtype('float64'))