GPU Lab / interactive book
GPU mapSource ↗

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, :] * 2
Desktop setup

Our 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 * 2

Predict 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.

1[0, 0]2[0, 1]3[0, 2]4[0, 3]5[0, 4]6[1, 0]7[1, 1]8[1, 2]9[1, 3]10[1, 4]11[2, 0]12[2, 1]13[2, 2]14[2, 3]15[2, 4]16[3, 0]17[3, 1]18[3, 2]19[3, 3]20[3, 4]21[4, 0]22[4, 1]23[4, 2]24[4, 3]25[4, 4]

Selected values and results

  1. Selected value 0
    1
    From [0, 0]× 2 →
    ?
  2. Selected value 1
    2
    From [0, 1]× 2 →
    ?
  3. Selected value 2
    3
    From [0, 2]× 2 →
    ?
  4. Selected value 3
    4
    From [0, 3]× 2 →
    ?
  5. Selected value 4
    5
    From [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 * 2

Predict 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.

1[0, 0]2[0, 1]3[0, 2]4[0, 3]5[0, 4]6[1, 0]7[1, 1]8[1, 2]9[1, 3]10[1, 4]11[2, 0]12[2, 1]13[2, 2]14[2, 3]15[2, 4]16[3, 0]17[3, 1]18[3, 2]19[3, 3]20[3, 4]21[4, 0]22[4, 1]23[4, 2]24[4, 3]25[4, 4]

Selected values and results

  1. Selected value 0
    1
    From [0, 0]× 2 →
    ?
  2. Selected value 1
    6
    From [1, 0]× 2 →
    ?
  3. Selected value 2
    11
    From [2, 0]× 2 →
    ?
  4. Selected value 3
    16
    From [3, 0]× 2 →
    ?
  5. Selected value 4
    21
    From [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].

References

Use these official references to check the concepts and code on this page.

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.

Install GPU Lab

Give GPU Lab its own icon and window. All lessons and simulators work offline once the course is saved.

Use your browser’s install option:

If your browser has no install option, try Chrome or Edge.

App status

Version

Checking for updates

Offline access

Not saved for offline use

Save all lessons, browser simulators, diagrams and example files. Desktop execution needs separate PyTorch or CUDA/GPGPU-Sim tools; their installations are not part of this offline download. External links need internet access.