A Pure Python Deep Learning Framework with Automatic Differentiation
Project description
๐ NeuroGrad
A Pure Python Deep Learning Framework with Automatic Differentiation
Built from scratch with no AI assistance - showcasing pure algorithmic understanding
๐ Overview
NeuroGrad is a lightweight, educational deep learning framework built entirely from scratch in Python. It implements automatic differentiation (backpropagation) with a clean, intuitive API similar to PyTorch.
Perfect for:
- ๐ Learning: Understanding how deep learning frameworks work
- ๐ฌ Research: Rapid prototyping of new algorithms
- ๐ Education: Teaching autodiff and neural network concepts
- ๐ ๏ธ Experimentation: Testing custom operations
Educational Foundation: Built following Andrew Ng's Deep Learning Specialization principles with minimal AI assistance for core implementation.
โจ Key Features
๐ฅ Core Capabilities
- Automatic Differentiation: Full reverse-mode autodiff with computational graph tracking
- Mixed Precision Training: Automatic mixed precision (AMP) with PyTorch-compatible API โก NEW!
- GPU Acceleration: Seamless CPU/CUDA support via NumPy/CuPy backend switching
- Dynamic Graphs: Build and modify computational graphs on-the-fly
- Memory Efficient: Optimized gradient computation with cycle detection
๐ง Neural Network Components
- Layers: Linear, Conv2D, MaxPool2D/AveragePool2D, MLP with batch normalization and dropout
- Activations: ReLU, Sigmoid, Tanh, LeakyReLU, Softmax
- Loss Functions: MSE, RMSE, MAE, Binary/Categorical Cross-Entropy
- Optimizers: SGD (with momentum), Adam, RMSprop
- Data Utilities: Dataset and DataLoader classes
- Metrics: Classification and regression metrics
๐ ๏ธ Developer Tools
- Graph Visualization: Beautiful computational graph plotting
- Gradient Checking: Numerical gradient verification
- Mixed Precision: 1.5-2x speedup, 40-50% memory reduction
๐ Quick Start
Installation
# Install from PyPI
pip install neurograd
# With GPU support
pip install neurograd[gpu]
# Everything (GPU, visualization, examples)
pip install neurograd[all]
# From source
git clone https://github.com/b-ionut-r/neurograd.git
cd neurograd && pip install -e .
Basic Usage
import neurograd as ng
# Create tensors with gradient tracking
x = ng.Tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
y = ng.Tensor([[2.0, 1.0], [1.0, 2.0]], requires_grad=True)
# Perform operations
z = x @ y + x.sin() # Matrix multiplication + element-wise sine
loss = z.sum() # Scalar loss
# Automatic differentiation
loss.backward()
print(f"x.grad: {x.grad}")
๐ง Neural Networks
Complete Training Example
from neurograd.nn.layers.linear import Linear, MLP
from neurograd.nn.losses import MSE
from neurograd.optim.adam import Adam
from neurograd.utils.data import Dataset, DataLoader
from neurograd.nn.metrics import accuracy_score
# Create dataset and model
dataset = Dataset(X_train, y_train)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)
model = MLP([784, 128, 64, 10]) # Input -> Hidden -> Output
# Define loss and optimizer
criterion = MSE()
optimizer = Adam(model.named_parameters(), lr=0.001)
# Training loop
for epoch in range(100):
for X_batch, y_batch in dataloader:
# Forward pass
output = model(X_batch)
loss = criterion(y_batch, output)
# Backward pass
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Evaluate
model.eval()
pred = model(X_test)
acc = accuracy_score(y_test, pred)
model.train()
print(f"Epoch {epoch}, Loss: {loss.data:.4f}, Accuracy: {acc:.4f}")
Mixed Precision Training โก NEW!
PyTorch-compatible automatic mixed precision for faster training:
from neurograd.amp import autocast, GradScaler
from neurograd.nn.layers.conv import Conv2D, MaxPool2D
from neurograd.nn.layers.linear import Linear
from neurograd.nn.module import Sequential
from neurograd.functions.activations import ReLU, Softmax
# Create CNN model (channels-first: NCHW)
model = Sequential(
Conv2D(1, 32, kernel_size=3, padding="same", activation="relu"),
MaxPool2D(pool_size=2),
Conv2D(32, 64, kernel_size=3, padding="same", activation="relu"),
MaxPool2D(pool_size=2),
Flatten(),
Linear(64 * 7 * 7, 128, activation="relu"),
Linear(128, 10),
Softmax(axis=1)
)
# Setup mixed precision
optimizer = Adam(model.named_parameters(), lr=0.001)
loss_fn = CategoricalCrossEntropy()
scaler = GradScaler()
# Training with mixed precision
for epoch in range(num_epochs):
for batch_x, batch_y in dataloader:
optimizer.zero_grad()
# Mixed precision forward pass
with autocast(enabled=True):
predictions = model(batch_x) # Auto FP16 where safe
loss = loss_fn(batch_y, predictions) # Auto FP32 for stability
# Gradient scaling for FP16 stability
scaled_loss = scaler.scale(loss)
scaled_loss.backward()
scaler.step(optimizer) # Unscales gradients automatically
scaler.update() # Updates scale factor
print(f"Loss: {loss.data.item():.4f}, Scale: {scaler.get_scale():.0f}")
# Benefits: โก 1.5-2x faster, ๐พ 40-50% less memory, ๐ฏ same accuracy
Layers and Operations
# Linear layers with built-in features
layer = Linear(784, 128, activation="relu", dropout=0.2,
batch_normalization=True, weights_initializer="he")
# Convolutional layers (channels-first: NCHW)
conv = Conv2D(3, 64, kernel_size=(3,3), padding="same", activation="relu")
pool = MaxPool2D(pool_size=(2,2), strides=(2,2))
# Activations and losses
from neurograd.functions.activations import ReLU, Sigmoid, Softmax
from neurograd.nn.losses import MSE, CategoricalCrossEntropy
# Optimizers
optimizer = Adam(model.named_parameters(), lr=0.001, beta1=0.9, beta2=0.999)
optimizer = SGD(model.named_parameters(), lr=0.01, beta=0.9, weight_decay=1e-4)
๐งฎ Core Operations
Mathematical Functions
x = ng.Tensor([1.0, 2.0, 3.0], requires_grad=True)
# Arithmetic: +, -, *, /, **
z = x + y, x * y, x ** 2
# Math functions
y = x.log(), x.exp(), x.sin(), x.sqrt(), x.abs()
# Linear algebra
C = A @ B # Matrix multiplication
D = A.transpose() # Transpose
# Reductions with axis support
s = x.sum(axis=0), x.mean(axis=1, keepdims=True), x.max(), x.std()
Data Utilities
from neurograd.utils.data import Dataset, DataLoader
dataset = Dataset(X, y)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True, seed=42)
for batch_idx, (X_batch, y_batch) in enumerate(dataloader):
output = model(X_batch)
loss = criterion(y_batch, output)
๐ง Advanced Usage
Custom Functions
from neurograd.functions.base import Function
class Swish(Function):
def forward(self, x):
self.sigmoid_x = 1 / (1 + ng.xp.exp(-x))
return x * self.sigmoid_x
def backward(self, grad_output):
x = self.parent_tensors[0]
swish_grad = self.sigmoid_x * (1 + x.data * (1 - self.sigmoid_x))
return grad_output * swish_grad if x.requires_grad else None
Gradient Checking
from neurograd.utils.grad_check import gradient_check
is_correct = gradient_check(model, X, y, loss_fn, epsilon=1e-7)
print(f"Gradients correct: {is_correct}")
Visualization
# Visualize computational graphs
fig = loss.visualize_graph(title="Training Loss Graph")
loss.save_graph("computation_graph.png")
loss.print_graph()
# Graph statistics
stats = loss.graph_stats()
print(f"Nodes: {stats['num_tensors']}, Depth: {stats['max_depth']}")
Checkpointing (PyTorch-style)
import neurograd as ng
# Save checkpoint
ng.save({
'model_state': model.state_dict(),
'optimizer_state': optimizer.state_dict(),
# optional: 'scaler_state': scaler.state_dict(), 'epoch': epoch
}, 'checkpoint.pth')
# Or use the convenience helper
ng.save_checkpoint(model=model, optimizer=optimizer, path='checkpoint.pth', epoch=epoch)
# Load checkpoint later
ckpt = ng.load('checkpoint.pth') # or ng.load_checkpoint('checkpoint.pth')
model.load_state_dict(ckpt['model_state'])
optimizer.load_state_dict(ckpt['optimizer_state'])
๐๏ธ Architecture
neurograd/
โโโ tensor.py # Core Tensor class
โโโ functions/ # Mathematical operations
โ โโโ base.py # Function base class
โ โโโ arithmetic.py # +, -, *, /, **
โ โโโ math.py # log, exp, sin, cos, etc.
โ โโโ activations.py # Neural network activations
โ โโโ conv.py # Convolution operations
โ โโโ tensor_ops.py # Tensor ops (includes Cast)
โ โโโ reductions.py # sum, mean, max, etc.
โโโ amp/ # โก Mixed precision (NEW!)
โ โโโ autocast.py # Automatic precision context
โ โโโ grad_scaler.py # Gradient scaling
โ โโโ utils.py # AMP utilities
โโโ nn/ # Neural network components
โ โโโ layers/ # Network layers
โ โโโ losses.py # Loss functions
โ โโโ metrics.py # Evaluation metrics
โ โโโ module.py # Base module system
โโโ optim/ # Optimization algorithms
โ โโโ sgd.py, adam.py, rmsprop.py
โโโ utils/ # Utilities
โโโ grad_check.py # Gradient verification
โโโ graph.py # Visualization
โโโ data.py # Dataset/DataLoader
๐ฏ Roadmap
โ Completed Features
- Automatic differentiation with dynamic graphs โ
- Neural network layers (Linear, Conv2D, Pooling) โ
- Loss functions and optimizers (SGD, Adam, RMSprop) โ
- Data utilities (Dataset, DataLoader) โ
- Evaluation metrics and visualization โ
- Mixed precision training (AMP) โก NEW! โ
๐ Upcoming
- Recurrent layers (RNN, LSTM, GRU)
- Advanced optimizers (AdaGrad, Nadam)
- Model serialization/loading
- Distributed training support
- Dynamic quantization and pruning
๐ Resources & Contributing
Educational Foundation
This framework implements concepts from Andrew Ng's Deep Learning Specialization and mathematical foundations of automatic differentiation.
Contributing
- ๐ Bug Reports: Use GitHub Issues with minimal reproduction code
- ๐ก Features: Discuss API design in issues first
- ๐ง Development:
git cloneโpip install -e .โpytest
Testing
# Run comprehensive tests
jupyter notebook comprehensive_framework_test.ipynb
# Gradient checking
python -c "from neurograd.utils.grad_check import *; test_all()"
๐ License & Contact
MIT License - see LICENSE file for details.
- Issues: Report bugs/features
- Discussions: Community forum
โญ Star this repository if you find it helpful! โญ
Built with โค๏ธ for the deep learning community
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-5.1.4.tar.gz.
File metadata
- Download URL: neurograd-5.1.4.tar.gz
- Upload date:
- Size: 67.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b5fac21e7882280d74abe881fa4a83ddb5a95082bb9e34dec86d1c4e66b3146
|
|
| MD5 |
67cb507e8a0b9417395f6fef590bc93e
|
|
| BLAKE2b-256 |
5e6c8537e87443bf4423c0da2c1e491d935505611c74b7248ce1a727ccb876fc
|
File details
Details for the file neurograd-5.1.4-py3-none-any.whl.
File metadata
- Download URL: neurograd-5.1.4-py3-none-any.whl
- Upload date:
- Size: 77.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6306e5487037377d91b09c3d91973a6568a6b370b526a25e36244cbe8f5d89bc
|
|
| MD5 |
75e91d8c0405a399be3f4cc5d74c4ccb
|
|
| BLAKE2b-256 |
48cb184b4e785e772a5ff999589bbe37bd3bd206b6b94a3c565fcc5d2e5434b2
|