Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds uv support and toml file #83

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
.ipynb_checkpoints/
.idea/
.venv/
*.egg-info
build/
.python-version
__pycache__/
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ The notebook `demo.ipynb` provides a full demo of training an 2-layer neural net
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).

```python
from micrograd import nn
from micrograd import nn , Value
n = nn.Neuron(2)
x = [Value(1.0), Value(-2.0)]
y = n(x)
dot = draw_dot(y)
# draw_dot function was defined in `trace_graph.ipynb` file.
# dot = draw_dot(y)
```

![2d neuron](gout.svg)
Expand Down
7 changes: 7 additions & 0 deletions micrograd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from micrograd.engine import Value
from micrograd.graph import draw_dot

__all__ = [
'Value',
'draw_dot'
]
35 changes: 35 additions & 0 deletions micrograd/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from graphviz import Digraph

def trace(root):
nodes, edges = set(), set()

def build(v):
if v not in nodes:
nodes.add(v)
for child in v._prev:
edges.add((child, v))
build(child)

build(root)
return nodes, edges


def draw_dot(root, format='svg', rankdir='LR'):
"""
format: png | svg | ...
rankdir: TB (top to bottom graph) | LR (left to right)
"""
assert rankdir in ['LR', 'TB']
nodes, edges = trace(root)
dot = Digraph(format=format, graph_attr={'rankdir': rankdir}) # , node_attr={'rankdir': 'TB'})

for n in nodes:
dot.node(name=str(id(n)), label="{ data %.4f | grad %.4f }" % (n.data, n.grad), shape='record')
if n._op:
dot.node(name=str(id(n)) + n._op, label=n._op)
dot.edge(str(id(n)) + n._op, str(id(n)))

for n1, n2 in edges:
dot.edge(str(id(n1)), str(id(n2)) + n2._op)

return dot
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "micrograd"
version = "0.1.0"
authors = [
{ name = "Andrej Karpathy", email = "[email protected]" }
]
description = "A tiny scalar-valued autograd engine with a small PyTorch-like neural network library on top."
readme = "README.md"
license = { text = "MIT" }
requires-python = ">=3.8"
dependencies = [
"graphviz>=0.20.3",
"pytest>=8.3.4",
"torch>=2.5.1",
]

[project.urls]
Repository = "https://github.com/karpathy/micrograd"
Empty file added test/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions test/test_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from micrograd import nn, Value, draw_dot


def test_graph():
n = nn.Neuron(2)
x = [Value(1.0), Value(-2.0)]
y = n(x)
dot = draw_dot(y)
assert dot
471 changes: 471 additions & 0 deletions uv.lock

Large diffs are not rendered by default.