flip

tripy.flip(input: Tensor, dims: int | Sequence[int] | None = None) Tensor[source]

Return a new tensor with the same value as the input tensor, with the values in the dimension(s) given in dims reversed. If a value in dims is negative, it is counted backwards from the last dimension.

Note that slicing with a negative step size is implemented using flip; e.g., t[::-1] is equivalent to flipping that dimension.

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

  • dims (int | Sequence[int] | None) – The dimensions that should be reversed. If None, all dimensions will be reversed. If a given dimension is negative, it will be counted backwards from the last dimension.

Returns:

[dtype=T1] A new tensor with the same values as input, with the specified dimensions reversed.

Return type:

Tensor

TYPE CONSTRAINTS:
Example
Example
1input = tp.reshape(tp.arange(10), (2, 5))
2output = tp.flip(input) # equivalent to tp.flip(input, dims=[0, 1])
>>> input
tensor(
    [[0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
     [5.0000, 6.0000, 7.0000, 8.0000, 9.0000]], 
    dtype=float32, loc=gpu:0, shape=(2, 5))
>>> output
tensor(
    [[9.0000, 8.0000, 7.0000, 6.0000, 5.0000],
     [4.0000, 3.0000, 2.0000, 1.0000, 0.0000]], 
    dtype=float32, loc=gpu:0, shape=(2, 5))
Example: Reversing only one dimension.
Reversing only one dimension.
1input = tp.reshape(tp.arange(10), (2, 5))
2output = tp.flip(input, dims=-1)
>>> input
tensor(
    [[0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
     [5.0000, 6.0000, 7.0000, 8.0000, 9.0000]], 
    dtype=float32, loc=gpu:0, shape=(2, 5))
>>> output
tensor(
    [[4.0000, 3.0000, 2.0000, 1.0000, 0.0000],
     [9.0000, 8.0000, 7.0000, 6.0000, 5.0000]], 
    dtype=float32, loc=gpu:0, shape=(2, 5))