Learn / PyTorch
Intermediate

Building Models with nn.Module

Layers, forward passes, and what nn.Linear does under the hood.

nn.Linear(in_features, out_features) computes the affine transform y = x @ W.T + b internally, with W and b as learnable parameters. Every model is a subclass of nn.Module that defines its layers in __init__ and how data flows through them in forward.

LayerUseExample

nn.Linear(in, out)

fully-connected (dense) layer

nn.Linear(4, 2) maps a length-4 input to a length-2 output

nn.ReLU(), nn.Sigmoid()

activation functions

nn.ReLU()(torch.tensor([-1., 2.]))tensor([0., 2.])

nn.Conv2d(...)

2-D convolution

slides a learned kernel over an image tensor

nn.Dropout(p)

randomly zero elements during training (regularization)

nn.Dropout(0.5) zeroes ~half the values, only while training

nn.Sequential(layer1, layer2, ...)

chain layers without writing a class

nn.Sequential(nn.Linear(4,2), nn.ReLU()) runs both in order

Now practice it

Exercises that use what this lesson just covered.

AutogradLoss Functions & Training Loop