Deep learning and neural networksLoading progress…

Devices and debugging

Translate an inspected NumPy model into tensors, modules, automatic differentiation and a clear training loop.

Devices and dtypes must agree

A model's parameters and the tensors it processes must live on the same device. Move a model to a GPU and the input stays on the CPU, and the next call fails. The same goes for element types (a float model cannot take an integer input without a conversion). These errors are not subtle once you know to read them.

model is oninput x is on
model = model.to('cpu') x = x.to('cpu') out = model(x) # fine: everything is on cpu

"cuda:0" here is only a label: the exercises have no GPU. The rule being taught is the real one: the model's parameters and every tensor it touches must be on the same device.

Move the model and the input independently. ("cuda:0" is a label in this simulation.)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
for xb, yb in loader:
    xb, yb = xb.to(device), yb.to(device)   # move every batch too
    ...

Debug small, then scale up

When something fails, shrink the problem: take one tiny batch, run it through the model layer by layer and print each shape. A bigger GPU only runs the same wrong shapes faster.

Predict first. Will a bigger GPU repair an incorrect matrix dimension?

Chapter context

PyTorch provides tensor operations and differentiation infrastructure. The framework removes bookkeeping, but it does not remove the need to understand dimensions, parameter ownership, device placement and evaluation behavior.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

For a batch X of shape (B,d), a dense output with h features has shape (B,h). torch.nn.Linear stores weight with shape (h,d) and computes X times weight-transpose plus bias. Parameter count is d*h+h. PyTorch execution belongs in a linked notebook or supported runtime; the chapter does not assume browser PyTorch.

Worked example

A batch has shape (2,4). A Linear(4,3) layer stores a (3,4) weight and a length-three bias. Its output is (2,3). The parameter count is 4×3+3=15. This differs from the batch's element count: model parameters are shared across both examples.

Reference code

The following example uses the Python standard library and does not call a remote service.

batch, inputs, outputs = 2,4,3
weight_shape = (outputs,inputs)
output_shape = (batch,outputs)
assert weight_shape == (3,4)
assert output_shape == (2,3)
assert inputs*outputs+outputs == 15

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

eval mode and no_grad are independent. Model parameters do not multiply with batch size. A tensor detached for logging must not replace the tensor used to compute the training loss.