liveBook · Manning

Forward pass

As explained in Natural Language Processing in Action, Second Edition

Forward propagation is the computation of a neural network's output by passing inputs through its units in the forward direction.

A forward pass computes a neural network's output by carrying inputs through its units in the forward direction. Each neuron combines incoming features using learned weights, then produces a signal for the next layer. In a single-neuron model, this resembles linear regression followed by an activation function. section 5.1.3

Layered networks repeat this process: one group of predictions becomes the input to the next, allowing successive layers to refine outputs and represent more complicated functions. A forward pass supplies the predictions that a loss function can evaluate. section 5.1.3 section 5.2.5

A minimal PyTorch forward pass follows this pattern:

  • Define the network layers in the constructor, such as one linear layer receiving every feature. section 5.2.5
  • Implement forward() to send the input through the layer and apply the sigmoid activation. section 5.2.5
  • Call model(X) to obtain predictions, compute binary cross-entropy, and then use backpropagation and an optimizer to update weights. section 5.2.5

Predicting from names

A logistic-regression neural network accepts 3,663 TF-IDF features and returns one probability for each name. Passing names such as John, Ruby, and Sarah through the model produces values such as 0.0196, 0.4964, and 0.8199. These are forward-pass outputs; training uses them to calculate loss before changing the weights. section 5.2.5

>>> import torch
>>> class LogisticRegressionNN(torch.nn.Module):

...    def __init__(self, num_features, num_outputs=1):
...         super().__init__()
...         self.linear = torch.nn.Linear(num_features, num_outputs)

...    def forward(self, X):
...        return torch.sigmoid(self.linear(X))

>>> model = LogisticRegressionNN(num_features=vecs.shape[1], num_outputs=1)
>>> model
LogisticRegressionNN(
  (linear): Linear(in_features=3663, out_features=1, bias=True)
)

Inspect the compact PyTorch implementation of a one-neuron forward() method. section 5.2.5

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains single-neuron and layered propagation, then demonstrates a PyTorch implementation without developing forward-pass mathematics further.
Examples
Uses a basic perceptron, a layered network, and PyTorch name-gender predictions from TF-IDF features.
What it assumes
Assumes familiarity with inputs, features, weights, and neural networks while introducing propagation through a neuron from first principles.
What you can do after
Apply it via PyTorch by defining forward(), generating predictions, and feeding them into a loss function.
How it connects
Connects neural network layers, activation functions, loss functions, backpropagation, and optimization.

Also explained in

Compare how 8 books explain forward pass →

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.