Friday, October 10, 2025
HomeLanguagesJavascriptTensorflow.js tf.mod() Function

Tensorflow.js tf.mod() 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.mod() function returns element-wise remainder of division.

Operation: floor(x / y) * y + mod(x, y) = x.

Note: It supports broadcasting.

Syntax:

tf.mod(x, y).

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

  • x: It is a Tensor.
  • y: It is a Tensor. It must be of the same type as x.

Return Value: A Tensor. Has the same type as x.

Example 1: We also expose tf.mod() which has the same signature as this operation and asserts that a and b have the same shape (does not broadcast).

Javascript




// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
const x = tf.tensor([9, 12, 3, 20, 7]);
const y = tf.tensor([2, 2, 9, 4, 2]);
  
tf.mod(x,y).print();


Output:

Tensor
    [1, 0, 3, 0, 1]

Example 2: The simplest broadcasting example when an array and a scalar value are combined in an operation:

Javascript




// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Broadcast a mod b.
const x = tf.tensor([2, 4, 5, 8]);
const y = tf.scalar(5);
  
x.mod(y).print();


Output:

Tensor
    [2, 4, 0, 3]

Reference: https://js.tensorflow.org/api/latest/#mod

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!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS