Skip to main content

A minimal scalar-valued autograd engine for automatic differentiation

Project description

ScalarGrad Banner

ScalarGrad

Python Version License: MIT

A minimal scalar-valued automatic differentiation (autograd) engine with neural network capabilities, built from scratch in Python.

Features

  • ๐Ÿ”ฅ Core Autograd Engine: Reverse-mode automatic differentiation for scalar values
  • ๐Ÿง  Neural Networks: Full-featured MLP implementation with customizable architectures
  • ๐Ÿ“Š Optimizers: SGD with momentum and Adam optimizer
  • ๐Ÿ“‰ Loss Functions: MSE and SVM loss implementations
  • ๐ŸŽจ Visualization: Computation graph and training metric visualization
  • โœจ Rich Terminal Output: Themed console output, animated progress bars, and styled tables via rich
  • ๐ŸŒˆ Theme System: Five built-in palettes (default, dark, cyberpunk, light, minimal) with live switching and custom theme registration
  • ๐Ÿ› ๏ธ Production-Ready: Comprehensive logging, error handling, and configuration management

Installation

From PyPI (when published)

pip install scalargrad

From Source

git clone https://github.com/THAMIZH-ARASU/scalargrad.git
cd scalargrad
pip install -e .

Dependencies

rich>=13.0.0 is a required runtime dependency and is installed automatically.

Optional Dependencies

For visualization features:

pip install scalargrad[viz]      # For computation graph visualization (requires Graphviz binary)
pip install scalargrad[plot]     # For training plots and decision boundaries
pip install scalargrad[all]      # Install all optional dependencies

Windows / Graphviz note: After pip install scalargrad[viz], also install the Graphviz binary:

winget install graphviz

Then add C:\Program Files\Graphviz\bin to your PATH.

For development:

pip install -r requirements-dev.txt

Quick Start

Basic Autograd Example

from scalargrad import Scalar

# Create scalar values
a = Scalar(2.0, label='a')
b = Scalar(3.0, label='b')

# Build computation graph
c = a * b + b ** 2
c.label = 'c'

# Compute gradients
c.backward()

print(f"c = {c.data}")       # c = 15.0
print(f"dc/da = {a.grad}")   # dc/da = 3.0
print(f"dc/db = {b.grad}")   # dc/db = 8.0

Neural Network Training

from scalargrad import MLP, Adam, MSELoss, Trainer

# Create a neural network
model = MLP(
    nin=2,                              # 2 input features
    layer_sizes=[16, 16, 1],            # Hidden layers and output
    activations=['relu', 'relu', 'linear']
)

# Setup training
optimizer = Adam(model.parameters(), lr=0.01)
loss_fn = MSELoss()
trainer = Trainer(model, optimizer, loss_fn)

# Training data
X = [[0, 0], [0, 1], [1, 0], [1, 1]]
y = [0, 1, 1, 0]  # XOR problem

# Train the model
history = trainer.fit(X, y, epochs=100)

Architecture

The package is organized into the following modules:

scalargrad/
โ”œโ”€โ”€ core.py           # Core Scalar class and autograd engine
โ”œโ”€โ”€ config.py         # Configuration, logging (RichHandler), theme/display fields
โ”œโ”€โ”€ theme.py          # StyleTheme dataclass, THEMES dict, console proxy, theme API
โ”œโ”€โ”€ nn/               # Neural network components
โ”‚   โ”œโ”€โ”€ module.py     # Base Module class
โ”‚   โ”œโ”€โ”€ neuron.py     # Neuron implementation
โ”‚   โ”œโ”€โ”€ layer.py      # Layer implementation
โ”‚   โ””โ”€โ”€ mlp.py        # MLP implementation
โ”œโ”€โ”€ optim/            # Optimization algorithms
โ”‚   โ”œโ”€โ”€ optimizer.py  # Base Optimizer class
โ”‚   โ”œโ”€โ”€ sgd.py        # SGD with momentum
โ”‚   โ””โ”€โ”€ adam.py       # Adam optimizer
โ”œโ”€โ”€ loss.py           # Loss functions
โ”œโ”€โ”€ training.py       # Trainer with Progress / Live-table display modes
โ””โ”€โ”€ visualization.py  # Computation graph and training metric visualization

API Reference

Core Components

Scalar

The fundamental building block representing a scalar value with automatic differentiation.

from scalargrad import Scalar

x = Scalar(5.0, label='x')
y = x ** 2 + 3 * x + 1
y.backward()  # Compute gradients

Supported Operations:

  • Arithmetic: +, -, *, /, **
  • Activation functions: relu(), tanh(), sigmoid()
  • Mathematical: exp(), log()

Module

Base class for all neural network components.

from scalargrad.nn import Module

class CustomLayer(Module):
    def __init__(self):
        super().__init__()
        # Initialize parameters
    
    def __call__(self, x):
        # Forward pass
        pass
    
    def parameters(self):
        # Return trainable parameters
        pass

Neural Networks

MLP (Multi-Layer Perceptron)

from scalargrad import MLP

