Free Python for AI course
Learn Python from your first line of code to your first call to a language model, with nothing to install.
Reshape, view and copy, where and clip, top-k, stacking and the (N, C, H, W) batch a model expects.
Every input to a model is an array with a shape, and most first-week bugs are shape or type bugs: an axis that vanished, a uint8 that wrapped around, a reshape that scrambled pixels, a slice that changed its parent. This lesson names the handful of NumPy moves that the image project, pandas and PyTorch all lean on, so you meet them once, slowly, before they appear mixed into larger code.
12
A shape (3, 4) is 3 rows of 4 numbers: 3 times 4 is 12. The shape says how the numbers are laid out; the count is the product of its parts.
This lesson rests on NumPy arrays and the Foundation's Tensors and broadcasting. An array holds its numbers in one block of memory. Two small labels say how to read the block: its shape (how the numbers are laid out in rows and columns) and its dtype (what kind of number each cell holds).
The same twelve numbers can be read as a list of 12, a grid of 3 rows and 4 columns, or two grids of 2 by 3. Changing the shape changes the labels, not the numbers:
import numpy as np a = np.arange(12) print(a.shape) print(a.reshape(3, 4).shape) print(a.reshape(2, 2, 3).shape) print(a.reshape(2, -1).shape)
The -1 means "work this one out": 12 numbers in 2 rows leaves 6 columns. Every model input is an array with a shape like this, so most AI bugs are shape bugs.
The dtype matters just as much. Whole numbers are int64 by default on most computers (int32 here in the browser, as the first line below prints), numbers with a decimal point are float64, and pixels are uint8: whole numbers from 0 to 255 in one byte each. PyTorch's default is float32, half the size of float64, which is why a model's weights fit in memory. A mismatch between the two is a common error message: the cure is .astype in NumPy and .float() in PyTorch.
import numpy as np print(np.array([1, 2]).dtype, np.array([1, 2.5]).dtype) print(np.array([1, 2.5], dtype=np.float32).dtype) small = np.array([250, 5], dtype=np.uint8) print(small.dtype, (small + 10).tolist())
A decimal anywhere in a list makes the whole array float. A uint8 can't go above 255, so a sum that does wraps around instead of growing.
import numpy as np print(np.array([1, 2.5]).dtype)
float64
One decimal makes the whole array float, and NumPy's default float is float64.
import numpy as np grid = np.zeros((4, 6)) print(grid.shape, grid.size)
(4, 6) 24
np.zeros((4, 6)) has 4 rows and 6 columns, and size counts all 24 cells.
A reshape doesn't copy the numbers: b is another way of reading the same block as a.
Write a sentence or two, then compare it with a good answer in the lesson.
Purpose. The seven moves you reach for most, and the one trap behind them: a view is not a copy.
Many bugs come from a result that has lost an axis. sum(axis=1) collapses the columns and returns a flat list of row totals, which no longer lines up with the grid. keepdims=True keeps the collapsed axis with length 1, so the result still broadcasts against the grid. [:, None] does the same for any flat list, and .T swaps rows and columns:
import numpy as np counts = np.array([[2.0, 6.0], [1.0, 3.0]]) totals = counts.sum(axis=1) print(totals.shape, counts.sum(axis=1, keepdims=True).shape) share = counts / counts.sum(axis=1, keepdims=True) print(share.tolist()) print(counts.T.shape)
Each row now sums to 1: row totals as a column divide every row by its own total.
The flat (2,) result matches the columns, not the rows
Without keepdims the row totals are a flat (2,) array, and broadcasting lines it up with the last axis, the columns. With keepdims the totals are (2, 1) and divide row by row.
np.where(condition, a, b) picks from a where the condition holds and from b everywhere else. np.clip(x, low, high) pins values into a range. A boolean mask picks the cells that pass a test:
import numpy as np scores = np.array([0.2, 0.9, 0.55, 0.7]) print(np.where(scores >= 0.5, "pass", "fail").tolist()) print(np.clip(scores, 0.3, 0.8).tolist()) print(scores[scores >= 0.5].tolist(), (scores >= 0.5).sum())
where keeps the shape and gives a value for every cell. The mask gives back only the cells that pass, so the result is shorter. Adding the booleans counts them: True is 1.
np.argsort gives the positions that would sort the values; reversed and cut, it gives the top few. np.concatenate joins arrays along an axis that already exists, and np.stack makes a new axis to hold them. a @ b is the matrix product:
import numpy as np scores = np.array([0.2, 0.9, 0.55, 0.7]) top2 = np.argsort(scores)[::-1][:2] print(top2.tolist(), scores[top2].tolist()) a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) print(np.concatenate([a, b]).shape, np.stack([a, b]).shape) print(np.array([[1, 2], [3, 4]]) @ np.array([[1], [1]]))
Two vectors of length 3 stack into a (2, 3) grid, but concatenate into one vector of length 6.
Write row_shares(a) that divides each row of the 2D array a by its own total, so every row sums to 1. Use keepdims.
import numpy as np def row_shares(a): pass
Write top_k(scores, k) that returns the positions of the k highest scores, best first, as a list.
import numpy as np def top_k(scores, k): pass
Slicing and reshaping return views: new labels on the same memory. That is fast, and it is also how one array silently changes another. .copy() makes an independent array, and np.shares_memory tells you which you have:
import numpy as np a = np.arange(6) view = a[:3] safe = a[:3].copy() view[0] = 100 safe[1] = 200 print(a.tolist()) print(np.shares_memory(a, view), np.shares_memory(a, safe)) rng = np.random.default_rng(0) first = rng.integers(0, 10, size=3).tolist() again = np.random.default_rng(0).integers(0, 10, size=3).tolist() print(first == again)
The last line shows why to pass a seed: default_rng(0) gives the same numbers every run, so an experiment can be repeated.
import numpy as np a = np.arange(3) view = a[:] safe = a.copy() view[0] = 9 print(a.tolist(), safe.tolist())
[9, 1, 2] [0, 1, 2]
view is a slice of a, so writing through it changes a. safe is a copy, so a change to the original later doesn't reach it: it still holds the original 0, 1, 2.
A model gave scores to five answers. Keep the three best, set the rest to zero, and rescale what's left so it sums to 1.
import numpy as np scores = np.array([0.1, 0.6, 0.3, 0.8, 0.2]) top = np.argsort(scores)[::-1][:3] mask = np.zeros(len(scores), dtype=bool) mask[top] = True kept = np.where(mask, scores, 0.0) probs = kept / kept.sum() print(top.tolist()) print(kept.tolist()) print(round(float(probs.sum()), 6))
argsort gives the positions of the best three. A boolean mask marks those positions, and np.where keeps their scores and writes 0.0 elsewhere. Dividing by the total rescales the survivors so they sum to 1. This is the idea behind top-k sampling in language models.
Write a sentence or two, then compare it with a good answer in the lesson.
Purpose. A model takes a batch of inputs as one array. Shape and dtype mistakes at that boundary cause most of the first-week bugs.
An image from a file is (height, width, channels). A PyTorch model wants a batch as (N, channels, height, width), with numbers between 0 and 1 as float32. So the batch goes through the moves above: reorder the axes, change the dtype, scale. The same lines exist in PyTorch with other names.
A shape error rarely appears where the mistake was made: it shows up many lines later, as a message about two shapes that can't be multiplied. Two habits catch it early. Print shape and dtype right after any step that changes them. And write the shape you expect as an assert at the entrance of a function, so a wrong batch stops at the door with a message that names the problem:
import numpy as np def check_batch(x): assert x.ndim == 4, f"want 4 axes, got {x.ndim}" assert x.shape[1] == 3, f"want channels in axis 1, got shape {x.shape}" return x print(check_batch(np.zeros((2, 3, 4, 6))).shape)
An assert costs nothing when the shapes are right, and when they are wrong it points at the function that was handed the wrong thing, not at a line deep inside the model.
permute(0, 3, 1, 2)
permute moves the axes into the new order without touching the numbers: channels (axis 3) go to position 1. reshape would keep the numbers in memory order and scramble the pixels.
Use these tools on pictures in Project: images are arrays, on tables in DataFrames with pandas, and on tensors with gradients in PyTorch tensors.
Write a sentence or two, then compare it with a good answer in the lesson.
What does reshape(2, -1) do to an array of 12 numbers?
575 lessons in thirteen phases. 176 are free without an account, and a free account opens the rest. See every lesson.
Learn Python from your first line of code to your first call to a language model, with nothing to install.
Go from your first line of code to working as a Python developer: projects, testing, data, databases, web apps and APIs, all in your browser.
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write