# reverse¶

paddle.fluid.layers.tensor. reverse ( x, axis ) [source]
alias_main

alias

old_api

The OP reverses the tensor `x` along the given `axis`.

```Case 1:

Given a LoDTensor:
x = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
axis = [0, 1]

Then:
output = [[8, 7, 6], [5, 4, 3], [2, 1, 0]]

Case 2:

Given a LoDTensorArray:
x = {[[0, 1], [2, 3]],
[[4, 5, 6]],
[[7],[8], [9]]}
axis = 0

Then:
output = {[[7],[8], [9]],
[[4, 5, 6]],
[[0, 1], [2, 3]]}
```
Parameters
• x (Variable) – A tensor or LoDTensorArray to be reversed, its data type supports bool, float32, float64, int32, int64 and uint8. If input is a LoDTensorArray, returns a new reversed LoDTensorArray without changing the internal order of each inner tensor.

• axis (int|tuple|list) – A dimension or a set of dimensions of `x` to reverse. Must be in the range [-rank( `x` ), rank( `x` )). If it is a tuple or a list, reversing will be apply on each axis in the tuple or list. If input is a LoDTensorArray, the value of axis shall be 0, or a list [0] or tuple (0, ) with shape [1].

Returns

The reversed tensor with the same shape and data type as `x`.

Return type

Variable

Examples

```import paddle.fluid as fluid
import numpy as np
data = fluid.layers.assign(np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype='float32')) # [[0., 1., 2.], [3., 4., 5.], [6., 7., 8.]]
result1 = fluid.layers.reverse(data, 0) # [[6., 7., 8.], [3., 4., 5.], [0., 1., 2.]]
result2 = fluid.layers.reverse(data, [0, 1]) # [[8., 7., 6.], [5., 4., 3.], [2., 1., 0.]]

# example of LoDTensorArray
data1 = fluid.layers.assign(np.array([[0, 1, 2]], dtype='float32'))
data2 = fluid.layers.assign(np.array([[3, 4, 5]], dtype='float32'))
tensor_array = fluid.layers.create_array(dtype='float32')
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0)
fluid.layers.array_write(data1, i, tensor_array)
fluid.layers.array_write(data2, i+1, tensor_array)

reversed_tensor_array = fluid.layers.reverse(tensor_array, 0) # {[[3, 4, 5]], [[0, 1, 2]]}
```