Tensorflow.js tf.reverse() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js.
The tf.reverse() function is used to reverse a tf.tensors along a specific axis.
Syntax:
tf.reverse(x, axis)
Parameters:
- x: An input tensor to be reversed.
- axis: The set of dimensions to be set.
Return Value: This function returns tf.Tensor.
Example 1:
Javascript
// Importing the tensorflow.js libraryimport * as tf from "@tensorflow/tfjs"// Creating a tensorconst gfg = tf.tensor1d([4, 3, 2, 1]);// Printing tensorgfg.reverse().print(); |
Output:
Tensor
[1, 2, 3, 4]
Example 2:
Javascript
// Importing the tensorflow.js libraryimport * as tf from "@tensorflow/tfjs"// Creating a tensorconst x = tf.tensor2d([2, 1, 4, 3, 6, 5], [3, 2]);// Initialize the axisconst axis = 1;// Printing the tensorx.reverse(axis).print(); |
Output:
Tensor
[[1, 2],
[3, 4],
[5, 6]]
Reference: https://js.tensorflow.org/api/latest/#reverse
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!



