Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jaykef/mlx_micrograd
mlx port of Karpathy's micrograd - a tiny scalar-valued autograd engine with a small PyTorch-like neural network library on top.
https://github.com/jaykef/mlx_micrograd
Last synced: 4 months ago
JSON representation
mlx port of Karpathy's micrograd - a tiny scalar-valued autograd engine with a small PyTorch-like neural network library on top.
- Host: GitHub
- URL: https://github.com/jaykef/mlx_micrograd
- Owner: Jaykef
- Created: 2024-05-05T02:48:12.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-05-06T02:57:16.000Z (10 months ago)
- Last Synced: 2024-09-18T11:47:02.712Z (5 months ago)
- Language: Jupyter Notebook
- Homepage:
- Size: 48.8 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mlx-micrograd
An mlx port of Karpathy's [micrograd](https://github.com/karpathy/micrograd) - a tiny scalar-valued autograd engine with a small PyTorch-like neural network library on top.
### Installation
```bash
pip install mlx_micrograd
```### Example usage
Example showing a number of possible supported operations:
```python
from mlx_micrograd.engine import Valuea = 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}') # prints array(24.7041, dtype=float32), the outcome of this forward pass
g.backward()
print(f'{a.grad}') # prints array(138.834, dtype=float32), i.e. the numerical value of dg/da
print(f'{b.grad}') # prints array(645.577, dtype=float32), i.e. the numerical value of dg/db
```### Training a neural net
`demo.ipynb` provides a full demo of training an 2-layer neural network (MLP) binary classifier.
### Running tests
To run the unit tests cd into tests folder - which has [test_engine.py](https://github.com/Jaykef/mlx_micrograd/blob/main/tests/test_engine.py) that does sanity checks with pytorch's autograd and [tests.py](https://github.com/Jaykef/mlx_micrograd/blob/main/tests/tests.py) which run tests on some supported ops.
```bash
python test_engine.py
```
```bash
python tests.py
```### License
MIT