Skip to main content

How to Find the Smallest Value in a Tensor with tf.reduce_min

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

The darkest pixel in a photo, the lowest price in a list, the coldest hour of the day: each is the smallest value of a tensor, or of one of its rows or columns. tf.reduce_min finds it. This guide finds the cheapest coffee in a small price table in TensorFlow 2.22.0, overall, per town and per cafe.

Syntax​

Syntax
tf.reduce_min(<tensor>, axis=<axis>)
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

The tf.math.reduce_min API page says it "Computes the tf.math.minimum of elements across dimensions of a tensor", and what axis does: "Reduces input_tensor along the dimensions given in axis. Unless keepdims is true, the rank of the tensor is reduced by 1 for each of the entries in axis". And without it: "If axis is None, all dimensions are reduced, and a tensor with a single element is returned." tf.reduce_min is the same function under a shorter name.

The Example​

The price of a coffee at three cafes in two towns: one row per town, one column per cafe.

Cafe 1Cafe 2Cafe 3
Town 13.202.803.50
Town 22.903.102.60

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​

cheapest.py
import tensorflow as tf

# Price of a coffee at three cafes (columns) in two towns (rows)
prices = tf.constant([[3.20, 2.80, 3.50], [2.90, 3.10, 2.60]])

print(tf.reduce_min(prices))
print(tf.reduce_min(prices, axis=1))
print(tf.reduce_min(prices, axis=0))

The cheapest.py script in a macOS-style code window: a prices table of two towns by three cafes, then tf.reduce_min over the whole table, with axis=1 and with axis=0.

Three questions: the cheapest coffee anywhere, the cheapest in each town (along each row, axis=1), and the cheapest at each cafe across the towns (down each column, axis=0).

Step 2: Run It​

Terminal
uv run python cheapest.py
Output
tf.Tensor(2.6, shape=(), dtype=float32)
tf.Tensor([2.8 2.6], shape=(2,), dtype=float32)
tf.Tensor([2.9 2.8 2.6], shape=(3,), dtype=float32)

The terminal after uv run python cheapest.py: 2.6 for the whole table, 2.8 and 2.6 per town with axis=1, and 2.9, 2.8 and 2.6 per cafe with axis=0.

The cheapest coffee costs 2.6. Per town, it's 2.8 in town 1 and 2.6 in town 2: one value per row, so the shape is (2,). Per cafe, it's 2.9, 2.8 and 2.6: one value per column, shape (3,). In each case the axis you named disappeared from the shape. 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_min(tf.constant([], dtype=tf.float32)) returned tf.Tensor(inf, shape=(), dtype=float32): infinity, with no errorCheck tf.size(...) first when a tensor can be empty
Mixing up the axesaxis=0 goes down the columns and gives one result per column; axis=1 goes along the rows and gives one per rowName the axis you want to remove: rows are axis 0 of a table

FAQ​

How do I find where the smallest value is?​

Use tf.argmin, which returns the position instead of the value: tf.argmin(prices, axis=1) returned [1 2], cafe 2 in town 1 and cafe 3 in town 2, counted from 0.

Can I count the axis from the end?​

Yes. axis=-1 means the last axis, so for this table it's the same as axis=1, and it returned [2.8 2.6] too.

Conclusion​

tf.reduce_min found the cheapest coffee in the table, 2.6, then the cheapest per town with axis=1 and per cafe with axis=0. The axis you name is the one that disappears, and an empty tensor gives inf rather than an error.

Additional Resources​