PyTorch資料和NumPy資料之間的互操作

敲程式碼的小風發表於2020-12-05

說明,由於Python的快取協議,只要PyTorch的資料是在cpu上,不是在GPU上,那麼torch.Tensor型別的資料和numpy.ndarray的資料是共享記憶體的,相互之間的改變相互影響.This zero-copy interoperability with NumPy arrays is due to the storage system working with the Python buffer protocol (https://docs.python.org/3/c-api/buffer.html).

numpy轉為torch:

(ssd4pytorch1_2_0) C:\Users\chenxuqi>
(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import torch
>>> points_np = np.ones(12).reshape(3,4)
>>> points_np
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
>>> points = torch.from_numpy(points_np)
>>> points_cuda = torch.from_numpy(points_np).cuda()
>>> ##########################################################
>>> points_np
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
>>> points
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]], dtype=torch.float64)
>>> points_cuda
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]], device='cuda:0', dtype=torch.float64)
>>> points_np[0][0]=999
>>> points[0][1]=888
>>> points_cuda[0][2]=777
>>> points_np
array([[999., 888.,   1.,   1.],
       [  1.,   1.,   1.,   1.],
       [  1.,   1.,   1.,   1.]])
>>> points
tensor([[999., 888.,   1.,   1.],
        [  1.,   1.,   1.,   1.],
        [  1.,   1.,   1.,   1.]], dtype=torch.float64)
>>> points_cuda
tensor([[  1.,   1., 777.,   1.],
        [  1.,   1.,   1.,   1.],
        [  1.,   1.,   1.,   1.]], device='cuda:0', dtype=torch.float64)
>>> id(points_np)
1999751313328
>>> id(points)
1999751939640
>>> id(points_cuda)
1999804358072
>>>
>>> ^Z

torchGPU轉為numpy:

(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> points_cuda = torch.ones(3, 4).cuda()
>>> points_cpu = points_cuda.cpu()
>>> points_np = points_cuda.numpy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
>>> points_np = points_cuda.cpu().numpy()
>>> id(points_np)
1990030518992
>>> id(points_cpu)
1989698386344
>>> id(points_cuda)
1990030519736
>>> points_cuda
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]], device='cuda:0')
>>> points_cpu
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])
>>> points_np
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]], dtype=float32)
>>> points_cuda[0][0] = 99
>>> points_cpu[0][1]=88
>>> points_np[0][2]=77
>>> points_cuda
tensor([[99.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.]], device='cuda:0')
>>> points_cpu
tensor([[ 1., 88.,  1.,  1.],
        [ 1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.]])
>>> points_np
array([[ 1.,  1., 77.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]], dtype=float32)
>>>

torchCPU轉為numpy:

(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> points_cpu = torch.ones(3, 4)
>>> points_np = points_cpu.numpy()
>>> points_cpu
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])
>>> points_np
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]], dtype=float32)
>>> id(points_cpu)==id(points_np)
False
>>> points_cpu[0][0]=999
>>> points_np[0][1]=888
>>> points_np
array([[999., 888.,   1.,   1.],
       [  1.,   1.,   1.,   1.],
       [  1.,   1.,   1.,   1.]], dtype=float32)
>>> points_cpu
tensor([[999., 888.,   1.,   1.],
        [  1.,   1.,   1.,   1.],
        [  1.,   1.,   1.,   1.]])
>>>
>>> type(points_cpu)
<class 'torch.Tensor'>
>>> id(points_np)
1291906427600
>>> type(points_np)
<class 'numpy.ndarray'>
>>>
>>>

相關文章