Open In App

Tensorflow.js tf.constraints.minMaxNorm() Function

Last Updated : 21 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.minMaxNorm() function is used to create a minMaxNorm constraint based on the given config object. It is inherited from constraint class. Constraints are the attributes of layers like weight, kernels, biases. minMaxNorm is a weight constraint.

Syntax:

tf.constraints.minMaxNorm(config)

Parameters: This function takes the config object as a parameter which can have the following properties:

  • maxValue: It specifies the maximum norm for incoming weight.
  • mixValue: It specifies the minimum norm for incoming weight.
  • axis: It specifies the axis along which to calculate norm.
  • rate: It specifies the rate of enforcing the constraints.

Return value: It returns a tf.constraints.Constraint.

Example 1:

Javascript




// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs"
 
// Use maxNorm() function
const constraint = tf.constraints.minMaxNorm(1,0)
   
// Print the output
console.log(constraint)


 
 Output: 

{
  "defaultMinValue": 0,
  "defaultMaxValue": 1,
  "defaultRate": 1,
  "defaultAxis": 0,
  "minValue": 0,
  "maxValue": 1,
  "rate": 1,
  "axis": 0
}

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

Javascript




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


 
 Output: 

Tensor
    [[1.5594537, 0.1787095, 0.3462192, -1.7434707],
     [1.5594537, 0.1787095, 0.3462192, -1.7434707]]

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads