在这种情况下 "view" 的正式定义是什么?

What is the formal definition for "view" in this context?

考虑名为 The essence of tensors 的子部分中名为 张量:多维数组 部分的以下段落名为 的章节以 Eli Stevens 等人Eli Stevens 等人Deep Learning with PyTorch 一书中的张量 开头

Python lists or tuples of numbers are collections of Python objects that are individually allocated in memory, as shown on the left in figure 3.3. PyTorch tensors or NumPy arrays, on the other hand, are views over (typically) contiguous memory blocks containing unboxed C numeric types rather than Python objects. Each element is a 32-bit (4-byte) float in this case, as we can see on the right side of figure 3.3. This means storing a 1D tensor of 1,000,000 float numbers will require exactly 4,000,000 contiguous bytes, plus a small overhead for the metadata (such as dimensions and numeric type).

他们所指的图如下所示,摘自书中

上面的段落是说张量是对连续内存块的看法。在这种情况下,视图到底是什么意思?

“视图”是您解释此数据的方式,或者更准确地说,是张量的形状。例如,给定一个具有 40 个连续字节(10 个连续浮点数)的内存块,您可以查看它作为 2x5 张量或 5x2 张量。

在pytorch中,改变张量视图的API是view()。一些例子:

Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> x = torch.randn(10, dtype=torch.float32)
>>> x.shape
torch.Size([10])
>>>
>>> x = x.view(2, 5)
>>> x.shape
torch.Size([2, 5])
>>>
>>> x = x.view(5, 2)
>>> x.shape
torch.Size([5, 2])

当然,有些观点是禁止10个花车的:

>>> x = x.view(3, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: shape '[3, 3]' is invalid for input of size 10

view不改变底层内存中的数据。它只是改变了您“查看”张量的方式。