ScratchRNNilla is a fully handwritten Vanilla RNN regression model framework developed in C++20 using module files (.ixx). It is designed to predict stock prices using historical 5-year stock data and was built from the ground up with no machine learning libraries.
This project features:
- 🧮 Hand-crafted
Matrixclass with full support for linear algebra operations - 🔁 Fully custom RNN architecture with
tanhactivation - 🔄 Reverse-mode automatic differentiation (ReverseAutoDiff)
- 🧠 Stock price prediction with 5-year dataset
- ✂️ Gradient clipping, L2 regularization
- 📉 MSE loss for regression
- 📈 Metrics: RMSE, R² Score
ScratchRNNilla aims to demonstrate that it's possible to design and train a sequence model from scratch — handling both forward propagation and dynamic gradient computation — using only modern C++20 features. It showcases how core ML building blocks like autodiff and matrix calculus work internally.
At the core of the framework lies a robust and minimal Matrix class that implements:
- Element-wise and matrix multiplication
- Transpose and broadcasting support
- Matrix activation functions (
tanh,sigmoid,relu) - Gradients of activation functions (
dTanh,dSigmoid, etc.)
The entire numerical engine is implemented manually, ensuring transparency over every computation.
ReverseAutoDiff is a custom autodiff engine for backpropagation:
- Each
Nodestores its value (DATA), gradient (GRADIENT), parents, and a backward lambda - Operations like
+,-,*,elementwise,tanh,sigmoidbuild a dynamic graph - Calling loss function (like
MSE) triggers backward propagation through the entire graph
This design allows the model to perform gradient descent training without any external dependency.
This RNN model is trained to predict future stock prices based on a sequence of 4 features:
- Open
- High
- Low
- Close
- Real stock CSV files over a 5-year period
- Preprocessing includes normalization, splitting target from sequence, and padding
- Layer size: 4 neurons
- Epochs: 10
- Learning rate: 0.01
- L2 penalty: 0.01
- Loss: MSE (Mean Squared Error)
model.RLayer(4, 4);
model.Train(dataset, target, 10, 0.01, 0.05, 0.01, 0.5);- Root Mean Squared Error (RMSE)
- Coefficient of Determination (R² Score)
These metrics help assess how well the model performs on real test data.
Ansham Maurya Email: [email protected]
"The best way to master machine learning is to implement it from scratch."
MIT License — free to use, modify, distribute, or fork.

