In the TensorFlow.js library, tf.reverse() function is used to reverse the elements in a tensor.
tf.reverse() Function – 1D Tensor
If the input tensor is one-dimensional, it doesn’t take any parameters.
Syntax:
Example 1
Create a 1D tensor with 4 integers and reverse them using the tf.reverse() function.
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<script>
//tensor
let tensor = tf.tensor1d([45,67,1,2]);
document.write("<b>Actual Tensor:</b> ",tensor);
document.write("<br>");
//reverse the tensor
document.write("<b>Reversed elements in a Tensor: </b> "+tensor.reverse());
</script>
</body>
</html>
Output:
Elements in a tensor are reversed.
Example 2
Create a 1D tensor with 10 integers and reverse them using tf.reverse().
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<script>
//tensor
let tensor = tf.tensor1d([1,2,3,4,5,6,7,8,9,10]);
document.write("<b>Actual Tensor:</b> ",tensor);
document.write("<br>");
//reverse the tensor
document.write("<b>Reversed elements in a Tensor: </b> "+tensor.reverse());
</script>
</body>
</html>
Output:
Elements in a tensor are reversed.
tf.reverse() Function – 2D Tensor
If the input tensor is two-dimensional, then the syntax is shown below:
Syntax:
Parameter:
It takes an optional parameter axis.
It takes two possible values, 0 and 1.
If axis=0, then rows will be reversed and if axis=0, then columns will be reversed.
If both are not specified, then both are not specified, and elements are reversed in a linear fashion.
Example 1
Create a 2D tensor with 5 rows and 2 columns and reverse the rows of the tensor.
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<script>
//tensor
let tensor = tf.tensor2d([10,2,30,4,5,6,100,8,9,10],[5,2]);
document.write("<b>Actual Tensor:</b> ",tensor);
document.write("<br>");
//reverse the rows of a tensor
document.write("<b>Reversed elements in a Tensor: </b> "+tensor.reverse(0));
</script>
</body>
</html>
Output:
Rows in a tensor are reversed.
Example 2
Create a 2D tensor with 5 rows and 2 columns and reverse the columns of the tensor.
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<script>
//tensor
let tensor = tf.tensor2d([10,2,30,4,5,6,100,8,9,10],[5,2]);
document.write("<b>Actual Tensor:</b> ",tensor);
document.write("<br>");
//reverse the columns of a tensor
document.write("<b>Reversed elements in a Tensor: </b> "+tensor.reverse(1));
</script>
</body>
</html>
Output:
Columns in a tensor are reversed.
Example 3
Create a 2D tensor with 5 rows and 2 columns and reverse the elements in a tensor.
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<script>
//tensor
let tensor = tf.tensor2d([10,2,30,4,5,6,100,8,9,10],[5,2]);
document.write("<b>Actual Tensor:</b> ",tensor);
document.write("<br>");
//reverse the elements of a tensor
document.write("<b>Reversed elements in a Tensor: </b> "+tensor.reverse());
</script>
</body>
</html>
Output:
Here, the axis parameter is not specified. So, elements are reversed in a linear fashion.
Conclusion
In this tutorial, we saw how to reverse elements in one/two-dimensional tensors with the TensorFlow.js library. If the input tensor is one-dimensional, then tf.reverse() won’t take any parameters and simply reverse in a linear fashion. It is possible to reverse rows and columns in a two-dimensional tensor using the axis parameter. If it is not specified, elements are reversed in a linear fashion.