Course materials · AI Applications

Tensor Shapes and operations

English translation

Create your own copy to run, edit, and record results.

Self-study material

1. Create Tensors

Print the axes and dtype and compare them with your predictions.

A. Baseline example · Read and run first

import sys, numpy as np, matplotlib.pyplot as plt
import torch
from torch import nn
import torch.nn.functional as F
torch.manual_seed(42)
torch.set_num_threads(2)
print('Python:', sys.version.split()[0], '| PyTorch:', torch.__version__)
x=torch.arange(12,dtype=torch.float32).reshape(4,3)
print(x,'\nshape:',x.shape,'dtype:',x.dtype)
print('features mean:',x.mean(dim=0));print('samples mean:',x.mean(dim=1))

B. Hands-on activities

  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.

Your prediction may describe a shape or an increasing/decreasing trend rather than an exact number. Use the example above to write code; answer explanation-only items in the Text Cell below.

# ✍ Write your own: add your code below this cell.
# You may add Code Cells if the code gets long.

C. Your record · Double-click this Text Cell to edit

2. cat·stack·reshape

Apply different combination methods to the same inputs.

A. Baseline example · Read and run first

a=torch.arange(6).reshape(2,3);b=a+10
print('cat0:',torch.cat([a,b],0).shape)
print('cat1:',torch.cat([a,b],1).shape)
print('stack:',torch.stack([a,b],0).shape)
print('reshape:',x.reshape(2,2,3).shape)
print('unsqueeze:',x.unsqueeze(0).shape)
print('restore:',x.unsqueeze(0).squeeze(0).shape)

B. Hands-on activities

  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.

Your prediction may describe a shape or an increasing/decreasing trend rather than an exact number. Use the example above to write code; answer explanation-only items in the Text Cell below.

# ✍ Write your own: add your code below this cell.
# You may add Code Cells if the code gets long.

C. Your record · Double-click this Text Cell to edit

3. Find axis and operation errors

A must have as many columns as B has rows to compute A@B. First print both array shapes to check whether multiplication is possible.

A. Baseline example · Read and run first

A=torch.arange(6,dtype=torch.float32).reshape(2,3)
B=torch.arange(12,dtype=torch.float32).reshape(3,4)
print((A@B).shape)
print((A+torch.tensor([10,20,30])).shape)
assert (A@B).shape==(2,4)

B. Hands-on activities

  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.

Your prediction may describe a shape or an increasing/decreasing trend rather than an exact number. Use the example above to write code; answer explanation-only items in the Text Cell below.

# ✍ Write your own: add your code below this cell.
# You may add Code Cells if the code gets long.

C. Your record · Double-click this Text Cell to edit

Practice through repetition · Reason before running

Summarize the three conditions compared in the preceding activities in this table. You do not need to repeat experiments already completed. If comparisons are insufficient, supplement them by changing just one condition.

Changed condition Predicted shape/value/result Actual result Cause of any error
⟦Enter your response 1⟧ ⟦Enter your response⟧ ⟦Enter your response⟧ ⟦Enter your response⟧
⟦Enter your response 2⟧ ⟦Enter your response⟧ ⟦Enter your response⟧ ⟦Enter your response⟧
⟦Enter your response 3⟧ ⟦Enter your response⟧ ⟦Enter your response⟧ ⟦Enter your response⟧
# ✍ Code for repeated experiments

Optional exercise · Find the cause of an error and fix the code

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.

Check before saving

  1. Check the response fields, outputs, and plots, then run all cells in order in a fresh runtime.
  2. Save to your Drive, then download using .ipynb.

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.