PyTorch数据和NumPy数据之间的互操作

说明,由于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'>
>>>
>>>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值