Saturday, October 25, 2025
HomeLanguagesJavascriptTensorflow.js tf.constraints.nonNeg() Function

Tensorflow.js tf.constraints.nonNeg() 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.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 library
import * as tf from "@tensorflow/tfjs"
 
// Use nonNeg() function
const constraint = tf.constraints.nonNeg( )
 
// Print
console.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 library
import * as tf from "@tensorflow/tfjs"
 
// Create nonNeg constraint 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
output.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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS