Skip to main content

Learning Objectives

By the end of this lesson, you should understand:
  • Neurons, weights, and biases
  • Input, hidden, and output layers
  • Activation functions
    • ReLU
    • Sigmoid
    • Softmax
  • Forward propagation
  • Loss functions
  • Backpropagation
  • Gradient descent
  • Learning rate
  • Epochs, batches, and iterations
  • Overfitting and underfitting
  • Basic neural-network implementation in Python

1. What is a Neural Network?

A neural network is a machine-learning model made up of interconnected neurons arranged in layers. A typical neural network looks like:
Example:
The network learns by adjusting its weights and biases so that its predictions become more accurate.

2. Artificial Neuron

A neuron receives inputs and performs three main operations:
  1. Multiply inputs by weights
  2. Add the bias
  3. Apply an activation function
The mathematical representation is: z=w1x1+w2x2++wnxn+bz = w_1x_1 + w_2x_2 + \cdots + w_nx_n + b Then: a=f(z)a = f(z) Where:

Simple Example

Output:
So the neuron first calculates:
The activation function then processes 2.6.

3. Weights

A weight controls how strongly an input influences a neuron. For example:
The network learns these weights during training. A weight can be:

4. Bias

A bias is an additional learnable parameter added to the weighted sum.
Example:
Output:
Without bias:
With bias:
The bias gives the neuron additional flexibility.

5. Input, Hidden, and Output Layers

A neural network consists of different types of layers.

Input Layer

The input layer receives the features. For example, a house-price model may have:
Therefore:

Hidden Layer

Hidden layers perform learned transformations. Example:
The neurons in hidden layers usually use activation functions such as ReLU.

Output Layer

The output layer produces the final prediction. Examples:

Regression

Binary Classification

Multi-Class Classification


6. Neural Network Architecture Example

A simple binary-classification network:
In code, this can be represented as:

7. Activation Functions

Activation functions determine the output of a neuron after the weighted sum. They introduce non-linearity into the neural network. Without activation functions, multiple linear layers would still behave as one linear transformation. Important activation functions:
  • ReLU
  • Sigmoid
  • Softmax

8. ReLU

ReLU stands for Rectified Linear Unit. ReLU(x)=max(0,x)ReLU(x) = \max(0,x)
Output:
Examples: ReLU is commonly used in hidden layers.

9. Implementing ReLU with NumPy

Output:

10. Sigmoid

The sigmoid function converts a value into the range (0, 1). σ(x)=11+ex\sigma(x) = \frac{1}{1 + e^{-x}} Python implementation:
Output:
Sigmoid is commonly used in the output layer for binary classification. Example:
can be interpreted as approximately:

11. Softmax

Softmax converts multiple scores into probabilities. softmax(zi)=ezijezjsoftmax(z_i) = \frac{e^{z_i}} {\sum_j e^{z_j}} Example:
Example output:
Therefore:
Prediction:
Softmax is commonly used for multi-class classification.

12. Activation Function Comparison

Typical architecture:

13. Forward Propagation

Forward propagation is the process of passing input data through the network to produce a prediction.
For a single neuron:
Output:
Applying ReLU:
Output:

14. Forward Propagation Through a Small Network

Consider:
Python:
The hidden output becomes the input to the next layer.
This process continues until the output layer produces the final prediction.

15. Loss Function

A loss function measures how different the prediction is from the actual target.
The goal of training is to minimize the loss.

16. Mean Squared Error

MSE is commonly used for regression. MSE=1ni=1n(yiy^i)2MSE = \frac{1}{n} \sum_{i=1}^{n} (y_i-\hat{y}_i)^2 Where:
Python:
Output:

17. Binary Cross-Entropy

Binary cross-entropy is commonly used for binary classification. L=[ylog(y^)+(1y)log(1y^)]L = -\left[ y\log(\hat y) + (1-y)\log(1-\hat y) \right] Python:
Output:
A confident correct prediction generally produces a low loss.

18. Backpropagation

After calculating the loss, the neural network needs to determine:
Which parameters caused the error?
This is done using backpropagation. The training process becomes:
Backpropagation uses the chain rule to calculate gradients efficiently. For a weight: Lw\frac{\partial L}{\partial w} This represents how the loss changes when the weight changes.

19. Gradient Descent

