Skip to content

Commit 224da0a

Browse files
author
Jencir Lee
committed
update README
1 parent 5cf6b82 commit 224da0a

File tree

1 file changed

+57
-33
lines changed

1 file changed

+57
-33
lines changed

README.md

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,75 @@
11

22
# micrograd
3+
A tiny Autograd engine whose only dependency is NumPy. 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.
34

4-
![awww](assets/puppy.jpg)
5-
6-
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.
7-
8-
### Installation
5+
This version is capable of working with matrices and higher-order tensors. For @karpathy's original scalar-based version, locate the code with tag `scalar`.
96

7+
## Installation
108
```bash
11-
pip install micrograd
9+
python3 -m venv venv
10+
. venv/bin/activate
11+
pip install .
1212
```
1313

14-
### Example usage
14+
## Get Started
15+
```python
16+
from micrograd import Value
17+
from numpy import array
18+
19+
a = Value(array([[2, 3], [4, 5]]))
20+
b = Value(array([6, 7]))
21+
c = a @ b
22+
print(c) # Value(data=[33 59], grad=None)
23+
c.backward()
24+
print(a) # Value(data=..., grad=[[6. 7.], [6. 7.]])
25+
print(b) # Value(data=..., grad=[6. 8.])
26+
```
1527

16-
Below is a slightly contrived example showing a number of possible supported operations:
28+
## Lazy evaluation
29+
When defining a tensor, one may just indicate `shape` and `name`, and later on provide the value.
1730

1831
```python
19-
from micrograd.engine import Value
20-
21-
a = Value(-4.0)
22-
b = Value(2.0)
23-
c = a + b
24-
d = a * b + b**3
25-
c += c + 1
26-
c += 1 + c + (-a)
27-
d += d * 2 + (b + a).relu()
28-
d += 3 * d + (b - a).relu()
29-
e = c - d
30-
f = e**2
31-
g = f / 2.0
32-
g += 10.0 / f
33-
print(f'{g.data:.4f}') # prints 24.7041, the outcome of this forward pass
34-
g.backward()
35-
print(f'{a.grad:.4f}') # prints 138.8338, i.e. the numerical value of dg/da
36-
print(f'{b.grad:.4f}') # prints 645.5773, i.e. the numerical value of dg/db
32+
from micrograd import Value
33+
from numpy import array
34+
35+
a = Value(shape=(2, 2), name='var1')
36+
b = Value(shape=(2,), name='var2')
37+
c = (a @ b).relu()
38+
c.forward(var1=array([[2, 3], [4, 5]]),
39+
var2=array([1, -1]))
40+
c.backward()
3741
```
3842

39-
### Training a neural net
43+
The **essential pattern** is to call `forward()` once with the values for the varialbes, then `backward()` once for the mathematical derivatives.
44+
45+
```python
46+
x.forward(var1=value1, var2=value2, ...)
47+
x.backward()
48+
```
4049

50+
Each time the `forward()` is called (e.g. for minibatch evaluation), the lazily defined variables have to be fed values in the function signature. Otherwise, it will take all `nan` as value. The final result will likely be `nan` to signal missing values for some variables.
51+
52+
## Efficient operator dependency topology computation
53+
The operator dependency topology computation is only calculated once then cached, supposing the topology is static once a variable is defined.
54+
55+
## Supported operators
56+
* `__pow__`
57+
* `__matmul__`
58+
* `tensordot` for tensor contraction
59+
* `relu`
60+
* `log`
61+
* `log1p`
62+
* `arctanh`
63+
* `T` for transpose
64+
* `sum`
65+
* `mean`
66+
67+
## Training a neural net
4168
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:
4269

4370
![2d neuron](assets/moon_mlp.png)
4471

45-
### Tracing / visualization
46-
72+
## Tracing / visualization
4773
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).
4874

4975
```python
@@ -56,14 +82,12 @@ dot = draw_dot(y)
5682

5783
![2d neuron](assets/gout.svg)
5884

59-
### Running tests
60-
85+
## Running tests
6186
To run the unit tests:
6287

6388
```bash
6489
python -m unittest tests/*.py
6590
```
6691

67-
### License
68-
92+
## License
6993
MIT

0 commit comments

Comments
 (0)