Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! 🚀
Forward Pass
The forward pass is when data flows through the neural network from input to output, layer by layer. This is how a trained network makes predictions.
What Happens in a Forward Pass
A forward pass is just calculation flowing forward through the layers:
- 1Input data enters the first layer
- 2Each neuron in the hidden layer: calculates weighted sum + bias, applies activation
- 3The hidden layer outputs flow to the next layer
- 4This repeats for all hidden layers
- 5The output layer produces the final prediction
It is called 'forward' because information moves only forward: from input to output.
Deep Learning ⊂ Machine Learning ⊂ Artificial Intelligence
Complete Forward Pass Example
import math
def relu(z):
return max(0, z)
def sigmoid(z):
return 1 / (1 + math.exp(-z))
# Simple network: 2 inputs -> 2 hidden neurons -> 1 output neuron
# Weights are already trained (we set them manually for illustration)
# Layer 1 (hidden layer): 2 neurons
hidden_weights = [
[0.5, -0.3], # neuron 1 weights for [input1, input2]
[0.8, 0.4], # neuron 2 weights
]
hidden_biases = [0.1, -0.2]
# Layer 2 (output layer): 1 neuron
output_weights = [0.6, 0.9]
output_bias = -0.5
def forward_pass(x1, x2):
print(f"Input: x1={x1}, x2={x2}")
print()
# Layer 1: hidden neurons
hidden_outputs = []
for i, (weights, bias) in enumerate(zip(hidden_weights, hidden_biases)):
z = x1 * weights[0] + x2 * weights[1] + bias
output = relu(z)
hidden_outputs.append(output)
print(f"Hidden neuron {i+1}: z={z:.3f}, after ReLU={output:.3f}")
print()
# Layer 2: output neuron
z_out = sum(h * w for h, w in zip(hidden_outputs, output_weights)) + output_bias
final_output = sigmoid(z_out)
print(f"Output neuron: z={z_out:.3f}, after sigmoid={final_output:.3f}")
print()
print(f"Final prediction: {final_output:.3f} ({final_output*100:.1f}% probability)")
return final_output
# Run a forward pass
result = forward_pass(0.7, 0.3)Tip
Tip
The forward pass is the same whether the network is being trained or deployed. During training, after each forward pass, the network checks how wrong it was (loss) and adjusts the weights. During deployment, only the forward pass runs.