Skip to content
Related Articles

Related Articles

Tensorflow.js tf.mod() Function

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 25 May, 2021

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.


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).


// 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]);


    [1, 0, 3, 0, 1]

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


// 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);


    [2, 4, 0, 3]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!