Skip to content

ChaiAndZain/MatrixFlow-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MatrixFlow C++ Deep Learning Library (TensorFlow from scratch)

MatrixFlow is a lightweight C++ library designed to build and train neural networks from scratch. It implements core deep learning concepts like matrix operations, layers stacking, forward propagation, and backward propagation.

✨ Features

  • Matrix Operations: A robust Matrix class with support for:
    • Dot products, transpose, and element-wise operations.
    • Scalar mathematics and randomization.
  • Layers:
    • Dense: Fully connected layers with learnable weights and biases.
    • Activation: Base class for activation functions.
  • Activation Functions:
    • ReLU: Rectified Linear Unit.
    • Sigmoid: Standard sigmoid activation.
  • Model API:
    • Sequential: A container to stack layers and manage forward/backward passes.
  • Training:
    • Trainer: Handles batch generation, loss calculation (MSE), and the training loop.

πŸš€ Getting Started

Prerequisites

  • A C++ compiler (e.g., g++ or clang++).
  • Standard Library support (C++11 or later).

Compilation

You can compile the library with your main.cpp using the following command:

g++ main.cpp -o main.exe

Running

To run the compiled executable:

./main.exe

πŸ“š Usage Example

Here is a quick example of defining a XOR model using MatrixFlow:

#include "MatrixFlow.h"
#include "Matrix.h"

int main() {
    // 1. Prepare Data (XOR)
    Matrix X(4, 2);
    X.data = {0,0, 1,0, 0,1, 1,1};

    Matrix Y(4, 1);
    Y.data = {0, 1, 1, 0};

    // 2. Define Architecture
    Sequential model({
        new Dense(2, 10),
        new ReLU(),
        new Dense(10, 1),
        new Sigmoid()
    });

    // or
    // Sequential model;
    // model.add(new Dense(2, 10));
    // model.add(new ReLU());
    // model.add(new Dense(10, 1));
    // model.add(new Sigmoid());

    // 3. Setup Trainer
    Trainer trainer(model, X, Y, 10000, 4, 0.1, 1000);

    // 4. Train
    trainer.train();

    // 5. Predict
    model.forward(X).print();

    return 0;
}

πŸ“ File Structure

  • Matrix.h: Core matrix manipulation library.
  • MatrixFlow.h: Neural network components (Layers, Model, Trainer).
  • main.cpp: Example entry point demonstrating an XOR implementation.

πŸ› οΈ Built With

  • Language: C++
  • Focus: Object-Oriented Programming (OOP)
  • Domain: Deep Learning

To Do

  • Add more layers (Flaten, Softmax, etc.).
  • Add more activation functions (Tanh, Leaky ReLU, etc.).
  • Add optimizers (SGD, Adam, etc.).
  • Add custom loss functions options.

About

Simple Tensor Flow clone from scratch in C++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages