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 tensordim
: [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]])