CHAPTER 3
How do we select values from a tensor?
Select rows and columns, then multiply the chosen values.
Select five values, then multiply
PyTorch
import torch
x = torch.arange(1, 26, dtype=torch.float32, device="cuda").reshape(5, 5)
y = x[0, :] * 2Our original five numbers become the first row of a larger tensor. arange(1, 26) creates 1 through 25; reshape(5, 5) arranges them in five rows and five columns. dtype=torch.float32 chooses the four-byte numbers introduced in chapter 2.
Definitions
- Shape
- The size of each dimension of a tensor. Here (5, 5) means five rows and five columns.
- Slice
- A selection of elements using indices and ranges, such as one whole row.
In x[row, column], both indices start at zero. The experiments below highlight the selected cells before showing the results.
Run the examples below in your browser with no installation or GPU required. The browser simulates the calculation; it does not execute PyTorch or GPU code. To run the PyTorch snippet itself, use a Python environment with PyTorch and an NVIDIA GPU.
Idea 1 of 2
Select a row
x[0, :] selects row 0 and all its columns. The colon means “all”. Multiply those five selected values by two.
Try it
Predict the five results for row 0. Run, then select row 1 and predict again.
PyTorch · operation for these controls
values = x[0, :]
y = values * 2Predict the outputs, then press Run.
Browser simulation · this example keeps its own controls and results.
x: five rows, five columns
Highlighted cells supply the five inputs.
Selected values and results
- Selected value 0
1From [0, 0]× 2 →
? - Selected value 1
2From [0, 1]× 2 →
? - Selected value 2
3From [0, 2]× 2 →
? - Selected value 3
4From [0, 3]× 2 →
? - Selected value 4
5From [0, 4]× 2 →
?
The selected row supplies five inputs. Changing the row changes the inputs; the calculation stays the same.
Idea 2 of 2
Select a column
x[:, 0] selects all rows and column 0. Read down the highlighted column to find the five inputs.
Try it
Predict the five results for column 0. Run, then choose column 1 and predict again. Compare the selected values with the row example above.
PyTorch · operation for these controls
values = x[:, 0]
y = values * 2Predict the outputs, then press Run.
Browser simulation · this example keeps its own controls and results.
x: five rows, five columns
Highlighted cells supply the five inputs.
Selected values and results
- Selected value 0
1From [0, 0]× 2 →
? - Selected value 1
6From [1, 0]× 2 →
? - Selected value 2
11From [2, 0]× 2 →
? - Selected value 3
16From [3, 0]× 2 →
? - Selected value 4
21From [4, 0]× 2 →
?
Both examples multiply five values. Selecting a column changes which values participate, not how many.
Explain what you found
Try a new case
Predict the five results of x[2, :] * 2 before changing the row control. What does x[:, 2] select instead?
Compare your explanation
Row 2 contains [11, 12, 13, 14, 15], giving [22, 24, 26, 28, 30]. Column 2 contains [3, 8, 13, 18, 23].
Run the example on your desktop
Download this chapter’s opening PyTorch example. See the complete example index and setup appendix for CPU PyTorch, the supported GPGPU-Sim programs and optional NVIDIA GPU execution.
The browser still requires no installation. Downloads are generated from the displayed source; CPU checks and simulator checks do not establish GPU performance.
References
Use these official references to check the concepts and code on this page.
- PyTorch: tensor views and indexing (new tab)
Find basic indexing and slicing examples. The next chapter explains shared storage.
- PyTorch: Tensor.shape (new tab)
Check how a tensor reports the sizes of its dimensions.
These browser exercises do not run an installed PyTorch or CUDA toolkit. For optional local execution, choose documentation matching your installed tools. Links open online in a new tab; the questions and answer comparisons below work offline.
Use the documentation
Use the references to explain each answer, then apply it to this page’s example. No installation is needed.