Sunday, December 29, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.prod() Function

Tensorflow.js tf.prod() 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.prod() function is used to calculate product of the elements of a specified Tensor across its dimension. It reduces the given input elements along the dimensions of axes. If the parameter “keepDims” is true, the reduced dimensions are retained with length 1 else the rank of Tensor is reduced by 1. If the axes parameter has no entries, it returns a Tensor with a single element with all reduced dimensions.

Syntax:

tf.prod (x, axis?, keepDims?)

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

  • x: The input tensor on which prod operation is being computed. If the data type is Boolean value, it will be converted into int32 and the returned output will also be in int32.
  • axis: The specified dimension(s) to reduce. By default it reduces all dimensions. It is optional parameter.
  • keepDims: If this parameter value is true, it retains reduced dimensions with length 1 else the rank of Tensor is reduced by 1. It is also optional parameter.

Return Value: It returns a Tensor for the result of product operation.

Example 1:

Javascript




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


Output:

Tensor
   0
Tensor
   15
Tensor
   56

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Initializing a some tensors
const a = tf.tensor1d([0, 1]);
const b = tf.tensor2d([3, 5, 2, 8], [2, 2]);
const c = tf.tensor1d([2, 4, 7]);
 
// Initializing a axis parameters
const axis1 = -1;
const axis2 = -2;
const axis3 = 0;
 
// Calling the .prod() function over
// the above tensors
a.prod(axis1).print();
b.prod(axis2, true).print();
c.prod(axis1, false).print();
b.prod(axis3, false).print();


Output:

Tensor
   0
Tensor
    [[6, 40],]
Tensor
   56
Tensor
   [6, 40]

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

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

Recent Comments