7 PyTorch Tasks (Advanced)

Seven runnable PyTorch tasks on one tensor: keep a column, stack vs cat, keepdim, pairwise distances, clone, reshape vs view, and softmax's dim.

Work through seven small tensor tasks using the same six values. Each answer is a complete CPU Python snippet, so you can run it independently with PyTorch installed.

The main tensor is [[1, 2, 3], [4, 5, 6]], shape (2, 3): two rows, three columns. Axis numbers and indices start at zero. Write down the output shape as well as the values before opening an answer. That habit catches mistakes that a printed list alone can hide.

In the video, the tasks build a transformer layer's pieces instead: reductions with keepdim, variance and standard deviation (and why PyTorch divides by n − 1 by default), LayerNorm and a Linear layer from scratch, and one training step. The tasks here practise the shape and axis habits those pieces rely on.

1. Keep one feature as a column

Select the middle column while keeping shape (2, 1). An integer column index removes an axis; choose a slice that retains it.

2. Rebuild a batch from separate rows

Take each row of the tensor and combine the rows into shape (2, 3) using stack. What would cat do to those two one-dimensional rows?

3. Subtract each row's own mean

Use floating-point values. Produce two rows with mean zero without writing a Python loop. Keep the computed means as a (2, 1) column so they align with rows during subtraction.

4. Compare every row with every row

Find a (2, 2) table of squared Euclidean distances between the two rows. Squared distance adds the squared differences across features. The diagonal should be zero, because each row is compared with itself.

5. Change one row without changing the source

Extract the last row, set its first value to 99 and leave the original tensor intact. Decide where to request a copy.

6. Flatten after a transpose

Transpose the two axes, then flatten in the transposed reading order. Predict the six-value sequence. Should you require a storage-sharing view, or allow reshape to copy if necessary?

7. Normalize scores across the right axis

Treat each row as scores for three competing answers. Apply softmax so each row sums to one. Then deliberately normalize on the other axis and inspect what sums to one instead.

If a task failed, keep the smallest failing expression and print each input's shape before changing it. The PyTorch tensor tutorial provides API examples to compare with your attempt.

Optional challenge set: Tensor Puzzles asks you to rebuild operations with a small set of tensor tools. Try its outer puzzle after the pairwise-distance task; inspect how two added axes create every pair. Its notebook has its own setup instructions, separate from these seven snippets.

  • Write the intended output shape before choosing the operation.
  • Use a slice to keep a feature axis, and stack separate rows to create a batch axis.
  • Keep row means as columns for rowwise subtraction; add axes deliberately for all-pairs calculations.
  • Clone a slice before editing when the source must remain unchanged.
  • Transpose changes axis order; reshape can flatten that order and may copy.
  • Softmax's dim chooses which entries compete. One row per example usually means normalizing along its answer columns.