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@git+https://github.com/RaulBarbaRojas/gradlite.git

🧩 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.0.tar.gz (101.6 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.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gradlite-0.2.0.tar.gz
  • Upload date:
  • Size: 101.6 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.0.tar.gz
Algorithm Hash digest
SHA256 a67c04cafce2f3f2b00d283d36f85cd20c278b94aba8f7defe85f9d21479a89a
MD5 327c24c497bad2c44a573023df3cc0d0
BLAKE2b-256 df6afefae1387d4a7f28a15606e094ceff3086431b832aa9780c28d66053e444

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gradlite-0.2.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 93959c0c3ccaf2c3d17a2b1706775df2d382c3d39964df50f047bb5f0b4311a1
MD5 f57384cb88dcf66faa034233ac5ce0da
BLAKE2b-256 12d0fba7cc907db756314f271c65af0c6eb43b76976370ebebd6c05ec98c2864

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