On this page
torch.squeeze
torch.squeeze(input, dim=None) → Tensor-
Returns a tensor with all specified dimensions of
inputof size1removed.For example, if
inputis of shape: then theinput.squeeze()will be of shape: .When
dimis given, a squeeze operation is done only in the given dimension(s). Ifinputis of shape: ,squeeze(input, 0)leaves the tensor unchanged, butsqueeze(input, 1)will squeeze the tensor to the shape .Note
The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other.
Warning
If the tensor has a batch dimension of size 1, then
squeeze(input)will also remove the batch dimension, which can lead to unexpected errors. Consider specifying only the dims you wish to be squeezed.- Parameters
Example:
>>> x = torch.zeros(2, 1, 2, 1, 2) >>> x.size() torch.Size([2, 1, 2, 1, 2]) >>> y = torch.squeeze(x) >>> y.size() torch.Size([2, 2, 2]) >>> y = torch.squeeze(x, 0) >>> y.size() torch.Size([2, 1, 2, 1, 2]) >>> y = torch.squeeze(x, 1) >>> y.size() torch.Size([2, 2, 1, 2]) >>> y = torch.squeeze(x, (1, 2, 3)) torch.Size([2, 2, 2])
© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/2.1/generated/torch.squeeze.html