Shape Recognizer
I built this shape-recognizing neural network from scratch in Rust to gain a deep, foundational understanding of machine learning algorithms without relying on external deep learning frameworks. The project features a custom forward and backpropagation pipeline, parallelized batch training using Rayon to maximize multi-core execution, and Gzip-compressed binary serialization for ultra-fast model loading and compact storage.
Parallel Batch Training with Rayon
To achieve high throughput when training across millions of samples, I implemented a parallel mini-batch architecture powered by Rayon. The train_batch method delegates sample gradient calculations across available CPU threads using par_iter(). The partial gradients are synthesized concurrently with a custom map-reduce pattern using zero_gradients and accumulate_gradients, eliminating data races before applying the batch adjustments to the primary weights.
Manual Backpropagation & Gradient Descent
Rather than relying on automatic differentiation libraries, I manually implemented backpropagation to directly update layer weights and biases. The train function executes a full forward pass, calculates output deltas against target classes, and propagates error signals backward through the hidden layer. It computes the activation derivative using ReLU and scales updates by the defined learning rate, maintaining complete mathematical transparency throughout the network.
Compressed Binary Model Persistence
To maintain a minimal disk footprint and eliminate slow textual parsing (such as JSON or CSV), I built a custom binary serialization format using flate2. Floating-point weight matrices are converted to little-endian byte arrays (to_le_bytes) and streamed into a Gzip encoder. Upon initialization, load_weights_compressed decodes the stream directly back into f32 primitives using slice conversions, ensuring rapid boot times even with large weight matrices.
Technical Architecture & Performance
By eliminating heavy machine learning frameworks, this implementation delivers exceptional efficiency, consuming negligible memory overhead during execution. Training on generated shape datasets demonstrates how low-level memory control in Rust—paired with parallel CPU concurrency—enables high-performance AI training loops directly on standard hardware.
Try It Out
DownloadIf you want to try this app out for yourself, click on the download button and install rust-neural-network.rar from GitHub, extract the .rar file and run the rust-neural-network executable. It comes pretrained on a generated 6 Million shapes. If you want to train it yourself, delete the weights1_v6.bin.gz and weights2_v6.bin.gz files.
Video Demonstration
External Media Required
This video requires third-party cookies from YouTube to be displayed. Accepting will enable all external media across the site.