model = MLP(
    nin=3,                           # Input features
    layer_sizes=[16, 16, 1],         # Layer sizes
    activations=['relu', 'relu', 'linear'],  # Activations per layer
    init_scale=1.0                   # Weight initialization scale
)

Optimizers

SGD (Stochastic Gradient Descent)

from scalargrad import SGD

optimizer = SGD(
    parameters=model.parameters(),
    lr=0.01,          # Learning rate
    momentum=0.9      # Momentum coefficient
)

Adam

from scalargrad import Adam

optimizer = Adam(
    parameters=model.parameters(),
    lr=0.001,         # Learning rate
    beta1=0.9,        # First moment decay
    beta2=0.999,      # Second moment decay
    eps=1e-8          # Numerical stability
)

Loss Functions

MSELoss (Mean Squared Error)

from scalargrad import MSELoss

loss_fn = MSELoss()
loss = loss_fn(predictions, targets)

SVMLoss (Support Vector Machine Loss)

from scalargrad import SVMLoss

loss_fn = SVMLoss()
loss = loss_fn(predictions, targets)  # targets should be -1 or 1

Training

Trainer

from scalargrad import Trainer

trainer = Trainer(
    model=model,
    optimizer=optimizer,
    loss_fn=loss_fn
)

history = trainer.fit(
    X=training_data,
    y=training_labels,
    epochs=100,
    batch_size=32,      # None for full batch
    verbose=True
)

Visualization

Computation Graph

from scalargrad import Visualizer

# Visualize computation graph
graph = Visualizer.draw_graph(output_scalar, format='svg')
graph.render('computation_graph', view=True)

Training History

# Plot training metrics
Visualizer.plot_training_history(trainer.history)

Decision Boundary

# Plot decision boundary (for 2D data)
Visualizer.plot_decision_boundary(model, X, y)

Examples

Check the examples/ directory for complete examples:

  • basic_operations.py: Demonstrates basic autograd functionality
  • neural_network.py: Full neural network training example

Run examples:

python examples/basic_operations.py
python examples/neural_network.py

Running Tests

# Install development dependencies
pip install -r requirements-dev.txt

# Run tests
pytest

# Run with coverage
pytest --cov=scalargrad --cov-report=html

Publishing to PyPI

  1. Build the package:
python -m build
  1. Upload to TestPyPI (optional):
python -m twine upload --repository testpypi dist/*
  1. Upload to PyPI:
python -m twine upload dist/*

Theming

ScalarGrad ships with five built-in colour palettes:

Name Description
default Blue-toned professional look
dark Muted greys for dark terminals
cyberpunk High-contrast neon cyan/magenta
light Subdued colours for light terminals
minimal Monochrome, no colour
from scalargrad import set_theme, list_themes, register_theme, StyleTheme

# Switch theme globally
set_theme("cyberpunk")

# List available themes
list_themes()

# Register a custom theme
register_theme("my_theme", StyleTheme(
    header="bold yellow",
    value="cyan",
    success="green",
    # ... other fields use defaults
))

You can also switch the theme via the global config object:

from scalargrad import config
config.theme = "dark"   # live switch โ€” no re-import needed

Configuration

Global configuration can be modified:

from scalargrad import config, LogLevel

config.log_level = LogLevel.DEBUG
config.seed = 42
config.gradient_clip = 1.0   # Clip gradients to this magnitude

# Theme ("default", "dark", "cyberpunk", "light", "minimal", or custom)
config.theme = "cyberpunk"

# Training display mode: "progress" (animated bar) or "table" (live epoch table)
config.training_display = "table"

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Inspired by:

  • micrograd by Andrej Karpathy
  • PyTorch's autograd system
  • Modern deep learning frameworks

Citation

If you use ScalarGrad in your research, please cite:

@software{scalargrad2024,
  title = {ScalarGrad: A Production-Grade Scalar-Valued Autograd Engine},
  author = {Thamizharasu Saravanan},
  year = {2026},
  url = {https://github.com/THAMIZH-ARASU/scalargrad}
}

Support

For questions and support:


Made with โค๏ธ for the deep learning community

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

scalargrad-1.0.5.tar.gz (8.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scalargrad-1.0.5-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file scalargrad-1.0.5.tar.gz.

File metadata

  • Download URL: scalargrad-1.0.5.tar.gz
  • Upload date:
  • Size: 8.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for scalargrad-1.0.5.tar.gz
Algorithm Hash digest
SHA256 1c5440b1a86fc15d0226a88692a8cb5ed35082e390fba9deacfaa30e5c64f040
MD5 25a90b87d4f2fd916eba4157707c373f
BLAKE2b-256 81074903dacaafae421996198ae7875f728a220574e719d838079928d8518a06

See more details on using hashes here.

File details

Details for the file scalargrad-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: scalargrad-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 27.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for scalargrad-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6ebada41ef9dc5697403e952c9a2677409a65ea4a0abd71b9b64b1e4957a44ea
MD5 52946660357bf34f7e788a7b8a57e49f
BLAKE2b-256 1538315c308266d587a713c574ae140ff60baff1a6c0f06d14fe45424464ee23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page