This project is an archived programming assignment from Andrew Ng's Deep Learning Specialization. It demonstrates how to build a logistic regression classifier to recognize cat images, implementing the algorithm from scratch using a "Neural Network mindset."
The objective of this notebook is to build a simple image-recognition algorithm that can correctly classify pictures as cat (y=1) or non-cat (y=0).
Instead of relying on high-level machine learning libraries like Scikit-Learn or TensorFlow, this project walks through building the mathematical foundations of logistic regression purely using NumPy. By structuring the code like a neural network (with forward propagation, backward propagation, and parameter updates), it builds intuition for more complex deep learning architectures.
The project relies on a dataset (data.h5 loaded via lr_utils.py) containing:
- Training set:
m_trainimages labeled as cat (1) or non-cat (0). - Test set:
m_testimages labeled as cat (1) or non-cat (0). - Image Shape: Each image is of shape
(64, 64, 3), representing a 64x64 pixel image with 3 color channels (RGB).
During preprocessing, the images are flattened into feature vectors of shape (12288, 1) and standardized by dividing by 255.
The logistic regression algorithm is structured as a simple 1-layer neural network with a sigmoid activation function.
For a single example
-
Linear combination:
$z^{(i)} = w^T x^{(i)} + b$ -
Activation (Sigmoid):
$\hat{y}^{(i)} = a^{(i)} = \sigma(z^{(i)})$ -
Loss function:
$\mathcal{L}(a^{(i)}, y^{(i)}) = - y^{(i)} \log(a^{(i)}) - (1-y^{(i)}) \log(1-a^{(i)})$
The overall Cost Function
sigmoid(z): Computes the sigmoid activation.initialize_with_zeros(dim): Initializes the weights vectorwand biasbto zero.propagate(w, b, X, Y): Performs the forward propagation (computes cost) and backward propagation (computes gradientsdw,db).optimize(w, b, X, Y, num_iterations, learning_rate): Updates parameters using Gradient Descent.predict(w, b, X): Predicts labels for given input images using the learned parameters.model(X_train, Y_train, X_test, Y_test): Integrates all the above functions into a unified training pipeline.
To run this notebook, the following Python packages are required:
numpy: For fundamental matrix/vector operations.h5py: For interacting with the dataset stored in an H5 file.matplotlib: For plotting the cost curve and displaying images.scipy/PIL: For testing the final model with custom images.
- Understanding the shapes and dimensions of data matrices is crucial in deep learning.
- Vectorization (avoiding
for/whileloops via NumPy dot products) is heavily utilized to optimize the performance of the algorithm. - Logistic Regression can be effectively viewed and implemented as the simplest possible Neural Network.