Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Building and Propagating Feedforward Networks

Introduction

In our last session, we investigated the critical role of non-linear activation functions. We saw that they are the essential ingredient that allows a network of neurons to escape the limitations of linear models and learn complex, non-linear patterns. We now have the fundamental building block: a neuron that computes a weighted sum of its inputs, adds a bias, and then passes the result through a non-linear activation function.

This lesson takes the logical next step. We will assemble these individual neurons into a structured, multi-layered architecture known as a feedforward neural network. You will learn to construct these networks by defining their layers—input, hidden, and output. Then, we will precisely define the mathematical process of forward propagation, the mechanism by which an input signal flows through the network to produce a final prediction. This process is the foundation for everything that follows, as a network must first be able to make a prediction before it can learn from its errors.

Constructing a Feedforward Neural Network

A feedforward neural network (FFNN), also called a multi-layer perceptron (MLP), is organized into a series of layers. Information flows in one direction—forward—from the input layer, through one or more hidden layers, to the output layer. There are no cycles or feedback loops, hence the name "feedforward."

The core components are:

  • Input Layer: This layer represents the input features of your data. It doesn't perform any computation; it simply passes the raw input values () to the first hidden layer. The number of neurons in this layer is equal to the dimensionality of the input data (e.g., the number of pixels in an image or the number of features in a dataset).
  • Hidden Layers: These are the layers between the input and output. Each neuron in a hidden layer is a computational unit as we defined it in the last lesson. These layers are "hidden" because they have no direct connection to the outside world—their inputs come from the previous layer, and their outputs go to the next. The magic of deep learning happens here; as data passes through successive hidden layers, the network learns increasingly abstract and complex features.
  • Output Layer: This is the final layer of the network. It produces the model's prediction, . The number of neurons and the activation function used in this layer depend on the task. For regression, it might be a single neuron with a linear activation. For binary classification, it could be a single neuron with a sigmoid activation. For multi-class classification, it's typically a layer with one neuron per class, using a softmax activation.

A common architectural choice is to make the layers fully-connected (or dense), meaning every neuron in one layer is connected to every neuron in the next.

11 Feedforward Neural Network and Backpropagation

These lecture notes from Princeton provide a concise formal definition of the components we've just discussed.

Read the initial section up to and including Definition 11.0.1. This will solidify the concepts of layers and the definition of a fully-connected layer.

The video below offers an excellent conceptual walkthrough, illustrating how we move from a single neuron (logistic regression) to a full multi-layer network and introduces the powerful idea that hidden layers learn a hierarchy of features.

Lecture 11 - Introduction to Neural Networks | Stanford CS229: Machine Learning (Autumn 2018)

This Stanford lecture provides the intuition behind stacking layers.

Watch the segment from the introduction of a multi-layer network. Pay close attention to the explanation of how early layers might learn simple features like edges, while deeper layers combine them to recognize more complex concepts like an eye or a face.

The number of hidden layers and the number of neurons in each are key hyperparameters of the model—structural choices you make before training begins.

A basic feedforward neural network architecture. It shows a two-dimensional input `(x1, x2)`, one hidden layer with four ReLU-activated neurons `(a1, a2, a3, a4)`, and a single output neuron with a Sigmoid activation, producing a final probability.

The Mathematics of Forward Propagation

Forward propagation is the computational process of passing an input vector through the network, layer by layer, to generate a prediction . At each layer , two steps are performed for each neuron:

  1. Compute the weighted sum (pre-activation): A linear transformation of the activations from the previous layer, . We denote this intermediate value as .
  2. Apply the non-linear activation function: Pass through a non-linear function (like ReLU) to get the final output of the layer, .

This process is repeated until we reach the output layer.

Formal Definition

Let's define this process with mathematical rigor. The following article provides a clear, step-by-step derivation, which is ideal given your background in computer science and mathematics. It starts with the scalar form and builds up to the fully vectorized form used in practice.

Feedforward Neural Networks in Depth, Part 1: Forward and Backward Propagations | I, Deep Learning

This article meticulously breaks down the mathematics of forward propagation.

