Micro-Grad is automatic differentiation (autograd) engine written from scratch in Python. It provides the fundamental building blocks for creating and training simple neural networks, offering a clear view into the mechanics of backpropagation. This project is inspired by Andrej Karpathy's micrograd.
- Scalar Autograd Engine: Implements a
Valueobject that tracks gradients for scalar values. - Dynamic Computation Graph: Automatically builds a graph of operations for backpropagation.
- Neural Network Primitives: Includes
Neuron,Layer, andMLPclasses to construct simple feed-forward networks. - Optimization: Provides an
SGDoptimizer and aStepLRSchedulerfor learning rate decay.
engine.py: The heart of the library, containing theValueclass. It overloads standard arithmetic operations (+,*,-,/) to build a computation graph. Thebackward()method performs a topological sort on this graph to compute gradients for all nodes using the chain rule.neuron.py,layer.py,MLP.py: These files define the neural network architecture.- A
Neuronhas weights and a bias. - A
Layeris a collection of neurons. - An
MLP(Multi-Layer Perceptron) stacks multiple layers to form a network.
- A
optimizer.py&LRScheduler.py: These handle the model's weight updates. TheSGDoptimizer adjusts parameters based on their gradients, andStepLRSchedulercan be used to adapt the learning rate during training.loss_functions.py: Contains loss functions likeMSELoss(Mean Squared Error) to evaluate model performance.
- Andrej Karpathy, Micrograd: A tiny autograd engine in Python – YouTube