Skip to content

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

License

Notifications You must be signed in to change notification settings

AnhQuoc533/neural-network

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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, 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 -i https://test.pypi.org/simple/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 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 is licensed under the MIT License, as found in the LICENSE file.