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

问题描述 投票:17回答:3

我正在尝试深入了解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

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

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.from_numpy()可以完成这项工作时,专用函数torch.Tensor()存在?

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

python numpy multidimensional-array deep-learning pytorch
3个回答
7
投票

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

因此,如果将int64数组传递给torch.Tensor,输出张量是浮动张量,它们不会共享存储。 torch.from_numpy按预期给你torch.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

1
投票

这来自_torch_docs.py;也有可能讨论“为什么”here

def from_numpy(ndarray): # real signature unknown; restored from __doc__
    """
    from_numpy(ndarray) -> Tensor

    Creates a :class:`Tensor` from a :class:`numpy.ndarray`.

    The returned tensor and `ndarray` share the same memory. 
    Modifications to the tensor will be reflected in the `ndarray` 
    and vice versa. The returned tensor is not resizable.

    Example::

        >>> a = numpy.array([1, 2, 3])
        >>> t = torch.from_numpy(a)
        >>> t
        torch.LongTensor([1, 2, 3])
        >>> t[0] = -1
        >>> a
        array([-1,  2,  3])
    """
    pass

取自numpy文档:

不同的ndarrays可以共享相同的数据,因此在一个ndarray中进行的更改可能在另一个中可见。也就是说,ndarray可以是另一个ndarray的“视图”,它所指的数据由“基础”ndarray处理。

Pytorch docs

如果给出numpy.ndarraytorch.Tensortorch.Storage,则返回共享相同数据的新张量。如果给出了Python序列,则从序列的副本创建新的张量。


0
投票

在Pytorch中建立张量的推荐方法是使用以下两个工厂函数:torch.tensortorch.as_tensor

torch.tensor总是复制数据。例如,torch.tensor(x)相当于x.clone().detach()

torch.as_tensor总是试图避免数据的副本。 as_tensor避免复制数据的一种情况是原始数据是一个numpy数组。

© www.soinside.com 2019 - 2024. All rights reserved.