This project implements a complete scalar-valued automatic differentiation (autograd) engine and a minimal Neural Network library from scratch in Python.
The core goal is to build the mathematical foundation of deep learning—specifically, the reverse-mode backpropagation algorithm—without relying on high-level libraries (like PyTorch or TensorFlow) for gradient computation. This implementation provides full algorithmic transparency into how the chain rule is applied during neural network training.
The project is driven by a custom Value object that forms a dynamically constructed computational graph:
ValueClass: A custom scalar data structure that holds both the numericdataand the computedgrad(derivative) at every node.- Dynamic DAG: Mathematical operations (
+,*,tanh,relu,pow) are overloaded to dynamically build a Directed Acyclic Graph (DAG), which tracks dependencies for the backward pass. - Backpropagation: The
.backward()method performs reverse-mode backpropagation, recursively applying the chain rule to compute and accumulate the gradient for all operations in the graph.
A small, functional Deep Learning library is built directly on top of the custom Value object:
-
Neuron,Layer, andMLP: Implemented custom classes to handle the standard components of an MLP (Multi-Layer Perceptron). -
Activation Functions: Supports non-linearities like
$\mathbf{tanh}$ and$\mathbf{relu}$ .
The custom engine's capabilities are validated by training a complete classifier:
- Dataset: Trained a 2-layer MLP on a custom dataset for binary classification.
- Loss & Optimization: Used a custom max-margin binary classification loss (similar to SVM loss) and an iterative Stochastic Gradient Descent (SGD) loop for weight updates, demonstrating manual control over the optimization process.
- Visualization: The notebook includes visualization of the final decision boundary learned by the network, proving the autograd engine's functionality.
- Clone this repository.
- Open the notebook:
micrograd_from_scratch.ipynb. - Execute the cells to follow the implementation of the
Valueclass, build the MLP, and train the final classifier.