/mlx-neural-networks

💻 Explore the art and science of neural networks through hands-on examples using Apple's MLX framework. This repository is a collection of my personal journey—capturing the lessons, experiments, and insights gained from designing, implementing, and evaluating neural networks.

Primary LanguagePython

đź’» MLX Neural Networks

🚀 This repository contains code examples of designing, implementing, and evaluating neural networks. All code examples are from my PoCs, learning, and personal experience.

🎓 You can use these code examples for educational or work purposes. I would be grateful for citing the materials provided here. Please also cite and credit all materials created by other authors you use for your work.

⚡️ You can subscribe to my Medium account to read articles about artificial intelligence, cloud computing, state-of-the-art technologies, and also audio engineering! Here is a link:

My Articles on Medium

🙌 This collection was created by Oleh Chaplia and is constantly updated.

Table of Contents

  1. MLX Framework
  2. Neural network as a XOR gate
  3. Neural network as a calculator

MLX Framework

These source code examples contain the usage of MLX Framework. This framework is designed to build ML apps for Apple Silicon with unified memory architecture, such as M chips.

MLX is an array framework designed for efficient and flexible machine learning research on Apple silicon.

MLX is an array framework optimized for the unified memory architecture of Apple silicon. The NumPy-like API makes it familiar to use and flexible. The higher level neural net and optimizer packages along with function transformations for automatic differentiation and graph optimization let you build more complex yet efficient machine learning models. MLX also has Swift, C++, and C bindings and can run on any Apple platform.

Apple Open Source Project - MLX

Requirements

All Python dependencies are defined in the requirements.txt file.

To install all dependencies, run the command:

pip install -r requirements.txt

Neural network as a XOR gate

The first example contains source code for a simple neural network that simulates the XOR gate.

This code was inspired by the "An Introduction to Apple's MLX: Implementing an XOR Gate" created by Circuit Chronicles.

The logic behind the xor gate:

Input 1 Input 2 XOR Output
0 0 0
0 1 1
1 0 1
1 1 0

A neural network consists of two linear layers. The input layer consists of two neurons, and the output layer contains one neuron. As input, the neural network takes two integer numbers and outputs one number as a result. Stochastic gradient descent (SGD) optimizer is used.

Project files are here.

The project files contain neural networks, test dataset generation, model training, model testing, and model inferencing for a single input. The model is also saved in a safetensors format, and a result plots are created.

You can open safetensors model file using Netron.

The training dataset contains 10000 random duplicated combinations of the XOR logic. Training results are provided below. After ~6000 iterations, the loss is almost zero, providing 100% of correct results for the validation dataset.

training loss plot validation results

Project files:

To run the training process:

python 1_xor_gate/model/model_training.py

To run the validation process:

python 1_xor_gate/model/model_testing.py

To run the inferencing:

python 1_xor_gate/model/model_inferencing.py

Neural network as a calculator

This example contains source code for a simple neural network that works as a calculator for two numbers and supports two operations, "+" and "-."

A neural network consists of 3 linear layers—three neurons for input, ten neurons in a hidden layer, and one neuron for output. As input, the neural network takes two integer numbers, an encoded numerical value that represents "+" (0) or "-" (1), and outputs one number as a result. Mean squared error loss and Adam optimizer are used.

Project files are here.

The training dataset contains 20000 random duplicated combinations. The dataset includes two numbers, an operation, and an expected result. Training results are provided below. After ~2000 training iterations, the loss is pretty stable. The validation dataset contains 4000 combinations.

The neural network returns the answer very close to the expected result. The difference is very minor. However, when comparing the returned and expected numbers strictly, all results are not the same. Therefore, a math round operation returns the value from the neural network. After rounding the result, the neural network provides 100% of the correct results for the validation dataset.

training loss plot

training loss plot validation results

Project files:

To run the training process:

python 2_calculator/model/model_training.py

To run the validation process:

python 2_calculator/model/model_testing.py

To run the inferencing:

python 2_calculator/model/model_inferencing.py