Skip to main content

Melpy is a package made to learn machine learning and make its uses as simple as possible.

Project description

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

Crafting Deep Learning from the Ground Up


Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

The project started in 2022 when I was still in high school. While taking an online machine learning course, I got frustrated with not fully understanding how the algorithms worked. To solve this, I decided to implement them myself to gain a deeper and clearer understanding.

What started as a simple Python script has become Melpy, a deep learning library built entirely from scratch using NumPy. Melpy is inspired by the best tools available and makes it easy to create and train models like FNNs and CNNs. It also includes tools for data preprocessing and data visualization, making it a complete solution for deep learning.

(back to top)

Built With

Numpy Matplotlib tqdm

(back to top)

Getting Started

Prerequisites

Melpy requires an up-to-date python environment. I recommend conda, which is dedicated to the scientific use of python.

All other dependencies will be installed automatically during the library installation process.

Installation

Melpy is available on PyPI as melpy. Run the following command to install it in your environment:

pip3 install melpy --upgrade

(back to top)

Usage

To demonstrate Melpy’s capabilities, let’s work through a mini-project together. We will classify the Iris dataset, a classic example in machine learning. The dataset contains three classes: Setosa, Versicolor, and Virginica, described by the following features: Sepal Length, Sepal Width, Petal Length, and Petal Width.

First, let’s load the data and split it into training and test sets:

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

iris_dataset = load_iris()

X_train, X_test, y_train, y_test = train_test_split(
        iris_dataset['data'], iris_dataset['target'], test_size=0.25, random_state=0)

Next, visualize the data to identify any patterns:

import matplotlib.pyplot as plt

plt.figure()
plt.scatter(X_train[:,0], X_train[:,1], c=y_train, alpha=0.3, cmap="coolwarm")
plt.show()

Logo

Figure 1

As we can see in Figure 1, there is a clear correlation between species and features like Sepal Length and Sepal Width.

Preprocessing

FNNs require input data to be scaled close to zero. It is why we are now going to use StandardScaler from melpy.preprocessing :

The Standard Scaler is a pre-processing technique which consists of removing the mean from a data set and dividing by its variance. You can find out more about data scaling here: Feature Scaling.

from melpy.preprocessing import StandardScaler

sc = StandardScaler()
y_train = sc.transform(y_train) # Scales data
y_test = sc.transform(y_test) # Scaled with the same mean and variance than X_train

Next, we encode the target labels using OneHotEncoder, also from melpy.preprocessing:

One-hot encoding is a method of representing categorical data as binary vectors. Each unique category is assigned a unique vector where one element is set to 1 (hot) and all others are 0. You can find out more about data encoding here : One-hot.

from melpy.preprocessing import OneHotEncoder

ohe = OneHotEncoder()
X_train = ohe.transform(X_train) # Encodes data
X_test = ohe.transform(X_test) # Encodes with the same encoding than y_train

Model Creation

We’re tackling a multi-class classification problem using tabular data, which requires:

Now, let’s build the model using Melpy’s Sequential class:

Sequential models are neural networks where layers are stacked in a linear order. Data flows through them one by one in sequence.

import melpy.NeuralNetworks as nn

model = nn.Sequential(X_train, y_train, X_test, y_test)

model.add(nn.Dense(X_train.shape[1], 6), nn.ReLU())
model.add(nn.Dense(6, y_train.shape[1]), nn.Softmax())

model.compile(cost_function=nn.CategoricalCrossEntropy(), optimizer=nn.SGD(learning_rate=0.01))

We define:

These functions together form what we call an architecture. If you’re new to deep learning, I highly recommend 3Blue1Brown's excellent video series on the topic. It provides a clear explanation of how and why these functions are used.

Model Summary

We can view the model structure with:

model.summary()
Dense: (1, 6)
ReLU: (1, 6)
Dense: (1, 3)
Softmax: (1, 3)

Training the Model

Finally, we train the model with 5000 epochs and observe the results with verbose and LiveMetrics :

model.fit(epochs=5000, verbose = 1, callbacks=[nn.LiveMetrics()])
model.results()
Logo

Figure 2

Epoch [5000/5000]: 100%|██████████| 5000/5000 [00:03<00:00, 1543.94it/s, loss=0.0389, accuracy=0.988]

-------------------------------------------------------------------
| [TRAINING METRICS] train_loss: 0.03893 · train_accuracy: 0.9881 |
-------------------------------------------------------------------
| [VALIDATION METRICS] val_loss: 0.06848 · val_accuracy: 0.98246  |
-------------------------------------------------------------------

Our model achieves 98% accuracy on both training and test datasets, which is good! With further optimization you could potentially reach 100%. Feel free to experiment!

If you look closely, you will notice that the plot on the right closely resembles Figure 1. It’s actually the model’s inputs colored by the outputs, allowing us to visually assess whether the model is well trained.

Save Your Work

Save your trained parameters and metrics for future use:

model.save_params("iris_parameters")
model.save_histories("iris_metrics")

You can reload the parameters with load_params(path) and the metrics using the pickle library.

For more examples, please refer to the Documentation

(back to top)

Roadmap

I plan to speed up computations using Numba or JAX and to implement additional deep learning architectures, as well as more traditional machine learning algorithms.

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Lenny Malard - lennymalard@gmail.com or linkedin

Project Link: https://github.com/lennymalard/melpy-project

(back to top)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

melpy-1.0.0.5.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

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

melpy-1.0.0.5-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

Details for the file melpy-1.0.0.5.tar.gz.

File metadata

  • Download URL: melpy-1.0.0.5.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.6

File hashes

Hashes for melpy-1.0.0.5.tar.gz
Algorithm Hash digest
SHA256 d0324ea971b9d4408d5649fad191e478388b8bc9ea9b6db729dfbb5ab2fcaa8f
MD5 736d8e8c9e01c87730ee142769908d70
BLAKE2b-256 8464b4b4c9013dd35d8568aa3bc9ccf77f0d9692bed7d01007dc590379be1def

See more details on using hashes here.

File details

Details for the file melpy-1.0.0.5-py3-none-any.whl.

File metadata

  • Download URL: melpy-1.0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 26.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.6

File hashes

Hashes for melpy-1.0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b240d6f4c0042ce51bb9b8d53ab8cfa7744f9b9f60ec14b42e66047f057b91a8
MD5 7b275671784f420ba33c24bd4405bad7
BLAKE2b-256 0e7255158d3f62dfa9c31a59b010ece6ef7343a37c4eed35ad7f757f99b58f89

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