Wednesday, November 19, 2025
HomeLanguagesJavascriptTensorflow.js tf.constraints.unitNorm() Function

Tensorflow.js tf.constraints.unitNorm() Function

Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The tf.constraints.unitNorm() function us used to create a unitNorm() constraint. It is inherited from constraint class. Constraints are used as attributes for creating tf.layers.Layer. unitNorm constraint constrains every hidden unit which are instance of this weight to have unit norm.

Syntax:

tf.constraints.unitNorm(args) 

Parameters:

  • args: It specifies the object containing configurations.
    1. axis: It specifies the axis along which to calculate norm.

Return value: It returns tf.constraints.Constraint.

Example 1:

Javascript




// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs"
 
// Use unitNorm() function
const constraint = tf.constraints.unitNorm({axis :1})
   
// Print
console.log(constraint)


Output

{
  "defaultAxis": 0,
  "axis": 1
}

Example 2: In this example we will create a dense layer using unitNorm constraint.

Javascript




// Import tensorflow.js
import * as tf from "@tensorflow/tfjs"
 
// Create a new dense layer using unitNorm constraint
const denseLayer = tf.layers.dense({
    units: 4,
    kernelInitializer: 'heNormal',
    kernelConstraint: 'unitNorm',
    biasConstraint: 'unitNorm',
    useBias: true
});
   
// Create input tensor
const input = tf.ones([2, 2]);
 
// Apply dense layer to input tensor
const output = denseLayer.apply(input);
       
// Print the output
output.print()


Output

Tensor
    [[0.3154395, 0.3988628, 1.3295887, -0.0849797],
     [0.3154395, 0.3988628, 1.3295887, -0.0849797]]

Reference: https://js.tensorflow.org/api/1.0.0/#constraints.unitNorm

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!
RELATED ARTICLES

Most Popular

Dominic
32403 POSTS0 COMMENTS
Milvus
96 POSTS0 COMMENTS
Nango Kala
6773 POSTS0 COMMENTS
Nicole Veronica
11924 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11993 POSTS0 COMMENTS
Shaida Kate Naidoo
6900 POSTS0 COMMENTS
Ted Musemwa
7158 POSTS0 COMMENTS
Thapelo Manthata
6858 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS