"What I cannot create, I do not understand." — Richard Feynman
This project implements classic neural network architectures (MLP, CNN) without using torch.nn.Module.
The goal is to demystify the "black box" of Deep Learning. By rebuilding layers like Linear, Conv2d, and MaxPool2d using only raw tensors and Autograd, we can visualize the math behind the magic.
Most tutorials start with:
# The "Magic" Way
import torch.nn as nn
model = nn.Linear(10, 1) # What actually happens here?This repo does:
# The "From Scratch" Way (What we build)
import torch
class MyLinear:
def __init__(self, in_features, out_features):
# We manually initialize weights and bias
self.weights = torch.randn(out_features, in_features, requires_grad=True)
self.bias = torch.randn(out_features, requires_grad=True)
def forward(self, x):
# We implement the raw math: y = xA^T + b
return x @ self.weights.t() + self.bias| Module | Status | Key Concepts | Dataset & Accuracy |
|---|---|---|---|
| 01_Linear_Regression | ✅ Done | Numpy Manual Gradients vs Autograd | Housing Prices ( |
| 02_Logistic_Regression | ✅ Done | Sigmoid, Cross-Entropy Loss | Breast Cancer ( |
| 03_MLP | ✅ Done | Hidden Layers, ReLU, Chain Rule | Iris ( |
| 04_CNN | ✅ Done | Convolution, Pooling, Flatten | MNIST ( |
| 05_RNN | 🚧 Planned | Temporal Unrolling, Hidden States | - |
| 06_Attention | 🚧 Planned | QKV, Masking, Transformer Block | - |
Note: The directory structure reflects the learning path. Modules 05 and 06 are currently under active development.
nn-from-scratch/
├── 01_linear_regression/ # Numpy & PyTorch implementations
├── 02_logistic_regression/ # Binary classification
├── 03_mlp/ # Deep Feed-forward networks
├── 04_cnn/ # Computer Vision from scratch
├── utils/ # Evaluation & Plotting tools
└── requirements.txt
Designed for Python 3.8+.
# Create and activate
python3 -m venv venv
source venv/bin/activate # Linux/Mac
# venv\Scripts\activate # Windows
Choose the version matching your hardware:
- Standard (CPU / Older GPU):
pip install torch torchvision
- NVIDIA RTX 40xx / 30xx (CUDA 12.1):
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
- NVIDIA RTX 50xx (Blackwell / CUDA 12.8): ⚡
# Nightly build required for RTX 5080/5090 support
pip install torch torchvision --index-url https://download.pytorch.org/whl/nightly/cu128
- Apple Silicon (M1/M2/M3):
pip install torch torchvision
# Automatically supports MPS (Metal Performance Shaders)
pip install matplotlib numpy jupyter notebook
Run this quick snippet to verify Autograd is working:
import torch
x = torch.tensor([1.0, 2.0], requires_grad=True)
y = x.sum() ** 2
y.backward()
print(f"Gradient: {x.grad}") # Should be [6., 6.]- Linear Regression: Mastering Tensor operations and
requires_grad. - Logistic Regression: Understanding probability outputs and classification loss.
- MLP: Implementing the universal approximation theorem.
- CNN: Learning how kernels extract spatial features (Edge detection -> Shapes -> Objects).
- Next Steps: RNNs (Memory) and Attention (Context).
This is an educational project. If you find a bug in the math or code, feel free to open an issue or PR!