Gradient descent is an optimization method used to reduce the loss. The basic update rule is: w_{new} ======= ## w_{old} \eta \frac{\partial L}{\partial w} Where:
The basic idea:

20. Simple Gradient Descent Example

Suppose:
Output:
The weight moved from:
because the gradient was positive.

21. Learning Rate

The learning rate determines the size of parameter updates.

Very small learning rate

Very large learning rate

Good learning rate


22. Epochs

An epoch means one complete pass through the training dataset. Example:
The model sees the training dataset 20 times.

23. Batches

A batch is a subset of the training dataset. Example:
The model processes:
Using batches avoids processing the entire dataset at every parameter update.

24. Iterations

An iteration is generally one parameter update using one batch. Example:
Iterations per epoch:
Therefore:
For 20 epochs:

25. Epoch vs Batch vs Iteration

Example:

26. Complete Neural Network Training Cycle

This is the core learning mechanism of a neural network.

27. Overfitting

Overfitting happens when a model learns the training data too closely and performs poorly on unseen data. Example:
The model performs extremely well on training data but poorly on validation data. Typical pattern:
The validation loss starts increasing while training loss continues decreasing.

28. Underfitting

Underfitting occurs when the model is too simple or has not learned enough from the data. Example:
Both are poor. Possible causes:
  • Model too simple
  • Too few training epochs
  • Poor features
  • Excessive regularization

29. Overfitting vs Underfitting

Conceptually:

30. How to Reduce Overfitting

Common techniques:

1. More Training Data

2. Dropout

Randomly disables neurons during training.

3. Regularization

Common methods:
These add penalties to the objective to discourage overly complex parameter values.

4. Early Stopping

Stop training when validation performance stops improving.

5. Reduce Model Complexity

For example:
when the network is unnecessarily large.

31. Complete Neural Network Example with Scikit-Learn

We can use the MLPClassifier from scikit-learn to build a simple neural network.

Step 1: Import Libraries


32. Load Dataset

We will use the Iris dataset.
Output:
The dataset contains:

33. Split the Dataset

Output:

34. Feature Scaling

Neural networks generally benefit from appropriately scaled input features.
Important:
is used only on training data.
is used on test data. This prevents information from the test set from influencing preprocessing.

35. Build the Neural Network

Architecture:
For multiclass classification, the classifier internally handles the appropriate output representation and loss.

36. Train the Model

During training, the model repeatedly performs the conceptual cycle:

37. Make Predictions

Example:

38. Evaluate Accuracy

Example output:
The exact result can vary depending on the implementation and random state.

39. Classification Report

The report includes:
Example structure:

40. Complete Code

The entire example can be written as:

41. Understanding the Code

The important parameters are:

hidden_layer_sizes

Means:

activation

Uses ReLU in the hidden layers.

solver

Uses the Adam optimization algorithm.

learning_rate_init

Initial learning rate.

max_iter

Maximum number of optimization iterations used by the estimator.

42. Viewing Training Loss

MLPClassifier stores the training loss history in loss_curve_.
The expected behavior is generally:
A decreasing loss indicates that the optimization process is reducing the training objective.

43. Predict Probabilities

For classification, we can also obtain class probabilities.
Example:
Each row represents:
The probabilities in each row sum to approximately 1.

44. Architecture Summary

For our Iris model:

45. Important Formulas

Neuron

z=iwixi+bz = \sum_i w_i x_i + b

ReLU

ReLU(x)=max(0,x)ReLU(x)=\max(0,x)

Sigmoid

σ(x)=11+ex\sigma(x)=\frac{1}{1+e^{-x}}

Softmax

softmax(zi)=ezijezjsoftmax(z_i)= \frac{e^{z_i}} {\sum_j e^{z_j}}

MSE

MSE=1ni=1n(yiy^i)2MSE = \frac{1}{n} \sum_{i=1}^{n} (y_i-\hat y_i)^2

Gradient Descent

w_{new} ======= ## w_{old} \eta \frac{\partial L}{\partial w}

46. Neural Network Training Pipeline


47. Quick Revision


48. Day 22 Mental Model

The entire concept can be remembered with this flow:
Core idea: A neural network learns by making predictions, measuring its errors, calculating gradients through backpropagation, and updating its parameters to reduce the loss.