Concepts / Manipulating Tensors in NumPy

Manipulating Tensors in NumPy

Tensor slicing is the selection of specific elements in a tensor.

  • Programming

From Dataset to One Sample

A data tensor often contains multiple samples. In the MNIST example, those samples are images of digits. The practical question is how to work with one particular image when all of the images are stored together. Tensor slicing provides the answer: it selects specific elements from the tensor.

What do you think happens?

The tensor train_images contains many digit images. What does train_images[i] select when the first axis is the samples axis?

  • One complete digit image

  • Every image in the tensor

  • Only one pixel from every image

Reveal answer
Answer:

One complete digit image

The index i is applied along the first axis. Because that axis identifies samples, the indexed position identifies one digit image.

train_imagesmultiple digit imagesaxis 0samplessample ione digit imageorganizesindex i selects
Which element remains when an index is applied along the samples axis?

Axis 0 as the Samples Axis

For data tensors used in deep learning, axis 0 is the samples axis. This means that positions along axis 0 correspond to complete data samples. In the MNIST example, moving from one position on axis 0 to another means moving from one digit image to another.

Key point

The important meaning of an index is determined by the axis it addresses. An index applied along the samples axis identifies a sample, rather than automatically identifying a pixel or another internal part of that sample.

axis 0samples axisposition 0sample 0position 1sample 1position isample iidentifiesidentifiesidentifies
How does each position along axis 0 map to an individual sample?

Reading a Slice as a Selection

Definition

Tensor slicing is the selection of specific elements in a tensor.

A slice should be understood as an operation that chooses part of a larger tensor. It does not automatically mean the entire dataset. The selection may identify one sample or a group of samples, depending on how the samples axis is selected.

Worked example

Tracing One MNIST Selection

Interpret train_images[i] when train_images stores multiple digit images and axis 0 is the samples axis.

  1. 1
    Start

    Begin with the complete data tensor train_images, which contains multiple digit-image samples.

  2. 2
    Apply the index

    Apply the index i along the tensor's first axis, axis 0.

  3. 3
    Identify the result

    The indexed position identifies one complete digit image, because axis 0 is the samples axis.

Result

train_images[i] selects one digit image from the data tensor.

data tensormultiple samplessample ione selected sampledata tensormultiple samplessample groupcontiguous samplesselect one position on axis 0select a group on axis 0
What changes when the selection along axis 0 changes from one position to a contiguous group?

Samples and Data Batches

ConceptMeaningRelationship to axis 0
SampleOne complete item in the data tensor, such as one digit imageOne position along the samples axis identifies it
Data batchA group of samples considered togetherThe grouped samples are organized along the samples axis

A sample is one item; a data batch is a group of items.

A sample and a batch are related but not interchangeable. A sample is one complete data item. A data batch contains a group of samples considered together. Because the samples are organized along axis 0, selecting one position on that axis identifies one sample, while selecting multiple positions identifies a group of samples.

one digit imageone sampledigit-image groupdata batchdigit image 1sampledigit image 2samplecontainscontains
How is one sample related to a batch containing multiple samples?

Common Misreadings

  • Common mistake

    Assuming that a slice always represents the entire dataset.

    What it looks like

    Treating train_images[i] as though it refers to all of train_images.

    Why it fails

    The selection identifies a particular position along axis 0, so it selects one sample in the MNIST example.

    Do this instead

    Read the selection as a choice from the larger tensor. With the samples axis involved, one indexed position identifies one sample.

  • Common mistake

    Ignoring the meaning of axis 0.

    What it looks like

    Interpreting the index i without asking what the first axis represents.

    Why it fails

    In the data tensor described here, axis 0 is the samples axis.

    Do this instead

    Identify the axis first. Then interpret an index along axis 0 as a selection among complete samples.

  • Common mistake

    Confusing one sample with a data batch.

    What it looks like

    Calling a selected digit image a batch simply because it came from a tensor containing many images.

    Why it fails

    A sample is one data item, while a data batch is a group of samples considered together.

    Do this instead

    Use sample for one complete item and batch for a group selected or considered together.

Best practice

Trace every selection in three stages: start with the complete data tensor, identify the axis being selected, and describe the resulting sample or group of samples. This gives the brackets an explicit meaning instead of treating them as unexplained notation.

Trace the Selection

EASY

A tensor contains multiple MNIST digit images, and its axis 0 is the samples axis. Explain what happens when a selection identifies one position along axis 0. Then explain how the result differs when the selection identifies a group of positions along axis 0.

Hints
  • Start by naming what axis 0 represents.
  • One position on the samples axis identifies one complete sample.
  • A group of positions identifies multiple samples considered together.
Worked example

Checking a Selection

Explain the result of selecting one position along axis 0 from a tensor of digit images.

  1. 1
    Name the axis

    Axis 0 is the samples axis.

  2. 2
    Follow the selection

    The selected position identifies one complete sample in the tensor.

  3. 3
    Classify the result

    Because the result is one digit image, it is a sample rather than a data batch.

Result

The selection produces one sample from the larger data tensor.

Key Takeaways

  1. 1
    Tensor slicing selects specific elements from a tensor.
  2. 2
    For the data tensors described here, axis 0 is the samples axis.
  3. 3
    Selecting one position along axis 0 identifies one complete sample, such as one MNIST digit image.
  4. 4
    Selecting a group along axis 0 identifies multiple samples considered together.
  5. 5
    A sample is one data item, while a data batch is a group of samples.

At a glance

  • Tensor slicing is the selection of specific elements in a tensor.
  • Axis 0 is the samples axis in the data-tensor example.
  • An index along axis 0 can identify one complete sample.
  • A group selected along axis 0 represents multiple samples considered together.
  • A sample and a data batch are different: one is a single item, and the other is a group of items.

Related concepts

More concepts starting with M