nnlib
GPU-accelerated, C/C++ neural network library.
MNIST

Training a neural network to recognize hand-written digits from MNIST.

Files taken from https://github.com/Jaswar/nnlib/tree/main/examples/mnist.

The main.cpp file expects one argument which is the absolute path to the MNIST_train.txt file. The file can be downloaded from https://github.com/halimb/MNIST-txt.

#include <iostream>
#include <nnlib/network.h>
#include <nnlib/read.h>
#include <nnlib/verify.cuh>
#include <nnlib/onehot_encode.h>
int main(int argc, char** argv) {
if (argc < 2) {
std::cout << "Dataset file path was not specified." << std::endl;
return 1;
}
sTensor dataset = readCSV(argv[1], ",", 4);
sTensor X = std::make_shared<Tensor>(dataset->shape[0], dataset->shape[1] - 1);
sTensor yv = std::make_shared<Tensor>(dataset->shape[0]);
for (int i = 0; i < dataset->shape[0]; i++) {
yv->data[i] = dataset->data[i * dataset->shape[1] + 0];
for (int j = 1; j < dataset->shape[1]; j++) {
X->data[i * X->shape[1] + j - 1] = dataset->data[i * dataset->shape[1] + j] / 255;
}
}
sTensor y = oneHotEncode(yv);
std::cout << y << std::endl;
Network network = Network(X->shape[1], true);
network.add(64);
network.add(y->shape[1], "sigmoid");
std::vector<Metric*> metrics = {new CategoricalAccuracy(), new MeanSquaredError()};
network.train(X, y, 10, 10, 0.01, new CategoricalCrossEntropy(), metrics);
return 0;
}
The implementation of categorical accuracy.
Definition: metric.h:71
Class representing the Categorical Cross Entropy.
Definition: loss.h:76
Class representing the Mean Squared Error.
Definition: loss.h:48
Represents a neural network.
Definition: network.h:23
void train(sTensor &X, sTensor &y, int epochs, size_t batchSize, float learningRate, Loss *loss, std::vector< Metric * > &metrics)
Train the network.
Definition: network.cpp:198
void add(size_t numNeurons, const std::string &activation="linear")
Add a new layer to the network.
Definition: network.cpp:178
sTensor oneHotEncode(const sTensor &vector)
One hot encode a vector of data.
Definition: onehot_encode.cpp:31
sTensor readCSV(const std::string &filepath, const std::string &delim, int numThreads)
Read a csv file from a path.
Definition: read.cpp:107
void showCudaInfo()
Show information about CUDA and the available GPU(s).
Definition: verify.cu:17

The project can be built with the following CMake script. This script requires CMAKE_PREFIX_PATH to be set to the install directory of nnlib.

cmake_minimum_required(VERSION 3.20)
include(CheckLanguage)
project(mnist_nnlib LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
find_package(nnlib CONFIG REQUIRED)
check_language(CUDA)
if (CMAKE_CUDA_COMPILER)
enable_language(CUDA)
set(CMAKE_CUDA_STANDARD 14)
include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
# Add flag to specify the architecture of the GPU (compute capability)
if (NOT DEFINED CUDA_ARCHITECTURES)
set_target_properties(nnlib PROPERTIES CUDA_ARCHITECTURES "50")
endif()
set_target_properties(nnlib PROPERTIES
CUDA_SEPARABLE_COMPILATION ON
CUDA_RESOLVE_DEVICE_SYMBOLS ON)
endif()
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE nnlib)
Definition: functions.h:68