Skip to main content

A tiny scalar-valued autograd engine with a small PyTorch-like neural network library on top.

Project description

tonygrad

yeahhhhh

A tiny scalar-valued autograd engine with a small PyTorch-like neural network library on top. Implements backpropagation (reverse-mode autodiff) over a dynamically built DAG. The DAG only operates over scalar values, so e.g. we chop up each neuron into all of its individual tiny adds and multiplies. However, this is enough to build up entire deep neural nets doing binary classification, as the demo notebook shows.

Installation

pip install tonygrad

Example usage

Below there is a simple example showing a number of possible supported operations:

"""tanh() VERSION"""

# inputs x1,x2
x1 = Value(2.0, label='x1')
x2 = Value(0.0, label='x2')
# weights w1,w2
w1 = Value(-3.0, label='w1')
w2 = Value(1.0, label='w2')
# bias of the neuron
b = Value(6.8813735870195432, label='b')
# x1*w1 + x2*w2 + b
x1w1 = x1*w1; x1w1.label = 'x1w1'
x2w2 = x2*w2; x2w2.label = 'x2w2'
x1w1x2w2 = x1w1 + x2w2; x1w1x2w2.label = 'x1*w1 + x2*w2'
#output
n = x1w1x2w2 + b; n.label = 'n'
#apply tanh to the output 
o = n.tanh(); o.label = 'o'
#launch backprop on the built graph
o.backward()

Tracing / visualization

For added convenience, the notebook trace_graph.py produces graphviz visualizations. Here we draw the neural network graph built in the example above.

from tonygrad.trace_graph import draw_dot, trace

draw_dot(o)

2d neural net

Training a neural net

The notebook demo.ipynb provides a full demo of training an 2-layer neural network (MLP) binary classifier. This is achieved by initializing a neural net from tonygrad.nn module, implementing a simple svm "max-margin" binary classification loss and using stochastic gradient descent for optimization. As shown in the notebook, using a 2-layer neural net with two 16-node hidden layers we achieve the following decision boundary on the moon dataset:

2d neural net

License

MIT

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

tonygrad-1.1.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

tonygrad-1.1.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file tonygrad-1.1.2.tar.gz.

File metadata

  • Download URL: tonygrad-1.1.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for tonygrad-1.1.2.tar.gz
Algorithm Hash digest
SHA256 ce04a883f69bfd7c72e3ef560d4df306b8bd534339952a56397bda27db63d22e
MD5 be643e1482d652612c67ae837b253dab
BLAKE2b-256 821bb97403db59a5dba024412649672086a218dcabef0f9ae3fe6deb8e3602b3

See more details on using hashes here.

File details

Details for the file tonygrad-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: tonygrad-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for tonygrad-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b8297c377632c69b1067397796dcb878d6c1b5acecbf4437b5c1ca12a35dba17
MD5 400129dc8d0f05783de62a67e956dbdf
BLAKE2b-256 e3c1dc497b2af566adfad7af114afabc5d0c8624ae6f41298935ec9b22244321

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