Monday, June 15, 2026
HomeLanguagesJavascriptTensorflow.js tf.divNoNan() Function

Tensorflow.js tf.divNoNan() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.

The tf.divNoNan() function is used to divide two Tensors element-wise and returns 0 if the denominator is 0. It supports broadcasting.

Syntax:

tf.divNoNan (a, b)

Parameters: This function accepts two parameters which are illustrated below:

  • a: The first input tensor as the numerator.
  • b: The second input tensor as the denominator. It should have the same data type as “a”.

Return Value: It returns a Tensor for the result of a/b, where a is the first Tensor and b is the second Tensor. It returns 0 if the denominator is 0.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Initializing some Tensors
const a = tf.tensor1d([2, 5, 7, 10]);
const b = tf.tensor1d([1, 3, 2, 6]);
const c = tf.tensor1d([0, 0, 0, 0]);
 
// Calling the .divNoNan() function
// over the above Tensors as its parameters
a.divNoNan(b).print(); 
a.divNoNan(c).print();


Output:

Tensor
   [2, 1.6666665, 3.5, 1.6666665]
Tensor
   [0, 0, 0, 0]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Broadcasting div a with b and c
const a = tf.tensor1d([3, 6, 11, 17]);
const b = tf.scalar(2);
const c = tf.scalar(0);
 
// Calling the .divNoNan() function
// over the above Tensors as its parameters
a.divNoNan(b).print(); 
a.divNoNan(c).print();


Output:

Tensor
   [1.5, 3, 5.5, 8.5]
Tensor
   [0, 0, 0, 0]
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
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS