torch.Size([2, 6])
A view of a tensor x
is a tensor y
that points to the same data as x
but has a different shape. Because x
and y
share the same storage, creating a view is cheap: the only information that needs to be updated is meta-data in the form of the shape and the stride. The data in the storage is left untouched.
How can we translate indexes in x
into indexes in y
? For example, how do we find the element at index \((1, 2, 0)\) from x
in y
? Because the view took us from three 2-dimensional vectors to one 6-dimensional vector, we should expect it at index \((1, 4)\). Let’s check: