Adding lost bandicoot usage example - #245
Conversation
Signed-off-by: Omar Shrit <omar@avontech.fr>
Signed-off-by: Omar Shrit <omar@avontech.fr>
Signed-off-by: Omar Shrit <omar@avontech.fr>
Signed-off-by: Omar Shrit <omar@avontech.fr>
zoq
left a comment
There was a problem hiding this comment.
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).
| 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(); |
There was a problem hiding this comment.
| 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; | ||
| } | ||
|
|
There was a problem hiding this comment.
| 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
| 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); |
There was a problem hiding this comment.
| 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); |
| //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; |
There was a problem hiding this comment.
| //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); |
| //cout << "Accuracy: train = " << trainAccuracy << "%," | ||
| //<< "\t valid = " << validAccuracy << "%" << endl; |
There was a problem hiding this comment.
| //cout << "Accuracy: train = " << trainAccuracy << "%," | |
| //<< "\t valid = " << validAccuracy << "%" << endl; | |
| 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<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; |
There was a problem hiding this comment.
| //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 |
There was a problem hiding this comment.
| # 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 |
There was a problem hiding this comment.
| 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 |
There was a problem hiding this comment.
| # CXXFLAGS += -std=c++14 -Wall -Wextra -g -O0 | |
| # CXXFLAGS += -std=c++17 -Wall -Wextra -g -O0 |
| 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 |
There was a problem hiding this comment.
| 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) |
No description provided.