liveBook · Manning

Linear layer

As explained in Natural Language Processing in Action, Second Edition

A fully connected linear layer is a neural-network layer that computes an affine transformation from every input activation to its output units.

A linear layer is a trainable, fully connected mapping that combines every value in an input encoding to produce the output units needed for a prediction. It is especially useful after convolution and pooling have compressed a text representation into a fixed-size vector. section 7.5.3 section 7.5.1

Use a linear layer to:

  • Combine information from a high-dimensional encoding into an answer such as a binary newsworthiness prediction. section 7.5.3
  • Size the input to match the number of values produced by preceding convolution and pooling layers. section 7.5.1
  • Reduce a latent representation to the dimensions required by regression or classification targets. section 7.5.3
  • Compute predictions from recurrent hidden states, including inside hand-built RNNs and LSTMs. section 8.2.2 section 8.4.3

From pooled text features to a classifier

A CNN applies five convolution-and-pooling paths, each producing one pooled value per channel. With five channels per path, concatenation creates a 5 × 5 tensor, or 25 values. A linear output layer consumes those 25 values and produces the dimensions required by the classifier. Reducing each convolution from five channels to three changes the concatenated representation from 25 to 15 values, limiting overfitting while potentially slowing convergence. section 7.5.1

Listing 7.29 Computing the tensor size for the output of a 1D convolution
>>> out_pool_total = 0                                         #1
>>> for kernel_len, stride in zip(kernel_lengths, strides):
>>>     out_conv = (
...         (in_seq_len - dilation * (kernel_len - 1) - 1)
...         // stride
...         ) + 1
>>>     out_pool_total +=  (
...         (out_conv - dilation * (kernel_len - 1) - 1)
...         // stride
...         ) + 1

Inspect the calculation that totals the pooled convolution outputs before sizing the fully connected layer. section 7.5.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains dimensional sizing and architectural placement, but stops short of deriving the affine computation or optimization internals.
Examples
Uses pooled CNN text features, surname-nationality RNN predictions, LSTM hidden states, and transformer feedforward layers.
What it assumes
Assumes familiarity with tensor shapes and preceding neural layers while demonstrating how their outputs determine the linear-layer input size.
What you can do after
Build it from scratch with Linear layers, size inputs from preceding tensor shapes, and connect representations to classification or regression targets.
How it connects
Connects linear layers with convolution, pooling, embeddings, recurrent networks, LSTMs, attention, normalization, and transformers.

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 Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.