Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute
menu
close

Need Help? Talk to an Expert

+91 8000107255
Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute

Need Help? Talk to an Expert

+91 8000107255

torch.min(): Finding Minimum Value in a Tensor

Home torch.min(): Finding Minimum Value in a Tensor
PyTorch torch.min() Method with examples
  • Written by krunallathiya21
  • May 13, 2025
  • 0 Com
PyTorch

PyTorch torch.min() method finds the minimum value(s) across all tensor elements or along the specified dimension, or the element-wise minimum between two tensors.

torch.min() method

It returns a scalar-tensor for the global minimum value, a tuple (value, indices) for the dimension-specific minimum, or a tensor for the element-wise minimum, depending on your scenario. Additionally, it will return the index of the minimum value, optionally.

Syntax

torch.min(input, dim, keepdim=False, other)

# OR

torch.min(input)

Parameters

Argument Description
input (Tensor) It represents an input tensor from which we must find the minimum value.
dim (int, optional) It represents the dimension for reduction.
keepdim (bool, optional)

It is False by default, but it retains reduced dimension if True.

other (Tensor, optional) If you are looking for element-wise comparison between two tensors, this argument “other” is the second tensor to compare with.

Finding a global minimum

Let’s define a 1D tensor and find the minimum value from this tensor.
import torch

tensor = torch.tensor([40, 60, 20])

min_scalar_tensor = torch.min(tensor)

print(min_scalar_tensor)

# Output: tensor(20)

The output tensor(20) is a minimum scalar tensor object.

Minimum value in 2D Tensor

torch.min() method on 2D tensor

What if our input is a 2D tensor? In that case, if you don’t define the dimension along, it will return the global minimum value from the tensor.

import torch

tensor_2d = torch.tensor([[1.2, 1.1, -3.2], 
                          [5.5, 5.6, 0.0]])

minimum_from_across_2d = torch.min(tensor_2d)

print(minimum_from_across_2d)

# Output: tensor(-3.2000)

You can see that the only negative value is -3.2000, which is the minimum value; hence, our output is a tensor(-3.2000).

Minimum along the dimension (dim=0 and dim=1)

If you pass a dim argument, it will return a tuple consisting of a tensor of values and indices.

Along the columns

Along column-wise (dim=0)

Let’s find the minimum value along the columns by passing dim = 0.

import torch

tensor_2d = torch.tensor([[1.2, 5.6, -3.2],
                          [5.5, 1.1, 8.1]])

minimum_value_along_column, minimum_index_column = torch.min(tensor_2d, dim=0)

print(minimum_value_along_column)
# Output: tensor([1.2000, 1.1000, -3.2000])

print(minimum_index_column)
# Output: tensor([0, 1, 0])

Let me explain the above output:

  1. In the first column, there are two values: 1.2 and 5.5; you can see that 1.2 is the minimum value, and its index is 0. The index of the first element of the column is 0, and the second is 1, and so on.
  2. The second column has two values: 5.6 and 1.1; 1.1 is the minimum value, and its index is 1. 
  3. In the third column, there are two values: -3.2 and 8.1; -3.2 is the minimum value, and its index is 0.

Hence, the outputs are tensor([1.2000, 1.1000, -3.2000]) and tensor([0, 1, 0]).

Along the rows

Along row-wise (dim=1)

In the above figure, the first element of the first row has the 0th index. The second element of the first row has 1st index. The third element of the row has the 2nd index. You need to count the index row-wise.

Let’s find the minimum value along the rows by passing dim = 1.

import torch

tensor_2d = torch.tensor([[1.2, 5.6, -3.2],
                          [5.5, 1.1, 8.1]])

minimum_value_along_row, minimum_index_row = torch.min(tensor_2d, dim=1)

print(minimum_value_along_row)
# Output: tensor([-3.2000,  1.1000])

print(minimum_index_row)
# Output: tensor([2, 1])

