torch.squeeze
-
torch.squeeze(input, dim=None, *, out=None) → Tensor
-
Returns a tensor with all the dimensions of
input
of size1
removed.For example, if
input
is of shape: then theout
tensor will be of shape: .When
dim
is given, a squeeze operation is done only in the given dimension. Ifinput
is 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.- Parameters
- Keyword Arguments
-
out (Tensor, optional) – the output tensor.
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])
© 2019 Torch Contributors
Licensed under the 3-clause BSD License.
https://pytorch.org/docs/1.8.0/generated/torch.squeeze.html