Skip to content

About

micrograd in raw C++

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

micrograd in C++

Inspired by Andrej Karpathy's micrograd, I implemented micrograd myself from scratch in C++ as a learning exercise. Using a language different than the original Python is slightly more challenging and using C++ makes it fast enough to train a classifier for the MNIST dataset.

Build Instructions

You need a C++ compiler and CMake installed:

mkdir build
cd build
cmake ..
cmake --build . --config Release

Overview

engine.cc/.h implements backpropagation on a graph of scalar values that can be defined using the following operations:

  • Unary ops: Exp, Log, ReLu
  • Binary ops: Addition, Subtraction, Multiplication, Division

To add a new operation type, you need to implement the forward and backward pass. For example, the forward pass of the multiplication operation is implemented as:

ValuePtr operator*(ValuePtr a, ValuePtr b) {
  return std::make_shared<Value>(a->value_ * b->value_, Op::MULT,
                                 std::vector<ValuePtr>{a, b});
}

For the backward pass, you must implement the local derivative which is essential for the backpropagation algorithm. For multiplication, it is implemented as:

// d/da(a*b) = b
// d/db(a*b) = a
case Op::MULT: {
    prev_[0]->grad_ += prev_[1]->value_ * grad;
    prev_[1]->grad_ += prev_[0]->value_ * grad;
    break;
}

On top of the engine, nn.cc/.h provides a tiny machine learning framework with the following operations:

  • Linear layers and MLPs
  • SoftMax
  • Cross-entropy loss

Moons Example

This is the same example used in the original micrograd. The dataset is downloaded and written into .csv files using a Python script. Then, we train a MLP on it and write out predictions to .csv files. We obtain perfect accuracy in less than 100 epochs. Another Python scripts visualizes the decision boundary.

python scripts/download_moons.py
./build/example_moons
python scripts/visualize_moons_boundary.py

The visualized decision boundary:

decision boundary

Digits Example

python scripts/download_digits.py
./build/example_digits

We download the digits dataset from scikit-learn. It contains 1797 handwritten digits from zero to nine in a 8*8 resolution. The example trains a MLP with 1990 parameters on it until the cross-entropy loss reaches less than 0.01, corresponding to roughly e^-0.01 = 99% accuracy. This takes 17 epochs and 84 seconds on a AMD Ryzen PRO 7840U laptop CPU.

MNIST Example

python scripts/download_mnist.py
./build/example_mnist

The famous MNIST dataset contains 50'000 train and 10'000 test images of handwritten digits from zero to nine in a 28*28 resolution. The dataset size reaches the limits of what our tiny machine learning framework is able to train in a reasonable amount of time. We choose a MLP with 25'450 parameters. We train for one epoch, reaching a validation loss of 0.48 in 849s on the same AMD Ryzen Pro 7840U laptop CPU. On the test set, we classify 10'000 images with 91.08% accuracy in 32s.

About

micrograd in raw C++

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages