Skip to main content

Everything related to data science.

Project description

SnoScience

A package optimised for building small neural networks.

The MNIST dataset has been used for training. Each network is trained by using 64 samples from this dataset per epoch for 200 epochs. As can be seen from the graph, use Tensorflow or equivalent for networks with more than 3 layers (n>3) and/or more than 20 neurons per layer. SnScience networks with one (n=2) or no hidden layers (n=1) are extremely fast.

Installation

To install the SnoScience package either:

  • run the command 'pip install snoscience' in the command line.
  • install the package via your IDE.

Features

SnoScience.networks module

  1. The user is able to create a neural network capable of being trained and making predictions:
    1. this network is able to process an arbitrary number of inputs.
    2. this network can consist of an arbitrary number of layers.
    3. each layer in this network can consist of an arbitrary number of neurons.
  2. The neural network supports the following activation functions:
    1. sigmoid.
  3. The neural network supports the following loss functions:
    1. mean squared error (MSE).
  4. The neural network supports the following optimisers:
    1. stochastic gradient descent (SGD).
  5. The user can train the neural network with a single command:
    1. all inputs are checked for validity before the training proceeds.
  6. The user can let the network make predictions with a single command:
    1. all inputs are checked for validity before predictions are given.
    2. these predictions can be regressions of an arbitrary size.
    3. these predictions can be classifications of an arbitrary size.

SnoScience.metrics module

  1. The user is able to calculate the mean squared error of a regression.
  2. The user is able to calculate the accuracy of a classification.

Usage

from snoscience.networks import NeuralNetwork
from snoscience.metrics import calculate_mse, calculate_accuracy

# Create model.
model = NeuralNetwork(inputs=9, loss="MSE", optimiser="SGD")
model.add_layer(neurons=2, activation="sigmoid")
model.add_layer(neurons=1, activation="sigmoid")

# Define optimizer hyperparameters.
rate = 0.01

# Train model.
model.train(x=x_train, y=y_train, epochs=1000, samples=32, rate=rate, log=False)

# Make predictions (classification).
y_calc = model.predict(x=x_test, classify=True)

# Make predictions (regression).
y_calc = model.predict(x=x_test, classify=False)

# Calculate performance.
mse = calculate_mse(calc=y_calc, true=y_test)
accuracy = calculate_accuracy(calc=y_calc, true=y_test)

Calculations

Total derivative for neuron

dL / dy_1 = (dy_2_1 / dy_1) * (dy_3_1 / dy_2) ... (dL / dy_M_1) +  
            (dy_2_1 / dy_1) * (dy_3_2 / dy_2) ... (dL / dy_M_1) +  
            (dy_2_1 / dy_1) * (dy_3_P / dy_2) ... (dL / dy_M_1) +  
            ...  
            (dy_2_1 / dy_1) * (dy_3_1 / dy_2) ... (dL / dy_M_1) +  
            (dy_2_1 / dy_1) * (dy_3_1 / dy_2) ... (dL / dy_M_2) +  
            (dy_2_1 / dy_1) * (dy_3_1 / dy_2) ... (dL / dy_M_N) +  
            ...  
            (dy_2_Q / dy_1) * (dy_3_1 / dy_2) ... (dL / dy_M_1) +  
            (dy_2_Q / dy_1) * (dy_3_2 / dy_2) ... (dL / dy_M_1) +  
            (dy_2_Q / dy_1) * (dy_3_P / dy_2) ... (dL / dy_M_1) +  
            ...  
            (dy_2_Q / dy_1) * (dy_3_1 / dy_2) ... (dL / dy_M_1) +  
            (dy_2_Q / dy_1) * (dy_3_1 / dy_2) ... (dL / dy_M_2) +  
            (dy_2_Q / dy_1) * (dy_3_1 / dy_2) ... (dL / dy_M_N) +  
            ...  
            (dy_2_Q / dy_1) * (dy_3_P / dy_2) ... (dL / dy_M_N)  

L = loss
y_X = neuron in layer X.

M = last layer.
N = last neuron in last layer.
P = last neuron in third layer.
Q = last neuron in second layer.

Applicable chain rule:

dy_A / dy_B = y_dot_A * w_A[position_B]

A = current neuron
B = neuron from previous layer

Chain rule needs to be applied everywhere except for the starting neuron.

Total derivative for neuron weights and bias

dL / db = sum(dL / dy_A)
dL / dw = sum((dL / dy_A) * y_B)

A = current neuron
B = previous layer

Example
-------
y_B = [[1, 2, 3],
       [4, 5, 6]]

dL / dy_A = [[1],
             [2]]

dL / db = 3
dL / dw = [[9],
           [12],
           [15]]

Stochastic gradient descent

b_new = b_old - (learn * dL / db)
w_new = w_old - (learn * dL / dw)

Example
-------
rate = 0.1

b_old = 1
w_old = [[1],
         [1],
         [1]]

dL / db = 3
dL / dw = [[9],
           [12],
           [15]]
           
b_new = 0.7
w_new = [[0.1],
         [-0.2],
         [-0.5]]

Changelog

v1.0.0

  • SnoScience.networks features 1 through 6 added.
  • SnoScience.metrics features 1 and 2 added.

v0.1.0

  • Initial release.

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

SnoScience-1.0.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

SnoScience-1.0.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file SnoScience-1.0.0.tar.gz.

File metadata

  • Download URL: SnoScience-1.0.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for SnoScience-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7c7eab210e7c8eae37fa632092907424370dd9720e0e5785380cfc38a4b12f41
MD5 d393468a0e45a86c61772bc33112b2d2
BLAKE2b-256 c39973355cedf8ab01d74e299ecde3d429bdc74a75512218cec1ef5a3eb92337

See more details on using hashes here.

File details

Details for the file SnoScience-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: SnoScience-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for SnoScience-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b8a64e7045bd13861d735ec489d055bfa9de191af63eda700adc3f49688050ec
MD5 8384dc328704ac727b25f55865ac4582
BLAKE2b-256 239ad63a0c39fc74029289faecb4f56dcf34bca90eddc80ac7983340788b9193

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