squeeze

nvtripy.squeeze(input: Tensor, dims: Sequence[int] | int) Tensor[source]

Returns a new tensor with all specified singleton dimensions of the input tensor removed.

Parameters:
  • input (Tensor) – [dtype=T1] The input tensor.

  • dims (Sequence[int] | int) – The singleton dimension(s) to be removed. If this is not provided, all dimensions of size 1 are removed.

Raises:

TripyException – If any of the specified dimensions have a size that is not equal to 1.

Returns:

[dtype=T1] A new tensor.

Return type:

Tensor

TYPE CONSTRAINTS:
Example: Squeeze All Dimensions
1input = tp.iota((1, 2, 1), dtype=tp.float32)
2output = tp.squeeze(input, dims=(0, 2))
Local Variables
>>> input
tensor(
    [[[0.0000],
      [0.0000]]], 
    dtype=float32, loc=gpu:0, shape=(1, 2, 1))

>>> output
tensor([0.0000, 0.0000], dtype=float32, loc=gpu:0, shape=(2,))
Example: Squeeze First Dimension
1input = tp.iota((1, 2, 1), dtype=tp.float32)
2output = tp.squeeze(input, 0)
Local Variables
>>> input
tensor(
    [[[0.0000],
      [0.0000]]], 
    dtype=float32, loc=gpu:0, shape=(1, 2, 1))

>>> output
tensor(
    [[0.0000],
     [0.0000]], 
    dtype=float32, loc=gpu:0, shape=(2, 1))
Example: Squeeze First And Third Dimension
1input = tp.iota((1, 2, 1), dtype=tp.float32)
2output = tp.squeeze(input, (0, 2))
Local Variables
>>> input
tensor(
    [[[0.0000],
      [0.0000]]], 
    dtype=float32, loc=gpu:0, shape=(1, 2, 1))

>>> output
tensor([0.0000, 0.0000], dtype=float32, loc=gpu:0, shape=(2,))