Skip to main content

A Neural Network framework for building Multi-layer Perceptron model.

Project description

neural-network


neural-network is a Python package on TestPyPi that provides a Multi-Layer Perceptron (MLP) framework built using only NumPy. The framework supports Gradient Descent, Momentum, RMSProp, and Adam optimizers.

Table of Contents

  1. Installation
  2. Simple Usage
  3. Beyond the Framework
  4. License

Installation

Dependencies

python>=3.8
numpy>=1.22.1
matplotlib>=3.5.1 

User installation

You can install neural-network using pip:

pip install neural-network

Simple Usage

Designing the Model Architecture

To define your MLP model, you need to specify the number of layers and the number of neurons in each one.
Unless you want to manually set up the parameters, the size of the input layer is not needed, as it will be automatically determined in the initial training process.

from neural_network import NeuralNetwork
model = NeuralNetwork(neurons=[64, 120, 1])

In this example, we have a four-layer neural network containing auto-defined input neurons, first hidden layer with 64 neurons, second hidden layer with 120 neurons, and one output neuron.

Training the Model

To train the model, you need to provide the input data and the corresponding target (or label) data.

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])

model.fit(X, y, epochs=1000, learning_rate=0.1, optimizer='adam')

When training the model without setting the activation functions or/and the loss functions, the framework will automatically do the job for you. It will initialize the parameters and the functions according to the type of model (regression or classification) and its architecture.

Making predictions

Once the model has been trained, you can use it to make predictions by simple call predict method.

predictions = model.predict(X)

Beyond the Framework

Apart from the neural network framework, the package also provides:

Activation functions

Sigmoid function sigmoid()
Hyperbolic tangent function tanh()
Rectified linear unit relu()
Leaky Rectified linear unit leaky_relu()
Softmax function softmax()
Gaussian error linear unit gelu()

All above functions have 2 parameters:

  • x: The input values. Even though some functions can accept numeric primitive data type, it is advised to use NumPy array.
  • derivative: A boolean value indicating whether the function computes the derivative on the input x. Default is False.

Loss functions

Logistic loss function log_loss()
Cross-entropy loss function cross_entropy_loss()
Quadratic loss function quadratic_loss()

All above functions have 3 parameters:

  • y_pred: Predicted labels. It must be a 2D NumPy array and have the same size as y_true.
  • y_true: True labels. It must be a 2D NumPy array and have the same size as y_pred.
  • derivative: A boolean value indicating whether the function computes the derivative. Default is False.

2D Decision Boundary

This utility function is used for illustrative purpose. It takes a trained binary classification model, a 2D NumPy input data with 2 attributes, and the corresponding binary label data as input. The function then will plot a 2D decision boundary based on the prediction of the model.
The input model is not necessarily an instance of NeuralNetwork, but it must have predict method that accepts a 2D NumPy array as input.

plot_decision_boundary(model, train_x, train_y)

License

This project has MIT License, as found in the LICENSE file.

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

neural_network-0.1.2.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

neural_network-0.1.2-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file neural_network-0.1.2.tar.gz.

File metadata

  • Download URL: neural_network-0.1.2.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for neural_network-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5b15953ab0107004c3240b5ed6efae1adf6f0c134c6528ec8566ca1a07597a49
MD5 679b9743806705906dc185ca9a9d6ad9
BLAKE2b-256 caff5d0b223ff64be09cb530f76d7c64c9f313613a28eaef58e8a8c64ee6f3fd

See more details on using hashes here.

File details

Details for the file neural_network-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: neural_network-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for neural_network-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c20d31fb660f269f8da42897f30dfa38b71e43bdd8b65afa13e78f7231eae5fd
MD5 bae189c5b785b182025b2bf4fa86a258
BLAKE2b-256 cac33518033ed780905ecf63b3e33b5fbd4a4cd5750113a1dd6bfb73d49d1472

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