Metadata-Version: 2.4
Name: micrograd-viz
Version: 0.2.0
Summary: See every gradient: micrograd with labeled computation graphs.
Home-page: https://github.com/asinteger/micrograd-viz
Author: Andrej Karpathy
Maintainer: Mesut Erkekli
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: viz
Requires-Dist: graphviz>=0.20; extra == "viz"
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: torch; extra == "test"
Requires-Dist: graphviz>=0.20; extra == "test"
Dynamic: author
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: license-file
Dynamic: maintainer
Dynamic: provides-extra
Dynamic: requires-python
Dynamic: summary

# micrograd-viz

**See every gradient.** A small, educational fork of [Karpathy's micrograd](https://github.com/karpathy/micrograd) with named values and built-in computation graph visualization.

```python
from micrograd import Value

a = Value(2.0, label='a')
b = Value(-3.0, label='b')
c = Value(10.0, label='c')
e = a * b
e.label = 'e'
d = e + c
d.label = 'd'
f = Value(-2.0, label='f')
L = d * f
L.label = 'L'

L.backward()
L.draw_dot()  # Displays label, data, and grad in Jupyter
```

![Labeled computation graph](examples/labeled_graph.svg)

## Install

Install this fork directly from GitHub (it is not published to PyPI):

```sh
pip install 'micrograd-viz[viz] @ git+https://github.com/asinteger/micrograd-viz.git'
```

Or from a local checkout:

```sh
pip install -e '.[viz]'
```

Rendering additionally requires the [Graphviz system application](https://graphviz.org/download/) (`brew install graphviz` on macOS or `sudo apt install graphviz` on Debian/Ubuntu). Installing the Python `graphviz` package alone is insufficient to render images.

The distribution is named `micrograd-viz`; Python imports remain `micrograd` for compatibility. Use a separate virtual environment if you also use upstream micrograd: both distributions provide the same import package. The autodiff engine itself has no external runtime dependencies; omit `[viz]` when you do not need visualization.

## Visualize any Value

```python
# A single value or the entire graph feeding an output:
a.draw_dot()
L.draw_dot(rankdir='TB')
L.draw_dot(format='png').render('computation', cleanup=True)
```

Call `backward()` before drawing to show calculated gradients. Drawing does not run backpropagation or change values or gradients. Every call captures the current labels, data, and gradients; redraw after updating them. Intermediate outputs are unlabeled until you assign `.label`.

The notebook-style function is available too:

```python
from micrograd import draw_dot, trace

dot = draw_dot(L)  # equivalent to L.draw_dot()
nodes, edges = trace(L)
```

`draw_dot` returns a `graphviz.Digraph`, accepts `format='svg'` by default, and supports `rankdir='LR'` or `'TB'`. Labels can contain Unicode and Graphviz special characters. Nodes are identified by object identity, so equal labels do not merge different values.

## What changed from micrograd?

- `Value(data, _children=(), _op='', label='')` adds the lecture's optional label without changing existing positional arguments.
- `Value.draw_dot()` makes visualization available directly on each value.
- `micrograd.visualize` provides shared `draw_dot` and `trace` functions, with an optional Graphviz dependency.
- `trace_graph.ipynb` imports the packaged implementation instead of redefining it.

The scalar autodiff operations, backward pass, and `micrograd.nn` neural network API remain those of upstream micrograd. This fork does not port the lecture notebook's separate `tanh` implementation.

## Examples and tests

See [the labeled graph notebook](examples/labeled_graph.ipynb), [the graph tracing notebook](trace_graph.ipynb), and the original [neural network training demo](demo.ipynb). The training demo additionally uses NumPy, Matplotlib, and scikit-learn.

```sh
pip install -e '.[test]'
python -m pytest
```

The original engine tests compare gradients against PyTorch. Additional tests cover labels, shared graph nodes, optional dependencies, special characters, and drawing before and after backpropagation. Rendering tests require the Graphviz system application.

## Credits and license

Based on [Andrej Karpathy's micrograd](https://github.com/karpathy/micrograd), with the label and visualization design inspired by his [Neural Networks: Zero to Hero lecture notebook](https://github.com/karpathy/nn-zero-to-hero/blob/master/lectures/micrograd/micrograd_lecture_first_half_roughly.ipynb). Fork maintained by Mesut Erkekli; not an official Karpathy release.

MIT. The original copyright and [license](LICENSE) are preserved.
