-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added file on index_select * added all the edits * Update content/pytorch/concepts/tensor-operations/terms/index_select/index_select.md * Update content/pytorch/concepts/tensor-operations/terms/index_select/index_select.md * Update content/pytorch/concepts/tensor-operations/terms/index_select/index_select.md * Update content/pytorch/concepts/tensor-operations/terms/index_select/index_select.md * File has been modified. * Update index-select.md minor fixes * Update index-select.md ---------
- Loading branch information
1 parent
8a84a91
commit 0a90869
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
content/pytorch/concepts/tensor-operations/terms/index-select/index-select.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- | ||
Title: '.index_select()' | ||
Description: 'Extracts specific elements from a tensor along a specified dimension based on indices and returns a new tensor.' | ||
Subjects: | ||
- 'AI' | ||
- 'Data Science' | ||
Tags: | ||
- 'AI' | ||
- 'Arrays' | ||
- 'Data Structures' | ||
- 'Deep Learning' | ||
CatalogContent: | ||
- 'intro-to-py-torch-and-neural-networks' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
In PyTorch, the **`.index_select()`** function extracts specific elements from a tensor along a specified dimension based on indices and returns a new tensor. | ||
|
||
## Syntax | ||
|
||
```pseudo | ||
torch.index_select(input, dim, index, out=None) | ||
``` | ||
|
||
- `input`: The input tensor to select elements from. | ||
- `dim`: The dimension along which to index (e.g., `0` for rows, `1` for columns). | ||
- `index`: A 1-D tensor containing the indices to select from the specified dimension. | ||
- `out` (Optional): The output tensor to store the result. If specified, the result will be written to this tensor instead of creating a new one. | ||
|
||
> **Note:** If the `out` parameter is provided, the function stores the result in the given tensor. Otherwise, a new tensor is created and returned. | ||
## Example | ||
|
||
The following example demonstrates the use of `.index_select()`: | ||
|
||
```py | ||
import torch | ||
|
||
# Define a 2D tensor | ||
ten = torch.tensor([[1, 2, 3, -8], | ||
[4, 3, 8, 0], | ||
[-1, 7, 6, 3], | ||
[5, 6, 9, 0]]) | ||
|
||
# Define indices to select | ||
indices = torch.tensor([0, 2]) | ||
|
||
# Select rows from the tensor (dimension 0) | ||
r = torch.index_select(ten, 0, indices) | ||
print("Rows selected:") | ||
print(r) | ||
|
||
# Select columns from the tensor (dimension 1) | ||
c = torch.index_select(ten, 1, indices) | ||
print("Columns selected:") | ||
print(c) | ||
``` | ||
|
||
This will generate the output as follows: | ||
|
||
```shell | ||
Rows selected: | ||
tensor([[ 1, 2, 3, -8], | ||
[-1, 7, 6, 3]]) | ||
Columns selected: | ||
tensor([[ 1, 3], | ||
[ 4, 8], | ||
[-1, 6], | ||
[ 5, 9]]) | ||
``` |