Open In App

Tensorflow.js tf.norm() 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.

The tf.norm() function is used compute the norm of matrices, vectors, and scalar. This function can also compute several other vector norms such as the 1-norm, the 2-norm or Euclidean, the inf-norm, and in general the p-norm for p > 0 and matrix norms.



Syntax:

tf.norm (x, ord?, axis?, keepDims?)

Parameters: This function accepts four parameters which is illustrated below:



Return Value: It returns tf.tensor.

Below are the examples that illustrates the use of tf.norm() function.

Example 1:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a tensor of some elements
let Norm = tf.tensor1d([15, 14, 23, 52]);
  
// Calling the .norm() function over
// the above tensor as its parameter
// and printing the result.
Norm.norm().print();

Output:

Tensor
    60.44832992553711

Example 2:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a tensor of some elements
let Norm = tf.tensor1d([5, 4, 3, 2]);
  
// Calling the .norm() function over
// the above tensor as its parameter
// and printing the result.
tf.norm(Norm).print();

Output:

Tensor
    7.348469257354736

Reference: https://js.tensorflow.org/api/latest/#norm

Article Tags :