A plug-and-play library for neural networks written in Python

Overview

Synapses

A plug-and-play library for neural networks written in Python!

# run
pip install synapses-py==7.4.1
# in the directory of your project

Neural Network

Create a neural network

Import Synapses, call NeuralNetwork.init and provide the size of each layer.

from synapses_py import NeuralNetwork, ActivationFunction, DataPreprocessor, Statistics
layers = [4, 6, 5, 3]
neuralNetwork = NeuralNetwork.init(layers)

neuralNetwork has 4 layers. The first layer has 4 input nodes and the last layer has 3 output nodes. There are 2 hidden layers with 6 and 5 neurons respectively.

Get a prediction

inputValues = [1.0, 0.5625, 0.511111, 0.47619]
prediction = \
        NeuralNetwork.prediction(neuralNetwork, inputValues)

prediction should be something like [ 0.8296, 0.6996, 0.4541 ].

Note that the lengths of inputValues and prediction equal to the sizes of input and output layers respectively.

Fit network

learningRate = 0.5
expectedOutput = [0.0, 1.0, 0.0]
fitNetwork = \
        NeuralNetwork.fit(
            neuralNetwork,
            learningRate,
            inputValues,
            expectedOutput
        )

fitNetwork is a new neural network trained with a single observation.

To train a neural network, you should fit with multiple datapoints

Create a customized neural network

The activation function of the neurons created with NeuralNetwork.init, is a sigmoid one. If you want to customize the activation functions and the weight distribution, call NeuralNetwork.customizedInit.

def activationF(layerIndex):
    if layerIndex == 0:
        return ActivationFunction.sigmoid
    elif layerIndex == 1:
        return ActivationFunction.identity
    elif layerIndex == 2:
        return ActivationFunction.leakyReLU
    else:
        return ActivationFunction.tanh

def weightInitF(_layerIndex):
    return 1.0 - 2.0 * random()

customizedNetwork = \
        NeuralNetwork.customizedInit(
            layers,
            activationF,
            weightInitF
        )

Visualization

Call NeuralNetwork.toSvg to take a brief look at its svg drawing.

Network Drawing

The color of each neuron depends on its activation function while the transparency of the synapses depends on their weight.

svg = NeuralNetwork.toSvg(customizedNetwork)

Save and load a neural network

JSON instances are compatible across platforms! We can generate, train and save a neural network in Python and then load and make predictions in Javascript!

toJson

Call NeuralNetwork.toJson on a neural network and get a string representation of it. Use it as you like. Save json in the file system or insert into a database table.

json = NeuralNetwork.toJson(customizedNetwork)

ofJson

loadedNetwork = NeuralNetwork.ofJson(json)

As the name suggests, NeuralNetwork.ofJson turns a json string into a neural network.

Encoding and decoding

One hot encoding is a process that turns discrete attributes into a list of 0.0 and 1.0. Minmax normalization scales continuous attributes into values between 0.0 and 1.0. You can use DataPreprocessor for datapoint encoding and decoding.

The first parameter of DataPreprocessor.init is a list of tuples (attributeName, discreteOrNot).

setosaDatapoint = {
    "petal_length": "1.5",
    "petal_width": "0.1",
    "sepal_length": "4.9",
    "sepal_width": "3.1",
    "species": "setosa"
}

versicolorDatapoint = {
    "petal_length": "3.8",
    "petal_width": "1.1",
    "sepal_length": "5.5",
    "sepal_width": "2.4",
    "species": "versicolor"
}

virginicaDatapoint = {
    "petal_length": "6.0",
    "petal_width": "2.2",
    "sepal_length": "5.0",
    "sepal_width": "1.5",
    "species": "virginica"
}

datasetList = [ setosaDatapoint,
                versicolorDatapoint,
                virginicaDatapoint ]

dataPreprocessor = \
        DataPreprocessor.init(
             [ ("petal_length", False),
               ("petal_width", False),
               ("sepal_length", False),
               ("sepal_width", False),
               ("species", True) ],
             iter(datasetList)
        )

encodedDatapoints = map(lambda x:
        DataPreprocessor.encodedDatapoint(dataPreprocessor, x),
        datasetList
)

encodedDatapoints equals to:

[ [ 0.0     , 0.0     , 0.0     , 1.0     , 0.0, 0.0, 1.0 ],
  [ 0.511111, 0.476190, 1.0     , 0.562500, 0.0, 1.0, 0.0 ],
  [ 1.0     , 1.0     , 0.166667, 0.0     , 1.0, 0.0, 0.0 ] ]

Save and load the preprocessor by calling DataPreprocessor.toJson and DataPreprocessor.ofJson.

Evaluation

To evaluate a neural network, you can call Statistics.rootMeanSquareError and provide the expected and predicted values.

expectedWithOutputValuesList = \
        [ ( [ 0.0, 0.0, 1.0], [ 0.0, 0.0, 1.0] ),
          ( [ 0.0, 0.0, 1.0], [ 0.0, 1.0, 1.0] ) ]

expectedWithOutputValuesIter = \
        iter(expectedWithOutputValuesList)

rmse = Statistics.rootMeanSquareError(
                        expectedWithOutputValuesIter
)
Owner
Dimos Michailidis
Dimos Michailidis
Python Interview Questions

