INDEX | HOME

Feedforward Neural Network with Backpropagation

A "Feedforward Neural Network" is a universal function approximator.

It's simple and effective but it does have important limitations. For example it has no spatial invariance. This means If you train an FNN to recognize a cat in the top-left corner of an image, it won't recognize the same cat in the bottom-right corner.

The backpropagation algorithm helps us train it.

Algorithm choices

Activation function

Use ReLU for hidden layers.

For the output layer it depends on your problem.

Backprop

Backpropagation means producing a "blame" value for every weight and bias in the matrix. The output has essentially the same structure as the weights of the network.

Optimizer

Start with SGD. Then can upgrade to:

  • SGD with Momentum
  • Adam

Plain SGD is stateless. SGD with momentum requires tracking "velocity". Adam requires tracking several extra bits of state.

Batches

I'd recommend ignore batching initially. Just train on one sample at a time in a loop.

Batching can be handy though:

  • if using hardware acceleration, batching allows use of matrix multipliers
  • batching can make the training more stable, however for small networks it may make it more likely to get stuck in local minima

The pseudocode for batching is to produce a batch of predictions, then calculate :

predictions = produce_prediction_matrix(batch_of_samples) 
loss        = calculate_mean_loss(predictions)        # calculate the loss of each, then average and output a scalar
grads       = calculate_gradients(predictions, loss)  # logically, we calculate the loss of each, then average
                                                      # in practice it's accelerated using matrix maths
optimize(grads)

Pseudo-code

def training_step(model, samples):
  for each sample:
    prediction = model(sample)
    loss = lossfn(prediction)
    print(mean(loss))
    gradients = backprop(loss, model)
    recur optimize_weights(gradients, model_weights)

def main():
  for epoch in _:
    training_step()

Problems

You need a worth-while problem to solve. Here's a few:

  • Adult Census Income
    • Goal: Predict whether a person makes over $50,000 a year based on census data
    • Output Activation: Sigmoid
    • Loss Function: Binary Cross-Entropy
  • California Housing Prices
    • Goal: Predict the median house value for a California district
    • Output Activation: Linear (None)
    • Loss Function: Mean Squared Error (MSE)
  • Forest Cover Type Dataset
    • Goal: Predict the species of tree growing in a patch of forest from cartographic data
    • Output Activation: Softmax
    • Loss Function: Categorical Cross-Entropy

Copyright 2026 Joseph Graham (joseph@xylon.me.uk)