Skip to content

Latest commit

Β 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”¬ Deep Neural Network from Scratch with PyTorch – Breast Cancer Classification

This project implements a deep feedforward neural network from scratch using PyTorch, without using high-level modules like torch.nn.Module or torch.optim. The model classifies breast cancer cases (malignant vs. benign) using the Breast Cancer Wisconsin Diagnostic Dataset from Kaggle.


πŸ“Œ Project Highlights

  • βœ… Pure PyTorch (no nn.Module, no optim)
  • βœ… Manual forward pass, backward pass, and parameter updates
  • βœ… Supports any architecture (you define layer sizes)
  • βœ… Trained on real-world breast cancer dataset
  • βœ… Visualized loss & accuracy over epochs

🧠 Model Architecture

The neural network used in this project is fully connected, with the following architecture:

Input Layer : 30 features (after preprocessing) Hidden Layer 1 : 4 neurons + ReLU Hidden Layer 2 : 3 neurons + ReLU Hidden Layer 3 : 2 neurons + ReLU Output Layer : 1 neuron + Sigmoid

yaml Copy Edit

Loss function: Binary Cross-Entropy (BCE)
Activation: ReLU (hidden), Sigmoid (output)
Optimizer: Manual gradient descent using loss.backward() and weight updates


πŸ“Š Dataset Overview

Preprocessing:

  • βœ… Dropped non-informative columns (id)
  • βœ… Encoded target: M β†’ 1, B β†’ 0
  • βœ… Scaled features using StandardScaler (mean=0, std=1)
  • βœ… Converted data to PyTorch tensors (dtype=torch.float64)

πŸ“ˆ Training Results

  • Final Accuracy: ~61%
  • Final Loss: ~0.72
  • Plotted loss vs epochs and accuracy vs epochs
  • Trained for 100 epochs with learning rate 0.01

Note: Since this is a pure-from-scratch implementation, there’s room for optimization by tuning learning rate, epochs, adding regularization, or using better architectures.


πŸ“¦ File Structure

β”œβ”€β”€ deep_nn_from_scratch.py # All model, training, and evaluation logic β”œβ”€β”€ breast_cancer.csv # Dataset (from Kaggle) β”œβ”€β”€ README.md # This file β”œβ”€β”€ requirements.txt # Minimal dependencies

yaml Copy Edit


πŸ“Œ Key PyTorch Concepts Practiced

  • requires_grad=True
  • .backward() for autograd
  • Manual parameter updates with torch.no_grad()
  • Broadcasting and shape alignment
  • ReLU and Sigmoid activation functions
  • Manual loss function implementation (Binary Cross-Entropy)

πŸš€ Getting Started

1. Install dependencies

pip install torch matplotlib pandas scikit-learn
2. Run the training script
bash
Copy
Edit
python deep_nn_from_scratch.py
πŸ“Š Sample Output
yaml
Copy
Edit
Epoch 0 | Loss: 0.6932 | Accuracy: 0.56
Epoch 10 | Loss: 0.6821 | Accuracy: 0.61
...
Epoch 99 | Loss: 0.7234 | Accuracy: 0.61

About

building deep convolutions layers from scratch using Pytorch

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages