Skip to content

Adding lost bandicoot usage example - #245

Open
shrit wants to merge 4 commits into
mlpack:masterfrom
shrit:coot_2
Open

Adding lost bandicoot usage example#245
shrit wants to merge 4 commits into
mlpack:masterfrom
shrit:coot_2

Conversation

@shrit

@shrit shrit commented Feb 22, 2025

Copy link
Copy Markdown
Member

No description provided.

Signed-off-by: Omar Shrit <omar@avontech.fr>
@github-actions

Copy link
Copy Markdown

Binder 👈 Launch a binder notebook on branch shrit/examples/coot_2

Signed-off-by: Omar Shrit <omar@avontech.fr>
@github-actions github-actions Bot closed this Apr 11, 2025
@shrit shrit reopened this May 5, 2025
Signed-off-by: Omar Shrit <omar@avontech.fr>
Signed-off-by: Omar Shrit <omar@avontech.fr>

@zoq zoq left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great example, I used it to verify the Vulkan backend, I left a few suggestions to make this example build by default against the CUDA backend, but to make it work against Vulkan or OpenCL you just need to uncomment the corresponding line the the Makefile.

I also commented in the inference part after the training is done. Also also added the accuracy function to output the accuracy after the training finished. That said, all backends have very similar loss curves and final loss as well.

Note, I needed to build against the bandicoot unstable branch to make this work (we have a few patches open as MR to get this working across all backends).

Comment on lines +30 to +35
coot::Row<size_t> getLabels(coot::mat predOut)
{
coot::Row<size_t> predLabels(predOut.n_cols);
for (coot::uword i = 0; i < predOut.n_cols; ++i)
{
// predLabels(i) = predOut.col(i).index_max();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
coot::Row<size_t> getLabels(coot::mat predOut)
{
coot::Row<size_t> predLabels(predOut.n_cols);
for (coot::uword i = 0; i < predOut.n_cols; ++i)
{
// predLabels(i) = predOut.col(i).index_max();
coot::Row<size_t> getLabels(const coot::mat& predOut)
{
coot::Row<size_t> predLabels(predOut.n_cols);
for (coot::uword i = 0; i < predOut.n_cols; ++i)
{
predLabels(i) = predOut.col(i).index_max();

}
return predLabels;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
double accuracy(const coot::Row<size_t>& predLabels, const coot::mat& labels)
{
const coot::Row<size_t> trueLabels = coot::conv_to<coot::Row<size_t>>::from(labels);
return coot::accu(predLabels == trueLabels) / (double) labels.n_elem * 100;
}

to measure accuracy

Comment on lines +72 to +75
const coot::mat trainX =
coot::conv_to<coot::mat>::from(train.submat(1, 0, train.n_rows - 1, train.n_cols - 1) / 255.0);
const coot::mat validX =
coot::conv_to<coot::mat>::from(valid.submat(1, 0, valid.n_rows - 1, valid.n_cols - 1) / 255.0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const coot::mat trainX =
coot::conv_to<coot::mat>::from(train.submat(1, 0, train.n_rows - 1, train.n_cols - 1) / 255.0);
const coot::mat validX =
coot::conv_to<coot::mat>::from(valid.submat(1, 0, valid.n_rows - 1, valid.n_cols - 1) / 255.0);
const coot::mat trainX = coot::conv_to<coot::mat>::from(
train.submat(1, 0, train.n_rows - 1, train.n_cols - 1) / 255.0);
const coot::mat validX = coot::conv_to<coot::mat>::from(
valid.submat(1, 0, valid.n_rows - 1, valid.n_cols - 1) / 255.0);

Comment on lines +149 to +161
//coot::mat predOut;
//// Getting predictions on training data points.
//model.Predict(trainX, predOut);
//// Calculating accuracy on training data points.
//coot::Row<size_t> 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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//coot::mat predOut;
//// Getting predictions on training data points.
//model.Predict(trainX, predOut);
//// Calculating accuracy on training data points.
//coot::Row<size_t> 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);

Comment on lines +163 to +164
//cout << "Accuracy: train = " << trainAccuracy << "%,"
//<< "\t valid = " << validAccuracy << "%" << endl;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//cout << "Accuracy: train = " << trainAccuracy << "%,"
//<< "\t valid = " << validAccuracy << "%" << endl;
cout << "Accuracy: train = " << trainAccuracy << "%,"
<< "\t valid = " << validAccuracy << "%" << endl;

Comment on lines +166 to +189
//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<size_t> 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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//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<size_t> 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;
// Loading test dataset (the one whose predicted labels
// should be sent to kaggle website).
data::Load("../../../data/mnist_test.csv", dataset, true);
const coot::mat testY = coot::conv_to<coot::mat>::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<coot::mat>::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;

# 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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

LIBS_NAME := bandicoot openblas armadillo

CXX := g++
CXXFLAGS += -std=c++17 -Wall -Wextra -O3 -g -DNDEBUG -fopenmp

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# CXXFLAGS += -std=c++14 -Wall -Wextra -g -O0
# CXXFLAGS += -std=c++17 -Wall -Wextra -g -O0

Comment on lines +23 to +28
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants