Skip to main content

A tiny, elegant automatic differentiation engine built from scratch.

Project description

🚀 GradLite

A tiny, elegant automatic differentiation engine built from scratch.

GradLite is a lightweight module for automatic gradient computation, the core mechanism behind training neural networks using backpropagation.

Inspired by:

...but extended to support more features than MicroGrad, without all the complexity of PyTorch. This module supports building complete neural networks and has a clear educational purpose.


✨ Features

  • 🧠 Reverse-mode automatic differentiation (backpropagation)
  • 🔢 Scalar-based computational graph
  • 🔄 Dynamic graph construction
  • 🏗️ Build full neural networks (Linear layer support for now! 🗓️)
  • 🪶 Lightweight and easy to read
  • 📚 Fully Educational

📦 Installation

You can run the following command to install gradlite in your system/virtual environment:

pip install gradlite

🧩 Example Usage

The usage of gradlite to build expressions and run backpropagation (automatic differentation) is shown next:

from gradlite.core import Parameter

a = Parameter(2.0)
b = Parameter(0.3)
c = Parameter(1.0)

ab = a * b
f = ab + c
print(f'f={f.value:.4f}')
f.backward()
print(f'grad_f={f.grad:.4f}')
print(f'grad_a={a.grad:.4f}')
print(f'grad_b={b.grad:.4f}')
print(f'grad_c={c.grad:.4f}')

An example training loop of gradlite is given next:

from gradlite.nn.linear import Linear
from gradlite.nn.activations import ReLU
from gradlite.nn.loss.mse import MSE
from gradlite.optimizer.sgd import SGD

X = [2.00, 2.10, 2.20, 2.30, 2.40]
y = [3.00, 3.10, 3.13, 3.16, 3.19]

epochs = 2000
model = Linear(input_features=1, output_features=1, activation_fn=ReLU())
model.neurons[0].weights[0].value = 0.5  # Using positive weight to prevent Dying ReLU
optimizer = SGD(model.parameters(), lr=1e-2)
loss_fn = MSE()

for _ in range(epochs):
    for x_value, y_true in zip(X, y):
        optimizer.zero_grad()
        y_pred = model([x_value])
        loss = loss_fn(y_pred, [y_true])
        loss.backward()
        optimizer.step()
        print(f'Loss: {loss.value:.4f}')

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

gradlite-0.2.1.tar.gz (117.7 kB view details)

Uploaded Source

Built Distribution

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

gradlite-0.2.1-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file gradlite-0.2.1.tar.gz.

File metadata

  • Download URL: gradlite-0.2.1.tar.gz
  • Upload date:
  • Size: 117.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for gradlite-0.2.1.tar.gz
Algorithm Hash digest
SHA256 3f6f7019ce61f78f3a812140fa1745785eef199e539290a27f15d1358d52912b
MD5 25ec19b18a51ee079496cb9b0c100201
BLAKE2b-256 1a0c02dcafbb3a3bb0873832829a8d7cc322bfdbfa583c2e7585928fc909ce8e

See more details on using hashes here.

File details

Details for the file gradlite-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: gradlite-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for gradlite-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3368f81288af2ade12437ef63b59a3f250a4d5b59a8410af4948614f22bd60b0
MD5 567b080cd26fce234a9e2c59f7d38a49
BLAKE2b-256 14389852641c18091a3187fb8d7e8c5be9c097bfbe013961d0c5580c3020278c

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