liveBook · Manning

Linear layer

As explained in Learn Generative AI with PyTorch

A PyTorch layer that applies a learned linear transformation to its input.

A linear layer applies a learned linear transformation to incoming data. In PyTorch, nn.Linear creates the dense layer that maps input features to output features, making it a basic building block for classifiers, sequence models, autoencoders, generators, and discriminators. section 2.3.2 section 8.4

Choose the first layer’s input size to match the input data. In a clothing classifier, each 28 × 28 image is flattened into 784 values before entering nn.Linear(784, 256). Later layers use the preceding layer’s output size as their input size, such as nn.Linear(256, 128). section 2.3.2

A binary image classifier

A sequential classifier can stack linear layers with ReLU activations and finish with nn.Linear(32, 1), followed by sigmoid. The hidden layers use 256, 128, and 32 neurons; the final value is interpreted as the probability that the image is an ankle boot rather than a t-shirt. The same pattern appears in a discriminator, where nn.Linear(2, 256) accepts two input values and nn.Linear(64, 1) produces one output. section 3.3.1

Linear layers need not be the final stage of a model. An LSTM can feed its outputs into a linear layer whose output size equals the vocabulary size, producing logits for the softmax function. Dense layers are also used to build autoencoder encoders and decoders. section 8.4 section 7.2

For images, compare linear layers with convolutional layers. Flattening makes dense processing possible, but using dense layers alone on high-resolution color images can create enormous parameter counts; convolutional layers reduce connections by examining local input regions. section 4.0 section 2.3.2

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains input and output dimensions, stacking, activations, and image flattening, but does not derive the transformation mathematically.
Examples
Demonstrates clothing classification, two-dimensional discrimination, text generation, autoencoding handwritten digits, and image generation.
What it assumes
Assumes basic tensors and neural-network terminology while teaching image flattening and feature-size matching through examples.
What you can do after
Build it from scratch with nn.Sequential by matching tensor dimensions and combining linear layers with task-appropriate activations.
How it connects
Connects linear layers with dense layers, fully connected layers, ReLU, sigmoid, dropout, LSTM, embeddings, and convolutional neural networks.

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 Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.