In-depth explanation of Pytorch function - torch.unsqueeze

Category: General catalog of "Pytorch functions in simple terms"
Related articles:
Pytorch functions in simple terms——torch.squeeze
Pytorch functions in simple terms—torch.unsqueeze


Returns a new tensor with a new dimension of size 1 inserted at the specified position. The returned tensor shares the same underlying data as the tensor.

grammar

torch.unsqueeze(input, dim) → Tensor

parameter

  • input:[ Tensor] input tensor
  • dim: [ int] The position index of the inserted dimension, the range is [ -input.dim() − 1 , input.dim() + 1 ] [\text{-input.dim()} - 1, \text{input.dim()} + 1][-input.dim()1,input.dim()+1 ] , the default value isinput.dim() + 1 \text{input.dim()} + 1input.dim()+1

example

enter:

x = torch.tensor([1, 2, 3, 4])
torch.unsqueeze(x, 0)

output:

tensor([[ 1,  2,  3,  4]])

enter:

x = torch.tensor([1, 2, 3, 4])
torch.unsqueeze(x, 1)

output:

tensor([[1],
        [2],
        [3],
        [4]])

Guess you like

Origin blog.csdn.net/hy592070616/article/details/131872525