Skip to main content

A lightweight autograd framework for AI training

Project description

MiniTorch

A lightweight autograd framework for AI training, built from scratch with NumPy.

CI/CD Pipeline PyPI Version Python Versions License: MIT

Features

  • Automatic Differentiation: Reverse-mode automatic differentiation with computational graph
  • NumPy Backend: All tensor operations powered by NumPy
  • Computational Graph Visualization: Interactive graph visualization using pyvis
  • Gradient Checking: Numerical gradient verification for debugging
  • Clean API: Simple and intuitive interface inspired by PyTorch

Installation

From PyPI (coming soon)

pip install minitorch

From Source

git clone <repository-url>
cd framework
uv pip install -e .

Quick Start

Basic Usage

import numpy as np
from MiniTorch import Variable, square, add, mul

# Create variables
x = Variable(np.array(2.0))
y = Variable(np.array(3.0))

# Perform operations
z = add(square(x), square(y))  # z = x² + y²

# Compute gradients
z.backward()

print(f"z = {z.data}")        # z = 13.0
print(f"dz/dx = {x.grad}")    # dz/dx = 4.0
print(f"dz/dy = {y.grad}")    # dz/dy = 6.0

Linear Regression Example

import numpy as np
from MiniTorch import Variable, square, mul, sub, sum

# Generate synthetic data
np.random.seed(0)
x = np.random.rand(100, 1)
y = 2.0 + 3.0 * x + 0.1 * np.random.randn(100, 1)

# Initialize parameters
W = Variable(np.zeros((1, 1)))
b = Variable(np.zeros(1))

def predict(x):
    return add(mul(x, W), b)

def loss(y_pred, y_true):
    diff = sub(y_pred, y_true)
    return mean_squared_error(diff)

# Training loop
lr = 0.1
for i in range(100):
    y_pred = predict(x)
    l = loss(y_pred, y)
    
    # Reset gradients
    W.grad = None
    b.grad = None
    
    # Backward pass
    l.backward()
    
    # Update parameters
    W.data -= lr * W.grad
    b.data -= lr * b.grad
    
    if i % 10 == 0:
        print(f"Epoch {i}, Loss: {l.data}")

print(f"W = {W.data}, b = {b.data}")

Computational Graph Visualization

from MiniTorch import Variable, square, add, visualize_graph

x = Variable(np.array(2.0), name='x')
y = Variable(np.array(3.0), name='y')
z = add(square(x), square(y), name='z')

# Generate interactive visualization
visualize_graph(z, filename='graph.html')

API Reference

Core Classes

Class Description
Variable Main tensor class with automatic differentiation
Function Base class for all operations

Operations

Basic Math

  • add, sub, mul, div, neg, pow, square

Math Functions

  • exp, sin, cos, tanh

Matrix Operations

  • matmul, reshape, transpose

Reduction Operations

  • sum, sum_to, broadcast_to

Loss Functions

  • mean_squared_error

Utilities

Function Description
numerical_diff Compute numerical gradient for verification
as_array Convert input to NumPy array
visualize_graph Generate interactive computational graph
no_grad Context manager to disable gradient computation
with_grad Context manager to enable gradient computation

Project Structure

MiniTorch/
├── core/              # Core autograd engine
│   ├── variable.py    # Variable class definition
│   ├── function.py    # Function base class
│   └── config.py      # Configuration and context managers
├── ops/               # Operation implementations
│   ├── add.py, mul.py, exp.py, etc.
└── utils/             # Utility functions
    ├── numer_diff.py  # Numerical differentiation
    └── visualize.py   # Graph visualization

Development

Setup Development Environment

uv venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
uv pip install -e .[dev]

Running Tests

python -m unittest tests/test.py

Building the Package

uv build

License

This project is provided for educational purposes.

Acknowledgments

MiniTorch is inspired by PyTorch and designed as a teaching tool to understand automatic differentiation and deep learning frameworks from first principles.

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

minitorchbr-0.1.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

minitorchbr-0.1.0-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file minitorchbr-0.1.0.tar.gz.

File metadata

  • Download URL: minitorchbr-0.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for minitorchbr-0.1.0.tar.gz
Algorithm Hash digest
SHA256 86cafb22a859492b2ae1d6243c9afb0d388897eb37250d8e95a9d58e75237756
MD5 51413c7b643ced2cd2a755e4001a6462
BLAKE2b-256 d080f8cda12c1b6a9f292ab02ed3f55533afca39afa5297b7075a9037cb33299

See more details on using hashes here.

Provenance

The following attestation bundles were made for minitorchbr-0.1.0.tar.gz:

Publisher: workflow.yml on BriceLucifer/MiniTorch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file minitorchbr-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: minitorchbr-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for minitorchbr-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83aa6c4a2ac5c9042329d7e25fa36703692c9e3229c3f6ed48bc1c2ad4088f20
MD5 337ae7667735c5025710fe44dd84ed1c
BLAKE2b-256 eca0462598007886b35eec2c8dc1f297cf394ec0f820310bbcd71c9ea84bd40d

See more details on using hashes here.

Provenance

The following attestation bundles were made for minitorchbr-0.1.0-py3-none-any.whl:

Publisher: workflow.yml on BriceLucifer/MiniTorch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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