Start at the heading "Forward Propagation." First, examine the scalar equations for z_{j, i}^{[l]} and a_{j, i}^{[l]}. Then, carefully review the table of notations that follows; understanding this notation is crucial. The superscripts [l] denote the layer number, while the subscripts j and k index neurons within layers. Finally, follow the derivation as it vectorizes these equations, first for the nodes in a layer (\vec{z} and \vec{a}) and then across multiple training examples (\vec{Z} and \vec{A}). The key equations you should focus on are the vectorized forms for a single example: \mathbf{z}^{[l]} = \mathbf{W}^{[l]} \mathbf{a}^{[l-1]} + \mathbf{b}^{[l]} \mathbf{a}^{[l]} = g^{[l]}(\mathbf{z}^{[l]}) Note that the input data \mathbf{x} is simply the activation of layer 0, i.e., \mathbf{a}^{[0]} = \mathbf{x}.

To make this tangible, let's trace the flow of data through a concrete example.

A feedforward network with specific numerical values for inputs, weights, biases, and neuron activations during a forward pass. This diagram shows a 3-4-2 network architecture.

Let's verify the calculation for the top neuron in the hidden layer. Its pre-activation value, , is the weighted sum of the inputs plus its bias.

  • Inputs (): [1.0, 2.0, 3.0]
  • Weights to this neuron (): [0.01, 0.05, 0.09]
  • Bias for this neuron (): 0.13

The pre-activation is computed as:

The final activation, , is then calculated by applying the layer's activation function, , to this value: . The diagram shows a result of 0.4699. (Note: this specific value results from an unstated activation function, but the two-step process of weighted sum then activation is the key takeaway). This same procedure is repeated for every neuron in the hidden layer, and then again for the output layer using the hidden layer's activations as input.

From a Single Example to a Batch: Vectorization

In any real-world application, you will process thousands or millions of training examples. Performing forward propagation one example at a time is computationally inefficient. The true power and speed of modern deep learning frameworks come from vectorization—processing an entire "batch" of inputs simultaneously using matrix operations.

Instead of an input vector , we have an input matrix , where each column is a separate training example. The forward propagation equations are updated to operate on these matrices.

The equation for a layer becomes:

Here, is a matrix where each column is the activation vector from the previous layer for one example in the batch. Now, consider the dimensions. If layer has neurons, layer has neurons, and we have a batch of examples:

  • has shape .
  • has shape .
  • The product results in a matrix of shape .
  • The bias is a vector of shape .

This leads to a classic linear algebra problem: adding a matrix of shape to a vector of shape . This is resolved through a mechanism called broadcasting. The bias vector is implicitly duplicated times to form a matrix of shape , allowing for element-wise addition. Your familiarity with computational libraries like NumPy makes this concept very natural, as broadcasting is a fundamental feature of their design.

This short video explains the transition to batch processing and the concept of broadcasting with exceptional clarity.

Lecture 11 - Introduction to Neural Networks | Stanford CS229: Machine Learning (Autumn 2018)

This segment of the Stanford lecture generalizes the forward propagation equations to handle batches of data.

Watch the portion from "vectorizing" to the end of the broadcasting explanation. The lecturer clearly explains how the dimensions of the matrices change and why broadcasting is a necessary and efficient trick for adding the bias term.

The final step is to apply the activation function element-wise to the matrix to get the activation matrix , which then becomes the input for the next layer. This fully vectorized process is the engine of forward propagation in all modern deep learning frameworks.

Conclusion

In this lesson, we have moved from the single neuron to a complete, functioning feedforward neural network. We defined the architectural components and, most importantly, detailed the mathematical engine of forward propagation that allows the network to generate predictions.

The key takeaways are:

  • Architecture: Feedforward networks are built from an input layer, one or more hidden layers, and an output layer. In fully-connected networks, every neuron in a layer is connected to every neuron in the subsequent layer.
  • Forward Propagation: This is the process of computing a prediction by passing input data through the network. It consists of a sequence of layer-wise operations: a linear transformation (weighted sum + bias) followed by a non-linear activation.
  • Mathematical Formulation: For a layer , the core computation is , where is the input .
  • Vectorization: Processing a batch of examples at once is achieved by stacking inputs into matrices (e.g., of size ). The bias vector is added to the result of the matrix multiplication via broadcasting.

We now have a complete description of how a neural network produces an output from an input, given a set of weights and biases. However, we have not yet discussed how the network finds the correct values for these parameters. This is the task of training. In our next lesson, we will introduce the concept of a loss function and then derive the backpropagation algorithm, the recursive application of the chain rule that allows the network to compute the gradients needed to learn its parameters.

Can't find a good explanation? Sign up and we'll make it for you

Sign up