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.maxNorm() is inherited from constraint class.Constraints are the attributes of layers like weight, kernels, biases. It assigns to the layers at the time of model construction.MaxNorm is a weight constraint. It constrains the weight to each hidden layer have norm less than or equal to the desired value. In this post, we are going to see the maxNorm() function and how it works with constraints.
Syntax:
tf.constraints.maxNorm(maxValue, axis)
Parameters:
- maxValue: The maximum norm for incoming weight.
- axis: It is the axis along which norm calculate.
Returns: It returns tf.constraints.Constraint.
Example 1: In this example, we are going to create the maxNorm function and pass the maxValue as 2 and axis as 0.
Javascript
// Importing the tensorflow.Js library import * as tf from "@tensorflow/tfjs" // Use maxNorm() function var a=tf.constraints.maxNorm(2,0) // Print console.log(a) |
Output:
{ "defaultMaxValue": 2, "defaultAxis": 0, "maxValue": 2, "axis": 0 }
Example 2: In this example, we are going to create a model of dense layer and passing the kernel and bias constraint as maxNorm.
Javascript
// Import tensorflow.js import * as tf from "@tensorflow/tfjs" // Define dense layer const denseLayer = tf.layers.dense({ units: 6, kernelInitializer: 'heNormal' , kernelConstraint: 'maxNorm' , biasConstraint: 'maxNorm' , useBias: true }); // Define input and output const inp = tf.ones([2, 3]); const out = denseLayer.apply(inp); // Print the output out.print() |
Output:
Tensor [[0.770891, -0.191616, -1.3709277, -1.010246, 1.0177934, 0.6692461], [0.770891, -0.191616, -1.3709277, -1.010246, 1.0177934, 0.6692461]]
Reference: https://js.tensorflow.org/api/latest/#class:constraints.Constraint