Tensor-based autdiff engine and neural network API
Project description
mdgrad
A small autograd engine that implements backpropagation (reverse-mode autodiff). Heavily inspired by karpathy's micrograd, and extended to support operations on tensors instead of scalars. Includes a small neural network api for building and training neural networks. Has a PyTorch-like API.
Hopefully useful as an educational resource.
Installation
pip install mdgrad
Example Usage
A dumb example showing supported operations
import mdgrad
import mdgrad.nn as nn
a = 3 * mdgrad.randn(3, 2)
b = mdgrad.ones(shape=(2, 2))
c = a @ b
d = c * 3 / 2
e = d ** 2
f = e.sum()
print(f.data)
f.backward()
print(a.grad)
An example showing how to define and run a neural network. See the files in examples/ for more details on building and training models.
import mdgrad
import mdgrad.nn as nn
# Define the model and loss function
model = nn.Sequential(
nn.Linear(2, 20),
nn.ReLU(),
nn.Linear(20, 50),
nn.ReLU(),
nn.Linear(50, 15),
nn.ReLU(),
nn.Linear(15, 1),
nn.Sigmoid()
)
loss_fn = nn.MSELoss()
# Create dummy data
X = mdgrad.randn(100, 2)
target = mdgrad.randn(100, 1)
# Compute output and loss
out = model(X)
loss = loss_fn(out, target)
# Compute gradients of parameters
loss.backward()
Project details
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 mdgrad-0.3.tar.gz.
File metadata
- Download URL: mdgrad-0.3.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.8.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4457a9d014a65e7f5972d147db4d0edc248727d316c3cba7f5575d7e4a548fc
|
|
| MD5 |
5cc438e9c496ad59e0b6b5a4f0854742
|
|
| BLAKE2b-256 |
53ec42b7dfba183465ea519c7f7407b39a53cb7b9a4215cfacca62da07cddc63
|
File details
Details for the file mdgrad-0.3-py3-none-any.whl.
File metadata
- Download URL: mdgrad-0.3-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.8.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fad050d5ef563d6b5348d276420c394aacac7abe2a7e14a1cd1ebfefc01f0d52
|
|
| MD5 |
9aa352c2b2517be67d4e9e0a79583027
|
|
| BLAKE2b-256 |
72d5c51cc8724e7355b7720efbdca99470073609b40e64013a6fbda48c1d48d8
|