说明,由于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:Userschenxuqi>
(ssd4pytorch1_2_0) C:Userschenxuqi>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:Userschenxuqi>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:
代码语言:javascript复制(ssd4pytorch1_2_0) C:Userschenxuqi>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'>
>>>
>>>