Skip to main content

Deep learning library

Project description

Pynet

Pynet is a Python library containing all of the deep learning building blocks.

Features

Tensor operations

Tensor operations are included in the pynet.functional namespace. Pynet provides a couple of basic tensor operations such as matrix multiplication or tensor addition. New tensor operation can be created by implementing the Function abstract class. Tensor functions that are included in the library are:

Neural network layers and activation functions

Implementation of neural network layers and activation functions are included in the pynet.nn namespace. The namespace contains basic layer and activation function implementations and can be extended by implementing the Module abstract class. List of layers and activation functions included in the Pynet library:

  • Fully connected layer (Linear) [link]
  • Sequential module (container for other modules) [link]
  • Rectified linear unit (ReLU) activation function [link]
  • Sigmoid activation function [link]

Loss functions

Loss function implementations are included in the pynet.loss namespace. Custom loss functions can be created by implementing the Loss abstract class. Loss functions included in the Pynet library are:

Optimization algorithms

Optimization algorithm implementations are included in the pynet.optimizers namespace. New optimization algorithm can be created by implementing the Optimizer abstract class. Optimization algorithms included in the library:

  • Stochastic Gradient Descent (SGD) [link]

Data manipulation

pynet.data namespace provides abstraction over the datasets used for training the neural network. In-memory dataset implementation is already included in the library, however one can create their own dataset implementation by extending the Dataset abstract class.

Neural network training

For the neural network's training/testing procedure, one can either use default implementation of these procedures, which can be found in the DefaultTrainer class or implement their own training/testing logic by extending the Trainer abstract class. One can also omit both these options and implement its own logic from scratch (for example, see the DefaultTrainer train and test function implementations).

When using one of the Trainer's implementations, a Callback class was created to perform additional actions at various stages of training/testing procedure. There are a couple of callbacks implemented and ready to use, such as:

  • Callback providing printing all the measured model's metrics onto the console (PrintCallback)
  • Callback providing optimizer's learning rate scheduling (LrSchedule)

One can also easily create their own callback by extending the Callback abstract class.

Summary

Table below summarizes the feature section. Every row of a table contains name of the namespace, namespace description (what functionality this namespace provides) and a way of extending this functionality (by implementing the abstract class).

Namespace Functionality Abstract class
pynet.functional Tensor operations Function
pynet.nn Neural network layers and activation functions Module
pynet.loss Loss functions Loss
pynet.optimizers Optimization algorithms Optimizer
pynet.data Data manipulation Dataset
pynet.training.trainer Neural network training/testing procedure Trainer
pynet.training.callbacks Callback functions during the training/testing Callback

Installation

Use the package manager pip to install pynet.

pip install pynet-dl

Usage

Here is the simple example how to use the Pynet library. In this example we are going to train a very small neural network on the make_circles dataset from sklearn.dataset package.

Training

First, we need to import all the necessary stuff:

import numpy as np

from sklearn.datasets import make_circles

# Tensor class
from pynet.tensor import Tensor

# Neural network modules
from pynet.nn.sequential import Sequential
from pynet.nn.linear import Linear
from pynet.nn.relu import ReLU
from pynet.nn.sigmoid import Sigmoid

# Datasets
from pynet.data.in_memory import InMemoryDataset

# Loss functions
from pynet.loss.bce import BinaryCrossEntropy

# Optimizers
from pynet.optimizers.sgd import SGD

# Weight initializers
from pynet.initializers.he_normal import HeNormal

# Trainer and training/testing callbacks
from pynet.training.trainer.default import DefaultTrainer
from pynet.training.callbacks.print import PrintCallback

Then, load the dataset and do preprocessing. As the single input to the neural network (i. e. sample xi) is supposed to be of shape [n_features x 1] (i. e. column vector), we need to add one extra dimension to the X array:

X, y = make_circles(n_samples=1000, noise=0.025)
# inputs to neural net must be of shape [n, 1]
X = np.expand_dims(X, axis=2)

Then, we will create the network, dataset, optimizer, appropriate loss function, trainer and optionally also specify a list of callbacks:

epochs = 20

model = Sequential([
    Linear(inputs=2, neurons=16, initializer=HeNormal()),
    ReLU(),
    Linear(inputs=16, neurons=1, initializer=HeNormal()),
    Sigmoid()
])

dataset = InMemoryDataset(X, y)
loss_f = BinaryCrossEntropy()
sgd = SGD(learning_rate=0.01, momentum=0.9)
callbacks = [PrintCallback(), LrSchedule(optimizer=sgd, schedule=lr_schedule)]
trainer = DefaultTrainer()

And then run the training:

trainer.train(
    model=model,
    train_dataset=dataset,
    val_dataset=None,
    loss_f=loss_f,
    optimizer=sgd,
    epochs=epochs,
    callbacks=callbacks
)

Inference

After training the neural network, we can use it for inference.

# Make prediction for the i-th sample in the dataset
i = 0
y_pred = model.forward(Tensor(X[i])).ndarray.item()

Examples

More examples of the Pynet library usage can be found in the notebooks directory.

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

pynet-dl-0.0.10.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

pynet_dl-0.0.10-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

Details for the file pynet-dl-0.0.10.tar.gz.

File metadata

  • Download URL: pynet-dl-0.0.10.tar.gz
  • Upload date:
  • Size: 17.1 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.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for pynet-dl-0.0.10.tar.gz
Algorithm Hash digest
SHA256 c13fdfe7ec82a86e33016d4c7f6a6539af4cd6f50002bbdefaf0f06f848955fc
MD5 37f433dd0f1e4b4a483bed4c65408792
BLAKE2b-256 e3dd68e825c11e57d9913139a8bd372010ad683928b32963c7ef5ab46b69d8b4

See more details on using hashes here.

File details

Details for the file pynet_dl-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: pynet_dl-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 24.5 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.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for pynet_dl-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 49d92438e9b220dba26edc53300e1e0d295879f6641bf842dd709a5b3e39efa8
MD5 1c01be837ce050e4f2cb3eee03d690ec
BLAKE2b-256 59d43dd8c38de97d03d327c7feb3c59e0e3b4c31afc907a599a2e8641277dc1f

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