Thursday , December 3 2026
Artificial Neural Network – Plan of Attack

Artificial Neural Network – Plan of Attack

Updated: 28 November 2026Level: BeginnerReading time: about 15 minutesConcepts · NumPy · tested

This tutorial is the roadmap for understanding how an artificial neural network really works – from a single neuron to the way the whole network learns with gradient descent and backpropagation.

What You Will Learn

  • The neuron: inputs, weights and output
  • Activation functions: threshold, sigmoid, ReLU and tanh
  • How a trained network makes a prediction (house price example)
  • How a network learns: the loss (cost) function
  • Gradient descent, stochastic and mini-batch gradient descent
  • Backpropagation

Prerequisites

Introduction to ANN and basic Python.

1. The Neuron

A biological neuron receives signals through its dendrites and sends a signal out through its axon. The dendrites of one neuron are connected to the axons of others; the small gap between them is the synapse.

Biological neuron
Biological neuron
Dendrites connected to axons of other neurons
Dendrites connected to axons of other neurons

In an artificial network, a neuron receives signals from the neurons before it, in the same way.

Artificial neuron receiving signals
Artificial neuron receiving signals

Input values

The inputs are the independent variables for one row of your data, for example the area, bedrooms and age of one house. They should be standardised or normalised so that all inputs are on a similar scale.

Input values of a neuron
Input values of a neuron
Input values of a neuron
Input values of a neuron

Output value

The output can be continuous (a price), binary (yes/no) or categorical. For a categorical output there is one output value per category.

Output value of a neuron
Output value of a neuron
Several outputs for a categorical variable
Several outputs for a categorical variable

Weights

Every connection has a weight. Weights are how a neural network learns: by adjusting them, the network decides which signals are important for each neuron. Training a network means finding good values for all the weights, using gradient descent and backpropagation.

Weights on connections
Weights on connections

What happens inside the neuron

Step 1: the neuron adds up all inputs multiplied by their weights: z = Σ wᵢxᵢ + b. Step 2: it applies an activation function to z and passes the result on.

Weighted sum inside a neuron
Weighted sum inside a neuron
Applying the activation function
Applying the activation function

2. The Activation Function

Activation function
Activation function

There are many activation functions. The four most common are:

Types of activation functions
Types of activation functions
Function Formula Output range Typical use
Threshold (step) 1 if z ≥ 0 else 0 0 or 1 Simple yes/no decisions
Sigmoid 1 / (1 + e−z) 0 to 1 Output layer for probabilities
ReLU (rectifier) max(0, z) 0 to ∞ Hidden layers (most popular)
Tanh (ez − e−z) / (ez + e−z) −1 to 1 Hidden layers, RNNs
Sigmoid, ReLU and tanh graphs
Sigmoid, ReLU and tanh graphs
Sigmoid, ReLU and tanh graphs
Sigmoid, ReLU and tanh graphs
Sigmoid, ReLU and tanh graphs
Sigmoid, ReLU and tanh graphs
Example
import numpy as np

z = np.array([-2.0, -0.5, 0.0, 0.5, 2.0])
print("threshold:", np.where(z >= 0, 1, 0))
print("sigmoid:  ", np.round(1 / (1 + np.exp(-z)), 3))
print("relu:     ", np.maximum(0, z))
print("tanh:     ", np.round(np.tanh(z), 3))

Explanation: The same five inputs go through each function. Sigmoid turns them into probabilities between 0 and 1, ReLU sets negatives to zero and keeps positives, and tanh is like sigmoid but centred on zero.

Output
threshold: [0 0 1 1 1]
sigmoid:   [0.119 0.378 0.5   0.622 0.881]
relu:      [0.  0.  0.  0.5 2. ]
tanh:      [-0.964 -0.462  0.     0.462  0.964]
Note: Quick question: if the dependent variable is binary (0 or 1), which activation should the output layer use? Sigmoid (or threshold), because it gives the probability of class 1.
Activation for a binary dependent variable
Activation for a binary dependent variable

A common design is ReLU in the hidden layers and sigmoid in the output layer:

Neural network with ReLU hidden layer and sigmoid output
Neural network with ReLU hidden layer and sigmoid output
Neural network with ReLU hidden layer and sigmoid output
Neural network with ReLU hidden layer and sigmoid output
Neural network with ReLU hidden layer and sigmoid output
Neural network with ReLU hidden layer and sigmoid output

3. How Does a Neural Network Work?

Most simple ML models connect the inputs straight to the output, like this:

Inputs connected directly to output
Inputs connected directly to output

Adding a hidden layer lets the model combine inputs in many ways, which usually improves accuracy. That is an artificial neural network.

Network with a hidden layer
Network with a hidden layer

