I. Introduction to Functions
The view function in Pytorch is mainly used for Tensor dimension reconstruction, i.e., returning a Tensor with the same data but different dimensions.
Based on the above description, it is clear that the view function should operate on a Tensor type. If it is not of type Tensor, it can be converted by tensor = (data).
II. Examples
▶view(parameter a, parameter b, ...), where the total number of parameters denotes the dimension of the tensor that will be reconstructed.
import torch temp = [1,2,3,4,5,6] # temp is of type list, not Tensor temp = (temp) # Convert temp from list type to Tensor type print(temp) # ([6]) print((2,3)) # Change the dimension of temp to 2*3 print((2,3,1)) # Change the dimension of temp to 2*3*1 print((2,3,1,1)) # More dimensions are no problem,Just make sure the number of elements is the same before and after the dimension change,assume (office)2*3*1*1=6
▶view(parameter a, parameter b, ...), where if a parameter is -1, it means that the dimension depends on the other dimensions, complemented by Pytorch itself.
import torch temp = [[11,12,13,14,15,16], [21,22,23,24,25,26]] temp = (temp) print(temp) # ([2, 6]) print((3,-1,2)) # The -1 here means that the dimension depends on the other dimensions, i.e., it is equal to (2*6)÷3÷2=2 # ([3, 2, 2])
▶view(-1) indicates that the Tensor is converted to a one-dimensional Tensor.
import torch temp = [1,2,3,4,5,6] # temp is of type list, not Tensor temp = (temp) # Convert temp from list type to Tensor type print(temp) # It's a one-dimensional tensor in itself # print((-1)) # So the transformation is still one-dimensional, there's no transformation # temp1 = ([[1,2,3],[4,5,6]]) print(temp1) # ([2, 3]) print((-1)) # Conversion of a multidimensional tensor to a one-dimensional tensor
P.S. Points to note about the view function in Pytorch
I. Description of functions
Pytorch in the view function is mainly used for tensor tensor dimension modification, through the parameters inside the view, can be modified into any dimension of the tensor, provided that the modified tensor size and the original tensor size is the same, such as 45 of the tensor, can be modified into 225, but can not be modified into 32 * 3.
II. Data sharing
The tensor after modifying the dimensions through view, and the original tensor are sharing the underlying data, that is, if you modify the data in one tensor, both tensor data will be changed. For example, if the original tensor V1=(4,5) and the modified tensor V2=(2,2,5), if you make V2[0][0][0][0]=1, then V1[0][0] is also equal to 1.
III. Parameters
A parameter of view with -1 means that the size of this dimension is determined by other positions and only the remaining data dimension can be used.
Or V1=(4,5) and V3=(2,-1,5), then the second dimension size of V3 is 2. You can only have one default -1 dimension at a time, otherwise you can't calculate the exact dimension size.
IV. Dimensional changes
Or V1 = (4,5), V4 = (20,1) denotes a two-dimensional tensor 20*1, while V5 = (20), or V5 = (20,) denotes a one-dimensional tensor of length 20. care needs to be taken when training the model, because both of these have access to a fully connected layer or linear neural network, except that the results coming out will correspond to the dimensions of the inputs, and when calculating the error on the The target is treated differently and needs to be of the same dimension, that is, when loss = (output, label), it is important to note that the dimensions of ouput and label are the same.
summarize
To this point, this Pytorch view function examples to explain the article is introduced to this, more related Pytorch view function content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!