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.

nn.Linear applies an affine transformation: it combines input features with learned weights and adds a learned bias. Calling nn.Linear(d, C) creates a module with d inputs and C outputs, making it useful for regression, classification, and hidden layers in larger networks. section 2.1.5 section 2.7

For an input x, the operation can be written y = xW + b. W contains one set of feature connections for each output, while b shifts outputs independently of x. A layer such as nn.Linear(3, 4) maps three input features to four outputs; nn.Linear(4, 1) then maps those four values to one prediction. section 2.1.5 section 2.2.1

Use nn.Linear to:

  • Implement linear or logistic regression with a single layer. section 2.7
  • Build fully connected networks by stacking linear layers and placing nonlinearities between them. section 2.7
  • Represent feed-forward connections in which every output receives information from every feature in the preceding layer. section 2.2.1
  • Reuse one layer object when tied weights are required across multiple network positions. section 4.1.1

A compact fully connected classifier

D = 28 * 28 n = 256 classes = 10 model = nn.Sequential( nn.Flatten(), nn.Linear(D, n), nn.Tanh(), nn.Linear(n, n), nn.Tanh(), nn.Linear(n, classes), ) Here, Flatten converts each image into D features, the hidden layers transform those features, and the final nn.Linear produces one output per class. The same kind of layer can also be reused for tied weights or paired with a custom transpose layer in a PCA-style autoencoder. section 4.1.1 section 7.1.2

Figure 2.4 A simple feed-forward fully connected network with one input layer of d = 3 inputs, a hidden layers with n = 4 neurons, and one output in the output layer. Connections feed directly into the next layer only, and each node in one layer is connected to every neuron in the preceding layer.

Inspect the network diagram to connect nn.Linear dimensions with fully connected edges between successive layers. section 2.2.1

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

How deep it goes
Explains weights, bias, dimensions, module composition, and weight reuse, but defers lower-level gradient implementation.
Examples
Uses dimensioned layer pairs, an MNIST classifier, reused hidden layers, and a transpose-weight PCA autoencoder.
What it assumes
Assumes vectors, matrices, and basic PyTorch modules while teaching layer dimensions, bias, and fully connected structure explicitly.
What you can do after
Build it from scratch by choosing input and output dimensions, composing layers, adding nonlinearities, and reusing layer objects.
How it connects
Connects linear regression, logistic regression, fully connected networks, nonlinearities, Sequential, weight sharing, and autoencoders.

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.