Skip to main content

A minimal python library for automatic differentiation, built on top of NumPy.

Project description

ClumsyGrad

PyPI version Docs Tests

A minimal Python library for automatic differentiation, built on top of NumPy. The Tensor class has support for creating and expanding a computation graph dynamically with each operation.

Computation Graph

Features

  • Dynamic Computational Graphs: Graphs are created on the fly.
  • Automatic Differentiation: Compute gradients automatically using the chain rule.
  • Basic Tensor Operations: Supports addition, subtraction, multiplication, matrix multiplication, power, exp, etc.

Installation

You can install the library using pip:

pip install clumsygrad

Basics

Here's a brief overview of how to use the library:

Creating Tensors

from clumsygrad.tensor import Tensor, TensorType

# Create a tensor from a list (defaults to TensorType.INPUT)
a = Tensor([1.0, 2.0, 3.0])
print(a)
# Output: Tensor(id=0, shape=(3,), tensor_type=INPUT, grad_fn=None, requires_grad=False)

# Create a tensor that requires gradients (e.g., a parameter)
b = Tensor([[4.0], [5.0], [6.0]], tensor_type=TensorType.PARAMETER)
print(b)
# Output: Tensor(id=1, shape=(3, 1), tensor_type=PARAMETER, grad_fn=None, requires_grad=True)

Performing Operations

from clumsygrad.tensor import Tensor, TensorType
from clumsygrad.math import exp, sin

x = Tensor([1.0, 2.0, 3.0])
y = exp(x**2 + 3*x + 2)
z = sin(y)

# As implicitly tensors are of type INPUT, the computational graph is not built, signified by
# grad_fn = None.
print(z) # Tensor(id=6, shape=(3,), tensor_type=INPUT, grad_fn=None, requires_grad=False)
print(z.data) # [0.9648606  0.99041617 0.83529955]

x = Tensor([1.0, 2.0, 3.0], tensor_type=TensorType.PARAMETER)
y = exp(x**2 + 3*x + 2)
z = sin(y)

# Now, the tensor is of type PARAMETER, and the computational graph is built.
print(z) # Tensor(id=13, shape=(3,), tensor_type=INTERMEDIATE, grad_fn=sin_backward, requires_grad=True)
print(z.data) # [0.9648606  0.99041617 0.83529955]

Automatic Differentiation (Backpropagation)

Consider the function $~z = e^{sin(x)^2 + cos(y)}$. We can evaluate $\frac{dz}{dx}$ and $\frac{dz}{dy}$ at particular point as:

from clumsygrad.tensor import Tensor, TensorType
from clumsygrad.math import exp, sin, cos, sum

# Set tensor_type to PARAMETER to ensure gradients are tracked
x = Tensor(1.0, tensor_type=TensorType.PARAMETER)
y = Tensor(0.5, tensor_type=TensorType.PARAMETER)
z = exp(sin(x)**2 + cos(y))

# Calculating dz/dx and dz/dy
z.backward()

# Value of dz/dx
print(x.grad) # [4.43963]
# Value of dz/dy
print(y.grad) # [-2.34079]

License

This project is licensed under the MIT License.

Documentation

For more detailed information, tutorials, and API reference, you can check out the official documentation.

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

clumsygrad-0.2.1.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

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

clumsygrad-0.2.1-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for clumsygrad-0.2.1.tar.gz
Algorithm Hash digest
SHA256 076d829603fffdbff6025e5c88cf8b1db023112f0d04a68d1db6c790bd3a82cb
MD5 b0b55214891ea29f42433c20faad4fc9
BLAKE2b-256 10478de54ffd2558cdd63f88235836e3fb2d35bf09937b5756f8844e50f7c38d

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for clumsygrad-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c77a03263547ed00cba9ff9cf1b227b0a6155385f4e4367741418c7af4b7821f
MD5 c9e8ba52570e8b7efd57160776a2a7fd
BLAKE2b-256 e4397fb008996985e94ffdfee0243f4c28c63679617e4daedb36b482e769a343

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