Tensor Shapes and operations
English translation
Create your own copy to run, edit, and record results.
- Name: ⟦Enter here⟧
- Student ID: ⟦Enter here⟧
- Run Code Cells: ▶ or
Shift+Enter, in order from the top. - Edit a Text Cell: double-click → write
your response→Shift+Enter. - Response cell: enter your own code.
- After changing values: rerun that cell and the relevant cells below it.
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
- 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.
- Treat (4,3) as 4 samples with 3 features, predict the shapes of the per-feature and per-sample means, and verify using
dim=0and 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
- Conditions checked or changed: ⟦Enter your response⟧
- Prediction before execution: ⟦Enter your response⟧
- Value checked in the actual output/plot: ⟦Enter your response⟧
- Difference from prediction and explanation: ⟦Enter your response⟧
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
- Using
a,bfrom the concatenation example, runtorch.cat([a,b],dim=0)anddim=1separately. Also runtorch.stack([a,b],dim=0)anddim=1. - Record operation, dim, and output shape in four rows, distinguishing an existing axis becoming longer from a new axis being created.
- Reshape
x, which has 12 elements, into three shapes and record input and output shapes and element counts. For example, use , , and . 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
- Conditions checked or changed: ⟦Enter your response⟧
- Prediction before execution: ⟦Enter your response⟧
- Value checked in the actual output/plot: ⟦Enter your response⟧
- Difference from prediction and explanation: ⟦Enter your response⟧
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
- 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.
- Explain why A+
torch.tensor([10,20])fails, and change the Tensor shape to add 10 and 20 to the respective rows. - 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
- Conditions checked or changed: ⟦Enter your response⟧
- Prediction before execution: ⟦Enter your response⟧
- Value checked in the actual output/plot: ⟦Enter your response⟧
- Difference from prediction and explanation: ⟦Enter your response⟧
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
After running the example that creates
aandb, 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))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
- Check the response fields, outputs, and plots, then run all cells in order in a fresh runtime.
- 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.