(tensor, dim=0)
Reverse Tensor or Variable
Reverse Tensor or Variable
Reverse Tensor or Variable
def reverse_order(tensor, dim=0):
    """Reverse Tensor or Variable"""
    if isinstance(tensor, torch.Tensor) or isinstance(tensor, torch.LongTensor):
        idx = [i for i in range(tensor.size(dim)-1, -1, -1)]
        idx = torch.LongTensor(idx)
        inverted_tensor = tensor.index_select(dim, idx)
    if isinstance(tensor, torch.cuda.FloatTensor) or isinstance(tensor, torch.cuda.LongTensor):
        idx = [i for i in range(tensor.size(dim)-1, -1, -1)]
        idx = torch.cuda.LongTensor(idx)
        inverted_tensor = tensor.index_select(dim, idx)
        return inverted_tensor
    elif isinstance(tensor, Variable):
        variable = tensor
        variable.data = reverse_order(variable.data, dim)
        return variable
def reverse_order ( tensor , dim = 0 ) : if isinstance ( tensor , torch . Tensor ) or isinstance ( tensor , torch . LongTensor ) : idx = [ i for i in range ( tensor . size ( dim ) - 1 , - 1 , - 1 ) ] idx = torch . LongTensor ( idx ) inverted_tensor = tensor . index_select ( dim , idx ) if isinstance ( tensor , torch . cuda . FloatTensor ) or isinstance ( tensor , torch . cuda . LongTensor ) : idx = [ i for i in range ( tensor . size ( dim ) - 1 , - 1 , - 1 ) ] idx = torch . cuda . LongTensor ( idx ) inverted_tensor = tensor . index_select ( dim , idx ) return inverted_tensor elif isinstance ( tensor , Variable ) : variable = tensor variable . data = reverse_order ( variable . data , dim ) return variable