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.


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

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.


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.


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.

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.


2. The Activation Function

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

| 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 |



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.
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]

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



3. How Does a Neural Network Work?
Most simple ML models connect the inputs straight to the output, like this:

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

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:
- A neuron that uses only Area and Distance: bigger houses close to the city cost more.
- A neuron that uses Area, Bedrooms and Age: young families may want big, new houses.
- 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.
- A neuron that picks just two inputs.
- A neuron that uses all four inputs.







Together these neurons give a much better price prediction than any single input could. This is the final 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.




| 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 |



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.
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.
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.

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
- Initialise the weights with small random numbers.
- Feed the first row (or batch) forward through the network to get a prediction.
- Calculate the loss between prediction and actual value.
- Backpropagate: compute how each weight affects the error.
- Update the weights with gradient descent.
- Repeat for every row or batch. One pass over the whole dataset is an epoch; train for many epochs.

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
- Run the gradient-descent example with learning rate 0.5 and 1.1. What happens?
- Write a function for the derivative of the sigmoid and print it for z = 0.
- 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
Machine Learning Tutorials, Courses and Certifications