Skip to main content

How to Find the Largest Value in a Tensor with tf.reduce_max

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

The brightest pixel, the highest score, the hottest day: each is the largest value of a tensor, or of one of its rows or columns. tf.reduce_max finds it. This guide finds the best scores in a small game table in TensorFlow 2.22.0, overall, per player and per game.

Syntax​

Syntax
tf.reduce_max(<tensor>, axis=<axis>, keepdims=<True or False>)
PartWhat it means
<tensor>The values to search
axis=<axis>Optional. The axis to search along. Leave it out to search every value; for a table, axis=1 gives one result per row and axis=0 one per column
keepdims=<True or False>Optional, False by default. True keeps the searched axis, with a length of 1

The tf.math.reduce_max API page says it "Computes tf.math.maximum of elements across dimensions of a tensor", and explains both options: "Unless keepdims is true, the rank of the tensor is reduced by 1 for each of the entries in axis, which must be unique. If keepdims is true, the reduced dimensions are retained with length 1." tf.reduce_max is the same function under a shorter name.

The Example​

The scores of two players in four games: one row per player, one column per game.

Game 1Game 2Game 3Game 4
Player 11218915
Player 220111714

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Write the Script​

best_scores.py
import tensorflow as tf

# Scores of two players (rows) in four games (columns)
scores = tf.constant([[12, 18, 9, 15], [20, 11, 17, 14]])

print(tf.reduce_max(scores))
print(tf.reduce_max(scores, axis=1))
print(tf.reduce_max(scores, axis=0))

The best_scores.py script in a macOS-style code window: a scores table of two players by four games, then tf.reduce_max over the whole table, with axis=1 and with axis=0.

Three questions: the best score of all, each player's best (along each row, axis=1), and the best score in each game (down each column, axis=0).

Step 2: Run It​

Terminal
uv run python best_scores.py
Output
tf.Tensor(20, shape=(), dtype=int32)
tf.Tensor([18 20], shape=(2,), dtype=int32)
tf.Tensor([20 18 17 15], shape=(4,), dtype=int32)

The terminal after uv run python best_scores.py: 20 for the whole table, 18 and 20 per player with axis=1, and 20, 18, 17 and 15 per game with axis=0.

The best score is 20. Each player's best is 18 and 20, one per row, so the shape is (2,). Each game's best is 20, 18, 17 and 15, one per column, shape (4,). The scores are whole numbers, so the results are int32 too. The command works unchanged in PowerShell.

The first run in a new environment is slow, while TensorFlow loads for the first time.

Common Mistakes​

MistakeWhat happensDo this instead
Searching an empty tensortf.reduce_max(tf.constant([], dtype=tf.float32)) returned tf.Tensor(-inf, shape=(), dtype=float32): minus infinity, with no errorCheck tf.size(...) first when a tensor can be empty
Dropping the axis, then calculating with the tableTo see how far each score is from the player's best, scores - tf.reduce_max(scores, axis=1) stopped with Incompatible shapes: [2,4] vs. [2] [Op:Sub]: the (2,) result no longer lines up with the rowsPass keepdims=True: the result has the shape (2, 1), and the subtraction gave [[-6 0 -9 -3] [0 -9 -3 -6]]

FAQ​

How do I find which game a player scored best in?​

Use tf.argmax, which returns the position instead of the value: tf.argmax(scores, axis=1) returned [1 0], game 2 for player 1 and game 1 for player 2, counted from 0.

Does tf.reduce_max work on decimal numbers?​

Yes. The API page asks for a tensor with a "real numeric type", whole numbers or decimals, and the result has the same type as the tensor.

Conclusion​

tf.reduce_max found the best score, 20, then each player's best with axis=1 and each game's best with axis=0. keepdims=True kept the searched axis with a length of 1, and an empty tensor gave -inf rather than an error.

Additional Resources​