python – PyTorch内存模型:“torch.from_numpy()”vs“torch.Tensor()”

我正在尝试深入了解PyTorch Tensor内存模型的工作原理.# input numpy arrayIn [91]: arr = np.arange(10, dtype=float32).reshape(5, 2)# input tensors in two different waysIn [92]: t1, t2 = torch.Tensor(arr...

我正在尝试深入了解PyTorch Tensor内存模型的工作原理.

# input numpy array
In [91]: arr = np.arange(10, dtype=float32).reshape(5, 2)

# input tensors in two different ways
In [92]: t1, t2 = torch.Tensor(arr), torch.from_numpy(arr)

# their types
In [93]: type(arr), type(t1), type(t2)
Out[93]: (numpy.ndarray, torch.FloatTensor, torch.FloatTensor)

# ndarray 
In [94]: arr
Out[94]: 
array([[ 0.,  1.],
       [ 2.,  3.],
       [ 4.,  5.],
       [ 6.,  7.],
       [ 8.,  9.]], dtype=float32)

我知道PyTorch张量器共享NumPy ndarrays的内存缓冲区.因此,改变一个将反映在另一个.所以,在这里我正在切片并更新Tensor t2中的一些值

In [98]: t2[:, 1] = 23.0

正如预期的那样,它在t2和arr中更新,因为它们共享相同的内存缓冲区.

In [99]: t2
Out[99]: 

  0  23
  2  23
  4  23
  6  23
  8  23
[torch.FloatTensor of size 5x2]


In [101]: arr
Out[101]: 
array([[  0.,  23.],
       [  2.,  23.],
       [  4.,  23.],
       [  6.,  23.],
       [  8.,  23.]], dtype=float32)

但是,t1也会更新.请记住,t1是使用torch.Tensor()构建的,而t2是使用torch.from_numpy()构造的.

In [100]: t1
Out[100]: 

  0  23
  2  23
  4  23
  6  23
  8  23
[torch.FloatTensor of size 5x2]

因此,无论我们使用torch.from_numpy()还是torch.Tensor()从ndarray构造张量,所有这些张量和ndarray共享相同的内存缓冲区.

基于这种理解,我的问题是,为什么只有torch.Tensor()可以完成工作时才存在专用功能torch.from_numpy()?

我查看了PyTorch文档,但它没有提到任何相关内容?有什么想法/建议吗?

解决方法:

from_numpy()自动继承输入数组dtype.另一方面,torch.Tensor是torch.FloatTensor的别名.

因此,如果将int64数组传递给torch.Tensor,则输出张量为浮动张量,它们不会共享存储. torch.from_numpy按照预期给你火炬.LongTensor.

a = np.arange(10)
ft = torch.Tensor(a)  # same as torch.FloatTensor
it = torch.from_numpy(a)

a.dtype  # == dtype('int64')
ft.dtype  # == torch.float32
it.dtype  # == torch.int64

本文标题为:python – PyTorch内存模型:“torch.from_numpy()”vs“torch.Tensor()”

基础教程推荐