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.nonNeg() function us used to create a nonNeg constraint. nonNeg is a non-negative weight constraint. It is inherited from constraint class. Constraints are the attributes of layers.
Syntax:Â
tf.constraints.nonNeg()
Parameters:Â
- w: It specifies the input weight variable. It is an optional parameter.
Return value: It returns tf.constraints.Constraint.
Example 1:Â
Javascript
// Importing the tensorflow.Js libraryimport * as tf from "@tensorflow/tfjs"Â
// Use nonNeg() functionconst constraint = tf.constraints.nonNeg( )Â
// Printconsole.log(constraint) |
OutputÂ
{}
Example 2: In this example we will create a dense layer using nonNeg constraint and apply the layer formed to a tensor.
Javascript
// Importing the tensorflow.Js libraryimport * as tf from "@tensorflow/tfjs"Â
// Create nonNeg constraint using nonNeg() functionconst constraint = tf.constraints.nonNeg()Â
// Create a new dense layer using nonNeg constraintconst denseLayer = tf.layers.dense({    units: 4,    kernelInitializer: 'heNormal',    kernelConstraint: constraint ,    biasConstraint: constraint ,    useBias: true});Â
// Create inputconst input = tf.ones([2, 2]);Â
// Apply denseLayer to inputconst output = denseLayer.apply(input);Â
// Print the outputoutput.print() |
Â
Â
OutputÂ
Tensor
[[-0.7439224, -1.3572885, -1.2860565, 1.3913929],
[-0.7439224, -1.3572885, -1.2860565, 1.3913929]]
Â
Reference: https://js.tensorflow.org/api/1.0.0/#constraints.nonNeg
