Fully connected neural network built from scratch with flexible n-layer design and multiple activations.
Example Usage
Below is a minimal example of how to use the FlexNN NeuralNetwork library in your own C++ project:
#include <Eigen/Dense>
Eigen::MatrixXd X;
Eigen::VectorXd Y;
X = X.array() / 255.0;
Eigen::MatrixXd X_train = splits[0].first;
Eigen::VectorXd Y_train = splits[0].second;
Eigen::MatrixXd X_test = splits[1].first;
Eigen::VectorXd Y_test = splits[1].second;
X_train.transposeInPlace();
X_test.transposeInPlace();
});
nn.
train(X_train, Y_train, 0.5, 100);
double train_acc = nn.accuracy(X_train, Y_train);
double test_acc = nn.accuracy(X_test, Y_test);
std::cout << "Training accuracy: " << train_acc * 100 << "%" << std::endl;
std::cout << "Test accuracy: " << test_acc * 100 << "%" << std::endl;
return 0;
}
Header file for the FlexNN neural network library.
Represents a single layer in a neural network.
Definition Layer.h:38
Class representing a neural network.
Definition FlexNN.h:38
void train(const Eigen::MatrixXd &input, const Eigen::MatrixXd &target, double learningRate, int epochs)
Train the neural network.
Definition FlexNN.cpp:29
int main()
Main function to demonstrate a simple neural network for MNIST digit recognition.
Definition main.cpp:26
std::vector< std::pair< Eigen::MatrixXd, Eigen::VectorXd > > splitXY(const Eigen::MatrixXd &X, const Eigen::VectorXd &Y, const std::vector< double > &proportions)
Splits the dataset into multiple sets based on specified proportions.
Definition Utility.cpp:109
void readCSV_XY(const std::string &filename, Eigen::MatrixXd &X, Eigen::VectorXd &Y)
Reads a CSV file and splits it into features (X) and labels (Y).
Definition Utility.cpp:55
Note:
- You can customize the network architecture by changing the number and type of layers.
- Make sure your data is in the correct format and normalized as needed.
- See the
src/main.cpp
file for a more complete example.
Requirements
The C++ Neural Network library uses Eigen3
for matrix operations and OpenMP
for multithreading. The project uses CMake
for building the code.
On Ubuntu, you can install these requirements using:
sudo apt install cmake libeigen3-dev
Building and Running the Project
To build the project, follow these steps:
- Open a terminal and navigate to the project directory.
- Create a build directory:
- Run CMake to configure the project:
- Build the project:
- To run the program, go into the project root directory (so that dataset path is correctly resolved) And then run the
main
executable present inside the build
folder by:
API Reference
For details on the code structure, available classes, and how to use FlexNN in your own projects, please visit the full documentation here: https://docs.nalinangrish.me/FlexNN.
Known Issues / Limitations
- Only CPU computation is supported (no GPU).
- No support for convolutional or recurrent layers.
- Training large models may be slow.
- Currently there is no support for saving model architecture and weights, so you need to train the model on every execution.
License
This project is licensed under the Apache-2.0 License. See the LICENSE file for more details.