Open In App

Tensorflow.js tf.mod() Function

Last Updated : 25 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads