Course materials · AI Applications

Week 5 · Tensor Shapes and operations

English translation

Before practice

01 Meaning of values and positions

Example scenario Just as measurements for each student are organized into rows and columns, how should the numbers in a photo be organized?

Description
  • A student table distinguishes students and measurement items. Photos require distinguishing vertical and horizontal pixel positions and color components.
  • A Tensor is an array of numbers arranged along multiple axes in this way. You must also define what the positions of the values mean.

Review related concepts ↗

02 From one photo to many

Example scenario When grouping multiple color photos, what else must be distinguished compared with representing one photo?

Description
  • An axis is needed to distinguish image indices. For example, arrange axes in the order image count, color channel count, height, width.
  • A Tensor's number of axes reflects the items distinguished in the data, rather than physical spatial dimensions. Check axis order in the model used.

03 Meaning of operations

Which values are combined to compute per-student means and per-measurement means?

Description
  • Per-student means combine one student's measurement items; per-item means combine the same measurement across students.
  • This exercise examines which information remains when axis numbers change. The same principle applies to image batches and AI model data processing. PyTorch Tensors extend these array computations to GPU operations and automatic differentiation.

Practice

1. Create Tensors

  1. Create float Tensors: a 1D Tensor from 0~5, a (4,3) Tensor from 0~11, and a (2,3,4) Tensor from 0~23.
  2. Treat (4,3) as 4 samples with 3 features, predict the shapes of the per-feature and per-sample means, and verify using dim=0 and 1.

Compute the mean along each axis, then record the predicted shape and the actual result shape in two columns and state whether they agree.

2. cat·stack·reshape

  1. Using a,b from the concatenation example, run torch.cat([a,b],dim=0) and dim=1 separately. Also run torch.stack([a,b],dim=0) and dim=1.
  2. Record operation, dim, and output shape in four rows, distinguishing an existing axis becoming longer from a new axis being created.
  3. Reshape x, which has 12 elements, into three shapes and record input and output shapes and element counts. For example, use (2,6)(2,6), (3,4)(3,4), and (2,2,3)(2,2,3). Replace one axis in one shape with -1, run it, and record the inferred axis size.

3. Find axis and operation errors

  1. For the baseline example A.shape=(2,3), B.shape=(3,4), state which of A*B and A@B can execute and explain why.
  2. Explain why A+torch.tensor([10,20]) fails, and change the Tensor shape to add 10 and 20 to the respective rows.
  3. Keep the failing code only in Markdown and run the corrected Code Cell.

Submission and saving

Include code, actual outputs and plots, and your own interpretation. Submit through KLAS; check the semester schedule and latest announcements for the deadline.

Filename: Name_StudentID_Week5.ipynb.

Restart the runtime, run all code from the top, then save.

Optional practice

When sizes differ outside the concatenation axis

  1. After running the example that creates a and b, add a Code Cell at the bottom of the notebook and run the following code.
    for debug_second in [b[:, :2], b]:
        print("first:", a.shape, "second:", debug_second.shape)
        try:
            print("cat(dim=0):", torch.cat([a, debug_second], dim=0).shape)
        except (RuntimeError, ValueError) as error:
            print(type(error).__name__, str(error))
  2. In a Text Cell, tabulate both input shapes and execution success. For row concatenation with dim=0, record the axis number and size that must match, and explain the row and column counts in the successful result.