karpathy/micrograd

▲ 20 stars today★ 17,546⑂ 2,797

A tiny scalar-valued autograd engine and a neural net library on top of it with PyTorch-like API

About karpathy/micrograd

karpathy/micrograd is an open-source project on GitHub, mainly written in Jupyter Notebook. A tiny scalar-valued autograd engine and a neural net library on top of it with PyTorch-like API It currently holds 17,546 stars and 2,797 forks with 80 open issues, and was last pushed on 2026-08-03 (repository created 2020-04-13).

Project Overview

AI Homed tracks it on the Today's Trending board, currently at rank #62 with 20 new stars today.

GitHub Repository Details

Repository karpathy/micrograd · default branch master · size 241 KB · watchers 196 · source: GitHub REST API and repository README

README

micrograd

awww

A tiny Autograd engine (with a bite! :)). Implements backpropagation (reverse-mode autodiff) over a dynamically built DAG and a small neural networks library on top of it with a PyTorch-like API. Both are tiny, with about 100 and 50 lines of code respectively. The DAG only operates over scalar values, so e.g. we chop up each neuron into all of its individual tiny adds and multiplies. However, this is enough to build up entire deep neural nets doing binary classification, as the demo notebook shows. Potentially useful for educational purposes.

Installation

pip install micrograd

Example usage

Below is a slightly contrived example showing a number of possible supported operations:

from micrograd.engine import Value

a = Value(-4.0) b = Value(2.0) c = a + b d = a * b + b**3 c += c + 1 c += 1 + c + (-a) d += d * 2 + (b + a).relu() d += 3 * d + (b - a).relu() e = c - d f = e**2 g = f / 2.0 g += 10.0 / f print(f'{g.data:.4f}') # prints 24.7041, the outcome of this forward pass g.backward() print(f'{a.grad:.4f}') # prints 138.8338, i.e. the numerical value of dg/da print(f'{b.grad:.4f}') # prints 645.5773, i.e. the numerical value of dg/db

Training a neural net

The notebook demo.ipynb provides a full demo of training an 2-layer neural network (MLP) binary classifier. This is achieved by initializing a neural net from micrograd.nn module, implementing a simple svm "max-margin" binary classification loss and using SGD for optimization. As shown in the notebook, using a 2-layer neural net with two 16-node hidden layers we achieve the following decision boundary on the moon dataset:

2d neuron

Training a GPT

For a more advanced example, see microgpt, which trains and samples from a full GPT-2-like transformer in pure, dependency-free Python. It builds on a more efficient and better version of the autograd engine here (storing local gradients at forward time instead of per-op backward closures), and is the complete algorithm in a single file — everything else is just efficiency. See also the accompanying explainer post for a detailed walkthrough.

Tracing / visualization

For added convenience, the notebook trace_graph.ipynb produces graphviz visualizations. E.g. this one below is of a simple 2D neuron, arrived at by calling draw_dot on the code below, and it shows both the data (left number in each node) and the gradient (right number in each node).

from micrograd import nn
n = nn.Neuron(2)
x = [Value(1.0), Value(-2.0)]
y = n(x)
dot = draw_dot(y)
2d neuron

Running tests

To run the unit tests you will have to install PyTorch, which the tests use as a reference for verifying the correctness of the calculated gradients. Then simply:

python -m pytest

License

MIT

GitHub Stars & Activity

17,546Stars
2,797Forks
80Open issues
Jupyter NotebookLanguage

GitHub Popularity

GitHub stars17,546
Forks2,797
Open issues80
Primary languageJupyter Notebook
LicenseMIT
Stars gained today20
Created2020-04-13
Last pushed2026-08-03

Trending History

Daily boardrank #62 · ▲ 20 stars

Related AI Projects

1

microsoft / generative-ai-for-beginners

Jupyter Notebook★ 119,854⑂ 63,097▲ 77 stars
2

anthropics / prompt-eng-interactive-tutorial

Jupyter Notebook★ 38,194⑂ 4,230▲ 24 stars
3

anthropics / courses

Jupyter Notebook★ 22,843⑂ 2,479▲ 7 stars
4

facebookresearch / sam2

Jupyter Notebook★ 19,874⑂ 2,545▲ 12 stars
5

Infrasys-AI / AISystem

Jupyter Notebook★ 17,821⑂ 2,474▲ 16 stars
6

GoogleCloudPlatform / generative-ai

Jupyter Notebook★ 17,713⑂ 4,454▲ 10 stars
7

datawhalechina / llm-universe

Jupyter Notebook★ 13,990⑂ 1,415▲ 18 stars
8

udlbook / udlbook

Jupyter Notebook★ 9,905⑂ 2,322▲ 10 stars

More AI Rankings