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.
The tf.square() function is used to return the square of the specified tensor’s elements.
Syntax:
tf.square(x)
Parameters: This function accepts a parameter which is illustrated below:
- x: The specified input tensor.
Return Value: It returns the square of the specified tensor’s elements.
Example 1:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Initializing a tensor of some elements const x = tf.tensor1d([2, 3, 4, 5]); // Calling the .square() function over // the above tensor as its parameter x.square().print(); |
Output:
Tensor [4, 9, 16, 25]
Example 2:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Using a tensor of some elements // as the parameter for the .square() function tf.tensor1d([0, 1, -2, 1.7, -2.5]).square().print(); |
Output:
Tensor [0, 1, 4, 2.8900001, 6.25]