From 6f8bf5fedf8ec3d35b90a130c01d20333d8d3801 Mon Sep 17 00:00:00 2001 From: Kim Kutrovacz Date: Mon, 9 Feb 2026 13:19:40 -0600 Subject: [PATCH 1/3] Add entry for PyTorch .argmin() tensor operation --- .../tensor-operations/terms/argmin/argmin.md | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/argmin/argmin.md diff --git a/content/pytorch/concepts/tensor-operations/terms/argmin/argmin.md b/content/pytorch/concepts/tensor-operations/terms/argmin/argmin.md new file mode 100644 index 00000000000..19fbf4d4f8d --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/argmin/argmin.md @@ -0,0 +1,61 @@ +--- +Title: '.argmin()' +Description: 'Returns the index of the minimum value in a PyTorch tensor, or along a specified dimension.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Deep Learning' + - 'Methods' + - 'PyTorch' + - 'Tensor' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/data-science' +--- + +The **`.argmin()`** method in PyTorch returns the index of the minimum value in a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors). If a dimension is specified, it returns the indices of the minimum values along that dimension. This method is commonly used in tasks such as finding the closest data point, selecting the best prediction, or identifying the least likely class in machine learning workflows. + +## Syntax +```pseudo +torch.argmin(input, dim=None, keepdim=False) +``` + +- `input` (Tensor): The input tensor to search for the minimum value. +- `dim` (int, optional): The dimension to reduce. If not specified, the index of the minimum value in the flattened tensor is returned. +- `keepdim` (bool, optional): Whether the output tensor retains the reduced dimension. Defaults to `False`. + +The `.argmin()` method returns a `LongTensor` containing the index or indices of the minimum value(s). + +## Example + +This example shows how to use the `.argmin()` method to find the index of the minimum value in a 2D tensor: +```py +import torch + +# Define a 2D tensor +tensor = torch.tensor([[8, 3, 5], + [2, 7, 4]]) + +# Index of minimum in flattened tensor +print(torch.argmin(tensor)) + +# Index of minimum along each column (dim=0) +print(torch.argmin(tensor, dim=0)) + +# Index of minimum along each row (dim=1) +print(torch.argmin(tensor, dim=1)) +``` + +This example results in the following output: +```shell +tensor(3) +tensor([1, 0, 1]) +tensor([1, 0]) +``` + +In this example: + +- **Flattened tensor**: The tensor is treated as `[8, 3, 5, 2, 7, 4]`, and the minimum value `2` is at index `3`. +- **Along columns (dim=0)**: The minimum values in each column are `2`, `3`, and `4`, found in rows `1`, `0`, and `1`. +- **Along rows (dim=1)**: The minimum values in each row are `3` (at index `1`) and `2` (at index `0`). From a67035ef894c43ba83d8f1556b25d47ac0539379 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 11 Feb 2026 17:07:23 +0530 Subject: [PATCH 2/3] Update argmin.md with clarifications and examples Clarified the behavior of the .argmin() method regarding tensor flattening and return values. Added details about parameters and return value for better understanding. --- .../tensor-operations/terms/argmin/argmin.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/argmin/argmin.md b/content/pytorch/concepts/tensor-operations/terms/argmin/argmin.md index 19fbf4d4f8d..74cf6146183 100644 --- a/content/pytorch/concepts/tensor-operations/terms/argmin/argmin.md +++ b/content/pytorch/concepts/tensor-operations/terms/argmin/argmin.md @@ -14,22 +14,28 @@ CatalogContent: - 'paths/data-science' --- -The **`.argmin()`** method in PyTorch returns the index of the minimum value in a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors). If a dimension is specified, it returns the indices of the minimum values along that dimension. This method is commonly used in tasks such as finding the closest data point, selecting the best prediction, or identifying the least likely class in machine learning workflows. +The **`.argmin()`** method in PyTorch returns the index of the minimum value in a flattened [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) tensor by default, or along a specified dimension. If a dimension is specified, it returns the indices of the minimum values along that dimension. This method is commonly used in tasks such as finding the closest data point, selecting the best prediction, or identifying the least likely class in machine learning workflows. ## Syntax + ```pseudo torch.argmin(input, dim=None, keepdim=False) ``` +**Parameters:** + - `input` (Tensor): The input tensor to search for the minimum value. - `dim` (int, optional): The dimension to reduce. If not specified, the index of the minimum value in the flattened tensor is returned. - `keepdim` (bool, optional): Whether the output tensor retains the reduced dimension. Defaults to `False`. -The `.argmin()` method returns a `LongTensor` containing the index or indices of the minimum value(s). +**Return value:** + +The `.argmin()` method returns a `LongTensor` containing the index or indices of the minimum value(s). If `dim` is not specified, a scalar tensor is returned. ## Example This example shows how to use the `.argmin()` method to find the index of the minimum value in a 2D tensor: + ```py import torch @@ -48,6 +54,7 @@ print(torch.argmin(tensor, dim=1)) ``` This example results in the following output: + ```shell tensor(3) tensor([1, 0, 1]) @@ -57,5 +64,5 @@ tensor([1, 0]) In this example: - **Flattened tensor**: The tensor is treated as `[8, 3, 5, 2, 7, 4]`, and the minimum value `2` is at index `3`. -- **Along columns (dim=0)**: The minimum values in each column are `2`, `3`, and `4`, found in rows `1`, `0`, and `1`. -- **Along rows (dim=1)**: The minimum values in each row are `3` (at index `1`) and `2` (at index `0`). +- **Along columns (`dim=0`)**: The minimum values in each column are `2`, `3`, and `4`, found in rows `1`, `0`, and `1`. +- **Along rows (`dim=1`)**: The minimum values in each row are `3` (at index `1`) and `2` (at index `0`). From 8e18bafeb3097f6b13cc16219afa7998c0808ace Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 11 Feb 2026 17:07:59 +0530 Subject: [PATCH 3/3] Update description of .argmin() method Removed mention of returning indices along a specified dimension in the description of the .argmin() method. --- .../pytorch/concepts/tensor-operations/terms/argmin/argmin.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/argmin/argmin.md b/content/pytorch/concepts/tensor-operations/terms/argmin/argmin.md index 74cf6146183..34a55d3fe13 100644 --- a/content/pytorch/concepts/tensor-operations/terms/argmin/argmin.md +++ b/content/pytorch/concepts/tensor-operations/terms/argmin/argmin.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/data-science' --- -The **`.argmin()`** method in PyTorch returns the index of the minimum value in a flattened [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) tensor by default, or along a specified dimension. If a dimension is specified, it returns the indices of the minimum values along that dimension. This method is commonly used in tasks such as finding the closest data point, selecting the best prediction, or identifying the least likely class in machine learning workflows. +The **`.argmin()`** method in PyTorch returns the index of the minimum value in a flattened [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) tensor by default, or along a specified dimension. This method is commonly used in tasks such as finding the closest data point, selecting the best prediction, or identifying the least likely class in machine learning workflows. ## Syntax