espnet2.enh.layers.complex_utils.reverse
Less than 1 minute
espnet2.enh.layers.complex_utils.reverse
espnet2.enh.layers.complex_utils.reverse(a: Tensor | ComplexTensor, dim=0)
Reverse the elements of a tensor along a specified dimension.
This function takes a tensor (either a PyTorch tensor or a ComplexTensor) and reverses its elements along the specified dimension. If the input is a ComplexTensor, it uses the corresponding function from the torch_complex library; otherwise, it utilizes PyTorch’s built-in flip function.
- Parameters:
- a – A tensor to be reversed. This can be either a PyTorch tensor or a ComplexTensor.
- dim – The dimension along which to reverse the tensor. Default is 0.
- Returns: A tensor of the same type as a, with its elements reversed along the specified dimension.
Examples
>>> import torch
>>> from torch_complex import ComplexTensor
>>> a = torch.tensor([1, 2, 3, 4])
>>> reverse(a, dim=0)
tensor([4, 3, 2, 1])
>>> b = ComplexTensor(torch.tensor([1, 2]), torch.tensor([3, 4]))
>>> reverse(b, dim=0)
ComplexTensor(real=tensor([2, 1]), imag=tensor([4, 3]))
NOTE
- Ensure that the input tensor has the specified dimension.