Micro-Gradients is a tiny automatic differentiation engine for scalar values, built from scratch in Python. It demonstrates how reverse-mode autodiff works under the hood by constructing a dynamic computation graph and backpropagating gradients through it.
This project is inspired by Andrej Karpathy's micrograd and is designed for learning, experimentation, and interview-ready understanding of backprop basics.
- Scalar-based reverse-mode automatic differentiation
- Dynamic computation graph tracking parent nodes and operations
- Core operations: addition, multiplication, power
- Non-linear functions: ReLU, tanh, exp
- Topological-order backward pass for correct gradient propagation
- Minimal implementation in a single readable file
.
|- micro_gradients.py
|- README.md
- Python 3.8+
- numpy
git clone https://github.com/Boules123/Micro-Gradients.git
cd Micro-GradientsWindows (PowerShell):
python -m venv .venv
.\.venv\Scripts\Activate.ps1macOS/Linux:
python3 -m venv .venv
source .venv/bin/activatepip install numpypython micro_gradients.pyExpected output:
a: 21.0, b: 16.0, c: 5.0, d: 6.0, e: 1.0
from micro_gradients import Value
a = Value(2.0, label="a")
b = Value(3.0, label="b")
c = a * b # 6
d = a + b # 5
e = c * d # 30
e.backward()
print(a.grad) # 21.0
print(b.grad) # 16.0from micro_gradients import Value
x = Value(1.5, label="x")
y = (x * x + 2).tanh()
y.backward()
print(y.data) # tanh(4.25)
print(x.grad) # dy/dx- Every
Valuestores:data: scalar numeric valuegrad: gradient accumulator_prev: parent nodes in the graph_op: operation label for debugging_backward: local gradient function
- Mathematical operations create new
Valuenodes and capture local derivative rules in closures. - Calling
backward()on the final output:- builds a topological ordering of nodes
- seeds output gradient with
1.0 - executes local backward functions in reverse topological order
| Method / Operator | Description |
|---|---|
Value(data, label="") |
Create a scalar value node |
a + b |
Addition with gradient support |
a * b |
Multiplication with gradient support |
a ** p |
Power operation (p must be int/float) |
a.relu() |
ReLU activation |
a.tanh() |
tanh activation |
a.exp() |
Exponential function |
loss.backward() |
Backpropagate gradients from output node |
- Scalar-only engine (no tensors)
- No subtraction/division operator overloads yet
- No right-hand operator overloads (
2 + Value(...),2 * Value(...)) - No built-in gradient reset helper across graphs
- No automated tests included yet
This project is ideal if you want to:
- Understand backprop mechanics at a low level
- Connect calculus derivatives to actual code execution
- Build intuition before using full frameworks like PyTorch or JAX
- Add subtraction, division, and negation support
- Add right-hand operator overloads (
__radd__,__rmul__) - Add numerical gradient checking utilities
- Add simple graph visualization support
- Add unit tests and CI
- Micrograd by Andrej Karpathy: https://github.com/karpathy/micrograd
Contributions are welcome.
- Fork the repository
- Create a feature branch
- Commit your changes
- Open a pull request
This project is licensed under the MIT License - see the LICENSE file for details.