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.
- axis: It specifies the axis along which to calculate norm.
Return value: It returns tf.constraints.Constraint.
Example 1:
Javascript
// Importing the tensorflow.Js libraryimport * as tf from "@tensorflow/tfjs"Â
// Use unitNorm() functionconst constraint = tf.constraints.unitNorm({axis :1})Â Â Â // Printconsole.log(constraint) |
Output
{
"defaultAxis": 0,
"axis": 1
}
Example 2: In this example we will create a dense layer using unitNorm constraint.
Javascript
// Import tensorflow.jsimport * as tf from "@tensorflow/tfjs"Â
// Create a new dense layer using unitNorm constraintconst denseLayer = tf.layers.dense({     units: 4,    kernelInitializer: 'heNormal',    kernelConstraint: 'unitNorm',    biasConstraint: 'unitNorm',    useBias: true});   // Create input tensorconst input = tf.ones([2, 2]);Â
// Apply dense layer to input tensor const output = denseLayer.apply(input);Â Â Â Â Â Â Â // Print the outputoutput.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



