For PyTorch's basic data object Tensor (tensor), when dealing with problems, you need to change the dimensionality of the data frequently, in order to facilitate the calculation and further processing at a later stage, this article aims to list some of the methods of dimensional transformations and give examples, so that it is convenient for you to view.
Dimension View: ()
View the dimension of the current tensor
An example:
>>> import torch >>> a = ([[[1, 2], [3, 4], [5, 6]]]) >>> () ([1, 3, 2])
Tensor morphism: (*args) → Tensor
Returns a tensor with the same data but a different size. The returned tensor must have the same data and the same number of elements as the original tensor, but can have a different size. A tensor must be contiguous() to be viewed.
An example:
>>> x = (2, 9) >>> () ([2, 9]) >>> x tensor([[-1.6833, -0.4100, -1.5534, -0.6229, -1.0310, -0.8038, 0.5166, 0.9774, 0.3455], [-0.2306, 0.4217, 1.2874, -0.3618, 1.7872, -0.9012, 0.8073, -1.1238, -0.3405]]) >>> y = (3, 6) >>> () ([3, 6]) >>> y tensor([[-1.6833, -0.4100, -1.5534, -0.6229, -1.0310, -0.8038], [ 0.5166, 0.9774, 0.3455, -0.2306, 0.4217, 1.2874], [-0.3618, 1.7872, -0.9012, 0.8073, -1.1238, -0.3405]]) >>> z = (2, 3, 3) >>> () ([2, 3, 3]) >>> z tensor([[[-1.6833, -0.4100, -1.5534], [-0.6229, -1.0310, -0.8038], [ 0.5166, 0.9774, 0.3455]], [[-0.2306, 0.4217, 1.2874], [-0.3618, 1.7872, -0.9012], [ 0.8073, -1.1238, -0.3405]]])
You can see that the number of data in x and y and z and the size of each are equal, only the size or number of dimensions has changed.
Compression/decompression tensor: (), ()
- (input, dim=None, out=None)
Removes 1 from the input tensor shape and returns it. If the input is of shape (A×1×B×1×C×1×D), then the output shape is: (A×B×C×D)
When dim is given, then the squeeze operation is only on the given dimension. For example, if the input shape is: (A×1×B), squeeze(input, 0) will keep the tensor unchanged, only with squeeze(input, 1) the shape will become (A×B).
The return tensor shares memory with the input tensor, so changing the contents of one changes the other.
An example:
>>> x = (3, 1, 2) >>> x tensor([[[-0.1986, 0.4352]], [[ 0.0971, 0.2296]], [[ 0.8339, -0.5433]]]) >>> ().size() # Remove all dimensions with an element count of 1, without arguments. ([3, 2]) >>> () tensor([[-0.1986, 0.4352], [ 0.0971, 0.2296], [ 0.8339, -0.5433]]) >>> (x, 0).size() # Add the parameter to remove the elements of the first dimension, it doesn't work because there are 2 elements in the first dimension ([3, 1, 2]) >>> (x, 1).size() # Add the parameter, remove the elements of the second dimension, exactly 1, works ([3, 2])
You can see that if you add a parameter, only the positions in the dimension with a size of 1 will disappear
- (input, dim, out=None)
Returns a new tensor with dimension 1 inserted for the input formulation position
The return tensor shares memory with the input tensor, so changing the contents of one changes the other.
If dim is negative, it will be transformed dim+()+1
Moving on to an example using the data above:
>>> (0).size() ([1, 3, 1, 2]) >>> (0) tensor([[[[-0.1986, 0.4352]], [[ 0.0971, 0.2296]], [[ 0.8339, -0.5433]]]]) >>> (-1).size() ([3, 1, 2, 1]) >>> (-1) tensor([[[[-0.1986], [ 0.4352]]], [[[ 0.0971], [ 0.2296]]], [[[ 0.8339], [-0.5433]]]])
You can see that a dimension has been added at the specified location.
Expanded tensor: (*sizes) → Tensor
Returns a new view of the tensor, with the individual dimensions expanded to a larger size. A tensor can also be expanded to higher dimensions, and the newly added dimensions will be appended to the front. Expanding a tensor does not require allocating new memory, it just creates a new view of the tensor, where one dimension will be expanded to a higher dimension by setting stride to 0. Any dimension can be expanded to any value without allocating new memory.
An example:
>>> x = ([[1], [2], [3]]) >>> () ([3, 1]) >>> (3, 4) tensor([[1., 1., 1., 1.], [2., 2., 2., 2.], [3., 3., 3., 3.]]) >>> (3, -1) tensor([[1.], [2.], [3.]])
The original data is 3 rows and 1 column, after expanding it to 3 rows and 4 columns, the effect of filling -1 in the method is the same as that of 1, only the dimension of 1 can be expanded, if it is not 1, it can't be changed, and the dimension of dimension not 1 must be filled in the same way as the original one.
Repetition tensor: (*sizes)
Duplicates the tensor along the specified dimension. Unlike expand(), this function duplicates the data in the tensor.
An example:
>>> x = ([1, 2, 3]) >>> () ([3]) >>> (4, 2) [1., 2., 3., 1., 2., 3.], [1., 2., 3., 1., 2., 3.], [1., 2., 3., 1., 2., 3.]]) >>> (4, 2).size() ([4, 6])
The original data is 1 row and 3 columns, which is expanded 4 times in the row direction and 2 times in the column direction to 4 rows and 6 columns.
The change can be viewed as taking the original data as a whole and repeating it in the specified dimensions and sizes into a 4-row, 2-column matrix where each unit is the same, and then placing the original data into each unit.
Matrix transpose: (input, out=None) → Tensor
Inputs a matrix (2-dimensional tensor) and transposes it to 0, 1 dimensions. This can be thought of as a shorthand function for the function transpose(input, 0, 1).
An example:
>>> x = (3, 5) >>> x tensor([[-1.0752, -0.9706, -0.8770, -0.4224, 0.9776], [ 0.2489, -0.2986, -0.7816, -0.0823, 1.1811], [-1.1124, 0.2160, -0.8446, 0.1762, -0.5164]]) >>> () tensor([[-1.0752, 0.2489, -1.1124], [-0.9706, -0.2986, 0.2160], [-0.8770, -0.7816, -0.8446], [-0.4224, -0.0823, 0.1762], [ 0.9776, 1.1811, -0.5164]]) >>> (x) # Another use tensor([[-1.0752, 0.2489, -1.1124], [-0.9706, -0.2986, 0.2160], [-0.8770, -0.7816, -0.8446], [-0.4224, -0.0823, 0.1762], [ 0.9776, 1.1811, -0.5164]])
It must be a 2-dimensional tensor, i.e. a matrix, to be used.
Dimensional replacement: (), ()
- (input, dim0, dim1, out=None) → Tensor
Returns the transpose of the input matrix input. Swaps the dimensions dim0 and dim1. The output tensor shares memory with the input tensor, so changing one causes the other to be modified as well.
An example:
>>> x = (2, 4, 3) >>> x tensor([[[-1.2502, -0.7363, 0.5534], [-0.2050, 3.1847, -1.6729], [-0.2591, -0.0860, 0.4660], [-1.2189, -1.1206, 0.0637]], [[ 1.4791, -0.7569, 2.5017], [ 0.0098, -1.0217, 0.8142], [-0.2414, -0.1790, 2.3506], [-0.6860, -0.2363, 1.0481]]]) >>> (x, 1, 2).size() ([2, 3, 4]) >>> (x, 1, 2) tensor([[[-1.2502, -0.2050, -0.2591, -1.2189], [-0.7363, 3.1847, -0.0860, -1.1206], [ 0.5534, -1.6729, 0.4660, 0.0637]], [[ 1.4791, 0.0098, -0.2414, -0.6860], [-0.7569, -1.0217, -0.1790, -0.2363], [ 2.5017, 0.8142, 2.3506, 1.0481]]]) >>> (x, 0, 1).size() ([4, 2, 3]) >>> (x, 0, 1) tensor([[[-1.2502, -0.7363, 0.5534], [ 1.4791, -0.7569, 2.5017]], [[-0.2050, 3.1847, -1.6729], [ 0.0098, -1.0217, 0.8142]], [[-0.2591, -0.0860, 0.4660], [-0.2414, -0.1790, 2.3506]], [[-1.2189, -1.1206, 0.0637], [-0.6860, -0.2363, 1.0481]]])
Can transpose multi-dimensional tensors
- (dims)
Transpose the dimension of the tensor
Moving on to an example using the data above:
>>> () ([2, 4, 3]) >>> (2, 0, 1).size() ([3, 2, 4]) >>> (2, 0, 1) tensor([[[-1.2502, -0.2050, -0.2591, -1.2189], [ 1.4791, 0.0098, -0.2414, -0.6860]], [[-0.7363, 3.1847, -0.0860, -1.1206], [-0.7569, -1.0217, -0.1790, -0.2363]], [[ 0.5534, -1.6729, 0.4660, 0.0637], [ 2.5017, 0.8142, 2.3506, 1.0481]]])
Fill in the index of each dimension directly in the method and the tensor swaps the dimensions of the specified dimension, not limited to two-by-two swapping.
This is the whole content of this article.