In the first row, there are three values: 1.2, 5.6, and -3.2; you can see that -3.2 is the minimum value, and its index is 2. The index of the first element of the row is 0, the second is 1, and the third is 2.

The second row has three values: 5.5, 1.1, and 8.1; 1.1 is the minimum value, and its index is 1.

Hence, the outputs are tensor([-3.2000, 1.1000]) and tensor([2, 1]).

Element-wise minimum

For element-wise comparison, we need two tensors, each of which must be broadcastable to the same shape.

Passing both tensors to this method will return a tensor of the same shape with the minimum values.

import torch

tensor1 = torch.tensor([[11.0, 2.0], [3.0, 46.0]])
tensor2 = torch.tensor([[4.0, 14.0], [44.0, 37.0]])

element_wise_minimum = torch.min(tensor1, tensor2)

print(element_wise_minimum)

# Output:
# tensor([[ 4.,  2.],
#         [ 3., 37.]])

In the above code, torch.min() method compares tensor1 and tensor2 element by element, and keeps the smaller value from each corresponding position.

It is like comparing output_tensor[i][j] = min(tensor1[i][j], tensor2[i][j]), returning a new tensor with the smallest values at each position.

Broadcasting

You will ask what broadcastable means. Two tensors are broadcastable if PyTorch can automatically expand one or both to match in shape for element-wise operations.

import torch

tensor1 = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
tensor2 = torch.tensor([1.5, 2.5])

broadcasting = torch.min(tensor1, tensor2)

print(broadcasting)

# Output:
# tensor([[1.0000, 2.0000],
#         [1.5000, 2.5000]])

In the above code, the shape of the first input tensor1 is [2, 2], and the second input tensor2 is [2].

Here, PyTorch automatically expands tensor2 to match the shape of tensor1 by treating it as a row vector (i.e., [1.5, 2.5] gets repeated for each row).

So, the tensor2 becomes [[1.5, 2.5], [1.5, 2.5]], and now you can find the element-wise minimum value between two tensors.

With keepdim=True

Let’s retain dimensionality for broadcasting compatibility by passing keepdim = True argument.

import torch

tensor = torch.tensor([[21.0, 3.0], [0.0, 19.0]])

min_vals, _ = torch.min(tensor, dim=1, keepdim=True)

print(min_vals)

# Output:
tensor([[3.],
        [0.]])

The above output shows that we preserved the original tensor’s dimensionality (e.g., for broadcasting).

NaN handling

What if your input tensor contains a NaN value? How will it find the minimum value in that case? Well, if the tensor has NaN, it returns the NaN as the smaller value because we don’t know which value NaN represents.

For checking if a tensor contains a NaN value, use torch.isnan() method.
import torch

tensor_with_nan = torch.tensor([float('nan'), 12, 10])

min_nan = torch.min(tensor_with_nan)

print(min_nan)

# Output: tensor(nan)

And we get the tensor object of a NaN value.

To find the maximum value from the tensor, use torch.max() method. Additionally, to find the indices of the max value, use torch.argmax() method.

That’s all!

Post Views: 86
LEAVE A COMMENT Cancel reply
Please Enter Your Comments *

krunallathiya21

All Categories
  • JavaScript
  • Python
  • PyTorch
image
image
image
image
image
Sprint Chase Technologies logo

Address: TwinStar, South Block – 1202, 150 Feet Ring Rd, near RK Prime, Ram Vihar Society, Chandreshnagar, Rajkot, Gujarat 360004

sprintchasetechnologies@gmail.com

+91 8000107255

ABOUT US
  • About
  • Team Members
  • Testimonials
  • Contact
SUPPORT
  • Content Strategy
  • Copywriting
  • Content Marketing
  • Web Design
QUICK LINKS
  • Marketplace
  • Documentation
  • Customers
  • Carrers
INSTAGRAM

Sprint Chase Technologies. All Rights Reserved.

  • PRIVACY
  • TERMS & CONDITIONS