A micrograd-like autodiff library extended to vectors
Project description
NeuroGrad
A micrograd-like ({https://github.com/karpathy/micrograd}) autodiff library extended to support vector operations. NeuroGrad provides automatic differentiation capabilities for building and training neural networks from scratch.
Features
- Vector-based automatic differentiation: Extends micrograd's scalar operations to vectors
- Neural network components: Built-in
Neuron,Layer, andMLPclasses - Activation functions: ReLU and Tanh activations with automatic gradient computation
- Simple API: Easy-to-use interface for building neural networks (MLPs only for now)
Installation
Clone the repository and install:
git clone https://github.com/Inomjonov/neurograd.git
cd neurograd
pip install -e .
Or install directly from GitHub:
pip install git+https://github.com/Inomjonov/neurograd.git
Quick Start
from neurograd import VectorValue, Neuron, Layer, MLP
# Create a simple neural network
mlp = MLP(3, [4, 4, 1])
# Forward pass
x = VectorValue([1.0, 2.0, 3.0])
out = mlp(x)
# Backward pass
loss = (out - VectorValue([1.0])) ** 2
loss.backward()
# Access gradients
for p in mlp.parameters():
print(f"Parameter: {p.data}, Gradient: {p.grad}")
Components
VectorValue
The core class for automatic differentiation with vector support.
from neurograd import VectorValue
a = VectorValue([1.0, 2.0, 3.0])
b = VectorValue([4.0, 5.0, 6.0])
# Operations
c = a + b
d = a * b
e = a.dot(b)
f = a.sum()
Neuron
A single neuron with optional non-linearity.
from neurograd import Neuron
neuron = Neuron(nin=3, nonlin=True)
output = neuron(VectorValue([1.0, 2.0, 3.0]))
Layer
A layer of neurons.
from neurograd import Layer
layer = Layer(nin=3, nout=4)
output = layer(VectorValue([1.0, 2.0, 3.0]))
MLP
Multi-layer perceptron.
from neurograd import MLP
mlp = MLP(nin=3, nouts=[4, 4, 1])
output = mlp(VectorValue([1.0, 2.0, 3.0]))
Disclaimer
NeuroGrad was developed solely for educational purposes — to demonstrate how reverse-mode automatic differentiation and simple neural networks work from scratch. It is not intended for commercial or production use. For real workloads, use a mature framework such as PyTorch, TensorFlow, or JAX.
Known Limitations
This is a minimal teaching library and has several known limitations:
- No scalar broadcasting. Element-wise ops (
+,*) require operands of equal length, so mixing a bare scalar with a multi-element vector (e.g.vec * 2,10 - vec,vec + 1) raises anAssertionError. Negation and vector–vector subtraction do work. backward()accumulates across calls. Intermediate-node gradients are not reset, so callingbackward()more than once on the same graph double-counts. Rebuild the graph (a fresh forward pass) for each backward, or zero gradients yourself.- Non-scalar
backward()seeds every output with 1.0, i.e. it computes the gradient of the sum of the output components, not a full Jacobian. Callbackward()from a scalar (e.g. a loss) for meaningful gradients. - Recursive topological sort. The backward pass builds its ordering recursively, so very deep computation graphs can hit Python's recursion limit.
- Limited op set. Only the operations needed for MLPs are implemented (no division, no convolutions, no batching, etc.).
License
See LICENSE file for details.
Author
Mironshoh Inomjonov
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 neurograd_vec-0.1.0.tar.gz.
File metadata
- Download URL: neurograd_vec-0.1.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc2a8ffe6d430c69ee14cbd30e1c5aa12ce510bb2af66194af17068d0be91cbb
|
|
| MD5 |
6f8d6478ed9b23cb8a23d8d1600804da
|
|
| BLAKE2b-256 |
0468f480441e1a61309d3b9db3a19bca43bfdcdfd6e133f832746328a9149355
|
File details
Details for the file neurograd_vec-0.1.0-py3-none-any.whl.
File metadata
- Download URL: neurograd_vec-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e3a9b0e5c9e58c0042eeb99337b24253e5b52ec2423221c9c160d25c15fe1de
|
|
| MD5 |
f283de6cede5c9d23338387f4ef9bd22
|
|
| BLAKE2b-256 |
91207547e11b24d945d891b44ec6608aab4a74c3ab83cda4a8eee6628a41facb
|