Assume the network is already trained to predict a house price from four inputs: Area, Bedrooms, Distance to city and Age. Each hidden neuron focuses on a different combination:

  1. A neuron that uses only Area and Distance: bigger houses close to the city cost more.
  2. A neuron that uses Area, Bedrooms and Age: young families may want big, new houses.
  3. A neuron that uses only Age: older houses are usually cheaper, but houses over 100 years old can become valuable heritage property. A ReLU lets the neuron switch on only after that point.
  4. A neuron that picks just two inputs.
  5. A neuron that uses all four inputs.
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs
Hidden neurons focusing on different inputs

Together these neurons give a much better price prediction than any single input could. This is the final model:

Final neural network model
Final neural network model

4. How Does a Neural Network Learn?

The network compares its prediction ŷ with the real value y using a loss (cost) function, then changes the weights to make the loss smaller. When the network is shown 8 rows, it is the same network applied 8 times; the loss is added up over all rows.

Comparing predictions with actual values
Comparing predictions with actual values
Comparing predictions with actual values
Comparing predictions with actual values
Comparing predictions with actual values
Comparing predictions with actual values
Comparing predictions with actual values
Comparing predictions with actual values
Loss function Formula Used for
Mean Squared Error (MSE) mean((y − ŷ)²) Regression
Mean Absolute Error (MAE) mean(|y − ŷ|) Regression, robust to outliers
Binary cross-entropy −[y log ŷ + (1−y) log(1−ŷ)] Binary classification
Categorical cross-entropy −Σ y log ŷ Multi-class classification
Cost function
Cost function
Mean squared error
Mean squared error
Mean absolute error
Mean absolute error

Gradient Descent

Gradient descent finds the weights with the lowest loss. It looks at the slope (derivative) of the loss and takes a small step downhill. The size of the step is the learning rate. Take the simple cost y = x², starting at x = 5: the slope is dy/dx = 2x = 10, so with learning rate 0.1 the new value is 5 − 0.1 × 10 = 4.

Example
x = 5.0
lr = 0.1
for step in range(1, 6):
    grad = 2 * x          # derivative of y = x**2
    x = x - lr * grad
    print(f"step {step}: x = {x:.4f}, y = {x**2:.4f}")

Explanation: Each step moves x against the slope. The loss y falls from 25 towards 0, the minimum. A learning rate that is too large would jump past the minimum; one that is too small would be very slow.

Output
step 1: x = 4.0000, y = 16.0000
step 2: x = 3.2000, y = 10.2400
step 3: x = 2.5600, y = 6.5536
step 4: x = 2.0480, y = 4.1943
step 5: x = 1.6384, y = 2.6844

With many weights, the loss is a surface in many dimensions, and gradient descent chooses the steepest way down.

Gradient descent in higher dimensions
Gradient descent in higher dimensions

Batch, mini-batch and stochastic

Method Rows used per weight update Notes
Batch gradient descent The full dataset Smooth but slow; can get stuck in a local minimum
Mini-batch n rows chosen by you (e.g. 32 or 100) The usual choice; this is the batch_size in Keras
Stochastic (SGD) 1 row Noisy but fast; can escape local minima

Backpropagation

Backpropagation is how the error is sent backwards through the network. After a prediction, the loss is calculated at the output; then, using the chain rule of calculus, the algorithm works out how much each weight in each layer contributed to that error, and gradient descent updates all the weights together.

Training step by step

  1. Initialise the weights with small random numbers.
  2. Feed the first row (or batch) forward through the network to get a prediction.
  3. Calculate the loss between prediction and actual value.
  4. Backpropagate: compute how each weight affects the error.
  5. Update the weights with gradient descent.
  6. Repeat for every row or batch. One pass over the whole dataset is an epoch; train for many epochs.
Training a neural network
Training a neural network

Series

Deep Learning series: Introduction to Deep Learning · AI vs ML vs DL · Introduction to ANN · ANN – Plan of Attack · ANN Practical · Introduction to CNN · CNN Steps · CNN Practically · Introduction to RNN · RNN Steps · RNN Practically

Interview Questions

Q: Why is ReLU preferred in hidden layers?
It is cheap to compute and does not saturate for positive values, so gradients do not vanish as quickly as with sigmoid.

Q: What is the learning rate?
The size of each gradient-descent step. Too large overshoots; too small trains slowly.

Q: Difference between an epoch and a batch?
A batch is the group of rows used for one weight update; an epoch is one full pass over all the training data.

Q: What does backpropagation compute?
The gradient of the loss with respect to every weight, using the chain rule.

Practice Exercises

  1. Run the gradient-descent example with learning rate 0.5 and 1.1. What happens?
  2. Write a function for the derivative of the sigmoid and print it for z = 0.
  3. Which loss function would you use to predict house prices? And to predict if a customer leaves?

Summary

A neuron multiplies inputs by weights, adds them and applies an activation function. A network learns by measuring the loss and using gradient descent with backpropagation to update its weights, batch by batch, over many epochs.

Next: ANN Practical Example

About Machine Learning

Check Also

LSTM and GRU Explained (NumPy LSTM Cell + Keras Example)

LSTM and GRU explained: vanishing gradients, cell state and gates, an LSTM step in NumPy, GRU comparison, parameter counts and a Keras sentiment model.