A linear layer turns d input features into C outputs using `nn.Linear(d, C)`. It can be a complete single-layer regression model or one building block in a larger network. In a fully connected layer, every input contributes to every output. section 2.1.5 section 2.2.1
Write the transformation as xᵀW + b, with a weight for each input–output connection and a bias for each output. The bias matters: without it, an all-zero input must produce an all-zero output. Bias terms are often left out of written equations but still assumed to be present. section 2.1.5
From one prediction to a hidden layer
For one prediction from d features, start with `nn.Linear(d, 1)` and choose a regression loss such as `nn.MSELoss()`. To make a network with three inputs, four hidden outputs, and one final output, use `nn.Linear(3, 4)` followed by a nonlinearity and `nn.Linear(4, 1)`. The first layer’s output size must match the second layer’s input size. The nonlinearity matters because consecutive linear operations can be combined into a single linear operation. section 2.1.5 section 2.2.1 section 6.2.7
Common places to use a linear layer:
- Build a fully connected network by arranging `nn.Linear` layers and nonlinearities in `nn.Sequential`. For classification, the final layer produces one output per class. section 2.7 section 4.1.1
- Put `nn.Flatten()` between convolutional output and a linear prediction layer: the linear layer expects the spatial dimensions collapsed into one feature dimension. section 3.4.3
- Replace a pretrained model’s final fully connected layer when the new task needs a different number of outputs. The replacement starts with new weights, while preceding pretrained layers remain available. section 13.2.1
h_2 = nn.Linear(n, n) #1
model_shared = nn.Sequential(
nn.Flatten(),
nn.Linear(D, n),
nn.Tanh(), h_2, #2
nn.Tanh(), h_2, #3
nn.Tanh(),
nn.Linear(n, classes),
)
Reusing the same `nn.Linear(n, n)` object in two positions makes those positions share weights; creating two separate layer objects would not. section 4.1.1
A linear layer is also useful beyond ordinary prediction heads. Its weights can be reused in a transposed decoder for a PCA-style autoencoder, although the transposed operation needs its own bias if one is wanted. For training, pair the model’s predictions with a loss appropriate to the task: MSE or L1 for regression, or cross-entropy for classification. section 7.1.2 section 2.7












