liveBook · Manning

Linear layer

As explained in Inside Deep Learning: Math, Algorithms, Models

A PyTorch module that applies an affine linear transformation to its input.

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

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Defines dimensions and bias, builds multilayer examples, and examines weight reuse and linear-operation equivalence; leaves ordinary module backpropagation to PyTorch.
Examples
Uses three-to-four-to-one network dimensions, an MNIST network with shared hidden-layer weights, and a replacement ResNet-18 output layer.
What it assumes
Assumes familiarity with tensors and optimization while introducing `nn.Module`, input–output dimensions, and bias in a first model.
What you can do after
Apply it via PyTorch `nn.Linear` to build prediction layers, compose networks, reuse weights, and replace a pretrained output layer.
How it connects
Connects linear and logistic regression to fully connected networks, nonlinear activations, convolutional flattening, and weight sharing.

Also explained in

Compare how 13 books explain linear layer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.