[Pytorch Notes] 2. Tensor operations

Reference video:
Depth Eye official account-01-03-mp4-Tensor operation and linear regression

torch.cat()

Function: Splice two tensors according to the given dimensions.

torch.cat(tensors,
          dim=0,
          out=None)

tensors: a list or tuple containing a bunch of tensors;
dim: specifies the dimension of splicing;
out: which tensor to output.

import numpy as np
import torch

t1 = torch.ones((4, 4))
t2 = torch.zeros((4, 3))
t3 = torch.cat((t1, t2), dim=1)
print(t3)

Output:

tensor([[1., 1., 1., 1., 0., 0., 0.],
        [1., 1., 1., 1., 0., 0., 0.],
        [1., 1., 1., 1., 0., 0., 0.],
        [1., 1., 1., 1., 0., 0., 0.]])

torch.stack()

Function: Stack multiple tensors according to the specified dimensions to create a new tensor.

torch.stack(tensors,
            dim=0,
            out=None)
import numpy as np
import torch

t1 = torch.ones((2, 2))
t2 = torch.zeros((2, 2))
t3 = torch.stack((t1, t2), dim=2)
print(t3)

Output:

tensor([[[1., 0.],
         [1., 0.]],

        [[1., 0.],
         [1., 0.]]])

Two tensors can only differ in the dim dimension, and must be the same size in other dimensions.

torch.chunk()

Function: Split a tensor equally in a given dimension. If it cannot be split equally, the size of the last slice will be smaller than other slices.

torch.chunk(input,
            chunks,
            dim=0)

input: the tensor to be divided;
chunks: the number of parts to be divided;
dim: divided in the dim dimension

import numpy as np
import torch

t1 = torch.ones((4, 2))
t_list = torch.chunk(t1, chunks=2, dim=0)
print(t_list)

Output:

(tensor([[1., 1.],
        [1., 1.]]), tensor([[1., 1.],
        [1., 1.]]))

torch.split()

Function: Split the tensor according to the given length.

torch.split(tensor,
            split_size_or_sections,
            dim=0)

split_size_or_sections: If it is int, the length of the cut will be this value; if it is a list, it will be cut according to the number in the list as the size.
dim: Split on the dim dimension.

import numpy as np
import torch

t1 = torch.ones((4, 2))
t_list = torch.split(t1, (1, 2, 1), dim=0)
print(t_list)

Output:

(tensor([[1., 1.]]), tensor([[1., 1.],
        [1., 1.]]), tensor([[1., 1.]]))

torch.index_select()

Function: In the dimension dim, index the data by index and return the tensor spliced ​​according to the index index.

torch.index_select(input,
                   dim,
                   index,
                   out=None)

input: tensor of data to be searched;
dim: dimension on dimension dim;
index: tensor containing all indexing requirements.

import numpy as np
import torch

t1 = torch.tensor([[2, 3], [4, 5], [6, 7]])
t2 = torch.tensor([0, 2])
t_out = torch.index_select(t1, dim=0, index=t2)
print(t_out)

Output:

tensor([[2, 3],
        [6, 7]])

torch.masked_select()

Function: In the dimension dim, index the data by mask and return the tensor spliced ​​according to the mask index.

torch.masked_select(input,
                    mask,
                    out=None)

input: the tensor of the data to be searched;
mask: the position that needs to be indexed in the dimension dim is True, and the position that does not need to be indexed is False, forming a tensor that is all Boolean. The tensor has the same shape as the input.

import numpy as np
import torch

t1 = torch.tensor([[2, 3], [4, 5], [6, 7]])
t2 = torch.tensor([[True, False],[True, True], [False, True]])
t_out = torch.masked_select(t1, mask=t2)
print(t_out)

output

tensor([2, 4, 5, 7])

torch.reshape()

Function: Transform tensor shape.
(When the tensor is continuous in memory, the reshaped tensor will be stored in the original address)

torch.reshape(input, 
              shape)

input: tensor of the shape to be transformed;
shape: transformed shape. (At most one digit in the shape can be filled in with -1. Since the number of elements in the original tensor is fixed, the number that should be filled in at -1 will be automatically calculated)

import numpy as np
import torch

t1 = torch.tensor([[2, 3, 4], [5, 6, 7]])
t2 = torch.reshape(t1, (3, 2))
print(t2)

Output:

tensor([[2, 3],
        [4, 5],
        [6, 7]])

torch.transpose()

Function: Swap two dimensions of a tensor.

torch.transpose(input,
                dim0,
                dim1)

dim0, dim1: the two dimensions being exchanged.

import numpy as np
import torch

t = torch.tensor([[2, 3, 4], [5, 6, 7]])
t1 = torch.transpose(t, 0, 1)
print(t1)

Output:

tensor([[2, 5],
        [3, 6],
        [4, 7]])

torch.t()

Function: Dimension exchange limited to two-dimensional tensor.
(In a two-dimensional tensor, it is equivalent to torch.transpose(input, 0, 1))

torch.t(input)

input: tensor to be dimensionally exchanged.

import numpy as np
import torch

t = torch.tensor([[2, 3, 4], [5, 6, 7]])
t1 = torch.t(t)
print(t1)

Output:

tensor([[2, 5],
        [3, 6],
        [4, 7]])

torch.squeeze()

Function: Compress the axis with dimension 1.

torch.squeeze(input,
              dim=None,
              out=None)

dim: Specify the dimension for compression. The length of this dimension must be 1.

import numpy as np
import torch

t = torch.tensor([[2], [3], [4], [5], [6]])
t1 = torch.squeeze(t)
print(t1)

Output:

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

torch.unsqueeze()

Function: Expand dimensions according to dimension dim.

torch.unsqueeze(input,
                dim,
                out=None)

dim: Specify the dimension dim for expansion. Different from the default of torch.squeeze(), which is 0, the value of dim must be specified here.

import numpy as np
import torch

t = torch.tensor([2, 3, 4, 5, 6])
t1 = torch.unsqueeze(t, dim=0)
print(t1)

Output:

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

Guess you like

Origin blog.csdn.net/xhyu61/article/details/132924767
Recommended