Skip to main content

An auto-differentiation library for scalars

Project description

Autograd

Autograd is an auto-differentiation library for scalars, designed to facilitate the implementation of machine learning models. It provides a simple interface for defining and training neural networks using automatic differentiation.

Features

  • Automatic Differentiation: Compute gradients automatically for scalar operations.
  • Neural Network Components: Build neural networks using layers and neurons.
  • Optimizers: Implement optimization algorithms (Currently only Stochastic Gradient Descent)
  • Loss Functions: Support for common loss functions (Currently only Hinge Loss)

Installation

To install the library, clone the repository:

pip install py-autograd

The only requirement is numpy

Usage

Importing the Library

You can import the necessary components from the library as follows:

from autograd.scalar import Number, Layer, Network
from autograd.scalar.activations import ReLU
from autograd.scalar.losses import HingeLoss
from autograd.scalar.optimizers import Optimizer

Using the Number class

The Number class wraps around python's integers and floats. It has all the building blocks including addition, multiplication, exponentiation, power, etc. During each calculation, the Number class stores the function to get the local gradient at the point.

a = Number(3.0)
b = 10

c = a**10
d = c.exp()
e = d / a
f = 2*e

The number class also includes activation functions:

from autograd.scalar.activations import ReLU
a = Number(-5.0)
b = a.activation(ReLU)

Finally, running the .backward() function on any Number object will calculate the gradients of all the variables before it. These gradients can be accessed by the .grad attribute

f.backward()
a.grad

Defining a Neural Network

You can also define a multi-layered perceptron (MLP) using the Network and Layer classes:

model = Network([
    Layer(2, 16, ReLU()),
    Layer(16, 16, ReLU()),
    Layer(16, 1)
])

Training the Model

To train the model, create an instance of the Optimizer class and call the train method. Currently, the library uses Stochastic Gradient Descent to train the model

optimizer = Optimizer(model, HingeLoss)
optimizer.train(X, y, epochs=100, batch_size=32)

Example

An example of using the library can be found in the demo.ipynb Jupyter notebook. This notebook demonstrates how to create a dataset, define a model, train it, and visualize the results.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue for any suggestions or improvements.

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

pyautograd-1.1.0.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

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

pyautograd-1.1.0-py3-none-any.whl (2.5 kB view details)

Uploaded Python 3

File details

Details for the file pyautograd-1.1.0.tar.gz.

File metadata

  • Download URL: pyautograd-1.1.0.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for pyautograd-1.1.0.tar.gz
Algorithm Hash digest
SHA256 488950974f68f71dfb4764c51d8ee5fd98196feb2ae3ef25be95a94cbf7bc0a7
MD5 18f5c4cde6353bf59c00f64e3a495e99
BLAKE2b-256 9752cc2f8ebbc604f918e128cb8d9ce04066a136c511cfb9f95aa7f0df84f0aa

See more details on using hashes here.

File details

Details for the file pyautograd-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyautograd-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 2.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for pyautograd-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 644424153d63780f779cfad78dc9638fbe483b083989762cfa1b86c9d5c2a96e
MD5 9c5dd85a19d0a55d1b4ef7669e6bf573
BLAKE2b-256 251ef790308e2fe3e4ecf15483541bb8f8c68f2ae7a1ccbabe2cc53da227e549

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