Skip to main content

pygrad: A lightweight differentiation engine written in Python.

Documentation: https://baubels.github.io/pygrad/.

This is a lightweight (<300kB) automatic differentiation engine based on NumPy, Numba, and opt_einsum. Included is a differentiable Tensor class, layers such as Dropout/Linear/Attention, loss functions such as BCE/CCE, optimizers such as SGD/RMSProp/Adam, and an example DNN/CNN/Transformer architecture. This library is a good alternative if you want to do backpropagation on simple and small functions or networks, without much overhead.

The main component is the Tensor class supporting many math operations. Tensors have .value and .grad attributes, gradients being populated by calling .backward() on either self or any of its children. They can be used standalone, or for constructing more complex architectures such as a vanilla Transformer.

Installation

pip install pygradproject
# OR
git clone https://github.com/baubels/pygrad.git
pip install . (or .[examples] or .[dev])

Usage

Tensors accept the same input value as a NumPy array. Create them with Tensor(value) or tensor.array(value). Run backprop on them with .backward().

A simple usage example:

from pygrad.tensor import Tensor
x = Tensor(1)
(((x**3 + x**2 + x + 1) - 1)**2).backward()
x.value, x.grad  # 1.0, 36.0

Since Tensor store their value in .value and their gradient in .grad, it's easy to perform gradient descent.

for _ in range(100):
    (((x**3 + x**2 + x + 1) - 1)**2).backward()     # gradients are automatically reset when called
    x.value = x.value - 0.01*x.grad

Tensors can also be operated on with broadcast-friendly NumPy arrays or other Tensors whose value is broadcast friendly. Internally, a Tensor will always cast it's set value to a NumPy array.

import numpy as np
x  = Tensor(np.ones((10,20)))
y  = Tensor(np.ones((20,10)))
z1 = x@y
z2 = x@np.ones((20,10))       
np.all(z1.value == z2.value)  # True

There are enough expressions defined to be able to create many different models. For example usage and in-depth descriptions of each component of pygrad, check out the docs.

Citation/Contribution

If you find this project helpful in your research or work, I kindly ask that you cite it: View Citation. Thank you!

If there are issues with the project, please submit an issue. Otherwise, please read the current status for contributors.

Metadata

Release files for pygradproject 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygradproject 0.0.3
File Size Uploaded
pygradproject-0.0.3.tar.gz 30.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pygradproject 0.0.3
File Interpreter ABI Platform
pygradproject-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 55.3 kB

Release files / pygradproject-0.0.3.tar.gz

Download URL pygradproject-0.0.3.tar.gz
Size 30.0 kB
Tags Source
SHA-256 checksum
How to use checksums
29b0d54d9b22edc252508ddc90fd38ca0fa7823ef9d0cfb8cf801cd5e4a1a5e2
BLAKE2b-256 checksum
How to use checksums
d17ab35dd7261906174ab31f45e5cd8092f8f0a97f5d658fb47c45fa77b64b58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.5

Release files / pygradproject-0.0.3-py3-none-any.whl

Download URL pygradproject-0.0.3-py3-none-any.whl
Size 25.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f680978359c199f039c187abb421ba8623bb464c031ece773163a687f4d6b393
BLAKE2b-256 checksum
How to use checksums
7bf0ed0acb98d0fed08cbf745b850d5b5799a44d8e19c16acdc2dedf3172166e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page