Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Tensorflow.js tf.constraints.unitNorm() Function

  • Last Updated : 21 Jul, 2021

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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!




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


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


  "defaultAxis": 0,
  "axis": 1

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


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


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


My Personal Notes arrow_drop_up
Recommended Articles
Page :