Project DNN From Scratch

source

This project implements a deep neural network (DNN) from scratch in Rust, focusing on two key experiments: image classification with the MNIST dataset and signal strength-based predictions using an RSSI dataset. The project demonstrates building and training a neural network without relying on external machine learning libraries.

ยง ๐Ÿš€ Features

  • Custom Neural Network Implementation: Build and train DNNs using only Rust libraries and custom modules.
  • Examples for Two Experiments:
    • MNIST Dataset: Handwritten digit classification.
    • RSSI Dataset: Analysis and predictions based on signal strength data.
  • Modular Codebase: Cleanly separated concerns such as activation functions, loss computation, and neural network architecture.
  • Visualization: Generate reports and plots to visualize experiment results.

ยง ๐Ÿ“‚ Directory Structure

dnn-from-scratch/
โ”œโ”€โ”€ README.md                # Project overview and instructions
โ”œโ”€โ”€ Cargo.toml               # Project dependencies and configuration
โ”œโ”€โ”€ LICENSE                  # License information
โ”œโ”€โ”€ assets/                  # Datasets and auxiliary data
โ”‚   โ”œโ”€โ”€ mnist/
โ”‚   โ”‚   โ”œโ”€โ”€ x_test.npy       # MNIST test images
โ”‚   โ”‚   โ”œโ”€โ”€ x_train.npy      # MNIST training images
โ”‚   โ”‚   โ”œโ”€โ”€ y_test.npy       # MNIST test labels
โ”‚   โ”‚   โ””โ”€โ”€ y_train.npy      # MNIST training labels
โ”‚   โ””โ”€โ”€ rssi/
โ”‚       โ””โ”€โ”€ rssi-dataset.csv # RSSI dataset
โ”œโ”€โ”€ dnn_from_scratch/        # Core library for the neural network
โ”‚   โ”œโ”€โ”€ Cargo.toml           # Library-specific dependencies
โ”‚   โ””โ”€โ”€ src/
โ”‚       โ”œโ”€โ”€ activation.rs    # Activation functions
โ”‚       โ”œโ”€โ”€ fully_connected.rs # Fully connected layer module
โ”‚       โ”œโ”€โ”€ lib.rs           # Entry point for the library
โ”‚       โ”œโ”€โ”€ loss.rs          # Loss functions
โ”‚       โ”œโ”€โ”€ neural_network.rs # Neural network definition
โ”‚       โ”œโ”€โ”€ optimizer.rs     # Optimizer implementations (e.g., Adam)
โ”‚       โ”œโ”€โ”€ report.rs        # Reporting and result output
โ”‚       โ”œโ”€โ”€ utils.rs         # Utility functions for regression/classification
โ”‚       โ””โ”€โ”€ weights_initializer.rs # Weight initialization strategies
โ””โ”€โ”€ src/                     # Main application for experiments
    โ”œโ”€โ”€ main.rs              # Entry point for the executable
    โ”œโ”€โ”€ mnist_experiment/    # MNIST Experiment-related modules
    โ”‚   โ”œโ”€โ”€ dataset_setup.rs # MNIST dataset preprocessing
    โ”‚   โ”œโ”€โ”€ mod.rs           # MNIST Experiment module entry point
    โ”‚   โ””โ”€โ”€ plot.rs          # Plotting results for MNIST Experiment
    โ””โ”€โ”€ rssi_experiment/     # RSSI Experiment-related modules
        โ”œโ”€โ”€ dataset_setup.rs # RSSI dataset preprocessing
        โ”œโ”€โ”€ mod.rs           # RSSI Experiment module entry point
        โ””โ”€โ”€ plot.rs          # Plotting results for RSSI Experiment

ยง ๐Ÿ› ๏ธ Getting Started

To build and run the experiments, follow these steps:

Prerequisites

Clone the Repository

git clone https://github.com/akaTsunemori/dnn-from-scratch.git
    cd dnn-from-scratch

Build the Project

cargo build --release

Run Experiments

cargo run --release

ยง ๐Ÿ“š Documentation

View detailed API documentation at this page, or generate it locally:

cargo doc --release --workspace --no-deps --target-dir=docs

ยง ๐Ÿงช Datasets

  1. MNIST Dataset:
    • Stored in assets/mnist/.
    • Preprocessed as .npy files for seamless integration.
  2. RSSI Dataset:
    • Found in assets/rssi/rssi-dataset.csv.
    • Contains signal strength data and coordinates (X, Y) for analysis.

ยง ๐Ÿ“ˆ Results & Reporting

Each experiment generates reports and plots showcasing:

  • Training history.
  • Model performance metrics (e.g., accuracy for MNIST, CDF of RMSE for RSSI).

Plots and reports are saved in the output/ folder. You can check a preview of the expected results below by clicking to reveal the contents.

MNIST Experiment

Preview of training history:

Epoch 1/100 | Train: Loss 2.0665, Accuracy 0.1013 | Test: Loss 1.9560, Accuracy 0.3710
(...)
Epoch 100/100 | Train: Loss 0.1369, Accuracy 0.9609 | Test: Loss 0.1517, Accuracy 0.9519

Output plot:

MNIST Experiment Plot

RSSI Experiment

Preview of training history:

Epoch 1/2500 | Train: Loss 18046.3999, Error 134.3368 | Test: Loss 21310.2716, Error 145.9803
(...)
Epoch 2500/2500 | Train: Loss 5.5695, Error 2.3599 | Test: Loss 5.8578, Error 2.4202

Output plot:

RSSI Experiment Plot

ยง ๐Ÿ“œ License

Licensed under the MIT License.

ยง ๐Ÿค Contributing

Contributions are welcome! Feel free to fork the repository and submit a pull request.

  1. Fork the repo.
  2. Create a new branch (git checkout -b feature-name).
  3. Commit your changes (git commit -m "Add feature").
  4. Push to the branch (git push origin feature-name).
  5. Open a pull request.

ยง ๐Ÿ“ง Contact

Create an issue for any inquiries or support.

Enjoy building your neural networks from scratch! ๐ŸŽ‰