An mlx port of Karpathy's micrograd - a tiny scalar-valued autograd engine with a small PyTorch-like neural network library on top.
Project description
mlx-micrograd
An mlx port of Karpathy's micrograd - a tiny scalar-valued autograd engine with a small PyTorch-like neural network library on top.
Installation
pip install mlx_micrograd
Example usage
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}') # 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 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mlx_micrograd-0.1.0.tar.gz.
File metadata
- Download URL: mlx_micrograd-0.1.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
340e0e085c1b8f404fc2d1387cf73022d53e718faca2ae4ce822dba7b53bc534
|
|
| MD5 |
e97545b63783f8b78001fdfaeaf45832
|
|
| BLAKE2b-256 |
a17701e35cdb97b19f067990a06d6d4d95ec1c7a5496468442a3634ad2bf3cc1
|
File details
Details for the file mlx_micrograd-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mlx_micrograd-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a295d98144114cde2709431bca8c5948073126fef7401edc4e08015bb1ceaa62
|
|
| MD5 |
1d5e060e5ba9926e0bbf4fe59d1271a5
|
|
| BLAKE2b-256 |
0085fae2860a776edd17ddc2025e1d39868320cc32d496ab49e3ac9c268e71de
|