Python Interview Questions Clone the code to your computer. You need to understand the code in main.py and modify the content in if __name__ =='__main

ClassmateLin 575 Dec 28, 2022
Colab notebook for openai/glide-text2im.

GLIDE text2im on Colab This repository provides a Colab notebook to produce images conditioned on text prompts with GLIDE [1]. Usage Run text2im.ipynb

Wok 19 Oct 19, 2022
Official Pytorch Implementation of Relational Self-Attention: What's Missing in Attention for Video Understanding

Relational Self-Attention: What's Missing in Attention for Video Understanding This repository is the official implementation of "Relational Self-Atte

mandos 43 Dec 07, 2022
Canonical Appearance Transformations

CAT-Net: Learning Canonical Appearance Transformations Code to accompany our paper "How to Train a CAT: Learning Canonical Appearance Transformations

STARS Laboratory 54 Dec 24, 2022
Perturbed Self-Distillation: Weakly Supervised Large-Scale Point Cloud Semantic Segmentation (ICCV2021)

Perturbed Self-Distillation: Weakly Supervised Large-Scale Point Cloud Semantic Segmentation (ICCV2021) This is the implementation of PSD (ICCV 2021),

12 Dec 12, 2022
This is the source code for generating the ASL-Skeleton3D and ASL-Phono datasets. Check out the README.md for more details.

ASL-Skeleton3D and ASL-Phono Datasets Generator The ASL-Skeleton3D contains a representation based on mapping into the three-dimensional space the coo

Cleison Amorim 5 Nov 20, 2022
A PyTorch-based R-YOLOv4 implementation which combines YOLOv4 model and loss function from R3Det for arbitrary oriented object detection.

R-YOLOv4 This is a PyTorch-based R-YOLOv4 implementation which combines YOLOv4 model and loss function from R3Det for arbitrary oriented object detect

94 Dec 03, 2022
Miscellaneous and lightweight network tools

Network Tools Collection of miscellaneous and lightweight network tools to simplify daily operations, administration, and troubleshooting of networks.

Nicholas Russo 22 Mar 22, 2022
Semi-supervised learning for object detection

Source code for STAC: A Simple Semi-Supervised Learning Framework for Object Detection STAC is a simple yet effective SSL framework for visual object

Google Research 348 Dec 25, 2022
Medical image analysis framework merging ANTsPy and deep learning

ANTsPyNet A collection of deep learning architectures and applications ported to the python language and tools for basic medical image processing. Bas

Advanced Normalization Tools Ecosystem 118 Dec 24, 2022
The dataset of tweets pulling from Twitters with keyword: Hydroxychloroquine, location: US, Time: 2020

HCQ_Tweet_Dataset: FREE to Download. Keywords: HCQ, hydroxychloroquine, tweet, twitter, COVID-19 This dataset is associated with the paper "Understand

2 Mar 16, 2022
Training RNNs as Fast as CNNs

News SRU++, a new SRU variant, is released. [tech report] [blog] The experimental code and SRU++ implementation are available on the dev branch which

ASAPP Research 2.1k Jan 01, 2023
Official PyTorch Implementation of Rank & Sort Loss [ICCV2021]

Rank & Sort Loss for Object Detection and Instance Segmentation The official implementation of Rank & Sort Loss. Our implementation is based on mmdete

Kemal Oksuz 229 Dec 20, 2022
Lava-DL, but with PyTorch-Lightning flavour

Deep learning project seed Use this seed to start new deep learning / ML projects. Built in setup.py Built in requirements Examples with MNIST Badges

Sami BARCHID 4 Oct 31, 2022
Rendering Point Clouds with Compute Shaders

Compute Shader Based Point Cloud Rendering This repository contains the source code to our techreport: Rendering Point Clouds with Compute Shaders and

Markus Schütz 460 Jan 05, 2023
Ensembling Off-the-shelf Models for GAN Training

Vision-aided GAN video (3m) | website | paper Can the collective knowledge from a large bank of pretrained vision models be leveraged to improve GAN t

345 Dec 28, 2022
Log4j JNDI inj. vuln scanner

Log-4-JAM - Log 4 Just Another Mess Log4j JNDI inj. vuln scanner Requirements pip3 install requests_toolbelt Usage # make sure target list has http/ht

Ashish Kunwar 66 Nov 09, 2022
Riemannian Geometry for Molecular Surface Approximation (RGMolSA)

Riemannian Geometry for Molecular Surface Approximation (RGMolSA) Introduction Ligand-based virtual screening aims to reduce the cost and duration of

11 Nov 15, 2022
Cave Generation using metaballs in Blender. Originally created by sdfgeoff, Edited by Myself (Archie Jaskowicz).

Blender-Cave-Generation Cave Generation using metaballs in Blender. Originally created by sdfgeoff, Edited by Myself (Archie Jaskowicz). Installation

2 Dec 28, 2022
Exposure Time Calculator (ETC) and radial velocity precision estimator for the Near InfraRed Planet Searcher (NIRPS) spectrograph

NIRPS-ETC Exposure Time Calculator (ETC) and radial velocity precision estimator for the Near InfraRed Planet Searcher (NIRPS) spectrograph February 2

Nolan Grieves 2 Sep 15, 2022