Chapter 7: Neural Networks
🧠Neural Networks are inspired by the human brain and form the core of Deep Learning.
They can learn complex patterns and power everything from image recognition to language models like ChatGPT.
🧠What is a Neural Network?
A Neural Network is a series of connected layers of nodes (neurons) that transform input data to an output through mathematical operations.
🔹 1. Structure of a Neural Network
-
Input Layer: Takes features (e.g., pixels, words)
-
Hidden Layers: Do computations (deep = many layers)
-
Output Layer: Gives prediction (class, value)
🔗 Each neuron performs:
-
x
= input -
W
= weight -
b
= bias -
Activation
= nonlinear function
🔹 2. Types of Neural Networks
Type | Purpose | Example Use |
---|---|---|
Perceptron | Basic unit (1 neuron) | Logical operations |
Multilayer Perceptron (MLP) | Fully connected layers | Tabular data, basic tasks |
Convolutional NN (CNN) | Images | Face detection, X-rays |
Recurrent NN (RNN) | Sequences | Text, speech, time-series |
🔹 3. Activation Functions
Activation functions decide whether a neuron should fire or not (nonlinearity is essential!)
Function | Formula | Usage |
---|---|---|
Sigmoid | Outputs between 0 and 1 | |
ReLU | Fast, used in hidden layers | |
Tanh | Outputs between -1 and 1 | |
Softmax | Converts scores to probabilities | Used in final classification layer |
📉 4. Loss Functions
Measures how far the predicted output is from the actual value.
Loss Function | Used For | Formula (Concept) |
---|---|---|
MSE | Regression | Mean Squared Error |
Cross-Entropy | Classification | Log loss of predicted probability |
🔄 5. Backpropagation & Gradient Descent
The learning process of a neural network.
-
Forward Pass: Compute prediction
-
Loss Computation
-
Backward Pass (Backpropagation): Calculate gradients
-
Gradient Descent: Update weights to reduce error
Formula:
-
: learning rate
-
: gradient of loss wrt weights
🧪 6. Training a Neural Network
Steps:
-
Initialize weights
-
Feed input (Forward Pass)
-
Calculate loss
-
Backpropagate to get gradients
-
Update weights
-
Repeat for many epochs (iterations)
📌 Neural Network Terminology
Term | Meaning |
---|---|
Epoch | One full pass over training data |
Batch Size | Number of samples per update |
Learning Rate | How fast weights are updated |
Overfitting | Model fits training too well |
Regularization | Prevent overfitting (e.g., dropout, L2) |
💻 Example (Simple MLP with Keras):
💡 Where Neural Networks Are Used:
-
Facial recognition
-
Voice assistants (Siri, Alexa)
-
ChatGPT / BERT / LLMs
-
Medical diagnosis
-
Fraud detection
🧠Summary of Chapter 7
Concept | Description |
---|---|
Neural Networks | Simulate brain-like learning |
Layers | Input ➝ Hidden ➝ Output |
Activation | Adds nonlinearity |
Loss & Gradient | Used for learning |
Backpropagation | Optimizes weights |
Tools | TensorFlow, Keras, PyTorch |
✅ Mini Assignment
-
Build a neural network using Keras to classify MNIST digits.
-
Try different activation functions and observe training accuracy.
-
Implement a 2-layer MLP from scratch using NumPy.