Skip to main content

Lightweight Automatic Differentiation in Python.

Project description

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

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.

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

pygradproject-0.0.1.tar.gz (22.8 kB view details)

Uploaded Source

Built Distribution

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

pygradproject-0.0.1-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

Details for the file pygradproject-0.0.1.tar.gz.

File metadata

  • Download URL: pygradproject-0.0.1.tar.gz
  • Upload date:
  • Size: 22.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.5

File hashes

Hashes for pygradproject-0.0.1.tar.gz
Algorithm Hash digest
SHA256 82e9baee73a7f7784771615e06b1efa301259eab343c0a2f85f6845419b4c1c7
MD5 47fc5e3bb1ac26ead86058e83d971457
BLAKE2b-256 fab637bfe4e1f16b88c9cb31a053b7d3ee78d041667829d1e40ac3eb65b090cd

See more details on using hashes here.

File details

Details for the file pygradproject-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pygradproject-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 25.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.5

File hashes

Hashes for pygradproject-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ae5c7e9ab983ab2f70ee6359521ee32ff6dd797fb5f8180d0dcba3d7bebf384a
MD5 13138119354e563b537b832ff0c277e4
BLAKE2b-256 4be6d2cae17d43b3adf9a7e4068431bc547b54a0649fc9a0ec0277590c51f8d4

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