Skip to main content

A tiny, pedagogical neural network library with a pytorch-like API.

Project description

candl

A tiny, pedagogical implementation of a neural network library with a pytorch-like API. The primary use of this library is for education. Use the actual pytorch for more serious deep learning business.

The implementation is complete with tensor-valued autodiff (~100 lines) and a neural network API built off of it (~80 lines).

Learning

This little project is actually the result of an article I wrote. Using it, you can learn more about how neural networks work and implement everything in candl yourself from scratch.

Installation

pip install candl

Usage

First, import candl.

import candl

Candl comes with two modules: nn and Tensor. The nn module contains tools like modules, layers, SGD, MSE, etc. Candl tensors are extensions of numpy ndarrays that can be used to represent data and compute derivatives.

To train a neural net (let's try to learn XOR), first we can create a model.

nn = candl.nn

model = nn.Sequential(nn.Linear(2, 64), 
                      nn.ReLU(), 
                      nn.Linear(64, 32), 
                      nn.ReLU(), 
                      nn.Linear(32, 1))
lr = 1e-3

loss_fn = nn.MSE()
optimizer = nn.SGD(model.parameters(), lr)

Then, we train:

data = [([0, 0], [0]), ([0, 1], [1]), ([1, 0], [1]), ([1, 1], [0])]

for epoch in range(100):
    for sample in data:
        """ 
        Note that we only allow batches of data, so the shape of the tensor must be n x m,
        where m is the dimensionality of the input for each batch.
        """
        x = candl.tensor([sample[0]]) 
        y = candl.tensor([sample[1]])
        loss = loss_fn(model.forward(x), y)
        loss.backward()
        # The `True` argument automatically zeroes the gradients after a step
        optimizer.step(True) 

Features

  • Tensors built upon numpy's ndarrays
  • Tensor-valued autograd
  • Mean Squared Error Loss Function
  • Stochastic Gradient Descent (SGD)
  • Blocks (Modules) for putting together neural networks
  • Built-in layers: Linear, ReLU, Sigmoid, Tanh

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

candl-0.0.7.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

candl-0.0.7-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file candl-0.0.7.tar.gz.

File metadata

  • Download URL: candl-0.0.7.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.8.10

File hashes

Hashes for candl-0.0.7.tar.gz
Algorithm Hash digest
SHA256 d51b3cdb517664e8471471ddf6972dc494e3a4f5a04df6e7f5d9edbf74e408f1
MD5 eed04c4dd3e3de6e2c2ce15b752a4f3a
BLAKE2b-256 9e9063e22a6a75e1a13fd2bfc6dfe6063d4d8d3ea9bf5337db27c5ef6dc406bf

See more details on using hashes here.

File details

Details for the file candl-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: candl-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.8.10

File hashes

Hashes for candl-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b83031f1d7caec9f5ec5b67a2ac4eb0582172ebe79d90e1e7c0484714ecc71d9
MD5 578bd646459280ab50966484350d1968
BLAKE2b-256 939c2a75ca0c86e6d128d120b2e5bd7bf98ac77c4ddefbbfea623de2138d255c

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