Skip to content
Related Articles
Open in App
Not now

Related Articles

Tensorflow.js tf.norm() Function

Improve Article
Save Article
  • Last Updated : 12 May, 2021
Improve Article
Save Article

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:

  • x: The specifies the input array.
  • ord: This is optional. It specifies the order of the norm.
  • axis: This is optional. If axis is null, then the input is considered as a vector and a single vector norm is calculated over the whole set of values in the Tensor, i.e. norm(x, ord) is equivalent to norm(x.reshape([-1]), ord). Whereas if axis is an integer, then the input is inspected as a batch of vectors, and axis determines the x-axis over which vector norms is computed. If axis is a 2-tuple of integer it is considered as a batch of matrices and axis determines the axes in N-d array over which to compute a matrix norm.
  • keepDims: This is optional. If it is set to true, then the norm have the same dimension as the input.

Return Value: It returns tf.tensor.

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

Example 1:

Javascript




// 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:

Javascript




// 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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!