From a0d48515b96b8fc47443bea94ae26e995356cfeb Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 22 Feb 2025 16:20:56 +0100 Subject: [PATCH 01/14] Adding lost bandicoot usage example Signed-off-by: Omar Shrit --- .../mnist_simple_coot/Makefile | 46 +++++ .../mnist_simple_coot/mnist_simple_coot.cpp | 188 ++++++++++++++++++ 2 files changed, 234 insertions(+) create mode 100644 cpp/neural_networks/mnist_simple_coot/Makefile create mode 100644 cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp diff --git a/cpp/neural_networks/mnist_simple_coot/Makefile b/cpp/neural_networks/mnist_simple_coot/Makefile new file mode 100644 index 00000000..825efe16 --- /dev/null +++ b/cpp/neural_networks/mnist_simple_coot/Makefile @@ -0,0 +1,46 @@ +# This is a simple Makefile used to build the example source code. +# This example might requires some modifications in order to work correctly on +# your system. +# This example trains mlpack neural network on GPU via OpenCL or CUDA, and it uses +# the bandicoot library. + +TARGET := mnist_simple_coot +SRC := mnist_simple_coot.cpp +LIBS_NAME := bandicoot + +CXX := g++ +CXXFLAGS += -std=c++17 -Wall -Wextra -O3 -DNDEBUG -fopenmp +# Use these CXXFLAGS instead if you want to compile with debugging symbols and +# without optimizations. +# CXXFLAGS += -std=c++14 -Wall -Wextra -g -O0 + +LDFLAGS += -fopenmp +# Add header directories for any includes that aren't on the +# default compiler search path. + +INCLFLAGS := -I . +INCLFLAGS := -I/meta/tests/ensmallen/include +# If you have mlpack or ensmallen installed somewhere nonstandard, uncomment and +# update the lines below. +INCLFLAGS += -I/opt/cuda/targets/x86_64-linux/include +INCLFLAGS += -I/meta/mlpack/src +#INCLFLAGS += -I/meta/m +CXXFLAGS += $(INCLFLAGS) + +OBJS := $(SRC:.cpp=.o) +LIBS := $(addprefix -l,$(LIBS_NAME)) +CLEAN_LIST := $(TARGET) $(OBJS) + +# default rule +default: all + +$(TARGET): $(OBJS) + $(CXX) $(OBJS) -o $(TARGET) $(LDFLAGS) $(LIBS) + +.PHONY: all +all: $(TARGET) + +.PHONY: clean +clean: + @echo CLEAN $(CLEAN_LIST) + @rm -f $(CLEAN_LIST) diff --git a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp new file mode 100644 index 00000000..1f1aa4d4 --- /dev/null +++ b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp @@ -0,0 +1,188 @@ +/** + * An example of using Feed Forward Neural Network (FFN) for + * solving Digit Recognizer problem from Kaggle website. + * + * The full description of a problem as well as datasets for training + * and testing are available here https://www.kaggle.com/c/digit-recognizer + * + * mlpack is free software; you may redistribute it and/or modify it under the + * terms of the 3-clause BSD license. You should have received a copy of the + * 3-clause BSD license along with mlpack. If not, see + * http://www.opensource.org/licenses/BSD-3-Clause for more information. + * + * @author Eugene Freyman + * @author Omar Shrit + */ +#define MLPACK_ENABLE_ANN_SERIALIZATION +#define MLPACK_HAS_COOT + +#include +#include + +#if ((ENS_VERSION_MAJOR < 2) || \ + ((ENS_VERSION_MAJOR == 2) && (ENS_VERSION_MINOR < 13))) + #error "need ensmallen version 2.13.0 or later" +#endif + +using namespace mlpack; +using namespace std; + +coot::Row getLabels(coot::mat predOut) +{ + coot::Row predLabels(predOut.n_cols); + for (coot::uword i = 0; i < predOut.n_cols; ++i) + { + // predLabels(i) = predOut.col(i).index_max(); + } + return predLabels; +} + +int main() +{ + // Dataset is randomly split into validation + // and training parts in the following ratio. + constexpr double RATIO = 0.1; + // The number of neurons in the first layer. + constexpr int H1 = 200; + // The number of neurons in the second layer. + constexpr int H2 = 100; + // Step size of the optimizer. + const double STEP_SIZE = 5e-3; + // Number of data points in each iteration of SGD + const size_t BATCH_SIZE = 64; + // Allow up to 50 epochs, unless we are stopped early by EarlyStopAtMinLoss. + const int EPOCHS = 50; + + // Labeled dataset that contains data for training is loaded from CSV file, + // rows represent features, columns represent data points. + arma::mat dataset; + data::Load("../data/mnist_train.csv", dataset, true); + + // Originally on Kaggle dataset CSV file has header, so it's necessary to + // get rid of the this row, in Armadillo representation it's the first column. + arma::mat headerLessDataset = + dataset.submat(0, 1, dataset.n_rows - 1, dataset.n_cols - 1); + + // Splitting the training dataset on training and validation parts. + arma::mat train, valid; + data::Split(headerLessDataset, train, valid, RATIO); + + // Getting training and validating dataset with features only and then + // normalising + const coot::mat trainX = + ConvTo::from(train.submat(1, 0, train.n_rows - 1, train.n_cols - 1) / 255.0); + const coot::mat validX = + ConvTo::from(valid.submat(1, 0, valid.n_rows - 1, valid.n_cols - 1) / 255.0); + + // Labels should specify the class of a data point and be in the interval [0, + // numClasses). + + // Creating labels for training and validating dataset. + const coot::mat trainY = coot::conv_to::from(train.row(0)); + const coot::mat validY = coot::conv_to::from(valid.row(0)); + + // Specifying the NN model. NegativeLogLikelihood is the output layer that + // is used for classification problem. GlorotInitialization means that + // initial weights in neurons are a uniform gaussian distribution. + FFN, GlorotInitialization, coot::mat> model; + // This is intermediate layer that is needed for connection between input + // data and relu layer. Parameters specify the number of input features + // and number of neurons in the next layer. + model.Add>(H1); + // The first relu layer. + model.Add>(); + // Intermediate layer between relu layers. + model.Add>(H2); + // The second relu layer. + model.Add>(); + // Dropout layer for regularization. First parameter is the probability of + // setting a specific value to 0. + model.Add>(0.2); + // Intermediate layer. + model.Add>(10); + // LogSoftMax layer is used together with NegativeLogLikelihood for mapping + // output values to log of probabilities of being a specific class. + model.Add>(); + + cout << "Start training ..." << endl; + + // Set parameters for the Adam optimizer. + ens::Adam optimizer( + STEP_SIZE, // Step size of the optimizer. + BATCH_SIZE, // Batch size. Number of data points that are used in each + // iteration. + 0.9, // Exponential decay rate for the first moment estimates. + 0.999, // Exponential decay rate for the weighted infinity norm estimates. + 1e-8, // Value used to initialise the mean squared gradient parameter. + EPOCHS * trainX.n_cols, // Max number of iterations. + 1e-8, // Tolerance. + true); + + // Declare callback to store best training weights. + ens::StoreBestCoordinates bestCoordinates; + + // Train neural network. If this is the first iteration, weights are + // random, using current values as starting point otherwise. + model.Train(trainX, + trainY, + optimizer, + ens::PrintLoss(), + ens::ProgressBar(), + // Stop the training using Early Stop at min loss. + ens::EarlyStopAtMinLossType( + [&](const coot::mat& /* param */) + { + double validationLoss = model.Evaluate(validX, validY); + cout << "Validation loss: " << validationLoss << "." + << endl; + return validationLoss; + }), + // Store best coordinates (neural network weights) + bestCoordinates); + + // Save the best training weights into the model. + model.Parameters() = bestCoordinates.BestCoordinates(); + + //coot::mat predOut; + //// Getting predictions on training data points. + //model.Predict(trainX, predOut); + //// Calculating accuracy on training data points. + //coot::Row predLabels = getLabels(predOut); + //double trainAccuracy = + //coot::accu(predLabels == trainY) / (double) trainY.n_elem * 100; + //// Getting predictions on validating data points. + //model.Predict(validX, predOut); + //// Calculating accuracy on validating data points. + //predLabels = getLabels(predOut); + //double validAccuracy = + //coot::accu(predLabels == validY) / (double) validY.n_elem * 100; + + //cout << "Accuracy: train = " << trainAccuracy << "%," + //<< "\t valid = " << validAccuracy << "%" << endl; + + //data::Save("model.bin", "model", model, false); + + // Loading test dataset (the one whose predicted labels + // should be sent to kaggle website). + //data::Load("../data/mnist_test.csv", dataset, true); + //coot::mat testY = dataset.row(0); + //dataset.shed_row(0); // Strip labels before predicting. + //dataset /= 255.0; // Apply the same normalization as to the training data. + + //cout << "Predicting on test set..." << endl; + //coot::mat testPredOut; + //// Getting predictions on test data points. + //model.Predict(dataset, testPredOut); + //// Generating labels for the test dataset. + //coot::Row testPred = getLabels(testPredOut); + + //double testAccuracy = coot::accu(testPred == testY) / + //(double) testY.n_elem * 100; + //cout << "Accuracy: test = " << testAccuracy << "%" << endl; + + //cout << "Saving predicted labels to \"results.csv\" ..." << endl; + //testPred.save("results.csv", coot::csv_ascii); + + //cout << "Neural network model is saved to \"model.bin\"" << endl; + cout << "Finished" << endl; +} From c4ea7ac7002b2dca383280fbfce397d3ffd31d19 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 25 Feb 2025 21:18:07 +0100 Subject: [PATCH 02/14] Fix some errors Signed-off-by: Omar Shrit --- cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp index 1f1aa4d4..608b4dd9 100644 --- a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp +++ b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp @@ -70,9 +70,9 @@ int main() // Getting training and validating dataset with features only and then // normalising const coot::mat trainX = - ConvTo::from(train.submat(1, 0, train.n_rows - 1, train.n_cols - 1) / 255.0); + ConvTo::From(train.submat(1, 0, train.n_rows - 1, train.n_cols - 1) / 255.0); const coot::mat validX = - ConvTo::from(valid.submat(1, 0, valid.n_rows - 1, valid.n_cols - 1) / 255.0); + ConvTo::From(valid.submat(1, 0, valid.n_rows - 1, valid.n_cols - 1) / 255.0); // Labels should specify the class of a data point and be in the interval [0, // numClasses). From fc2633451b10f175707c1bc09292331b6e3c4ce3 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Wed, 24 Sep 2025 21:07:44 +0200 Subject: [PATCH 03/14] Fix the layers and conv_to Signed-off-by: Omar Shrit --- cpp/neural_networks/mnist_simple_coot/Makefile | 3 ++- .../mnist_simple_coot/mnist_simple_coot.cpp | 18 +++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cpp/neural_networks/mnist_simple_coot/Makefile b/cpp/neural_networks/mnist_simple_coot/Makefile index 825efe16..18d0645e 100644 --- a/cpp/neural_networks/mnist_simple_coot/Makefile +++ b/cpp/neural_networks/mnist_simple_coot/Makefile @@ -6,6 +6,7 @@ TARGET := mnist_simple_coot SRC := mnist_simple_coot.cpp +#SRC := main.cpp LIBS_NAME := bandicoot CXX := g++ @@ -19,7 +20,7 @@ LDFLAGS += -fopenmp # default compiler search path. INCLFLAGS := -I . -INCLFLAGS := -I/meta/tests/ensmallen/include +INCLFLAGS := -I/meta/ensmallen/include # If you have mlpack or ensmallen installed somewhere nonstandard, uncomment and # update the lines below. INCLFLAGS += -I/opt/cuda/targets/x86_64-linux/include diff --git a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp index 608b4dd9..f3c1d3c4 100644 --- a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp +++ b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp @@ -70,9 +70,9 @@ int main() // Getting training and validating dataset with features only and then // normalising const coot::mat trainX = - ConvTo::From(train.submat(1, 0, train.n_rows - 1, train.n_cols - 1) / 255.0); + coot::conv_to::from(train.submat(1, 0, train.n_rows - 1, train.n_cols - 1) / 255.0); const coot::mat validX = - ConvTo::From(valid.submat(1, 0, valid.n_rows - 1, valid.n_cols - 1) / 255.0); + coot::conv_to::from(valid.submat(1, 0, valid.n_rows - 1, valid.n_cols - 1) / 255.0); // Labels should specify the class of a data point and be in the interval [0, // numClasses). @@ -88,21 +88,21 @@ int main() // This is intermediate layer that is needed for connection between input // data and relu layer. Parameters specify the number of input features // and number of neurons in the next layer. - model.Add>(H1); + model.Add>(H1); // The first relu layer. - model.Add>(); + model.Add>(); // Intermediate layer between relu layers. - model.Add>(H2); + model.Add>(H2); // The second relu layer. - model.Add>(); + model.Add>(); // Dropout layer for regularization. First parameter is the probability of // setting a specific value to 0. - model.Add>(0.2); + model.Add>(0.2); // Intermediate layer. - model.Add>(10); + model.Add>(10); // LogSoftMax layer is used together with NegativeLogLikelihood for mapping // output values to log of probabilities of being a specific class. - model.Add>(); + model.Add>(); cout << "Start training ..." << endl; From 6f3231e1c0d6f8ec331f7ab6b44d813417b7a4a1 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 30 Sep 2025 13:42:02 +0200 Subject: [PATCH 04/14] Add more libs and fix the changes related to NN code base Signed-off-by: Omar Shrit --- cpp/neural_networks/mnist_simple_coot/Makefile | 4 ++-- .../mnist_simple_coot/mnist_simple_coot.cpp | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cpp/neural_networks/mnist_simple_coot/Makefile b/cpp/neural_networks/mnist_simple_coot/Makefile index 18d0645e..9d2f52b5 100644 --- a/cpp/neural_networks/mnist_simple_coot/Makefile +++ b/cpp/neural_networks/mnist_simple_coot/Makefile @@ -7,10 +7,10 @@ TARGET := mnist_simple_coot SRC := mnist_simple_coot.cpp #SRC := main.cpp -LIBS_NAME := bandicoot +LIBS_NAME := bandicoot openblas armadillo CXX := g++ -CXXFLAGS += -std=c++17 -Wall -Wextra -O3 -DNDEBUG -fopenmp +CXXFLAGS += -std=c++17 -Wall -Wextra -O3 -g -DNDEBUG -fopenmp # Use these CXXFLAGS instead if you want to compile with debugging symbols and # without optimizations. # CXXFLAGS += -std=c++14 -Wall -Wextra -g -O0 diff --git a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp index f3c1d3c4..bebb88c9 100644 --- a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp +++ b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp @@ -15,7 +15,7 @@ */ #define MLPACK_ENABLE_ANN_SERIALIZATION #define MLPACK_HAS_COOT - +#include #include #include @@ -56,7 +56,7 @@ int main() // Labeled dataset that contains data for training is loaded from CSV file, // rows represent features, columns represent data points. arma::mat dataset; - data::Load("../data/mnist_train.csv", dataset, true); + data::Load("../../../data/mnist_train.csv", dataset, true); // Originally on Kaggle dataset CSV file has header, so it's necessary to // get rid of the this row, in Armadillo representation it's the first column. @@ -74,6 +74,8 @@ int main() const coot::mat validX = coot::conv_to::from(valid.submat(1, 0, valid.n_rows - 1, valid.n_cols - 1) / 255.0); +// trainX.raw_print("trainx"); +// validX.raw_print("validX"); // Labels should specify the class of a data point and be in the interval [0, // numClasses). @@ -104,7 +106,7 @@ int main() // output values to log of probabilities of being a specific class. model.Add>(); - cout << "Start training ..." << endl; + cout << "Optimiser definition ..." << endl; // Set parameters for the Adam optimizer. ens::Adam optimizer( @@ -121,6 +123,7 @@ int main() // Declare callback to store best training weights. ens::StoreBestCoordinates bestCoordinates; + cout << "Start training ..." << endl; // Train neural network. If this is the first iteration, weights are // random, using current values as starting point otherwise. model.Train(trainX, From 4120a0fe2f34535ca8dfcbc62b8412aaa9049d64 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 15 Oct 2026 15:13:12 +0200 Subject: [PATCH 05/14] Update cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp Co-authored-by: Marcus Edel --- .../mnist_simple_coot/mnist_simple_coot.cpp | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp index bebb88c9..735b9164 100644 --- a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp +++ b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp @@ -146,19 +146,15 @@ int main() // Save the best training weights into the model. model.Parameters() = bestCoordinates.BestCoordinates(); - //coot::mat predOut; - //// Getting predictions on training data points. - //model.Predict(trainX, predOut); - //// Calculating accuracy on training data points. - //coot::Row predLabels = getLabels(predOut); - //double trainAccuracy = - //coot::accu(predLabels == trainY) / (double) trainY.n_elem * 100; - //// Getting predictions on validating data points. - //model.Predict(validX, predOut); - //// Calculating accuracy on validating data points. - //predLabels = getLabels(predOut); - //double validAccuracy = - //coot::accu(predLabels == validY) / (double) validY.n_elem * 100; + coot::mat predOut; + // Getting predictions on training data points. + model.Predict(trainX, predOut); + // Calculating accuracy on training data points. + double trainAccuracy = accuracy(getLabels(predOut), trainY); + // Getting predictions on validating data points. + model.Predict(validX, predOut); + // Calculating accuracy on validating data points. + double validAccuracy = accuracy(getLabels(predOut), validY); //cout << "Accuracy: train = " << trainAccuracy << "%," //<< "\t valid = " << validAccuracy << "%" << endl; From a41113d7463bf254b0c4a66f31f250aba19aa30c Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 15 Oct 2026 15:13:46 +0200 Subject: [PATCH 06/14] Update cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp Co-authored-by: Marcus Edel --- cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp index 735b9164..6f54ec06 100644 --- a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp +++ b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp @@ -27,12 +27,12 @@ using namespace mlpack; using namespace std; -coot::Row getLabels(coot::mat predOut) +coot::Row getLabels(const coot::mat& predOut) { coot::Row predLabels(predOut.n_cols); for (coot::uword i = 0; i < predOut.n_cols; ++i) { - // predLabels(i) = predOut.col(i).index_max(); + predLabels(i) = predOut.col(i).index_max(); } return predLabels; } From 37717fcef94dc42ea66ee86cd7b4dca621501e23 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 15 Oct 2026 15:13:56 +0200 Subject: [PATCH 07/14] Update cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp Co-authored-by: Marcus Edel --- cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp index 6f54ec06..9efa2165 100644 --- a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp +++ b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp @@ -156,8 +156,8 @@ int main() // Calculating accuracy on validating data points. double validAccuracy = accuracy(getLabels(predOut), validY); - //cout << "Accuracy: train = " << trainAccuracy << "%," - //<< "\t valid = " << validAccuracy << "%" << endl; + cout << "Accuracy: train = " << trainAccuracy << "%," + << "\t valid = " << validAccuracy << "%" << endl; //data::Save("model.bin", "model", model, false); From 9973a9d3663adfe7c8d696d552148caed03064b2 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 15 Oct 2026 15:14:05 +0200 Subject: [PATCH 08/14] Update cpp/neural_networks/mnist_simple_coot/Makefile Co-authored-by: Marcus Edel --- cpp/neural_networks/mnist_simple_coot/Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cpp/neural_networks/mnist_simple_coot/Makefile b/cpp/neural_networks/mnist_simple_coot/Makefile index 9d2f52b5..8ef4eb54 100644 --- a/cpp/neural_networks/mnist_simple_coot/Makefile +++ b/cpp/neural_networks/mnist_simple_coot/Makefile @@ -10,7 +10,11 @@ SRC := mnist_simple_coot.cpp LIBS_NAME := bandicoot openblas armadillo CXX := g++ -CXXFLAGS += -std=c++17 -Wall -Wextra -O3 -g -DNDEBUG -fopenmp +CXXFLAGS += -std=c++17 -Wall -Wextra -O3 -DNDEBUG -fopenmp +# Select the bandicoot backend to run the example on. +CXXFLAGS += -DCOOT_DEFAULT_BACKEND=CUDA_BACKEND +# CXXFLAGS += -DCOOT_DEFAULT_BACKEND=CL_BACKEND +# CXXFLAGS += -DCOOT_DEFAULT_BACKEND=VULKAN_BACKEND # Use these CXXFLAGS instead if you want to compile with debugging symbols and # without optimizations. # CXXFLAGS += -std=c++14 -Wall -Wextra -g -O0 From a65bd51269e19e9a756bcadf3ee55cf5f7cbacdd Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 15 Oct 2026 15:14:31 +0200 Subject: [PATCH 09/14] Update cpp/neural_networks/mnist_simple_coot/Makefile Co-authored-by: Marcus Edel --- cpp/neural_networks/mnist_simple_coot/Makefile | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cpp/neural_networks/mnist_simple_coot/Makefile b/cpp/neural_networks/mnist_simple_coot/Makefile index 8ef4eb54..0ffdf210 100644 --- a/cpp/neural_networks/mnist_simple_coot/Makefile +++ b/cpp/neural_networks/mnist_simple_coot/Makefile @@ -24,12 +24,15 @@ LDFLAGS += -fopenmp # default compiler search path. INCLFLAGS := -I . -INCLFLAGS := -I/meta/ensmallen/include -# If you have mlpack or ensmallen installed somewhere nonstandard, uncomment and -# update the lines below. -INCLFLAGS += -I/opt/cuda/targets/x86_64-linux/include -INCLFLAGS += -I/meta/mlpack/src -#INCLFLAGS += -I/meta/m +# If you have mlpack, ensmallen or bandicoot installed somewhere nonstandard, +# uncomment and update the lines below. +# INCLFLAGS += -I/path/to/mlpack/include/ +# INCLFLAGS += -I/path/to/ensmallen/include/ +# INCLFLAGS += -I/path/to/bandicoot/include/ +# The CUDA backend of bandicoot needs the CUDA headers; comment this out if +# bandicoot was built without CUDA support. +INCLFLAGS += -I/usr/local/cuda/include +CXXFLAGS += $(INCLFLAGS) CXXFLAGS += $(INCLFLAGS) OBJS := $(SRC:.cpp=.o) From c3e42064a172772c7790bbcf6cc78c1332d27fe6 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 15 Oct 2026 15:14:40 +0200 Subject: [PATCH 10/14] Update cpp/neural_networks/mnist_simple_coot/Makefile Co-authored-by: Marcus Edel --- cpp/neural_networks/mnist_simple_coot/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/neural_networks/mnist_simple_coot/Makefile b/cpp/neural_networks/mnist_simple_coot/Makefile index 0ffdf210..62f74d95 100644 --- a/cpp/neural_networks/mnist_simple_coot/Makefile +++ b/cpp/neural_networks/mnist_simple_coot/Makefile @@ -17,7 +17,7 @@ CXXFLAGS += -DCOOT_DEFAULT_BACKEND=CUDA_BACKEND # CXXFLAGS += -DCOOT_DEFAULT_BACKEND=VULKAN_BACKEND # Use these CXXFLAGS instead if you want to compile with debugging symbols and # without optimizations. -# CXXFLAGS += -std=c++14 -Wall -Wextra -g -O0 +# CXXFLAGS += -std=c++17 -Wall -Wextra -g -O0 LDFLAGS += -fopenmp # Add header directories for any includes that aren't on the From 64f8edf256f2b304f16d0644ba32e1e54811f076 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 15 Oct 2026 15:14:50 +0200 Subject: [PATCH 11/14] Update cpp/neural_networks/mnist_simple_coot/Makefile Co-authored-by: Marcus Edel --- cpp/neural_networks/mnist_simple_coot/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/neural_networks/mnist_simple_coot/Makefile b/cpp/neural_networks/mnist_simple_coot/Makefile index 62f74d95..bf2716df 100644 --- a/cpp/neural_networks/mnist_simple_coot/Makefile +++ b/cpp/neural_networks/mnist_simple_coot/Makefile @@ -1,7 +1,7 @@ # This is a simple Makefile used to build the example source code. # This example might requires some modifications in order to work correctly on # your system. -# This example trains mlpack neural network on GPU via OpenCL or CUDA, and it uses +# This example trains mlpack neural network on GPU via OpenCL, CUDA or Vulkan, and it uses # the bandicoot library. TARGET := mnist_simple_coot From 0dc7198ce023a271fb0f3c357a50195aa52de3c9 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 15 Oct 2026 15:14:59 +0200 Subject: [PATCH 12/14] Update cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp Co-authored-by: Marcus Edel --- .../mnist_simple_coot/mnist_simple_coot.cpp | 35 +++++++------------ 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp index 9efa2165..d9adad58 100644 --- a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp +++ b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp @@ -159,29 +159,20 @@ int main() cout << "Accuracy: train = " << trainAccuracy << "%," << "\t valid = " << validAccuracy << "%" << endl; - //data::Save("model.bin", "model", model, false); - // Loading test dataset (the one whose predicted labels // should be sent to kaggle website). - //data::Load("../data/mnist_test.csv", dataset, true); - //coot::mat testY = dataset.row(0); - //dataset.shed_row(0); // Strip labels before predicting. - //dataset /= 255.0; // Apply the same normalization as to the training data. - - //cout << "Predicting on test set..." << endl; - //coot::mat testPredOut; - //// Getting predictions on test data points. - //model.Predict(dataset, testPredOut); - //// Generating labels for the test dataset. - //coot::Row testPred = getLabels(testPredOut); - - //double testAccuracy = coot::accu(testPred == testY) / - //(double) testY.n_elem * 100; - //cout << "Accuracy: test = " << testAccuracy << "%" << endl; - - //cout << "Saving predicted labels to \"results.csv\" ..." << endl; - //testPred.save("results.csv", coot::csv_ascii); - - //cout << "Neural network model is saved to \"model.bin\"" << endl; + data::Load("../../../data/mnist_test.csv", dataset, true); + const coot::mat testY = coot::conv_to::from(dataset.row(0)); + dataset.shed_row(0); // Strip labels before predicting. + dataset /= 255.0; // Apply the same normalization as to the training data. + const coot::mat testX = coot::conv_to::from(dataset); + + cout << "Predicting on test set..." << endl; + coot::mat testPredOut; + // Getting predictions on test data points. + model.Predict(testX, testPredOut); + + double testAccuracy = accuracy(getLabels(testPredOut), testY); + cout << "Accuracy: test = " << testAccuracy << "%" << endl; cout << "Finished" << endl; } From e320d2a85e5dac39a0af89943f57924411d1613c Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 15 Oct 2026 15:15:10 +0200 Subject: [PATCH 13/14] Update cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp Co-authored-by: Marcus Edel --- .../mnist_simple_coot/mnist_simple_coot.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp index d9adad58..7fa89401 100644 --- a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp +++ b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp @@ -69,10 +69,10 @@ int main() // Getting training and validating dataset with features only and then // normalising - const coot::mat trainX = - coot::conv_to::from(train.submat(1, 0, train.n_rows - 1, train.n_cols - 1) / 255.0); - const coot::mat validX = - coot::conv_to::from(valid.submat(1, 0, valid.n_rows - 1, valid.n_cols - 1) / 255.0); + const coot::mat trainX = coot::conv_to::from( + train.submat(1, 0, train.n_rows - 1, train.n_cols - 1) / 255.0); + const coot::mat validX = coot::conv_to::from( + valid.submat(1, 0, valid.n_rows - 1, valid.n_cols - 1) / 255.0); // trainX.raw_print("trainx"); // validX.raw_print("validX"); From 3337440c3eb66eb50ff1c670a0950e165a78c858 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 15 Oct 2026 15:15:21 +0200 Subject: [PATCH 14/14] Update cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp Co-authored-by: Marcus Edel --- cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp index 7fa89401..487a1d4f 100644 --- a/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp +++ b/cpp/neural_networks/mnist_simple_coot/mnist_simple_coot.cpp @@ -36,6 +36,11 @@ coot::Row getLabels(const coot::mat& predOut) } return predLabels; } +double accuracy(const coot::Row& predLabels, const coot::mat& labels) +{ + const coot::Row trueLabels = coot::conv_to>::from(labels); + return coot::accu(predLabels == trueLabels) / (double) labels.n_elem * 100; +} int main() {