Course materials · AI Applications

CNN layer operations and Tensor shapes

English translation

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

Self-study material

1. Inspect layer shapes with a simple CNN

First run a model with one Conv Layer, then build a CNN with three Conv Layers yourself.

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__)
small=nn.Sequential(nn.Conv2d(1,4,3,padding=1),nn.ReLU(),nn.MaxPool2d(2),nn.Flatten(1),nn.Linear(4*14*14,10))
x=torch.randn(8,1,28,28)
for layer in small:
    x=layer(x);print(type(layer).__name__,tuple(x.shape))
assert x.shape==(8,10)

B. Hands-on activities

The input shape is (N,1,28,28). Implement a CNN in the following order.

  1. Conv2d(1,32,3,padding=1) → ReLU() → MaxPool2d(2)
  2. Conv2d(32,64,3,padding=1) → ReLU() → MaxPool2d(2)
  3. Conv2d(64,128,3,padding=1) → ReLU() → MaxPool2d(2,padding=1)
  4. Flatten(start_dim=1) → Linear(2048,625) → ReLU()
  5. Dropout() → Linear(625,10)

In one table, record each Layer's name, predicted output shape, and actual output shape. Fill in predictions before running the code. If any row differs, explain which operation changed the dimensions.

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. Batch size and parameters

Batch size is the number of images supplied at once. Check that changing the image count leaves the number of Filters and Weights unchanged.

A. Baseline example · Read and run first

for batch in [1,8,32]:
    print('batch:',batch,'output:',small(torch.randn(batch,1,28,28)).shape)
print('parameters:',sum(p.numel() for p in small.parameters()))

B. Hands-on activities

  1. Feed inputs with Batch Sizes N∈{1,8,32}N\in\{1,8,32\} to the same CNN built in the previous activity. Keep the other input axes at (1,28,28) and run in model.eval() mode.
  2. Record NN, final output shape, and Parameter count in three columns. Compute the Parameter count with sum(p.numel() for p in model.parameters()); model is the CNN you built.
  3. Check whether each row has output shape (N,10) and whether the Parameter count is the same across all three rows. Also distinguish which Layers have trainable Parameters.

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 input image size does not match Linear input size

  1. After running the small model in the first example, add a Code Cell at the bottom of the notebook and run the following code.

    for side in [32, 28]:
        debug_image = torch.randn(1, 1, side, side)
        with torch.no_grad():
            debug_features = small[:-1](debug_image)
            print("Input:", debug_image.shape,
                  "Flatten:", debug_features.shape,
                  "Linear in_features:", small[-1].in_features)
            try:
                print("Output:", small(debug_image).shape)
            except (RuntimeError, ValueError) as error:
                print(type(error).__name__, str(error))
  2. In a Text Cell, tabulate the Flatten shape and execution success for each image size. Find which of the two cases matches the input count required by Linear, and record how many inputs were supplied in the failing case.

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_Week9.ipynb.