Thursday, March 12, 2026
HomeLanguagesJavascriptTensorflow.js tf.max() Function

Tensorflow.js tf.max() 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.max() function is used to calculate the maximum value from the 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.max (x, axis?, keepDims?)

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

  • x: The input tensor for which maximum value is being computed.
  • 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 of maximum value.

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 .max() function over 
// the above tensors
a.max().print();
b.max().print();
c.max().print();


Output:

Tensor
   1
Tensor
   5
Tensor
   7

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 .max() function over 
// the above tensors
a.max(axis1).print();
b.max(axis2, true).print();
c.max(axis1, false).print();
b.max(axis3, false).print();


Output:

Tensor
   1
Tensor
    [[3, 8],]
Tensor
   7
Tensor
   [3, 8]

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

RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS