Forward Propagation in Neural Networks

Forward propagation is a fundamental process in neural networks, where the outputs of successive layers are evaluated using the output of the previous layer as input. This process is carried out layer by layer, which simplifies both the implementation and conceptualization of neural networks.

Overview

In forward propagation, each layer of the neural network processes its input to produce an output, which then serves as the input for the next layer. This sequential processing continues until the final output layer is reached. The process is described by a mathematical equation, often referred to as Equation 8.10 in the context of this discussion, which is a restatement of a previous equation (Equation 8.7). This approach allows for efficient memory usage, as only the current and previous layers need to be stored in memory at any given time.

Forward Propagation in Multi-Layer Perceptrons (MLPs)

In the context of Multi-Layer Perceptrons (MLPs) composed of linear layers, forward propagation involves evaluating the outputs of each layer one at a time. The output of each layer is computed using the weights and biases specific to that layer, and then passed through an activation function to introduce non-linearity into the model. This process is repeated for each layer in the network.

PyTorch Implementation

The following is a PyTorch implementation of forward propagation, illustrating how the process is carried out programmatically:

def Z(x, W_l, b_l):                    #1
    return torch.matmul(W_l, x) + b_l

def A(z_l):                            #2
    return torch.sigmoid(z_l)


def forward(x, W, b):                  #3
    L = len(W) - 1
    a_l = x
    for l in range(0, L + 1):          #4
        z_l = Z(a_l, W[l], b[l])       #5
        a_l = A(z_l)                   #6
    return a_l
#1 x: activation of layer l-1 (1-d vector) <i class="timesitalic">W<sub>l</sub></i>: Weight matrix of layer l <i class="timesitalic">b<sub>l</sub></i>: Bias vector of layer l
#2 Sigmoid activation function (nonlinear layer)
#3 x: 1-d input vector W: list of matrices for layers 0 to L. b: list of vectors for layers 0 to L
#4 Loops through layers 0 to L
#5 Computes Z
#6 Computes activation

Explanation of the Code

  1. Function Z(x, W_l, b_l): This function computes the linear transformation of the input x using the weight matrix W_l and bias vector b_l for layer l.

  2. Function A(z_l): This function applies the sigmoid activation function to the linear transformation z_l, introducing non-linearity into the model.

  3. Function forward(x, W, b): This is the main function that performs forward propagation. It takes an input vector x, a list of weight matrices W, and a list of bias vectors b for all layers. It iterates through each layer, computing the linear transformation and applying the activation function, ultimately returning the final output of the network.

This implementation highlights the simplicity and efficiency of forward propagation, where each layer's output is computed and passed to the next, ensuring that only the necessary data is kept in memory.