Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.constraints.nonNeg() Function

  • Last Updated : 16 Jul, 2021

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.




  • w: It specifies the input weight variable. It is an optional parameter.

Return value: It returns tf.constraints.Constraint.

Example 1: 


// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs"
// Use nonNeg() function
const constraint = tf.constraints.nonNeg( )
// Print



Example 2: In this example we will create a dense layer using nonNeg constraint and apply the layer formed to a tensor.


// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs"
// Create nonNeg constarint using nonNeg() function
const constraint = tf.constraints.nonNeg()
// Create a new dense layer using nonNeg constraint
const denseLayer = tf.layers.dense({
    units: 4,
    kernelInitializer: 'heNormal',
    kernelConstraint: constraint ,
    biasConstraint: constraint ,
    useBias: true
// Create input
const input = tf.ones([2, 2]);
// Apply denseLayer to input
const output = denseLayer.apply(input);
// Print the output



    [[-0.7439224, -1.3572885, -1.2860565, 1.3913929],
     [-0.7439224, -1.3572885, -1.2860565, 1.3913929]]



Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!