Simple L-layer neural network implementation for binary classification.
DeepNeuralNetwork.py— core implementation: initialization, forward/backward passes, training (L_layer_model) andpredict.train_and_test.py— script to load HDF5 datasets fromdatasets/, train the model, save parameters toparameters.npz, and evaluate train/test accuracy.predict_from_test.py— loads savedparameters.npzand runs prediction on the test set.datasets/— containstrain_catvnoncat.h5andtest_catvnoncat.h5used by the example scripts.
- Python 3.8+
- numpy
- matplotlib
- h5py
Install dependencies:
python3 -m pip install --user numpy matplotlib h5py- Ensure the HDF5 datasets are in
datasets/and namedtrain_catvnoncat.h5andtest_catvnoncat.h5. - Run the training script:
python3 train_and_test.pyThis will:
- Load and preprocess the datasets (flatten images and scale by 255).
- Train an L-layer network with default architecture
[n_x, 20, 7, 5, 1]. - Save trained parameters to
parameters.npz. - Print train and test accuracies.
After training (or if you already have parameters.npz), run:
python3 predict_from_test.pyThis loads parameters.npz, preprocesses the test set, and prints accuracy.
- Edit
train_and_test.pyto changelayers_dims,learning_rate, or number of iterations. - Edit
DeepNeuralNetwork.pyto modify the architecture or add utilities.
- The scripts assume binary labels shaped
(1, m). - If your HDF5 keys differ, update the loader functions in
train_and_test.pyandpredict_from_test.py. - For larger datasets, consider minibatch gradient descent and checkpointing.
If you want, I can make the scripts accept command-line arguments for dataset paths and hyperparameters.
Here is training plot of the neural network for 2400 iteration for model size of [12288, 20, 7, 5, 1]