Open In App

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:



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
console.log(constraint)

Output

{
  "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
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


Article Tags :