Skip to main content

Feedforward neural network library from scratch

1. Introduction and motivation

In order to fully understand how artificial neural networks are trained, I wanted to write a constructor for feedforward neural networks, which creates a neural net only by having specified a list of the desired hidden layers and some custom parameters, like learning rate and batch size for gradient descent.

I wrote the constructor from scratch, using only built-in python functions and NumPy. The training was optimized to make great use of vectorization and especially the highly optimized NumPy matrix multiplication. The constructor class stores a list of layers, which are their own class and store a weight matrix and various vectors for derivatives, intercepts and values each.

For classification, the network uses the softmax function before the output layer and optimizes using the cross-entropy loss function.

The constructor is demoed on the MNIST dataset, where it achieved 89.2% accuracy after a few minutes of training. The Jupyter Notebook file can be found in the main branch.

2. How to access the constructor

2.1 Create an instance of the class and pass the necessary arguments

To access the constructor, one has to define a variable, which is then an instance of the class 'ffnn':

model = ffnn([100,70]) # call the constructor and set the parameters. In this case two hidden layers with 100 and 70 nodes each.

The arguments in brackets are:

  1. List of the hidden layers (for example [20,20] for two hidden layers with 20 neurons each)
  2. Learning rate (step size for the adjustments in parameter learning. default is 0.04)
  3. Batch size (how much of the dataset is used for mini-batch gradient descent)
  4. Target Accuracy (constructor will stop training once this accuracy and sufficient convergence is achieved) Below are all the possible arguments:
model = ffnn(h_layers = [100,70], learning_rate = 0.05, batch_size = 0.1, targetAccuracy = 0.92)

2.2 Construct the network

The model is now stored in a variable. In our case, the variable is called 'model'. We can now call the construct method on our model, which builds the specified layers and nodes. We need to specify both the x and y part of the dataset, so that the model knows how large the input and output layer need to be.

model.construct(x,y) # construct the network with the dataset (needs it for shape of input and output layer)

2.3 Train the model

Now we can call the train method on our model to learn the parameters. The necessary arguments for this method are the whole dataset (x and y). Make sure that the dimensions of the y vector is n * 1 (if the numpy array has shape (n,) instead of (n,1) you need to reshape it).

model.train(x,y) # train the network with training data

2.4 predict on the test dataset

We now possess a trained model and can use it to make predictions on the test set with the 'predict' method. This will return an array with the predictions as well as the accuracy of the model on the training data.

model.predict(x,y) # predict on the test data with the trained model

Metadata

Release files for neuronetlib 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for neuronetlib 0.0.1
File Size Uploaded
neuronetlib-0.0.1.tar.gz 276.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for neuronetlib 0.0.1
File Interpreter ABI Platform
neuronetlib-0.0.1-py3-none-any.whl Python 3 none any Details
neuronetlib-0.0.1-cp310-cp310-manylinux1_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.5+ x86-64 Details

Total release size: 1.7 MB

Release files / neuronetlib-0.0.1.tar.gz

Download URL neuronetlib-0.0.1.tar.gz
Size 276.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2267782dac2bb48a60032e08122b7101d4533db322954c7d50282ca765cabc9a
BLAKE2b-256 checksum
How to use checksums
b01a5469fd2e55b19b009f2c74c3f4cab6a0a6e7b31b6185c1576e78314b4026
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.7

Release files / neuronetlib-0.0.1-py3-none-any.whl

Download URL neuronetlib-0.0.1-py3-none-any.whl
Size 2.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e452ee06d898fa76a9e319431031038d776b47d287e3b0e907a539ea910d0450
BLAKE2b-256 checksum
How to use checksums
78320a76b50efd79cc214f405aaffe5b5ab320440cf8b1ca11bf5d462056b885
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.7

Release files / neuronetlib-0.0.1-cp310-cp310-manylinux1_x86_64.whl

Download URL neuronetlib-0.0.1-cp310-cp310-manylinux1_x86_64.whl
Size 1.4 MB
Tags CPython 3.10 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
552009803fffe1a580349317b4f9e35d13e048e5a9b4843b60115f75a50194db
BLAKE2b-256 checksum
How to use checksums
52969925e92f836564368ab7717037a158569fbc9c1223a68f590cb5b2a97718
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.7

Release history Release notifications | RSS feed

This release

0.0.1 This release

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page