Skip to main content

Feed Forward Neural Networks using NumPy

This library is a modification of my previous one. Click Here to check my previous library.

Installation

$ [sudo] pip3 install nicenet

Development Installation

$ git clone https://github.com/Subhash3/Neural_Net_Using_NumPy.git

Usage

>>> from nicenet import NeuralNetwork

Creating a Neural Network

inputs = 2
outputs = 1
network = NeuralNetwork(inputs, outputs, cost="mse")

# Add 2 hidden layers with 16 neurons each and activation function 'tanh'
network.add_layer(16, activation_function="tanh") 
network.add_layer(16, activation_function="tanh")

# Finish the neural network by adding the output layer with sigmoid activation function.
network.compile(activation_function="sigmoid")

Building a dataset

The package contains a Dataset class to create a dataset.

>>> from nicenet import Dataset

Make sure you have inputs and target values in seperate files in csv format.

input_file = "inputs.csv"
target_file = "targets.csv"

# Create a dataset object with the same inputs and outputs defined for the network.
dataset_handler = Dataset(inputs, outputs)
dataset_handler.make_dataset(input_file, target_file)
data, size = dataset_handler.get_raw_data()

If you want to manually make a dataset, follow these rules:

  • Dataset must be a list of data samples.
  • A data sample is a tuple containing inputs and target values.
  • Input and target values are column vector of size (inputs x 1) and (outputs x 1) respectively.

For eg, a typical XOR data set looks something like :

>>> XOR_data = [
    (
        np.array([[0], [0]]),
        np.array([[0]])
    ),
    (
        np.array([[0], [1]]),
        np.array([[1]])
    ),
    (
        np.array([[1], [0]]),
        np.array([[1]])
    ),
    (
        np.array([[1], [1]]),
        np.array([[0]])
    )
]
>>> size = 4

Training The network

The library provides a Train function which accepts the dataset, dataset size, and two optional parameters epochs, and logging.

def Train(self, dataset: T_Dataset, size, epochs=100, logging=False, epoch_logging=True, prediction_evaulator=None):
	....
	....

For Eg: If you want to train your network for 1000 epochs.

>>> network.Train(data, size, epochs=1000)

Notice that I didn't change the value of logging as I want the output to be printed for each epoch.

Debugging

Plot a nice epoch vs error graph

>>> network.epoch_vs_error()

Know how well the model performed.

>>> network.evaluate()

To take a look at all the layers' info

>>> network.display()

Sometimes, learning rate might have to be altered for better convergence.

>>> network.set_learning_rate(0.1)

Exporting Model

You can export a trained model to a json file which can be loaded and used for predictions in the future.

filename = "model.json"
network.export_model(filename)

Load Model

To load a model from an exported model (json) file. load_model is a static function, so you must not call this on a NeuralNetwork object!.

filename = "model.json"
network = NeuralNetwork.load_model(filename)

Todo

- [x] Generalize the gradient descent algorithm
    - [x] Generalise the loss function => Write a separate class for it!
- [x] Implement Cross Entropy Loss
- [ ] Data scaling
    - [x] Min Max scaler
    - [ ] Data Standardization
- [x] Change the datasample type to a tuple instead of a list.
- [x] Show Progress bar if epoch_logging is False
- [x] Use a function as a parameter to Train method to compare predictions and actual targets.
- [x] convert all camel-cased vars to snake-case.

- [ ] API docs
    - [x] Add doc strings to all functions.
    - [x] Make the class/function declarations' docs collapsable.
    - [ ] Merge API md files and embed them in Readme.
    - [ ] Create a section, API, in README to provide documentation for all prototypes.

- [ ] Implement Batch Training
- [ ] Write a separate class for Scalers as the scaling methods increase.
- [ ] Linear and Relu activation functions
- [ ] Ability to perform regression
- [ ] Separate out outputlayer from other layers. => Create a separate class for output layer which inherits Layer.


- [ ] Convolution Nets
- [ ] Recurrent Nets

Metadata

Release files for nicenet 0.0.7

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

Source distribution (sdist)

Source distribution for nicenet 0.0.7
File Size Uploaded
nicenet-0.0.7.tar.gz 14.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nicenet 0.0.7
File Interpreter ABI Platform
nicenet-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 28.3 kB

Release files / nicenet-0.0.7.tar.gz

Download URL nicenet-0.0.7.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f66ff65fa8456bf8e7bd24df08566bd05aa3e374fd3ffe9452cf30dfa38752f9
BLAKE2b-256 checksum
How to use checksums
08bfe0157e90e7b1bcfe38f6eb77992806033881f2704fc6c948c848aee5edb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.6.9

Release files / nicenet-0.0.7-py3-none-any.whl

Download URL nicenet-0.0.7-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cdf6839b3779dab16f80974338891c04be839452287e0a9be7edb7a345442627
BLAKE2b-256 checksum
How to use checksums
9bfbfc58c0c2c34d4b6745586ca48a96a955c586b5167b6cadd947bef3a3c25b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.6.9

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.2

1 release file

0.0.1

2 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