SoFunction
Updated on 2024-11-16

Based on the usage description of view in PyTorch

Equivalent to resize() in numpy, but the usage may not be the same.

My understanding is:

Arrange the data in the original tensor into a one-dimensional data in row-first order (here it should be because of the requirement that the addresses are stored contiguously), and then combine them into tensors of other dimensions according to the parameters.

For example, it doesn't matter if your original data is [[[1,2,3],[4,5,6]]] or [1,2,3,4,5,6], because they are arranged into a one-dimensional vector of 6 elements, so as long as the parameters behind the VIEW are the same, you get the same result.

For example.

a=([[[1,2,3],[4,5,6]]])
b=([1,2,3,4,5,6])
print((1,6))
print((1,6))

The results you get are all

tensor([[1., 2., 3., 4., 5., 6.]]) 

Look at another example:

a=([[[1,2,3],[4,5,6]]])
print((3,2))

Will get:

tensor([[1., 2.],
    [3., 4.],
    [5., 6.]])

This is equivalent to taking an array from 1, 2, 3, 4, 5, 6 and filling it with the desired shapes. But if you want to get the following result:

tensor([[1., 4.],
    [2., 5.],
    [3., 6.]])

It's time to use another function: permute(). See my other blog for usage:Usage of permute in PyTorch

In addition, the parameter may not be null. The -1 in the parameter means that the position is inferred from the number of the other positions, as long as the view parameter can be inferred without ambiguity, i.e., if a person can infer the shape, the view function can also infer it.

For example, the number of data in a tensor is 6. If view(1, -1), we can infer that -1 represents 6 based on the number of elements in the tensor.

And if it's view(-1, -1, 2), the human doesn't know how to infer it, and neither does the machine.

There is another case that a human can infer, but a machine can't: view(-1, -1, 6), where a human can know that all -1's represent 1's, but the machine doesn't allow two negative 1's at the same time.

If there is no -1, then the product of all parameters has to match the total number of elements in the tensor, otherwise an error will occur.

Addendum: () and permute usage in pytorch

() usage in pytorch

You will often see () in pytorch, which represents the transformation of the Tensor's dimensions into the dimensions specified by the view, somewhat similar to the resize function

b=([[[[1,2,3],[4,5,6],[7,8,9]],[[1,2,3],[4,5,6],[7,8,9]]]])
print(())
(1, 2, 3, 3)
print(((0),-1))
tensor([[1., 2., 3., 4., 5., 6., 7., 8., 9., 1., 2., 3., 4., 5., 6., 7., 8., 9.]])
print(((0),-1).size())
(1, 18)

(0) indicates that dimension 0 in b == 1 and -1 is the number of columns automatically assigned according to the original data.

a=([[[1,2,3],[4,5,6]]])
print(())
(1, 2, 3)
print((6,-1))
tensor([[1.],
[2.],
[3.],
[4.],
[5.],
[6.]])
print((6,-1).size())
(6, 1)

Convert a to 6 rows and 1 column

print((-1,6).size())
(1, 6)

Or convert a to 1 row and 6 columns

It is also common to see the view function followed by the permute() function in programs that do dimension transposition

print((-1,6).permute(1,0))
tensor([[1.],
[2.],
[3.],
[4.],
[5.],
[6.]])
print((-1,6).permute(1,0).size())
(6, 1)

With the addition of permute, a goes from (1,6) to (6,1).

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more. If there is any mistake or something that has not been fully considered, please do not hesitate to give me advice.