From 31d5d6a5327af7d6659c9b6121eb395a1994b719 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 13 Nov 2021 18:04:01 +0000 Subject: [PATCH 01/50] Make save image header only Signed-off-by: Omar Shrit --- src/mlpack/core/data/CMakeLists.txt | 2 +- src/mlpack/core/data/save.hpp | 8 ++++---- .../core/data/{save_image.cpp => save_image.hpp} | 16 ++++++++-------- 3 files changed, 13 insertions(+), 13 deletions(-) rename src/mlpack/core/data/{save_image.cpp => save_image.hpp} (90%) diff --git a/src/mlpack/core/data/CMakeLists.txt b/src/mlpack/core/data/CMakeLists.txt index 33f0c23dab..1bce85a23f 100644 --- a/src/mlpack/core/data/CMakeLists.txt +++ b/src/mlpack/core/data/CMakeLists.txt @@ -24,7 +24,7 @@ set(SOURCES normalize_labels_impl.hpp save.hpp save_impl.hpp - save_image.cpp + save_image.hpp split_data.hpp string_algorithms.hpp imputer.hpp diff --git a/src/mlpack/core/data/save.hpp b/src/mlpack/core/data/save.hpp index 19e11ee161..4b18a63498 100644 --- a/src/mlpack/core/data/save.hpp +++ b/src/mlpack/core/data/save.hpp @@ -163,10 +163,10 @@ bool Save(const std::vector& files, /** * Helper function to save files. Implementation in save_image.cpp. */ -bool SaveImage(const std::string& filename, - arma::Mat& image, - ImageInfo& info, - const bool fatal = false); +inline bool SaveImage(const std::string& filename, + arma::Mat& image, + ImageInfo& info, + const bool fatal = false); } // namespace data } // namespace mlpack diff --git a/src/mlpack/core/data/save_image.cpp b/src/mlpack/core/data/save_image.hpp similarity index 90% rename from src/mlpack/core/data/save_image.cpp rename to src/mlpack/core/data/save_image.hpp index 86ff95a106..edffc9712a 100644 --- a/src/mlpack/core/data/save_image.cpp +++ b/src/mlpack/core/data/save_image.hpp @@ -30,10 +30,10 @@ namespace mlpack { namespace data { -bool SaveImage(const std::string& filename, - arma::Mat& image, - ImageInfo& info, - const bool fatal) +inline bool SaveImage(const std::string& filename, + arma::Mat& image, + ImageInfo& info, + const bool fatal) { // Check to see if the file type is supported. if (!ImageFormatSupported(filename, true)) @@ -127,10 +127,10 @@ bool SaveImage(const std::string& filename, namespace mlpack { namespace data { -bool SaveImage(const std::string& /* filename */, - arma::Mat& /* image */, - ImageInfo& /* info */, - const bool fatal) +inline bool SaveImage(const std::string& /* filename */, + arma::Mat& /* image */, + ImageInfo& /* info */, + const bool fatal) { if (fatal) { From 1c3cb3e7162ecc2209653a47071c165e5762227f Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 13 Nov 2021 18:12:02 +0000 Subject: [PATCH 02/50] Finish inlining the data dir Signed-off-by: Omar Shrit --- src/mlpack/core/data/CMakeLists.txt | 4 ++-- src/mlpack/core/data/detect_file_type.hpp | 10 ++++++---- ...t_file_type.cpp => detect_file_type_impl.hpp} | 8 ++++---- .../core/data/{load_image.cpp => load_image.hpp} | 16 ++++++++-------- 4 files changed, 20 insertions(+), 18 deletions(-) rename src/mlpack/core/data/{detect_file_type.cpp => detect_file_type_impl.hpp} (97%) rename src/mlpack/core/data/{load_image.cpp => load_image.hpp} (87%) diff --git a/src/mlpack/core/data/CMakeLists.txt b/src/mlpack/core/data/CMakeLists.txt index 1bce85a23f..a5c0438bf7 100644 --- a/src/mlpack/core/data/CMakeLists.txt +++ b/src/mlpack/core/data/CMakeLists.txt @@ -4,7 +4,7 @@ set(SOURCES dataset_mapper.hpp dataset_mapper_impl.hpp detect_file_type.hpp - detect_file_type.cpp + detect_file_type_impl.hpp extension.hpp format.hpp has_serialize.hpp @@ -14,7 +14,7 @@ set(SOURCES load_categorical_csv.hpp load.hpp load_image_impl.hpp - load_image.cpp + load_image.hpp load_model_impl.hpp load_vec_impl.hpp load_impl.hpp diff --git a/src/mlpack/core/data/detect_file_type.hpp b/src/mlpack/core/data/detect_file_type.hpp index 14a9fc4a6d..278ceaba0a 100644 --- a/src/mlpack/core/data/detect_file_type.hpp +++ b/src/mlpack/core/data/detect_file_type.hpp @@ -25,7 +25,7 @@ namespace data { * * @param type Type to get the logical name of. */ -std::string GetStringType(const FileType& type); +inline std::string GetStringType(const FileType& type); /** * Given an istream, attempt to guess the file type. This is taken originally @@ -38,7 +38,7 @@ std::string GetStringType(const FileType& type); * * @param f Opened istream to look into to guess the file type. */ -FileType GuessFileType(std::istream& f); +inline FileType GuessInline FileType(std::istream& f); /** * Attempt to auto-detect the type of a file given its extension, and by @@ -53,7 +53,7 @@ FileType GuessFileType(std::istream& f); * @param filename Name of the file. * @return The detected file type. arma::file_type_unknown if unknown. */ -FileType AutoDetect(std::fstream& stream, +inline FileType AutoDetect(std::fstream& stream, const std::string& filename); /** @@ -62,9 +62,11 @@ FileType AutoDetect(std::fstream& stream, * @param filename Name of the file whose type we should detect. * @return Detected type of file. arma::file_type_unknown if unknown. */ -FileType DetectFromExtension(const std::string& filename); +inline FileType DetectFromExtension(const std::string& filename); } // namespace data } // namespace mlpack +#include "detect_file_type_impl.hpp" + #endif diff --git a/src/mlpack/core/data/detect_file_type.cpp b/src/mlpack/core/data/detect_file_type_impl.hpp similarity index 97% rename from src/mlpack/core/data/detect_file_type.cpp rename to src/mlpack/core/data/detect_file_type_impl.hpp index 0219f95074..83169e606f 100644 --- a/src/mlpack/core/data/detect_file_type.cpp +++ b/src/mlpack/core/data/detect_file_type_impl.hpp @@ -24,7 +24,7 @@ namespace data { * * @param type Type to get the logical name of. */ -std::string GetStringType(const FileType& type) +inline std::string GetStringType(const FileType& type) { switch (type) { @@ -50,7 +50,7 @@ std::string GetStringType(const FileType& type) * * @param f Opened istream to look into to guess the file type. */ -FileType GuessFileType(std::istream& f) +inline FileType GuessFileType(std::istream& f) { f.clear(); const std::fstream::pos_type pos1 = f.tellg(); @@ -186,7 +186,7 @@ FileType GuessFileType(std::istream& f) * @param filename Name of the file. * @return The detected file type. */ -FileType AutoDetect(std::fstream& stream, const std::string& filename) +inline FileType AutoDetect(std::fstream& stream, const std::string& filename) { // Get the extension. std::string extension = Extension(filename); @@ -304,7 +304,7 @@ FileType AutoDetect(std::fstream& stream, const std::string& filename) * @param filename Name of the file whose type we should detect. * @return Detected type of file. */ -FileType DetectFromExtension(const std::string& filename) +inline FileType DetectFromExtension(const std::string& filename) { const std::string extension = Extension(filename); diff --git a/src/mlpack/core/data/load_image.cpp b/src/mlpack/core/data/load_image.hpp similarity index 87% rename from src/mlpack/core/data/load_image.cpp rename to src/mlpack/core/data/load_image.hpp index a25e711796..fd08def4f7 100644 --- a/src/mlpack/core/data/load_image.cpp +++ b/src/mlpack/core/data/load_image.hpp @@ -24,10 +24,10 @@ namespace mlpack { namespace data { -bool LoadImage(const std::string& filename, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal) +inline bool LoadImage(const std::string& filename, + arma::Mat& matrix, + ImageInfo& info, + const bool fatal) { unsigned char* image; @@ -104,10 +104,10 @@ bool LoadImage(const std::string& filename, namespace mlpack { namespace data { -bool LoadImage(const std::string& /* filename */, - arma::Mat& /* matrix */, - ImageInfo& /* info */, - const bool fatal) +inline bool LoadImage(const std::string& /* filename */, + arma::Mat& /* matrix */, + ImageInfo& /* info */, + const bool fatal) { if (fatal) { From 001915659d2c0508a4302409689b14f15c2ba80c Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 13 Nov 2021 18:24:15 +0000 Subject: [PATCH 03/50] Fix comments and compilation bugs Signed-off-by: Omar Shrit --- src/mlpack/core/data/detect_file_type.hpp | 2 +- src/mlpack/core/data/detect_file_type_impl.hpp | 2 +- src/mlpack/core/data/load.hpp | 10 +++++----- src/mlpack/core/data/load_image.hpp | 2 +- src/mlpack/core/data/save.hpp | 2 +- src/mlpack/core/data/save_image.hpp | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/mlpack/core/data/detect_file_type.hpp b/src/mlpack/core/data/detect_file_type.hpp index 278ceaba0a..93069c8ce6 100644 --- a/src/mlpack/core/data/detect_file_type.hpp +++ b/src/mlpack/core/data/detect_file_type.hpp @@ -38,7 +38,7 @@ inline std::string GetStringType(const FileType& type); * * @param f Opened istream to look into to guess the file type. */ -inline FileType GuessInline FileType(std::istream& f); +inline FileType GuessFileType(std::istream& f); /** * Attempt to auto-detect the type of a file given its extension, and by diff --git a/src/mlpack/core/data/detect_file_type_impl.hpp b/src/mlpack/core/data/detect_file_type_impl.hpp index 83169e606f..afb6e7fbc0 100644 --- a/src/mlpack/core/data/detect_file_type_impl.hpp +++ b/src/mlpack/core/data/detect_file_type_impl.hpp @@ -1,5 +1,5 @@ /** - * @file core/data/detect_file_type.cpp + * @file core/data/detect_file_type_impl.hpp * @author Conrad Sanderson * @author Ryan Curtin * diff --git a/src/mlpack/core/data/load.hpp b/src/mlpack/core/data/load.hpp index bd0fd7a066..e1f56c54aa 100644 --- a/src/mlpack/core/data/load.hpp +++ b/src/mlpack/core/data/load.hpp @@ -284,11 +284,11 @@ bool Load(const std::vector& files, ImageInfo& info, const bool fatal = false); -// Implementation found in load_image.cpp. -bool LoadImage(const std::string& filename, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal = false); +// Implementation found in load_image.hpp. +inline bool LoadImage(const std::string& filename, + arma::Mat& matrix, + ImageInfo& info, + const bool fatal = false); } // namespace data } // namespace mlpack diff --git a/src/mlpack/core/data/load_image.hpp b/src/mlpack/core/data/load_image.hpp index fd08def4f7..b8aabf9fcf 100644 --- a/src/mlpack/core/data/load_image.hpp +++ b/src/mlpack/core/data/load_image.hpp @@ -1,5 +1,5 @@ /** - * @file core/data/load_image.cpp + * @file core/data/load_image.hpp * @author Mehul Kumar Nirala * * Implementation of image loading functionality via STB. diff --git a/src/mlpack/core/data/save.hpp b/src/mlpack/core/data/save.hpp index 4b18a63498..bd579c9c61 100644 --- a/src/mlpack/core/data/save.hpp +++ b/src/mlpack/core/data/save.hpp @@ -161,7 +161,7 @@ bool Save(const std::vector& files, const bool fatal = false); /** - * Helper function to save files. Implementation in save_image.cpp. + * Helper function to save files. Implementation in save_image.hpp. */ inline bool SaveImage(const std::string& filename, arma::Mat& image, diff --git a/src/mlpack/core/data/save_image.hpp b/src/mlpack/core/data/save_image.hpp index edffc9712a..353ee36a7a 100644 --- a/src/mlpack/core/data/save_image.hpp +++ b/src/mlpack/core/data/save_image.hpp @@ -1,5 +1,5 @@ /** - * @file core/data/save_image.cpp + * @file core/data/save_image.hpp * @author Mehul Kumar Nirala * * Implementation of image saving functionality via STB. From 526971af9bafe13c214fb1d79e638fbbd35746f7 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 13 Nov 2021 18:43:17 +0000 Subject: [PATCH 04/50] Fix the compilation warning related to include implementations Signed-off-by: Omar Shrit --- src/mlpack/core/data/load.hpp | 2 ++ src/mlpack/core/data/save.hpp | 1 + 2 files changed, 3 insertions(+) diff --git a/src/mlpack/core/data/load.hpp b/src/mlpack/core/data/load.hpp index e1f56c54aa..d4f35954dd 100644 --- a/src/mlpack/core/data/load.hpp +++ b/src/mlpack/core/data/load.hpp @@ -301,5 +301,7 @@ inline bool LoadImage(const std::string& filename, #include "load_vec_impl.hpp" // Include implementation of Load() for images. #include "load_image_impl.hpp" +// Include implementation of Load() for images. +#include "load_image.hpp" #endif diff --git a/src/mlpack/core/data/save.hpp b/src/mlpack/core/data/save.hpp index bd579c9c61..793cf89abe 100644 --- a/src/mlpack/core/data/save.hpp +++ b/src/mlpack/core/data/save.hpp @@ -173,5 +173,6 @@ inline bool SaveImage(const std::string& filename, // Include implementation. #include "save_impl.hpp" +#include "save_image.hpp" #endif From a8f6487d9993ad0a6a4d00424b114f3fecb489e4 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 13 Nov 2021 18:56:00 +0000 Subject: [PATCH 05/50] move the impl from .cpp to .impl Signed-off-by: Omar Shrit --- src/mlpack/core/math/lin_alg.cpp | 264 +------------------------------ 1 file changed, 1 insertion(+), 263 deletions(-) diff --git a/src/mlpack/core/math/lin_alg.cpp b/src/mlpack/core/math/lin_alg.cpp index 11f4f93fcd..c628842f1f 100644 --- a/src/mlpack/core/math/lin_alg.cpp +++ b/src/mlpack/core/math/lin_alg.cpp @@ -1,8 +1,7 @@ /** * @file core/math/lin_alg.cpp - * @author Nishant Mehta + * - * Linear algebra utilities. * * 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 @@ -16,264 +15,3 @@ using namespace mlpack; using namespace math; -/** - * Auxiliary function to raise vector elements to a specific power. The sign - * is ignored in the power operation and then re-added. Useful for - * eigenvalues. - */ -void mlpack::math::VectorPower(arma::vec& vec, const double power) -{ - for (size_t i = 0; i < vec.n_elem; ++i) - { - if (std::abs(vec(i)) > 1e-12) - vec(i) = (vec(i) > 0) ? std::pow(vec(i), (double) power) : - -std::pow(-vec(i), (double) power); - else - vec(i) = 0; - } -} - -/** - * Creates a centered matrix, where centering is done by subtracting - * the sum over the columns (a column vector) from each column of the matrix. - * - * @param x Input matrix - * @param xCentered Matrix to write centered output into - */ -void mlpack::math::Center(const arma::mat& x, arma::mat& xCentered) -{ - // Get the mean of the elements in each row. - arma::vec rowMean = arma::sum(x, 1) / x.n_cols; - - xCentered = x - arma::repmat(rowMean, 1, x.n_cols); -} - -/** - * Whitens a matrix using the singular value decomposition of the covariance - * matrix. Whitening means the covariance matrix of the result is the identity - * matrix. - */ -void mlpack::math::WhitenUsingSVD(const arma::mat& x, - arma::mat& xWhitened, - arma::mat& whiteningMatrix) -{ - arma::mat covX, u, v, invSMatrix, temp1; - arma::vec sVector; - - covX = mlpack::math::ColumnCovariance(x); - - svd(u, sVector, v, covX); - - size_t d = sVector.n_elem; - invSMatrix.zeros(d, d); - invSMatrix.diag() = 1 / sqrt(sVector); - - whiteningMatrix = v * invSMatrix * trans(u); - - xWhitened = whiteningMatrix * x; -} - -/** - * Overwrites a dimension-N vector to a random vector on the unit sphere in R^N. - */ -void mlpack::math::RandVector(arma::vec& v) -{ - v.zeros(); - - for (size_t i = 0; i + 1 < v.n_elem; i += 2) - { - double a = Random(); - double b = Random(); - double first_term = sqrt(-2 * log(a)); - double second_term = 2 * M_PI * b; - v[i] = first_term * cos(second_term); - v[i + 1] = first_term * sin(second_term); - } - - if ((v.n_elem % 2) == 1) - { - v[v.n_elem - 1] = sqrt(-2 * log(math::Random())) * cos(2 * M_PI * - math::Random()); - } - - v /= sqrt(dot(v, v)); -} - -/** - * Orthogonalize x and return the result in W, using eigendecomposition. - * We will be using the formula \f$ W = x (x^T x)^{-0.5} \f$. - */ -void mlpack::math::Orthogonalize(const arma::mat& x, arma::mat& W) -{ - // For a matrix A, A^N = V * D^N * V', where VDV' is the - // eigendecomposition of the matrix A. - arma::mat eigenvalues, eigenvectors; - arma::vec egval; - eig_sym(egval, eigenvectors, mlpack::math::ColumnCovariance(x)); - VectorPower(egval, -0.5); - - eigenvalues.zeros(egval.n_elem, egval.n_elem); - eigenvalues.diag() = egval; - - arma::mat at = (eigenvectors * eigenvalues * trans(eigenvectors)); - - W = at * x; -} - -/** - * Orthogonalize x in-place. This could be sped up by a custom - * implementation. - */ -void mlpack::math::Orthogonalize(arma::mat& x) -{ - Orthogonalize(x, x); -} - -/** - * Remove a certain set of rows in a matrix while copying to a second matrix. - * - * @param input Input matrix to copy. - * @param rowsToRemove Vector containing indices of rows to be removed. - * @param output Matrix to copy non-removed rows into. - */ -void mlpack::math::RemoveRows(const arma::mat& input, - const std::vector& rowsToRemove, - arma::mat& output) -{ - const size_t nRemove = rowsToRemove.size(); - const size_t nKeep = input.n_rows - nRemove; - - if (nRemove == 0) - { - output = input; // Copy everything. - } - else - { - output.set_size(nKeep, input.n_cols); - - size_t curRow = 0; - size_t removeInd = 0; - // First, check 0 to first row to remove. - if (rowsToRemove[0] > 0) - { - // Note that this implies that n_rows > 1. - output.rows(0, rowsToRemove[0] - 1) = input.rows(0, rowsToRemove[0] - 1); - curRow += rowsToRemove[0]; - } - - // Now, check i'th row to remove to (i + 1)'th row to remove, until i is the - // penultimate row. - while (removeInd < nRemove - 1) - { - const size_t height = rowsToRemove[removeInd + 1] - - rowsToRemove[removeInd] - 1; - - if (height > 0) - { - output.rows(curRow, curRow + height - 1) = - input.rows(rowsToRemove[removeInd] + 1, - rowsToRemove[removeInd + 1] - 1); - curRow += height; - } - - removeInd++; - } - - // Now that i is the last row to remove, check last row to remove to last - // row. - if (rowsToRemove[removeInd] < input.n_rows - 1) - { - output.rows(curRow, nKeep - 1) = input.rows(rowsToRemove[removeInd] + 1, - input.n_rows - 1); - } - } -} - -void mlpack::math::Svec(const arma::mat& input, arma::vec& output) -{ - const size_t n = input.n_rows; - const size_t n2bar = n * (n + 1) / 2; - - output.zeros(n2bar); - - size_t idx = 0; - for (size_t i = 0; i < n; ++i) - { - for (size_t j = i; j < n; ++j) - { - if (i == j) - output(idx++) = input(i, j); - else - output(idx++) = M_SQRT2 * input(i, j); - } - } -} - -void mlpack::math::Svec(const arma::sp_mat& input, arma::sp_vec& output) -{ - const size_t n = input.n_rows; - const size_t n2bar = n * (n + 1) / 2; - - output.zeros(n2bar, 1); - - for (auto it = input.begin(); it != input.end(); ++it) - { - const size_t i = it.row(); - const size_t j = it.col(); - if (i > j) - continue; - if (i == j) - output(SvecIndex(i, j, n)) = *it; - else - output(SvecIndex(i, j, n)) = M_SQRT2 * (*it); - } -} - -void mlpack::math::Smat(const arma::vec& input, arma::mat& output) -{ - const size_t n = static_cast - (ceil((-1. + sqrt(1. + 8. * input.n_elem))/2.)); - - - output.zeros(n, n); - - size_t idx = 0; - for (size_t i = 0; i < n; ++i) - { - for (size_t j = i; j < n; ++j) - { - if (i == j) - output(i, j) = input(idx++); - else - output(i, j) = output(j, i) = M_SQRT1_2 * input(idx++); - } - } -} - -void mlpack::math::SymKronId(const arma::mat& A, arma::mat& op) -{ - // TODO(stephentu): there's probably an easier way to build this operator - - const size_t n = A.n_rows; - const size_t n2bar = n * (n + 1) / 2; - op.zeros(n2bar, n2bar); - - size_t idx = 0; - for (size_t i = 0; i < n; ++i) - { - for (size_t j = i; j < n; ++j) - { - for (size_t k = 0; k < n; ++k) - { - op(idx, SvecIndex(k, j, n)) += - ((k == j) ? 1. : M_SQRT1_2) * A(i, k); - op(idx, SvecIndex(i, k, n)) += - ((k == i) ? 1. : M_SQRT1_2) * A(k, j); - } - op.row(idx) *= 0.5; - if (i != j) - op.row(idx) *= M_SQRT2; - idx++; - } - } -} From 08948122f5eba51240fb24413de5ed6a72c1c4bc Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 13 Nov 2021 19:11:32 +0000 Subject: [PATCH 06/50] Adding missing headers Signed-off-by: Omar Shrit --- src/mlpack/core/math/CMakeLists.txt | 1 - src/mlpack/core/math/lin_alg.cpp | 17 -- src/mlpack/core/math/lin_alg.hpp | 31 +-- src/mlpack/core/math/lin_alg_impl.hpp | 265 ++++++++++++++++++++++++++ 4 files changed, 281 insertions(+), 33 deletions(-) delete mode 100644 src/mlpack/core/math/lin_alg.cpp diff --git a/src/mlpack/core/math/CMakeLists.txt b/src/mlpack/core/math/CMakeLists.txt index 6134d5f583..5a1aa7175b 100644 --- a/src/mlpack/core/math/CMakeLists.txt +++ b/src/mlpack/core/math/CMakeLists.txt @@ -7,7 +7,6 @@ set(SOURCES digamma.hpp lin_alg.hpp lin_alg_impl.hpp - lin_alg.cpp log_add.hpp log_add_impl.hpp make_alias.hpp diff --git a/src/mlpack/core/math/lin_alg.cpp b/src/mlpack/core/math/lin_alg.cpp deleted file mode 100644 index c628842f1f..0000000000 --- a/src/mlpack/core/math/lin_alg.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file core/math/lin_alg.cpp - - * - * - * 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. - */ -#include "lin_alg.hpp" -#include -#include - -using namespace mlpack; -using namespace math; - diff --git a/src/mlpack/core/math/lin_alg.hpp b/src/mlpack/core/math/lin_alg.hpp index 6d5ed028b7..412a803f07 100644 --- a/src/mlpack/core/math/lin_alg.hpp +++ b/src/mlpack/core/math/lin_alg.hpp @@ -13,6 +13,7 @@ #define MLPACK_CORE_MATH_LIN_ALG_HPP #include +#include "ccov.hpp" /** * Linear algebra utility functions, generally performed on matrices or vectors. @@ -25,7 +26,7 @@ namespace math { * is ignored in the power operation and then re-added. Useful for * eigenvalues. */ -void VectorPower(arma::vec& vec, const double power); +inline void VectorPower(arma::vec& vec, const double power); /** * Creates a centered matrix, where centering is done by subtracting @@ -34,34 +35,34 @@ void VectorPower(arma::vec& vec, const double power); * @param x Input matrix * @param xCentered Matrix to write centered output into */ -void Center(const arma::mat& x, arma::mat& xCentered); +inline void Center(const arma::mat& x, arma::mat& xCentered); /** * Whitens a matrix using the singular value decomposition of the covariance * matrix. Whitening means the covariance matrix of the result is the identity * matrix. */ -void WhitenUsingSVD(const arma::mat& x, - arma::mat& xWhitened, - arma::mat& whiteningMatrix); +inline void WhitenUsingSVD(const arma::mat& x, + arma::mat& xWhitened, + arma::mat& whiteningMatrix); /** * Overwrites a dimension-N vector to a random vector on the unit sphere in R^N. */ -void RandVector(arma::vec& v); +inline void RandVector(arma::vec& v); /** * Orthogonalize x and return the result in W, using eigendecomposition. * We will be using the formula \f$ W = x (x^T x)^{-0.5} \f$. */ -void Orthogonalize(const arma::mat& x, arma::mat& W); +inline void Orthogonalize(const arma::mat& x, arma::mat& W); /** * Orthogonalize x in-place. This could be sped up by a custom * implementation. */ -void Orthogonalize(arma::mat& x); +inline void Orthogonalize(arma::mat& x); /** * Remove a certain set of rows in a matrix while copying to a second matrix. @@ -70,9 +71,9 @@ void Orthogonalize(arma::mat& x); * @param rowsToRemove Vector containing indices of rows to be removed. * @param output Matrix to copy non-removed rows into. */ -void RemoveRows(const arma::mat& input, - const std::vector& rowsToRemove, - arma::mat& output); +inline void RemoveRows(const arma::mat& input, + const std::vector& rowsToRemove, + arma::mat& output); /** * Upper triangular representation of a symmetric matrix, scaled such that, @@ -83,9 +84,9 @@ void RemoveRows(const arma::mat& input, * @param input A symmetric matrix * @param output */ -void Svec(const arma::mat& input, arma::vec& output); +inline void Svec(const arma::mat& input, arma::vec& output); -void Svec(const arma::sp_mat& input, arma::sp_vec& output); +inline void Svec(const arma::sp_mat& input, arma::sp_vec& output); /** * The inverse of Svec. That is, Smat(Svec(A)) == A. @@ -93,7 +94,7 @@ void Svec(const arma::sp_mat& input, arma::sp_vec& output); * @param input * @param output A symmetric matrix */ -void Smat(const arma::vec& input, arma::mat& output); +inline void Smat(const arma::vec& input, arma::mat& output); /** * Return the index such that A[i,j] == factr(i, j) * svec(A)[pos(i, j)], @@ -115,7 +116,7 @@ inline size_t SvecIndex(size_t i, size_t j, size_t n); * @param A * @param op */ -void SymKronId(const arma::mat& A, arma::mat& op); +inline void SymKronId(const arma::mat& A, arma::mat& op); /** * Signum function. diff --git a/src/mlpack/core/math/lin_alg_impl.hpp b/src/mlpack/core/math/lin_alg_impl.hpp index f0429921d6..64cf40df96 100644 --- a/src/mlpack/core/math/lin_alg_impl.hpp +++ b/src/mlpack/core/math/lin_alg_impl.hpp @@ -1,7 +1,10 @@ /** * @file core/math/lin_alg_impl.hpp * @author Stephen Tu + * @author Nishant Mehta * + * Linear algebra utilities. + * * 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 @@ -22,6 +25,268 @@ inline size_t SvecIndex(size_t i, size_t j, size_t n) return (j-i) + (n*(n+1) - (n-i)*(n-i+1))/2; } +/** + * Auxiliary function to raise vector elements to a specific power. The sign + * is ignored in the power operation and then re-added. Useful for + * eigenvalues. + */ +inline void VectorPower(arma::vec& vec, const double power) +{ + for (size_t i = 0; i < vec.n_elem; ++i) + { + if (std::abs(vec(i)) > 1e-12) + vec(i) = (vec(i) > 0) ? std::pow(vec(i), (double) power) : + -std::pow(-vec(i), (double) power); + else + vec(i) = 0; + } +} + +/** + * Creates a centered matrix, where centering is done by subtracting + * the sum over the columns (a column vector) from each column of the matrix. + * + * @param x Input matrix + * @param xCentered Matrix to write centered output into + */ +inline void Center(const arma::mat& x, arma::mat& xCentered) +{ + // Get the mean of the elements in each row. + arma::vec rowMean = arma::sum(x, 1) / x.n_cols; + + xCentered = x - arma::repmat(rowMean, 1, x.n_cols); +} + +/** + * Whitens a matrix using the singular value decomposition of the covariance + * matrix. Whitening means the covariance matrix of the result is the identity + * matrix. + */ +inline void WhitenUsingSVD(const arma::mat& x, + arma::mat& xWhitened, + arma::mat& whiteningMatrix) +{ + arma::mat covX, u, v, invSMatrix, temp1; + arma::vec sVector; + + covX = ColumnCovariance(x); + + svd(u, sVector, v, covX); + + size_t d = sVector.n_elem; + invSMatrix.zeros(d, d); + invSMatrix.diag() = 1 / sqrt(sVector); + + whiteningMatrix = v * invSMatrix * trans(u); + + xWhitened = whiteningMatrix * x; +} + +/** + * Overwrites a dimension-N vector to a random vector on the unit sphere in R^N. + */ +inline void RandVector(arma::vec& v) +{ + v.zeros(); + + for (size_t i = 0; i + 1 < v.n_elem; i += 2) + { + double a = Random(); + double b = Random(); + double first_term = sqrt(-2 * log(a)); + double second_term = 2 * M_PI * b; + v[i] = first_term * cos(second_term); + v[i + 1] = first_term * sin(second_term); + } + + if ((v.n_elem % 2) == 1) + { + v[v.n_elem - 1] = sqrt(-2 * log(math::Random())) * cos(2 * M_PI * + math::Random()); + } + + v /= sqrt(dot(v, v)); +} + +/** + * Orthogonalize x and return the result in W, using eigendecomposition. + * We will be using the formula \f$ W = x (x^T x)^{-0.5} \f$. + */ +inline void Orthogonalize(const arma::mat& x, arma::mat& W) +{ + // For a matrix A, A^N = V * D^N * V', where VDV' is the + // eigendecomposition of the matrix A. + arma::mat eigenvalues, eigenvectors; + arma::vec egval; + eig_sym(egval, eigenvectors, ColumnCovariance(x)); + VectorPower(egval, -0.5); + + eigenvalues.zeros(egval.n_elem, egval.n_elem); + eigenvalues.diag() = egval; + + arma::mat at = (eigenvectors * eigenvalues * trans(eigenvectors)); + + W = at * x; +} + +/** + * Orthogonalize x in-place. This could be sped up by a custom + * implementation. + */ +inline void Orthogonalize(arma::mat& x) +{ + Orthogonalize(x, x); +} + +/** + * Remove a certain set of rows in a matrix while copying to a second matrix. + * + * @param input Input matrix to copy. + * @param rowsToRemove Vector containing indices of rows to be removed. + * @param output Matrix to copy non-removed rows into. + */ +inline void RemoveRows(const arma::mat& input, + const std::vector& rowsToRemove, + arma::mat& output) +{ + const size_t nRemove = rowsToRemove.size(); + const size_t nKeep = input.n_rows - nRemove; + + if (nRemove == 0) + { + output = input; // Copy everything. + } + else + { + output.set_size(nKeep, input.n_cols); + + size_t curRow = 0; + size_t removeInd = 0; + // First, check 0 to first row to remove. + if (rowsToRemove[0] > 0) + { + // Note that this implies that n_rows > 1. + output.rows(0, rowsToRemove[0] - 1) = input.rows(0, rowsToRemove[0] - 1); + curRow += rowsToRemove[0]; + } + + // Now, check i'th row to remove to (i + 1)'th row to remove, until i is the + // penultimate row. + while (removeInd < nRemove - 1) + { + const size_t height = rowsToRemove[removeInd + 1] - + rowsToRemove[removeInd] - 1; + + if (height > 0) + { + output.rows(curRow, curRow + height - 1) = + input.rows(rowsToRemove[removeInd] + 1, + rowsToRemove[removeInd + 1] - 1); + curRow += height; + } + + removeInd++; + } + + // Now that i is the last row to remove, check last row to remove to last + // row. + if (rowsToRemove[removeInd] < input.n_rows - 1) + { + output.rows(curRow, nKeep - 1) = input.rows(rowsToRemove[removeInd] + 1, + input.n_rows - 1); + } + } +} + +inline void Svec(const arma::mat& input, arma::vec& output) +{ + const size_t n = input.n_rows; + const size_t n2bar = n * (n + 1) / 2; + + output.zeros(n2bar); + + size_t idx = 0; + for (size_t i = 0; i < n; ++i) + { + for (size_t j = i; j < n; ++j) + { + if (i == j) + output(idx++) = input(i, j); + else + output(idx++) = M_SQRT2 * input(i, j); + } + } +} + +inline void Svec(const arma::sp_mat& input, arma::sp_vec& output) +{ + const size_t n = input.n_rows; + const size_t n2bar = n * (n + 1) / 2; + + output.zeros(n2bar, 1); + + for (auto it = input.begin(); it != input.end(); ++it) + { + const size_t i = it.row(); + const size_t j = it.col(); + if (i > j) + continue; + if (i == j) + output(SvecIndex(i, j, n)) = *it; + else + output(SvecIndex(i, j, n)) = M_SQRT2 * (*it); + } +} + +inline void Smat(const arma::vec& input, arma::mat& output) +{ + const size_t n = static_cast + (ceil((-1. + sqrt(1. + 8. * input.n_elem))/2.)); + + + output.zeros(n, n); + + size_t idx = 0; + for (size_t i = 0; i < n; ++i) + { + for (size_t j = i; j < n; ++j) + { + if (i == j) + output(i, j) = input(idx++); + else + output(i, j) = output(j, i) = M_SQRT1_2 * input(idx++); + } + } +} + +inline void SymKronId(const arma::mat& A, arma::mat& op) +{ + // TODO(stephentu): there's probably an easier way to build this operator + + const size_t n = A.n_rows; + const size_t n2bar = n * (n + 1) / 2; + op.zeros(n2bar, n2bar); + + size_t idx = 0; + for (size_t i = 0; i < n; ++i) + { + for (size_t j = i; j < n; ++j) + { + for (size_t k = 0; k < n; ++k) + { + op(idx, SvecIndex(k, j, n)) += + ((k == j) ? 1. : M_SQRT1_2) * A(i, k); + op(idx, SvecIndex(i, k, n)) += + ((k == i) ? 1. : M_SQRT1_2) * A(k, j); + } + op.row(idx) *= 0.5; + if (i != j) + op.row(idx) *= M_SQRT2; + idx++; + } + } +} + } // namespace math } // namespace mlpack From 3e165bfc3e09dbb0c32f5aa79b7dce61cb019a88 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 13 Nov 2021 19:15:58 +0000 Subject: [PATCH 07/50] inline random_basis Signed-off-by: Omar Shrit --- src/mlpack/core/math/random_basis.hpp | 5 ++++- .../core/math/{random_basis.cpp => random_basis_impl.hpp} | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) rename src/mlpack/core/math/{random_basis.cpp => random_basis_impl.hpp} (95%) diff --git a/src/mlpack/core/math/random_basis.hpp b/src/mlpack/core/math/random_basis.hpp index 6d605b846e..75c9b1eef1 100644 --- a/src/mlpack/core/math/random_basis.hpp +++ b/src/mlpack/core/math/random_basis.hpp @@ -24,9 +24,12 @@ namespace math { * @param basis Matrix to store basis in. * @param d Desired number of dimensions in the basis. */ -void RandomBasis(arma::mat& basis, const size_t d); +inline void RandomBasis(arma::mat& basis, const size_t d); } // namespace math } // namespace mlpack +//! Include the implementation file +#include "random_basis_impl.hpp" + #endif diff --git a/src/mlpack/core/math/random_basis.cpp b/src/mlpack/core/math/random_basis_impl.hpp similarity index 95% rename from src/mlpack/core/math/random_basis.cpp rename to src/mlpack/core/math/random_basis_impl.hpp index f891b1e2eb..80cab9dbbc 100644 --- a/src/mlpack/core/math/random_basis.cpp +++ b/src/mlpack/core/math/random_basis_impl.hpp @@ -16,7 +16,7 @@ using namespace arma; namespace mlpack { namespace math { -void RandomBasis(mat& basis, const size_t d) +inline void RandomBasis(mat& basis, const size_t d) { while (true) { From 7558d383f7e5bb40a84c42f026bed40397aab633 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 13 Nov 2021 19:23:57 +0000 Subject: [PATCH 08/50] remove epanechnikov_kernel Signed-off-by: Omar Shrit --- .../core/kernels/epanechnikov_kernel.cpp | 63 ------------------- 1 file changed, 63 deletions(-) diff --git a/src/mlpack/core/kernels/epanechnikov_kernel.cpp b/src/mlpack/core/kernels/epanechnikov_kernel.cpp index c245200a76..193ad622c4 100644 --- a/src/mlpack/core/kernels/epanechnikov_kernel.cpp +++ b/src/mlpack/core/kernels/epanechnikov_kernel.cpp @@ -14,67 +14,4 @@ using namespace mlpack; using namespace mlpack::kernel; -/** - * Compute the normalizer of this Epanechnikov kernel for the given dimension. - * - * @param dimension Dimension to calculate the normalizer for. - */ -double EpanechnikovKernel::Normalizer(const size_t dimension) -{ - return 2.0 * pow(bandwidth, (double) dimension) * - std::pow(M_PI, dimension / 2.0) / - (std::tgamma(dimension / 2.0 + 1.0) * (dimension + 2.0)); -} -/** - * Evaluate the kernel not for two points but for a numerical value. - */ -double EpanechnikovKernel::Evaluate(const double distance) const -{ - return std::max(0.0, 1 - std::pow(distance, 2.0) * inverseBandwidthSquared); -} - -/** - * Evaluate gradient of the kernel not for two points - * but for a numerical value. - */ -double EpanechnikovKernel::Gradient(const double distance) const -{ - if (std::abs(bandwidth) < std::abs(distance)) - { - return 0; - } - else if (std::abs(bandwidth) > std::abs(distance)) - { - return -2 * inverseBandwidthSquared * distance; - } - else - { - // The gradient doesn't exist. - return arma::datum::nan; - } -} - -/** - * Evaluate gradient of the kernel not for two points - * but for a numerical value. - */ -double EpanechnikovKernel::GradientForSquaredDistance(const double - distanceSquared) const -{ - double bandwidthSquared = bandwidth * bandwidth; - if (distanceSquared < bandwidthSquared) - { - return -1 * inverseBandwidthSquared; - } - else if (distanceSquared > bandwidthSquared && - distanceSquared >= 0) - { - return 0; - } - else - { - // The gradient doesn't exist. - return arma::datum::nan; - } -} From 7433fae40a623df57e391cdb1ba19a92d710bd86 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 13 Nov 2021 19:33:05 +0000 Subject: [PATCH 09/50] Finishing the epanechnikov_kernel Signed-off-by: Omar Shrit --- src/mlpack/core/data/save_impl.hpp | 1 - src/mlpack/core/kernels/CMakeLists.txt | 1 - .../core/kernels/epanechnikov_kernel.hpp | 8 +-- .../core/kernels/epanechnikov_kernel_impl.hpp | 68 ++++++++++++++++++- src/mlpack/core/math/CMakeLists.txt | 2 +- src/mlpack/core/math/random_basis_impl.hpp | 2 +- 6 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/mlpack/core/data/save_impl.hpp b/src/mlpack/core/data/save_impl.hpp index 18a353dbc6..4542b1c6e7 100644 --- a/src/mlpack/core/data/save_impl.hpp +++ b/src/mlpack/core/data/save_impl.hpp @@ -361,7 +361,6 @@ bool Save(const std::string& filename, arma::Mat tmpMatrix = arma::conv_to>::from(matrix); - // Call out to .cpp implementation. return SaveImage(filename, tmpMatrix, info, fatal); } diff --git a/src/mlpack/core/kernels/CMakeLists.txt b/src/mlpack/core/kernels/CMakeLists.txt index fa77bd7beb..a0e1f6d9c5 100644 --- a/src/mlpack/core/kernels/CMakeLists.txt +++ b/src/mlpack/core/kernels/CMakeLists.txt @@ -6,7 +6,6 @@ set(SOURCES cosine_distance_impl.hpp epanechnikov_kernel.hpp epanechnikov_kernel_impl.hpp - epanechnikov_kernel.cpp example_kernel.hpp gaussian_kernel.hpp hyperbolic_tangent_kernel.hpp diff --git a/src/mlpack/core/kernels/epanechnikov_kernel.hpp b/src/mlpack/core/kernels/epanechnikov_kernel.hpp index d0c6b17b24..482b80c028 100644 --- a/src/mlpack/core/kernels/epanechnikov_kernel.hpp +++ b/src/mlpack/core/kernels/epanechnikov_kernel.hpp @@ -55,21 +55,21 @@ class EpanechnikovKernel * Evaluate the Epanechnikov kernel given that the distance between the two * input points is known. */ - double Evaluate(const double distance) const; + inline double Evaluate(const double distance) const; /** * Evaluate the Gradient of Epanechnikov kernel * given that the distance between the two * input points is known. */ - double Gradient(const double distance) const; + inline double Gradient(const double distance) const; /** * Evaluate the Gradient of Epanechnikov kernel * given that the squared distance between the two * input points is known. */ - double GradientForSquaredDistance(const double distanceSquared) const; + inline double GradientForSquaredDistance(const double distanceSquared) const; /** * Obtains the convolution integral [integral of K(||x-a||) K(||b-x||) dx] * for the two vectors. @@ -87,7 +87,7 @@ class EpanechnikovKernel * * @param dimension Dimension to calculate the normalizer for. */ - double Normalizer(const size_t dimension); + inline double Normalizer(const size_t dimension); /** * Serialize the kernel. diff --git a/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp b/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp index eee34daaee..21512e14af 100644 --- a/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp +++ b/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp @@ -41,8 +41,8 @@ inline double EpanechnikovKernel::Evaluate(const VecTypeA& a, const VecTypeB& b) * @return the convolution integral value. */ template -double EpanechnikovKernel::ConvolutionIntegral(const VecTypeA& a, - const VecTypeB& b) +inline double EpanechnikovKernel::ConvolutionIntegral(const VecTypeA& a, + const VecTypeB& b) { double distance = sqrt(metric::SquaredEuclideanDistance::Evaluate(a, b)); if (distance >= 2.0 * bandwidth) @@ -73,6 +73,70 @@ double EpanechnikovKernel::ConvolutionIntegral(const VecTypeA& a, } } +/** + * Compute the normalizer of this Epanechnikov kernel for the given dimension. + * + * @param dimension Dimension to calculate the normalizer for. + */ +inline double EpanechnikovKernel::Normalizer(const size_t dimension) +{ + return 2.0 * pow(bandwidth, (double) dimension) * + std::pow(M_PI, dimension / 2.0) / + (std::tgamma(dimension / 2.0 + 1.0) * (dimension + 2.0)); +} + +/** + * Evaluate the kernel not for two points but for a numerical value. + */ +inline double EpanechnikovKernel::Evaluate(const double distance) const +{ + return std::max(0.0, 1 - std::pow(distance, 2.0) * inverseBandwidthSquared); +} + +/** + * Evaluate gradient of the kernel not for two points + * but for a numerical value. + */ +inline double EpanechnikovKernel::Gradient(const double distance) const +{ + if (std::abs(bandwidth) < std::abs(distance)) + { + return 0; + } + else if (std::abs(bandwidth) > std::abs(distance)) + { + return -2 * inverseBandwidthSquared * distance; + } + else + { + // The gradient doesn't exist. + return arma::datum::nan; + } +} + +/** + * Evaluate gradient of the kernel not for two points + * but for a numerical value. + */ +inline double EpanechnikovKernel::GradientForSquaredDistance(const double + distanceSquared) const +{ + double bandwidthSquared = bandwidth * bandwidth; + if (distanceSquared < bandwidthSquared) + { + return -1 * inverseBandwidthSquared; + } + else if (distanceSquared > bandwidthSquared && + distanceSquared >= 0) + { + return 0; + } + else + { + // The gradient doesn't exist. + return arma::datum::nan; + } +} //! Serialize the kernel. template void EpanechnikovKernel::serialize(Archive& ar, diff --git a/src/mlpack/core/math/CMakeLists.txt b/src/mlpack/core/math/CMakeLists.txt index 5a1aa7175b..873fd1d13b 100644 --- a/src/mlpack/core/math/CMakeLists.txt +++ b/src/mlpack/core/math/CMakeLists.txt @@ -15,7 +15,7 @@ set(SOURCES random.hpp random.cpp random_basis.hpp - random_basis.cpp + random_basis_impl.hpp range.hpp range_impl.hpp round.hpp diff --git a/src/mlpack/core/math/random_basis_impl.hpp b/src/mlpack/core/math/random_basis_impl.hpp index 80cab9dbbc..08df445bad 100644 --- a/src/mlpack/core/math/random_basis_impl.hpp +++ b/src/mlpack/core/math/random_basis_impl.hpp @@ -1,5 +1,5 @@ /** - * @file core/math/random_basis.cpp + * @file core/math/random_basis_impl.hpp * @author Ryan Curtin * * Generate a random d-dimensional basis. From c2c17b59dca1078b0b912a4922a413882f5b6eaa Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 13 Nov 2021 19:39:32 +0000 Subject: [PATCH 10/50] Deleting epanechnikov_kernel and adding pspectrum_string_kernel Signed-off-by: Omar Shrit --- .../core/kernels/epanechnikov_kernel.cpp | 17 ------ .../core/kernels/pspectrum_string_kernel.cpp | 57 +------------------ 2 files changed, 1 insertion(+), 73 deletions(-) delete mode 100644 src/mlpack/core/kernels/epanechnikov_kernel.cpp diff --git a/src/mlpack/core/kernels/epanechnikov_kernel.cpp b/src/mlpack/core/kernels/epanechnikov_kernel.cpp deleted file mode 100644 index 193ad622c4..0000000000 --- a/src/mlpack/core/kernels/epanechnikov_kernel.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file core/kernels/epanechnikov_kernel.cpp - * @author Neil Slagle - * - * Implementation of non-template Epanechnikov kernels. - * - * 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. - */ -#include "epanechnikov_kernel.hpp" - -using namespace mlpack; -using namespace mlpack::kernel; - - diff --git a/src/mlpack/core/kernels/pspectrum_string_kernel.cpp b/src/mlpack/core/kernels/pspectrum_string_kernel.cpp index f83eb0c444..33b6897f87 100644 --- a/src/mlpack/core/kernels/pspectrum_string_kernel.cpp +++ b/src/mlpack/core/kernels/pspectrum_string_kernel.cpp @@ -28,60 +28,5 @@ using namespace mlpack::kernel; */ mlpack::kernel::PSpectrumStringKernel::PSpectrumStringKernel( const std::vector >& datasets, - const size_t p) : - p(p) -{ - // We have to assemble the counts of substrings. This is not a particularly - // fast operation, unfortunately, but it only needs to be done once. - Log::Info << "Assembling counts of substrings of length " << p << "." - << std::endl; + const size_t p) - // Resize for number of datasets. - counts.resize(datasets.size()); - - for (size_t dataset = 0; dataset < datasets.size(); ++dataset) - { - const std::vector& set = datasets[dataset]; - - // Resize for number of strings in dataset. - counts[dataset].resize(set.size()); - - // Inspect each string in the dataset. - for (size_t index = 0; index < set.size(); ++index) - { - // Convenience references. - const std::string& str = set[index]; - std::map& mapping = counts[dataset][index]; - - size_t start = 0; - while ((start + p) <= str.length()) - { - string sub = str.substr(start, p); - - // Convert all characters to lowercase. - bool invalid = false; - for (size_t j = 0; j < p; ++j) - { - if (!isalnum(sub[j])) - { - invalid = true; - break; // Only consider substrings with alphanumerics. - } - - sub[j] = tolower(sub[j]); - } - - // Increment position in string. - ++start; - - if (!invalid) - { - // Add to the map. - ++mapping[sub]; - } - } - } - } - - Log::Info << "Substring extraction complete." << std::endl; -} From 71ebcbcf031c924949506005c5c0fe9cbb9be8b6 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 13 Nov 2021 19:40:23 +0000 Subject: [PATCH 11/50] Finish the pspectrum_string_kernel Signed-off-by: Omar Shrit --- src/mlpack/core/kernels/CMakeLists.txt | 1 - .../core/kernels/pspectrum_string_kernel.cpp | 32 ----------- .../core/kernels/pspectrum_string_kernel.hpp | 56 ++++++++++++++++++- 3 files changed, 55 insertions(+), 34 deletions(-) delete mode 100644 src/mlpack/core/kernels/pspectrum_string_kernel.cpp diff --git a/src/mlpack/core/kernels/CMakeLists.txt b/src/mlpack/core/kernels/CMakeLists.txt index a0e1f6d9c5..06c5a93c69 100644 --- a/src/mlpack/core/kernels/CMakeLists.txt +++ b/src/mlpack/core/kernels/CMakeLists.txt @@ -15,7 +15,6 @@ set(SOURCES polynomial_kernel.hpp pspectrum_string_kernel.hpp pspectrum_string_kernel_impl.hpp - pspectrum_string_kernel.cpp spherical_kernel.hpp triangular_kernel.hpp ) diff --git a/src/mlpack/core/kernels/pspectrum_string_kernel.cpp b/src/mlpack/core/kernels/pspectrum_string_kernel.cpp deleted file mode 100644 index 33b6897f87..0000000000 --- a/src/mlpack/core/kernels/pspectrum_string_kernel.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/** - * @file core/kernels/pspectrum_string_kernel.cpp - * @author Ryan Curtin - * - * Implementation of the p-spectrum string kernel, created for use with FastMKS. - * Instead of passing a data matrix to FastMKS which stores the kernels, pass a - * one-dimensional data matrix (data vector) to FastMKS which stores indices of - * strings; then, the actual strings are given to the PSpectrumStringKernel at - * construction time, and the kernel knows to map the indices to actual strings. - * - * 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. - */ -#include "pspectrum_string_kernel.hpp" - -using namespace std; -using namespace mlpack; -using namespace mlpack::kernel; - -/** - * Initialize the PSpectrumStringKernel with the given string datasets. For - * more information on this, see the general class documentation. - * - * @param datasets Sets of string data. @param p The length of substrings to - * search. - */ -mlpack::kernel::PSpectrumStringKernel::PSpectrumStringKernel( - const std::vector >& datasets, - const size_t p) - diff --git a/src/mlpack/core/kernels/pspectrum_string_kernel.hpp b/src/mlpack/core/kernels/pspectrum_string_kernel.hpp index 9e8f74af23..2cbaf30630 100644 --- a/src/mlpack/core/kernels/pspectrum_string_kernel.hpp +++ b/src/mlpack/core/kernels/pspectrum_string_kernel.hpp @@ -73,8 +73,62 @@ class PSpectrumStringKernel * @param p The length of substrings to search. */ PSpectrumStringKernel(const std::vector >& datasets, - const size_t p); + const size_t p) : p(p) + { + // We have to assemble the counts of substrings. This is not a particularly + // fast operation, unfortunately, but it only needs to be done once. + Log::Info << "Assembling counts of substrings of length " << p << "." + << std::endl; + // Resize for number of datasets. + counts.resize(datasets.size()); + + for (size_t dataset = 0; dataset < datasets.size(); ++dataset) + { + const std::vector& set = datasets[dataset]; + + // Resize for number of strings in dataset. + counts[dataset].resize(set.size()); + + // Inspect each string in the dataset. + for (size_t index = 0; index < set.size(); ++index) + { + // Convenience references. + const std::string& str = set[index]; + std::map& mapping = counts[dataset][index]; + + size_t start = 0; + while ((start + p) <= str.length()) + { + string sub = str.substr(start, p); + + // Convert all characters to lowercase. + bool invalid = false; + for (size_t j = 0; j < p; ++j) + { + if (!isalnum(sub[j])) + { + invalid = true; + break; // Only consider substrings with alphanumerics. + } + + sub[j] = tolower(sub[j]); + } + + // Increment position in string. + ++start; + + if (!invalid) + { + // Add to the map. + ++mapping[sub]; + } + } + } + } + + Log::Info << "Substring extraction complete." << std::endl; + } /** * Evaluate the kernel for the string indices given. As mentioned in the * class documentation, a and b should be 2-element vectors, where the first From da95b27f9c800b8294efe13e6459f6f0df8e3073 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 13 Nov 2021 19:52:19 +0000 Subject: [PATCH 12/50] Adding a missing std string Signed-off-by: Omar Shrit --- src/mlpack/core/kernels/pspectrum_string_kernel.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/core/kernels/pspectrum_string_kernel.hpp b/src/mlpack/core/kernels/pspectrum_string_kernel.hpp index 2cbaf30630..3fe346b45e 100644 --- a/src/mlpack/core/kernels/pspectrum_string_kernel.hpp +++ b/src/mlpack/core/kernels/pspectrum_string_kernel.hpp @@ -100,7 +100,7 @@ class PSpectrumStringKernel size_t start = 0; while ((start + p) <= str.length()) { - string sub = str.substr(start, p); + std::string sub = str.substr(start, p); // Convert all characters to lowercase. bool invalid = false; From 82fc871aaa66b5bcce3508e3f652d9b87b9290d8 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 20 Nov 2021 17:06:11 +0000 Subject: [PATCH 13/50] Let us if this resolves the binding issues Signed-off-by: Omar Shrit --- src/mlpack/core/math/lin_alg.hpp | 1 + src/mlpack/core/math/lin_alg_impl.hpp | 4 ++-- src/mlpack/methods/cf/cf_main.cpp | 5 +++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/mlpack/core/math/lin_alg.hpp b/src/mlpack/core/math/lin_alg.hpp index 412a803f07..7452e2ed4b 100644 --- a/src/mlpack/core/math/lin_alg.hpp +++ b/src/mlpack/core/math/lin_alg.hpp @@ -14,6 +14,7 @@ #include #include "ccov.hpp" +#include "random.hpp" /** * Linear algebra utility functions, generally performed on matrices or vectors. diff --git a/src/mlpack/core/math/lin_alg_impl.hpp b/src/mlpack/core/math/lin_alg_impl.hpp index 64cf40df96..1a0408e3c4 100644 --- a/src/mlpack/core/math/lin_alg_impl.hpp +++ b/src/mlpack/core/math/lin_alg_impl.hpp @@ -91,8 +91,8 @@ inline void RandVector(arma::vec& v) for (size_t i = 0; i + 1 < v.n_elem; i += 2) { - double a = Random(); - double b = Random(); + double a = math::Random(); + double b = math::Random(); double first_term = sqrt(-2 * log(a)); double second_term = 2 * M_PI * b; v[i] = first_term * cos(second_term); diff --git a/src/mlpack/methods/cf/cf_main.cpp b/src/mlpack/methods/cf/cf_main.cpp index d756aedabe..3df5caf161 100644 --- a/src/mlpack/methods/cf/cf_main.cpp +++ b/src/mlpack/methods/cf/cf_main.cpp @@ -40,6 +40,7 @@ #include using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::cf; using namespace mlpack::amf; using namespace mlpack::svd; @@ -202,9 +203,9 @@ PARAM_STRING_IN("neighbor_search", "Algorithm used for neighbor search.", void BINDING_FUNCTION(util::Params& params, util::Timers& timers) { if (params.Get("seed") == 0) - math::RandomSeed(std::time(NULL)); + RandomSeed(std::time(NULL)); else - math::RandomSeed(params.Get("seed")); + RandomSeed(params.Get("seed")); // Validate parameters. RequireOnlyOnePassed(params, { "training", "input_model" }, true); From e62d097e27cf0fcbfadc16c080754f644de190ca Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 20 Nov 2021 19:29:07 +0000 Subject: [PATCH 14/50] Fix the gmm error Signed-off-by: Omar Shrit --- src/mlpack/methods/gmm/gmm_train_main.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/gmm/gmm_train_main.cpp b/src/mlpack/methods/gmm/gmm_train_main.cpp index e1ebf6a12c..ebeb2b074e 100644 --- a/src/mlpack/methods/gmm/gmm_train_main.cpp +++ b/src/mlpack/methods/gmm/gmm_train_main.cpp @@ -18,6 +18,7 @@ #define BINDING_NAME gmm_train #include +#include #include "gmm.hpp" #include "diagonal_gmm.hpp" @@ -27,6 +28,7 @@ #include using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::gmm; using namespace mlpack::util; using namespace mlpack::kmeans; @@ -155,9 +157,9 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) { // Check parameters and load data. if (params.Get("seed") != 0) - math::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); else - math::RandomSeed((size_t) std::time(NULL)); + RandomSeed((size_t) std::time(NULL)); RequireParamValue(params, "gaussians", [](int x) { return x > 0; }, true, "number of Gaussians must be positive"); From 880ea8fbc3004143912145aca367ca8ac143453b Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 20 Nov 2021 19:56:14 +0000 Subject: [PATCH 15/50] Adding missing math/random headers Signed-off-by: Omar Shrit --- src/mlpack/methods/gmm/gmm_generate_main.cpp | 6 ++++-- src/mlpack/methods/kmeans/kmeans_main.cpp | 6 ++++-- src/mlpack/methods/linear_svm/linear_svm_main.cpp | 5 +++-- src/mlpack/methods/lmnn/lmnn_main.cpp | 5 +++-- src/mlpack/methods/lsh/lsh_main.cpp | 7 ++++--- src/mlpack/methods/nca/nca_main.cpp | 5 +++-- src/mlpack/methods/neighbor_search/kfn_main.cpp | 6 ++++-- src/mlpack/methods/neighbor_search/knn_main.cpp | 6 ++++-- src/mlpack/methods/nmf/nmf_main.cpp | 6 ++++-- src/mlpack/methods/preprocess/preprocess_scale_main.cpp | 5 +++-- src/mlpack/methods/preprocess/preprocess_split_main.cpp | 5 +++-- src/mlpack/methods/random_forest/random_forest_main.cpp | 6 ++++-- src/mlpack/methods/range_search/range_search_main.cpp | 6 ++++-- src/mlpack/methods/rann/krann_main.cpp | 9 +++++---- 14 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/mlpack/methods/gmm/gmm_generate_main.cpp b/src/mlpack/methods/gmm/gmm_generate_main.cpp index b270cfa5e2..72fe415155 100644 --- a/src/mlpack/methods/gmm/gmm_generate_main.cpp +++ b/src/mlpack/methods/gmm/gmm_generate_main.cpp @@ -11,6 +11,7 @@ */ #include #include +#include #ifdef BINDING_NAME #undef BINDING_NAME @@ -22,6 +23,7 @@ using namespace std; using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::gmm; using namespace mlpack::util; @@ -74,9 +76,9 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& /* timers */) "no results will be saved"); if (params.Get("seed") == 0) - mlpack::math::RandomSeed(time(NULL)); + mlpack::RandomSeed(time(NULL)); else - mlpack::math::RandomSeed((size_t) params.Get("seed")); + mlpack::RandomSeed((size_t) params.Get("seed")); RequireParamValue(params, "samples", [](int x) { return x > 0; }, true, "number of samples must be greater than 0"); diff --git a/src/mlpack/methods/kmeans/kmeans_main.cpp b/src/mlpack/methods/kmeans/kmeans_main.cpp index 67b60a8d79..7b32db8171 100644 --- a/src/mlpack/methods/kmeans/kmeans_main.cpp +++ b/src/mlpack/methods/kmeans/kmeans_main.cpp @@ -11,6 +11,7 @@ */ #include #include +#include #ifdef BINDING_NAME #undef BINDING_NAME @@ -30,6 +31,7 @@ #include "dual_tree_kmeans.hpp" using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::kmeans; using namespace mlpack::util; using namespace std; @@ -191,9 +193,9 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) { // Initialize random seed. if (params.Get("seed") != 0) - math::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); else - math::RandomSeed((size_t) std::time(NULL)); + RandomSeed((size_t) std::time(NULL)); RequireOnlyOnePassed(params, { "refined_start", "kmeans_plus_plus" }, true, "Only one initialization strategy can be specified!", true); diff --git a/src/mlpack/methods/linear_svm/linear_svm_main.cpp b/src/mlpack/methods/linear_svm/linear_svm_main.cpp index 4dd20021b1..a037ec2271 100644 --- a/src/mlpack/methods/linear_svm/linear_svm_main.cpp +++ b/src/mlpack/methods/linear_svm/linear_svm_main.cpp @@ -26,6 +26,7 @@ using namespace std; using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::svm; using namespace mlpack::util; @@ -178,9 +179,9 @@ PARAM_MATRIX_OUT("probabilities", "If test data is specified, this " void BINDING_FUNCTION(util::Params& params, util::Timers& timers) { if (params.Get("seed") != 0) - math::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); else - math::RandomSeed((size_t) std::time(NULL)); + RandomSeed((size_t) std::time(NULL)); // Collect command-line options. const double lambda = params.Get("lambda"); diff --git a/src/mlpack/methods/lmnn/lmnn_main.cpp b/src/mlpack/methods/lmnn/lmnn_main.cpp index 18c430b63a..beea8339a8 100644 --- a/src/mlpack/methods/lmnn/lmnn_main.cpp +++ b/src/mlpack/methods/lmnn/lmnn_main.cpp @@ -187,6 +187,7 @@ PARAM_INT_IN("range", "Number of iterations after which impostors needs to be " PARAM_INT_IN("seed", "Random seed. If 0, 'std::time(NULL)' is used.", "s", 0); using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::lmnn; using namespace mlpack::metric; using namespace mlpack::util; @@ -238,9 +239,9 @@ double KNNAccuracy(const arma::mat& dataset, void BINDING_FUNCTION(util::Params& params, util::Timers& timers) { if (params.Get("seed") != 0) - math::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); else - math::RandomSeed((size_t) std::time(NULL)); + RandomSeed((size_t) std::time(NULL)); RequireAtLeastOnePassed(params, { "output" }, false, "no output will be saved"); diff --git a/src/mlpack/methods/lsh/lsh_main.cpp b/src/mlpack/methods/lsh/lsh_main.cpp index 66d93a46cd..48365e5a27 100644 --- a/src/mlpack/methods/lsh/lsh_main.cpp +++ b/src/mlpack/methods/lsh/lsh_main.cpp @@ -12,6 +12,7 @@ */ #include #include +#include #ifdef BINDING_NAME #undef BINDING_NAME @@ -19,13 +20,13 @@ #define BINDING_NAME lsh #include - #include #include "lsh_search.hpp" using namespace std; using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::neighbor; using namespace mlpack::util; @@ -114,9 +115,9 @@ PARAM_INT_IN("seed", "Random seed. If 0, 'std::time(NULL)' is used.", "s", 0); void BINDING_FUNCTION(util::Params& params, util::Timers& timers) { if (params.Get("seed") != 0) - math::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); else - math::RandomSeed((size_t) time(NULL)); + RandomSeed((size_t) time(NULL)); // Get all the parameters after checking them. if (params.Has("k")) diff --git a/src/mlpack/methods/nca/nca_main.cpp b/src/mlpack/methods/nca/nca_main.cpp index 44bb94dc3e..79b22e6f15 100644 --- a/src/mlpack/methods/nca/nca_main.cpp +++ b/src/mlpack/methods/nca/nca_main.cpp @@ -144,6 +144,7 @@ PARAM_DOUBLE_IN("max_step", "Maximum step of line search for L-BFGS.", "M", PARAM_INT_IN("seed", "Random seed. If 0, 'std::time(NULL)' is used.", "s", 0); using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::nca; using namespace mlpack::metric; using namespace mlpack::util; @@ -152,9 +153,9 @@ using namespace std; void BINDING_FUNCTION(util::Params& params, util::Timers& timers) { if (params.Get("seed") != 0) - math::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); else - math::RandomSeed((size_t) std::time(NULL)); + RandomSeed((size_t) std::time(NULL)); RequireAtLeastOnePassed(params, { "output" }, false, "no output will be saved"); diff --git a/src/mlpack/methods/neighbor_search/kfn_main.cpp b/src/mlpack/methods/neighbor_search/kfn_main.cpp index a0a902ddb9..0b6c1b01c9 100644 --- a/src/mlpack/methods/neighbor_search/kfn_main.cpp +++ b/src/mlpack/methods/neighbor_search/kfn_main.cpp @@ -12,6 +12,7 @@ */ #include #include +#include #ifdef BINDING_NAME #undef BINDING_NAME @@ -30,6 +31,7 @@ using namespace std; using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::neighbor; using namespace mlpack::tree; using namespace mlpack::metric; @@ -124,9 +126,9 @@ PARAM_DOUBLE_IN("percentage", "If specified, will do approximate furthest " void BINDING_FUNCTION(util::Params& params, util::Timers& timers) { if (params.Get("seed") != 0) - math::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); else - math::RandomSeed((size_t) std::time(NULL)); + RandomSeed((size_t) std::time(NULL)); // A user cannot specify both reference data and a model. RequireOnlyOnePassed(params, { "reference", "input_model" }, true); diff --git a/src/mlpack/methods/neighbor_search/knn_main.cpp b/src/mlpack/methods/neighbor_search/knn_main.cpp index 9a071caf3b..5ade397868 100644 --- a/src/mlpack/methods/neighbor_search/knn_main.cpp +++ b/src/mlpack/methods/neighbor_search/knn_main.cpp @@ -12,6 +12,7 @@ */ #include #include +#include #ifdef BINDING_NAME #undef BINDING_NAME @@ -32,6 +33,7 @@ using namespace std; using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::neighbor; using namespace mlpack::tree; using namespace mlpack::metric; @@ -132,9 +134,9 @@ PARAM_DOUBLE_IN("epsilon", "If specified, will do approximate nearest neighbor " void BINDING_FUNCTION(util::Params& params, util::Timers& timers) { if (params.Get("seed") != 0) - math::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); else - math::RandomSeed((size_t) std::time(NULL)); + RandomSeed((size_t) std::time(NULL)); // A user cannot specify both reference data and a model. RequireOnlyOnePassed(params, { "reference", "input_model" }, true); diff --git a/src/mlpack/methods/nmf/nmf_main.cpp b/src/mlpack/methods/nmf/nmf_main.cpp index eab57c5cb7..364ede383f 100644 --- a/src/mlpack/methods/nmf/nmf_main.cpp +++ b/src/mlpack/methods/nmf/nmf_main.cpp @@ -11,6 +11,7 @@ */ #include #include +#include #ifdef BINDING_NAME #undef BINDING_NAME @@ -29,6 +30,7 @@ #include using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::amf; using namespace mlpack::util; using namespace std; @@ -217,9 +219,9 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& /* timers */) { // Initialize random seed. if (params.Get("seed") != 0) - math::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); else - math::RandomSeed((size_t) std::time(NULL)); + RandomSeed((size_t) std::time(NULL)); // Gather parameters. const size_t r = params.Get("rank"); diff --git a/src/mlpack/methods/preprocess/preprocess_scale_main.cpp b/src/mlpack/methods/preprocess/preprocess_scale_main.cpp index 351421e89d..fd54c301ec 100644 --- a/src/mlpack/methods/preprocess/preprocess_scale_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_scale_main.cpp @@ -29,6 +29,7 @@ #include "mlpack/methods/preprocess/scaling_model.hpp" using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::util; using namespace mlpack::data; using namespace arma; @@ -122,9 +123,9 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) const std::string scalerMethod = params.Get("scaler_method"); if (params.Get("seed") == 0) - mlpack::math::RandomSeed(std::time(NULL)); + mlpack::RandomSeed(std::time(NULL)); else - mlpack::math::RandomSeed((size_t) params.Get("seed")); + mlpack::RandomSeed((size_t) params.Get("seed")); // Make sure the user specified output filenames. RequireAtLeastOnePassed(params, { "output", "output_model"}, false, diff --git a/src/mlpack/methods/preprocess/preprocess_split_main.cpp b/src/mlpack/methods/preprocess/preprocess_split_main.cpp index d74801895c..868ef4871c 100644 --- a/src/mlpack/methods/preprocess/preprocess_split_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_split_main.cpp @@ -106,6 +106,7 @@ PARAM_FLAG("no_shuffle", "Avoid shuffling the data before splitting.", "S"); PARAM_FLAG("stratify_data", "Stratify the data according to labels", "z") using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::data; using namespace mlpack::util; using namespace arma; @@ -119,9 +120,9 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) const bool stratifyData = params.Get("stratify_data"); if (params.Get("seed") == 0) - mlpack::math::RandomSeed(std::time(NULL)); + mlpack::RandomSeed(std::time(NULL)); else - mlpack::math::RandomSeed((size_t) params.Get("seed")); + mlpack::RandomSeed((size_t) params.Get("seed")); // Make sure the user specified output filenames. RequireAtLeastOnePassed(params, { "training" }, false, "no training set will " diff --git a/src/mlpack/methods/random_forest/random_forest_main.cpp b/src/mlpack/methods/random_forest/random_forest_main.cpp index d2663c8d09..ff724f0f46 100644 --- a/src/mlpack/methods/random_forest/random_forest_main.cpp +++ b/src/mlpack/methods/random_forest/random_forest_main.cpp @@ -18,10 +18,12 @@ #define BINDING_NAME random_forest #include +#include #include #include using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::tree; using namespace mlpack::util; using namespace std; @@ -171,9 +173,9 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) { // Initialize random seed if needed. if (params.Get("seed") != 0) - math::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); else - math::RandomSeed((size_t) std::time(NULL)); + RandomSeed((size_t) std::time(NULL)); // Check for incompatible input parameters. if (!params.Has("warm_start")) diff --git a/src/mlpack/methods/range_search/range_search_main.cpp b/src/mlpack/methods/range_search/range_search_main.cpp index b745eaabb3..26343d2715 100644 --- a/src/mlpack/methods/range_search/range_search_main.cpp +++ b/src/mlpack/methods/range_search/range_search_main.cpp @@ -13,6 +13,7 @@ */ #include #include +#include #ifdef BINDING_NAME #undef BINDING_NAME @@ -28,6 +29,7 @@ using namespace std; using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::range; using namespace mlpack::tree; using namespace mlpack::metric; @@ -123,9 +125,9 @@ PARAM_FLAG("single_mode", "If true, single-tree search is used (as opposed to " void BINDING_FUNCTION(util::Params& params, util::Timers& timers) { if (params.Get("seed") != 0) - math::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); else - math::RandomSeed((size_t) std::time(NULL)); + RandomSeed((size_t) std::time(NULL)); // A user cannot specify both reference data and a model. RequireOnlyOnePassed(params, { "reference", "input_model" }, true); diff --git a/src/mlpack/methods/rann/krann_main.cpp b/src/mlpack/methods/rann/krann_main.cpp index 4085bd4c9b..ad73b16ae0 100644 --- a/src/mlpack/methods/rann/krann_main.cpp +++ b/src/mlpack/methods/rann/krann_main.cpp @@ -19,13 +19,14 @@ #define BINDING_NAME krann #include - +#include +#include #include "ra_search.hpp" #include "ra_model.hpp" -#include using namespace std; using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::neighbor; using namespace mlpack::tree; using namespace mlpack::metric; @@ -127,9 +128,9 @@ PARAM_INT_IN("single_sample_limit", "The limit on the maximum number of " void BINDING_FUNCTION(util::Params& params, util::Timers& timers) { if (params.Get("seed") != 0) - math::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); else - math::RandomSeed((size_t) std::time(NULL)); + RandomSeed((size_t) std::time(NULL)); // A user cannot specify both reference data and a model. RequireOnlyOnePassed(params, { "reference", "input_model" }, true); From 66772db8d936302346767e2f20fcd90fb7a2ec61 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 20 Nov 2021 20:33:55 +0000 Subject: [PATCH 16/50] Remove the mlpack:: namespace Signed-off-by: Omar Shrit --- src/mlpack/methods/gmm/gmm_generate_main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/gmm/gmm_generate_main.cpp b/src/mlpack/methods/gmm/gmm_generate_main.cpp index 72fe415155..1fd588c809 100644 --- a/src/mlpack/methods/gmm/gmm_generate_main.cpp +++ b/src/mlpack/methods/gmm/gmm_generate_main.cpp @@ -76,9 +76,9 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& /* timers */) "no results will be saved"); if (params.Get("seed") == 0) - mlpack::RandomSeed(time(NULL)); + RandomSeed(time(NULL)); else - mlpack::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); RequireParamValue(params, "samples", [](int x) { return x > 0; }, true, "number of samples must be greater than 0"); From 29daacd17bc4a8b77baedb9fb3a3d84d2bd2d0b4 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 20 Nov 2021 22:03:31 +0000 Subject: [PATCH 17/50] Adjust namespace in preprocess Signed-off-by: Omar Shrit --- src/mlpack/methods/preprocess/preprocess_scale_main.cpp | 4 ++-- src/mlpack/methods/preprocess/preprocess_split_main.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mlpack/methods/preprocess/preprocess_scale_main.cpp b/src/mlpack/methods/preprocess/preprocess_scale_main.cpp index fd54c301ec..d345b80715 100644 --- a/src/mlpack/methods/preprocess/preprocess_scale_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_scale_main.cpp @@ -123,9 +123,9 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) const std::string scalerMethod = params.Get("scaler_method"); if (params.Get("seed") == 0) - mlpack::RandomSeed(std::time(NULL)); + RandomSeed(std::time(NULL)); else - mlpack::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); // Make sure the user specified output filenames. RequireAtLeastOnePassed(params, { "output", "output_model"}, false, diff --git a/src/mlpack/methods/preprocess/preprocess_split_main.cpp b/src/mlpack/methods/preprocess/preprocess_split_main.cpp index 868ef4871c..d088b9c659 100644 --- a/src/mlpack/methods/preprocess/preprocess_split_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_split_main.cpp @@ -120,9 +120,9 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) const bool stratifyData = params.Get("stratify_data"); if (params.Get("seed") == 0) - mlpack::RandomSeed(std::time(NULL)); + RandomSeed(std::time(NULL)); else - mlpack::RandomSeed((size_t) params.Get("seed")); + RandomSeed((size_t) params.Get("seed")); // Make sure the user specified output filenames. RequireAtLeastOnePassed(params, { "training" }, false, "no training set will " From 6d57ec31abf7fd59ec0f1169b7809c19c4346ac6 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 2 Dec 2021 19:39:05 +0000 Subject: [PATCH 18/50] Clean partly the namespace. Let us see if this resolve the macOS issues Signed-off-by: Omar Shrit --- .../methods/ann/init_rules/gaussian_init.hpp | 6 +- .../methods/ann/init_rules/glorot_init.hpp | 2 - src/mlpack/methods/det/dtree_impl.hpp | 128 +++++++++--------- src/mlpack/methods/hmm/hmm_util_impl.hpp | 6 +- 4 files changed, 67 insertions(+), 75 deletions(-) diff --git a/src/mlpack/methods/ann/init_rules/gaussian_init.hpp b/src/mlpack/methods/ann/init_rules/gaussian_init.hpp index 96478d4bad..50e8b93742 100644 --- a/src/mlpack/methods/ann/init_rules/gaussian_init.hpp +++ b/src/mlpack/methods/ann/init_rules/gaussian_init.hpp @@ -17,8 +17,6 @@ #include #include -using namespace mlpack::math; - namespace mlpack { namespace ann /** Artificial Neural Network. */ { @@ -55,7 +53,7 @@ class GaussianInitialization if (W.is_empty()) W.set_size(rows, cols); - W.imbue( [&]() { return arma::as_scalar(RandNormal(mean, variance)); } ); + W.imbue( [&]() { return arma::as_scalar(mlpack::math::RandNormal(mean, variance)); } ); } /** @@ -69,7 +67,7 @@ class GaussianInitialization if (W.is_empty()) Log::Fatal << "Cannot initialize an empty matrix." << std::endl; - W.imbue( [&]() { return arma::as_scalar(RandNormal(mean, variance)); } ); + W.imbue( [&]() { return arma::as_scalar(mlpack::math::RandNormal(mean, variance)); } ); } /** diff --git a/src/mlpack/methods/ann/init_rules/glorot_init.hpp b/src/mlpack/methods/ann/init_rules/glorot_init.hpp index 4c526b8ab0..f2d02bbb1c 100644 --- a/src/mlpack/methods/ann/init_rules/glorot_init.hpp +++ b/src/mlpack/methods/ann/init_rules/glorot_init.hpp @@ -18,8 +18,6 @@ #include "random_init.hpp" #include "gaussian_init.hpp" -using namespace mlpack::math; - namespace mlpack { namespace ann /** Artificial Neural Network. */ { diff --git a/src/mlpack/methods/det/dtree_impl.hpp b/src/mlpack/methods/det/dtree_impl.hpp index dfdb994049..ad8ea873d3 100644 --- a/src/mlpack/methods/det/dtree_impl.hpp +++ b/src/mlpack/methods/det/dtree_impl.hpp @@ -15,9 +15,6 @@ #include #include -using namespace mlpack; -using namespace det; - namespace details { @@ -95,7 +92,7 @@ void ExtractSplits(std::vector>& splitVec, const size_t minLeafSize) { // It's common sense, but we also use it in a check later. - Log::Assert(minLeafSize > 0); + mlpack::Log::Assert(minLeafSize > 0); typedef std::pair SplitItem; const size_t n_elem = end - start; @@ -118,7 +115,7 @@ void ExtractSplits(std::vector>& splitVec, const ElemType newVal = valsVec[i]; if (lastVal < ElemType(0) && newVal > ElemType(0) && zeroes > 0) { - Log::Assert(padding == 0); // We should arrive here once! + mlpack::Log::Assert(padding == 0); // We should arrive here once! // The minLeafSize > 0 also guarantees we're not entering right at the // start. @@ -150,7 +147,7 @@ void ExtractSplits(std::vector>& splitVec, } // namespace details template -DTree::DTree() : +mlpack::det::DTree::DTree() : start(0), end(0), splitDim(size_t(-1)), @@ -168,7 +165,7 @@ DTree::DTree() : { /* Nothing to do. */ } template -DTree::DTree(const DTree& obj) : +mlpack::det::DTree::DTree(const DTree& obj) : start(obj.start), end(obj.end), maxVals(obj.maxVals), @@ -190,8 +187,8 @@ DTree::DTree(const DTree& obj) : } template -DTree& DTree::operator=( - const DTree& obj) +mlpack::det::DTree& mlpack::det::DTree::operator=( + const mlpack::det::DTree& obj) { if (this == &obj) return *this; @@ -224,7 +221,7 @@ DTree& DTree::operator=( } template -DTree::DTree(DTree&& obj): +mlpack::det::DTree::DTree(DTree&& obj): start(obj.start), end(obj.end), maxVals(std::move(obj.maxVals)), @@ -260,8 +257,8 @@ DTree::DTree(DTree&& obj): } template -DTree& DTree::operator=( - DTree&& obj) +mlpack::det::DTree& mlpack::det::DTree::operator=( + mlpack::det::DTree&& obj) { if (this == &obj) return *this; @@ -312,9 +309,9 @@ DTree& DTree::operator=( // Root node initializers. template -DTree::DTree(const StatType& maxVals, - const StatType& minVals, - const size_t totalPoints) : +mlpack::det::DTree::DTree(const StatType& maxVals, + const StatType& minVals, + const size_t totalPoints) : start(0), end(totalPoints), maxVals(maxVals), @@ -334,7 +331,7 @@ DTree::DTree(const StatType& maxVals, { /* Nothing to do. */ } template -DTree::DTree(MatType & data) : +mlpack::det::DTree::DTree(MatType & data) : start(0), end(data.n_cols), maxVals(arma::max(data, 1)), @@ -356,11 +353,11 @@ DTree::DTree(MatType & data) : // Non-root node initializers. template -DTree::DTree(const StatType& maxVals, - const StatType& minVals, - const size_t start, - const size_t end, - const double logNegError) : +mlpack::det::DTree::DTree(const StatType& maxVals, + const StatType& minVals, + const size_t start, + const size_t end, + const double logNegError) : start(start), end(end), maxVals(maxVals), @@ -380,11 +377,11 @@ DTree::DTree(const StatType& maxVals, { /* Nothing to do. */ } template -DTree::DTree(const StatType& maxVals, - const StatType& minVals, - const size_t totalPoints, - const size_t start, - const size_t end) : +mlpack::det::DTree::DTree(const StatType& maxVals, + const StatType& minVals, + const size_t totalPoints, + const size_t start, + const size_t end) : start(start), end(end), maxVals(maxVals), @@ -404,7 +401,7 @@ DTree::DTree(const StatType& maxVals, { /* Nothing to do. */ } template -DTree::~DTree() +mlpack::det::DTree::~DTree() { delete left; delete right; @@ -413,7 +410,7 @@ DTree::~DTree() // This function computes the log-l2-negative-error of a given node from the // formula R(t) = log(|t|^2 / (N^2 V_t)). template -double DTree::LogNegativeError(const size_t totalPoints) const +double mlpack::det::DTree::LogNegativeError(const size_t totalPoints) const { // log(-|t|^2 / (N^2 V_t)) = log(-1) + 2 log(|t|) - 2 log(N) - log(V_t). double err = 2 * std::log((double) (end - start)) - @@ -434,19 +431,19 @@ double DTree::LogNegativeError(const size_t totalPoints) const // all possible splits. The dataset is the full data set but the start and // end are used to obtain the point in this node. template -bool DTree::FindSplit(const MatType& data, - size_t& splitDim, - ElemType& splitValue, - double& leftError, - double& rightError, - const size_t minLeafSize) const +bool mlpack::det::DTree::FindSplit(const MatType& data, + size_t& splitDim, + ElemType& splitValue, + double& leftError, + double& rightError, + const size_t minLeafSize) const { typedef std::pair SplitItem; // Ensure the dimensionality of the data is the same as the dimensionality of // the bounding rectangle. - Log::Assert(data.n_rows == maxVals.n_elem); - Log::Assert(data.n_rows == minVals.n_elem); + mlpack::Log::Assert(data.n_rows == maxVals.n_elem); + mlpack::Log::Assert(data.n_rows == minVals.n_elem); const size_t points = end - start; @@ -505,7 +502,7 @@ bool DTree::FindSplit(const MatType& data, { // Ensure that the right node will have at least the minimum number of // points. - Log::Assert((points - position) >= minLeafSize); + mlpack::Log::Assert((points - position) >= minLeafSize); // Now we have to see if the error will be reduced. Simple manipulation // of the error function gives us the condition we must satisfy: @@ -552,10 +549,11 @@ bool DTree::FindSplit(const MatType& data, } template -size_t DTree::SplitData(MatType& data, - const size_t splitDim, - const ElemType splitValue, - arma::Col& oldFromNew) const +size_t mlpack::det::DTree::SplitData( + MatType& data, + const size_t splitDim, + const ElemType splitValue, + arma::Col& oldFromNew) const { // Swap all columns such that any columns with value in dimension splitDim // less than or equal to splitValue are on the left side, and all others are @@ -587,14 +585,14 @@ size_t DTree::SplitData(MatType& data, // Greedily expand the tree. template -double DTree::Grow(MatType& data, - arma::Col& oldFromNew, - const bool useVolReg, - const size_t maxLeafSize, - const size_t minLeafSize) +double mlpack::det::DTree::Grow(MatType& data, + arma::Col& oldFromNew, + const bool useVolReg, + const size_t maxLeafSize, + const size_t minLeafSize) { - Log::Assert(data.n_rows == maxVals.n_elem); - Log::Assert(data.n_rows == minVals.n_elem); + mlpack::Log::Assert(data.n_rows == maxVals.n_elem); + mlpack::Log::Assert(data.n_rows == minVals.n_elem); double leftG, rightG; @@ -668,7 +666,7 @@ double DTree::Grow(MatType& data, else { // We can make this a leaf node. - Log::Assert((size_t) (end - start) >= minLeafSize); + mlpack::Log::Assert((size_t) (end - start) >= minLeafSize); subtreeLeaves = 1; subtreeLeavesLogNegError = logNegError; } @@ -736,9 +734,9 @@ double DTree::Grow(MatType& data, template -double DTree::PruneAndUpdate(const double oldAlpha, - const size_t points, - const bool useVolReg) +double mlpack::det::DTree::PruneAndUpdate(const double oldAlpha, + const size_t points, + const bool useVolReg) { // Compute gT. if (subtreeLeaves == 1) // If we are a leaf... @@ -822,7 +820,7 @@ double DTree::PruneAndUpdate(const double oldAlpha, gT = alphaUpper - std::log((double) (subtreeLeaves - 1)); } - Log::Assert(gT < std::numeric_limits::max()); + mlpack::Log::Assert(gT < std::numeric_limits::max()); return std::min((double) gT, std::min(leftG, rightG)); } @@ -851,7 +849,7 @@ double DTree::PruneAndUpdate(const double oldAlpha, // Future improvement: Open up the range with epsilons on both sides where // epsilon depends on the density near the boundary. template -bool DTree::WithinRange(const VecType& query) const +bool mlpack::det::DTree::WithinRange(const VecType& query) const { for (size_t i = 0; i < query.n_elem; ++i) if ((query[i] < minVals[i]) || (query[i] > maxVals[i])) @@ -862,9 +860,9 @@ bool DTree::WithinRange(const VecType& query) const template -double DTree::ComputeValue(const VecType& query) const +double mlpack::det::DTree::ComputeValue(const VecType& query) const { - Log::Assert(query.n_elem == maxVals.n_elem); + mlpack::Log::Assert(query.n_elem == maxVals.n_elem); if (root == 1) // If we are the root... { @@ -885,7 +883,7 @@ double DTree::ComputeValue(const VecType& query) const // Index the buckets for possible usage later. template -TagType DTree::TagTree(const TagType& tag, bool every) +TagType mlpack::det::DTree::TagTree(const TagType& tag, bool every) { if (subtreeLeaves == 1) { @@ -907,9 +905,9 @@ TagType DTree::TagTree(const TagType& tag, bool every) } template -TagType DTree::FindBucket(const VecType& query) const +TagType mlpack::det::DTree::FindBucket(const VecType& query) const { - Log::Assert(query.n_elem == maxVals.n_elem); + mlpack::Log::Assert(query.n_elem == maxVals.n_elem); if (root == 1) // If we are the root... { @@ -933,8 +931,8 @@ TagType DTree::FindBucket(const VecType& query) const } template -void DTree::ComputeVariableImportance(arma::vec& importances) - const +void mlpack::det::DTree::ComputeVariableImportance( + arma::vec& importances) const { // Clear and set to right size. importances.zeros(maxVals.n_elem); @@ -962,8 +960,8 @@ void DTree::ComputeVariableImportance(arma::vec& importances) } template -void DTree::FillMinMax(const StatType& mins, - const StatType& maxs) +void mlpack::det::DTree::FillMinMax(const StatType& mins, + const StatType& maxs) { if (!root) { @@ -986,8 +984,8 @@ void DTree::FillMinMax(const StatType& mins, template template -void DTree::serialize(Archive& ar, - const uint32_t /* version */) +void mlpack::det::DTree::serialize(Archive& ar, + const uint32_t /* version */) { ar(CEREAL_NVP(start)); ar(CEREAL_NVP(end)); diff --git a/src/mlpack/methods/hmm/hmm_util_impl.hpp b/src/mlpack/methods/hmm/hmm_util_impl.hpp index 017cc65bcd..5352690f93 100644 --- a/src/mlpack/methods/hmm/hmm_util_impl.hpp +++ b/src/mlpack/methods/hmm/hmm_util_impl.hpp @@ -78,18 +78,16 @@ void LoadHMMAndPerformActionHelper(const std::string& modelFile, char type; ar(CEREAL_NVP(type)); - using namespace mlpack::distribution; - switch (type) { case HMMType::DiscreteHMM: DeserializeHMMAndPerformAction>(ar, x); + HMM>(ar, x); break; case HMMType::GaussianHMM: DeserializeHMMAndPerformAction>(ar, x); + HMM>(ar, x); break; case HMMType::GaussianMixtureModelHMM: From 3c06db9bcf3e84960e07ac5e95c1b8a96f67dc62 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 2 Dec 2021 19:40:02 +0000 Subject: [PATCH 19/50] Fix the random_basis_impl in addition Signed-off-by: Omar Shrit --- src/mlpack/core/math/random_basis_impl.hpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/mlpack/core/math/random_basis_impl.hpp b/src/mlpack/core/math/random_basis_impl.hpp index 08df445bad..e8d0b0c4d4 100644 --- a/src/mlpack/core/math/random_basis_impl.hpp +++ b/src/mlpack/core/math/random_basis_impl.hpp @@ -11,21 +11,19 @@ */ #include "random_basis.hpp" -using namespace arma; - namespace mlpack { namespace math { -inline void RandomBasis(mat& basis, const size_t d) +inline void RandomBasis(arma::mat& basis, const size_t d) { while (true) { // [Q, R] = qr(randn(d, d)); // Q = Q * diag(sign(diag(R))); - mat r; - if (qr(basis, r, randn(d, d))) + arma::mat r; + if (qr(basis, r, arma::randn(d, d))) { - vec rDiag(r.n_rows); + arma::vec rDiag(r.n_rows); for (size_t i = 0; i < rDiag.n_elem; ++i) { if (r(i, i) < 0) From cf6b9c78c6fcb6879eb82076007463e90c727c06 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 2 Dec 2021 22:54:06 +0000 Subject: [PATCH 20/50] Clean all using namespaces from the headers. Signed-off-by: Omar Shrit --- src/mlpack/methods/ann/layer_names.hpp | 72 +++++++++--------- .../q_networks/categorical_dqn.hpp | 30 ++++---- .../q_networks/dueling_dqn.hpp | 74 +++++++++---------- .../q_networks/simple_dqn.hpp | 26 +++---- 4 files changed, 97 insertions(+), 105 deletions(-) diff --git a/src/mlpack/methods/ann/layer_names.hpp b/src/mlpack/methods/ann/layer_names.hpp index 15596efea3..b55deebbed 100644 --- a/src/mlpack/methods/ann/layer_names.hpp +++ b/src/mlpack/methods/ann/layer_names.hpp @@ -16,8 +16,6 @@ #include #include -using namespace mlpack::ann; - /** * Implementation of a class that returns the string representation of the * name of the given layer. @@ -36,7 +34,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type AdaptiveMaxPooling. * @return The string representation of the layer. */ - std::string LayerString(AdaptiveMaxPooling<> * /*layer*/) const + std::string LayerString(mlpack::ann::AdaptiveMaxPooling<> * /*layer*/) const { return "adaptivemaxpooling"; } @@ -47,7 +45,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type AdaptiveMeanPooling. * @return The string representation of the layer. */ - std::string LayerString(AdaptiveMeanPooling<> * /*layer*/) const + std::string LayerString(mlpack::ann::AdaptiveMeanPooling<> * /*layer*/) const { return "adaptivemeanpooling"; } @@ -58,7 +56,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type AtrousConvolution. * @return The string representation of the layer. */ - std::string LayerString(AtrousConvolution<>* /*layer*/) const + std::string LayerString(mlpack::ann::AtrousConvolution<>* /*layer*/) const { return "atrousconvolution"; } @@ -69,7 +67,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type AlphaDropout. * @return The string representation of the layer. */ - std::string LayerString(AlphaDropout<>* /*layer*/) const + std::string LayerString(mlpack::ann::AlphaDropout<>* /*layer*/) const { return "alphadropout"; } @@ -80,7 +78,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type BatchNorm. * @return The string representation of the layer. */ - std::string LayerString(BatchNorm<>* /*layer*/) const + std::string LayerString(mlpack::ann::BatchNorm<>* /*layer*/) const { return "batchnorm"; } @@ -91,7 +89,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type Constant. * @return The string representation of the layer. */ - std::string LayerString(Constant<>* /*layer*/) const + std::string LayerString(mlpack::ann::Constant<>* /*layer*/) const { return "constant"; } @@ -102,7 +100,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type Convolution. * @return The string representation of the layer. */ - std::string LayerString(Convolution<>* /*layer*/) const + std::string LayerString(mlpack::ann::Convolution<>* /*layer*/) const { return "convolution"; } @@ -113,7 +111,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type DropConnect. * @return The string representation of the layer. */ - std::string LayerString(DropConnect<>* /*layer*/) const + std::string LayerString(mlpack::ann::DropConnect<>* /*layer*/) const { return "dropconnect"; } @@ -124,7 +122,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type Dropout. * @return The string representation of the layer. */ - std::string LayerString(Dropout<>* /*layer*/) const + std::string LayerString(mlpack::ann::Dropout<>* /*layer*/) const { return "dropout"; } @@ -135,7 +133,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type FlexibleReLU. * @return The string representation of the layer. */ - std::string LayerString(FlexibleReLU<>* /*layer*/) const + std::string LayerString(mlpack::ann::FlexibleReLU<>* /*layer*/) const { return "flexiblerelu"; } @@ -146,7 +144,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type LayerNorm. * @return The string representation of the layer. */ - std::string LayerString(LayerNorm<>* /*layer*/) const + std::string LayerString(mlpack::ann::LayerNorm<>* /*layer*/) const { return "layernorm"; } @@ -157,7 +155,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type Linear. * @return The string representation of the layer. */ - std::string LayerString(Linear<>* /*layer*/) const + std::string LayerString(mlpack::ann::Linear<>* /*layer*/) const { return "linear"; } @@ -168,7 +166,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type LinearNoBias. * @return The string representation of the layer. */ - std::string LayerString(LinearNoBias<>* /*layer*/) const + std::string LayerString(mlpack::ann::LinearNoBias<>* /*layer*/) const { return "linearnobias"; } @@ -179,7 +177,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type NoisyLinear. * @return The string representation of the layer. */ - std::string LayerString(NoisyLinear<>* /*layer*/) const + std::string LayerString(mlpack::ann::NoisyLinear<>* /*layer*/) const { return "noisylinear"; } @@ -190,7 +188,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type MaxPooling. * @return The string representation of the layer. */ - std::string LayerString(MaxPooling<>* /*layer*/) const + std::string LayerString(mlpack::ann::MaxPooling<>* /*layer*/) const { return "maxpooling"; } @@ -201,7 +199,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type MeanPooling. * @return The string representation of the layer. */ - std::string LayerString(MeanPooling<>* /*layer*/) const + std::string LayerString(mlpack::ann::MeanPooling<>* /*layer*/) const { return "meanpooling"; } @@ -212,7 +210,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type LpPooling. * @return The string representation of the layer. */ - std::string LayerString(LpPooling<>* /*layer*/) const + std::string LayerString(mlpack::ann::LpPooling<>* /*layer*/) const { return "lppooling"; } @@ -223,7 +221,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type MultiplyConstant. * @return The string representation of the layer. */ - std::string LayerString(MultiplyConstant<>* /*layer*/) const + std::string LayerString(mlpack::ann::MultiplyConstant<>* /*layer*/) const { return "multiplyconstant"; } @@ -234,7 +232,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type ReLULayer. * @return The string representation of the layer. */ - std::string LayerString(ReLULayer<>* /*layer*/) const + std::string LayerString(mlpack::ann::ReLULayer<>* /*layer*/) const { return "relu"; } @@ -246,7 +244,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type TransposedConvolution. * @return The string representation of the layer. */ - std::string LayerString(TransposedConvolution<>* /*layer*/) const + std::string LayerString(mlpack::ann::TransposedConvolution<>* /*layer*/) const { return "transposedconvolution"; } @@ -257,7 +255,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type IdentityLayer. * @return The string representation of the layer. */ - std::string LayerString(IdentityLayer<>* /*layer*/) const + std::string LayerString(mlpack::ann::IdentityLayer<>* /*layer*/) const { return "identity"; } @@ -268,7 +266,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type TanHLayer. * @return The string representation of the layer. */ - std::string LayerString(TanHLayer<>* /*layer*/) const + std::string LayerString(mlpack::ann::TanHLayer<>* /*layer*/) const { return "tanh"; } @@ -279,7 +277,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type ELU. * @return The string representation of the layer. */ - std::string LayerString(ELU<>* /*layer*/) const + std::string LayerString(mlpack::ann::ELU<>* /*layer*/) const { return "elu"; } @@ -290,7 +288,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type HardTanH. * @return The string representation of the layer. */ - std::string LayerString(HardTanH<>* /*layer*/) const + std::string LayerString(mlpack::ann::HardTanH<>* /*layer*/) const { return "hardtanh"; } @@ -301,7 +299,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type LeakyReLU. * @return The string representation of the layer. */ - std::string LayerString(LeakyReLU<>* /*layer*/) const + std::string LayerString(mlpack::ann::LeakyReLU<>* /*layer*/) const { return "leakyrelu"; } @@ -312,7 +310,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type PReLU. * @return The string representation of the layer. */ - std::string LayerString(PReLU<>* /*layer*/) const + std::string LayerString(mlpack::ann::PReLU<>* /*layer*/) const { return "prelu"; } @@ -323,7 +321,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type SigmoidLayer. * @return The string representation of the layer. */ - std::string LayerString(SigmoidLayer<>* /*layer*/) const + std::string LayerString(mlpack::ann::SigmoidLayer<>* /*layer*/) const { return "sigmoid"; } @@ -334,7 +332,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type LogSoftMax. * @return The string representation of the layer. */ - std::string LayerString(LogSoftMax<>* /*layer*/) const + std::string LayerString(mlpack::ann::LogSoftMax<>* /*layer*/) const { return "logsoftmax"; } @@ -345,7 +343,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type LSTM. * @return The string representation of the layer. */ - std::string LayerString(LSTM<>* /*layer*/) const + std::string LayerString(mlpack::ann::LSTM<>* /*layer*/) const { return "lstm"; } @@ -356,7 +354,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type CReLU. * @return The string representation of the layer. */ - std::string LayerString(CReLU<>* /*layer*/) const + std::string LayerString(mlpack::ann::CReLU<>* /*layer*/) const { return "crelu"; } @@ -367,7 +365,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type Highway. * @return The string representation of the layer. */ - std::string LayerString(Highway<>* /*layer*/) const + std::string LayerString(mlpack::ann::Highway<>* /*layer*/) const { return "highway"; } @@ -378,7 +376,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type GRU. * @return The string representation of the layer. */ - std::string LayerString(GRU<>* /*layer*/) const + std::string LayerString(mlpack::ann::GRU<>* /*layer*/) const { return "gru"; } @@ -389,7 +387,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type Glimpse. * @return The string representation of the layer. */ - std::string LayerString(Glimpse<>* /*layer*/) const + std::string LayerString(mlpack::ann::Glimpse<>* /*layer*/) const { return "glimpse"; } @@ -400,7 +398,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type FastLSTM. * @return The string representation of the layer. */ - std::string LayerString(FastLSTM<>* /*layer*/) const + std::string LayerString(mlpack::ann::FastLSTM<>* /*layer*/) const { return "fastlstm"; } @@ -411,7 +409,7 @@ class LayerNameVisitor : public boost::static_visitor * @param * Given layer of type WeightNorm. * @return The string representation of the layer. */ - std::string LayerString(WeightNorm<>* /*layer*/) const + std::string LayerString(mlpack::ann::WeightNorm<>* /*layer*/) const { return "weightnorm"; } diff --git a/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp b/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp index aab2c4bc1b..d6f5374096 100644 --- a/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp @@ -23,8 +23,6 @@ namespace mlpack { namespace rl { -using namespace mlpack::ann; - /** * Implementation of the Categorical Deep Q-Learning network. * For more information, see the following. @@ -43,9 +41,9 @@ using namespace mlpack::ann; * @tparam NetworkType The type of network used for simple dqn. */ template< - typename OutputLayerType = EmptyLoss<>, - typename InitType = GaussianInitialization, - typename NetworkType = FFN + typename OutputLayerType = mlpack::ann::EmptyLoss<>, + typename InitType = mlpack::ann::GaussianInitialization, + typename NetworkType = mlpack::ann::FFN > class CategoricalDQN { @@ -83,21 +81,21 @@ class CategoricalDQN vMax(config.VMax()), isNoisy(isNoisy) { - network.Add(new Linear<>(inputDim, h1)); - network.Add(new ReLULayer<>()); + network.Add(new mlpack::ann::Linear<>(inputDim, h1)); + network.Add(new mlpack::ann::ReLULayer<>()); if (isNoisy) { noisyLayerIndex.push_back(network.Model().size()); - network.Add(new NoisyLinear<>(h1, h2)); - network.Add(new ReLULayer<>()); + network.Add(new mlpack::ann::NoisyLinear<>(h1, h2)); + network.Add(new mlpack::ann::ReLULayer<>()); noisyLayerIndex.push_back(network.Model().size()); - network.Add(new NoisyLinear<>(h2, outputDim * atomSize)); + network.Add(new mlpack::ann::NoisyLinear<>(h2, outputDim * atomSize)); } else { - network.Add(new Linear<>(h1, h2)); - network.Add(new ReLULayer<>()); - network.Add(new Linear<>(h2, outputDim * atomSize)); + network.Add(new mlpack::ann::Linear<>(h1, h2)); + network.Add(new mlpack::ann::ReLULayer<>()); + network.Add(new mlpack::ann::Linear<>(h2, outputDim * atomSize)); } } @@ -181,9 +179,9 @@ class CategoricalDQN */ void ResetNoise() { - for (size_t i = 0; i < noisyLayerIndex.size(); i++) + for (size_t i = 0; i < noisyLayerIndex.size(); ++i) { - boost::get*> + boost::get*> (network.Model()[noisyLayerIndex[i]])->ResetNoise(); } } @@ -236,7 +234,7 @@ class CategoricalDQN std::vector noisyLayerIndex; //! Locally-stored softmax activation function. - Softmax<> softMax; + mlpack::ann::Softmax<> softMax; //! Locally-stored activations from softMax. arma::mat activations; diff --git a/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp b/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp index 6a160a48ac..d8c78e8273 100644 --- a/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp @@ -22,8 +22,6 @@ namespace mlpack { namespace rl { -using namespace mlpack::ann; - /** * Implementation of the Dueling Deep Q-Learning network. * For more information, see the following. @@ -46,12 +44,12 @@ using namespace mlpack::ann; * @tparam ValueNetworkType The type of network used for value network. */ template < - typename OutputLayerType = EmptyLoss<>, - typename InitType = GaussianInitialization, - typename CompleteNetworkType = FFN, - typename FeatureNetworkType = Sequential<>, - typename AdvantageNetworkType = Sequential<>, - typename ValueNetworkType = Sequential<> + typename OutputLayerType = mlpack::ann::EmptyLoss<>, + typename InitType = mlpack::ann::GaussianInitialization, + typename CompleteNetworkType = mlpack::ann::FFN, + typename FeatureNetworkType = mlpack::ann::Sequential<>, + typename AdvantageNetworkType = mlpack::ann::Sequential<>, + typename ValueNetworkType = mlpack::ann::Sequential<> > class DuelingDQN { @@ -59,14 +57,14 @@ class DuelingDQN //! Default constructor. DuelingDQN() : isNoisy(false) { - featureNetwork = new Sequential<>(); - valueNetwork = new Sequential<>(); - advantageNetwork = new Sequential<>(); - concat = new Concat<>(true); + featureNetwork = new mlpack::ann::Sequential<>(); + valueNetwork = new mlpack::ann::Sequential<>(); + advantageNetwork = new mlpack::ann::Sequential<>(); + concat = new mlpack::ann::Concat<>(true); concat->Add(valueNetwork); concat->Add(advantageNetwork); - completeNetwork.Add(new IdentityLayer<>()); + completeNetwork.Add(new mlpack::ann::IdentityLayer<>()); completeNetwork.Add(featureNetwork); completeNetwork.Add(concat); } @@ -92,42 +90,42 @@ class DuelingDQN completeNetwork(outputLayer, init), isNoisy(isNoisy) { - featureNetwork = new Sequential<>(); - featureNetwork->Add(new Linear<>(inputDim, h1)); - featureNetwork->Add(new ReLULayer<>()); + featureNetwork = new mlpack::ann::Sequential<>(); + featureNetwork->Add(new mlpack::ann::Linear<>(inputDim, h1)); + featureNetwork->Add(new mlpack::ann::ReLULayer<>()); - valueNetwork = new Sequential<>(); - advantageNetwork = new Sequential<>(); + valueNetwork = new mlpack::ann::Sequential<>(); + advantageNetwork = new mlpack::ann::Sequential<>(); if (isNoisy) { noisyLayerIndex.push_back(valueNetwork->Model().size()); - valueNetwork->Add(new NoisyLinear<>(h1, h2)); - advantageNetwork->Add(new NoisyLinear<>(h1, h2)); + valueNetwork->Add(new mlpack::ann::NoisyLinear<>(h1, h2)); + advantageNetwork->Add(new mlpack::ann::NoisyLinear<>(h1, h2)); - valueNetwork->Add(new ReLULayer<>()); - advantageNetwork->Add(new ReLULayer<>()); + valueNetwork->Add(new mlpack::ann::ReLULayer<>()); + advantageNetwork->Add(new mlpack::ann::ReLULayer<>()); noisyLayerIndex.push_back(valueNetwork->Model().size()); - valueNetwork->Add(new NoisyLinear<>(h2, 1)); - advantageNetwork->Add(new NoisyLinear<>(h2, outputDim)); + valueNetwork->Add(new mlpack::ann::NoisyLinear<>(h2, 1)); + advantageNetwork->Add(new mlpack::ann::NoisyLinear<>(h2, outputDim)); } else { - valueNetwork->Add(new Linear<>(h1, h2)); - valueNetwork->Add(new ReLULayer<>()); - valueNetwork->Add(new Linear<>(h2, 1)); + valueNetwork->Add(new mlpack::ann::Linear<>(h1, h2)); + valueNetwork->Add(new mlpack::ann::ReLULayer<>()); + valueNetwork->Add(new mlpack::ann::Linear<>(h2, 1)); - advantageNetwork->Add(new Linear<>(h1, h2)); - advantageNetwork->Add(new ReLULayer<>()); - advantageNetwork->Add(new Linear<>(h2, outputDim)); + advantageNetwork->Add(new mlpack::ann::Linear<>(h1, h2)); + advantageNetwork->Add(new mlpack::ann::ReLULayer<>()); + advantageNetwork->Add(new mlpack::ann::Linear<>(h2, outputDim)); } - concat = new Concat<>(true); + concat = new mlpack::ann::Concat<>(true); concat->Add(valueNetwork); concat->Add(advantageNetwork); - completeNetwork.Add(new IdentityLayer<>()); + completeNetwork.Add(new mlpack::ann::IdentityLayer<>()); completeNetwork.Add(featureNetwork); completeNetwork.Add(concat); this->ResetParameters(); @@ -150,10 +148,10 @@ class DuelingDQN valueNetwork(valueNetwork), isNoisy(isNoisy) { - concat = new Concat<>(true); + concat = new mlpack::ann::Concat<>(true); concat->Add(valueNetwork); concat->Add(advantageNetwork); - completeNetwork.Add(new IdentityLayer<>()); + completeNetwork.Add(new mlpack::ann::IdentityLayer<>()); completeNetwork.Add(featureNetwork); completeNetwork.Add(concat); this->ResetParameters(); @@ -245,9 +243,9 @@ class DuelingDQN { for (size_t i = 0; i < noisyLayerIndex.size(); i++) { - boost::get*> + boost::get*> (valueNetwork->Model()[noisyLayerIndex[i]])->ResetNoise(); - boost::get*> + boost::get*> (advantageNetwork->Model()[noisyLayerIndex[i]])->ResetNoise(); } } @@ -262,7 +260,7 @@ class DuelingDQN CompleteNetworkType completeNetwork; //! Locally-stored concat network. - Concat<>* concat; + mlpack::ann::Concat<>* concat; //! Locally-stored feature network. FeatureNetworkType* featureNetwork; @@ -283,7 +281,7 @@ class DuelingDQN arma::mat actionValues; //! Locally-stored loss function. - MeanSquaredError<> lossFunction; + mlpack::ann::MeanSquaredError<> lossFunction; }; } // namespace rl diff --git a/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp b/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp index 818e004a4d..167e9f5e42 100644 --- a/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp @@ -21,17 +21,15 @@ namespace mlpack { namespace rl { -using namespace mlpack::ann; - /** * @tparam OutputLayerType The output layer type of the network. * @tparam InitType The initialization type used for the network. * @tparam NetworkType The type of network used for simple dqn. */ template< - typename OutputLayerType = MeanSquaredError<>, - typename InitType = GaussianInitialization, - typename NetworkType = FFN + typename OutputLayerType = mlpack::ann::MeanSquaredError<>, + typename InitType = mlpack::ann::GaussianInitialization, + typename NetworkType = mlpack::ann::FFN > class SimpleDQN { @@ -63,21 +61,21 @@ class SimpleDQN network(outputLayer, init), isNoisy(isNoisy) { - network.Add(new Linear<>(inputDim, h1)); - network.Add(new ReLULayer<>()); + network.Add(new mlpack::ann::Linear<>(inputDim, h1)); + network.Add(new mlpack::ann::ReLULayer<>()); if (isNoisy) { noisyLayerIndex.push_back(network.Model().size()); - network.Add(new NoisyLinear<>(h1, h2)); - network.Add(new ReLULayer<>()); + network.Add(new mlpack::ann::NoisyLinear<>(h1, h2)); + network.Add(new mlpack::ann::ReLULayer<>()); noisyLayerIndex.push_back(network.Model().size()); - network.Add(new NoisyLinear<>(h2, outputDim)); + network.Add(new mlpack::ann::NoisyLinear<>(h2, outputDim)); } else { - network.Add(new Linear<>(h1, h2)); - network.Add(new ReLULayer<>()); - network.Add(new Linear<>(h2, outputDim)); + network.Add(new mlpack::ann::Linear<>(h1, h2)); + network.Add(new mlpack::ann::ReLULayer<>()); + network.Add(new mlpack::ann::Linear<>(h2, outputDim)); } } @@ -134,7 +132,7 @@ class SimpleDQN { for (size_t i = 0; i < noisyLayerIndex.size(); i++) { - boost::get*> + boost::get*> (network.Model()[noisyLayerIndex[i]])->ResetNoise(); } } From ac303a688723f50466ecaf9924644e73fcddbba1 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 2 Dec 2021 23:54:27 +0000 Subject: [PATCH 21/50] Add forgetten layer name Signed-off-by: Omar Shrit --- src/mlpack/methods/ann/layer_names.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/layer_names.hpp b/src/mlpack/methods/ann/layer_names.hpp index b55deebbed..60b7cc4aa1 100644 --- a/src/mlpack/methods/ann/layer_names.hpp +++ b/src/mlpack/methods/ann/layer_names.hpp @@ -427,7 +427,7 @@ class LayerNameVisitor : public boost::static_visitor } //! Overload function call. - std::string operator()(MoreTypes layer) const + std::string operator()(mlpack::ann::MoreTypes layer) const { return layer.apply_visitor(*this); } From 14b065299a3aef15b40120535d5f5f2796812af2 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 16 Dec 2021 15:30:43 +0100 Subject: [PATCH 22/50] Update style in src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp Co-authored-by: Marcus Edel --- src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp b/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp index 21512e14af..e427067a4c 100644 --- a/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp +++ b/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp @@ -127,7 +127,7 @@ inline double EpanechnikovKernel::GradientForSquaredDistance(const double return -1 * inverseBandwidthSquared; } else if (distanceSquared > bandwidthSquared && - distanceSquared >= 0) + distanceSquared >= 0) { return 0; } From 3fbe75b20e2dd84b7258ed6a12900356577b65dc Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 16 Dec 2021 15:31:00 +0100 Subject: [PATCH 23/50] Fix style in src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp Co-authored-by: Marcus Edel --- src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp b/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp index e427067a4c..4bc302c00d 100644 --- a/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp +++ b/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp @@ -118,8 +118,8 @@ inline double EpanechnikovKernel::Gradient(const double distance) const * Evaluate gradient of the kernel not for two points * but for a numerical value. */ -inline double EpanechnikovKernel::GradientForSquaredDistance(const double - distanceSquared) const +inline double EpanechnikovKernel::GradientForSquaredDistance( + const double distanceSquared) const { double bandwidthSquared = bandwidth * bandwidth; if (distanceSquared < bandwidthSquared) From aacdb42aed64cf56dd46344dd3352111079becae Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 16 Dec 2021 15:31:24 +0100 Subject: [PATCH 24/50] Add parantheses in src/mlpack/core/math/lin_alg_impl.hpp Co-authored-by: Marcus Edel --- src/mlpack/core/math/lin_alg_impl.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mlpack/core/math/lin_alg_impl.hpp b/src/mlpack/core/math/lin_alg_impl.hpp index 1a0408e3c4..76597fd2a5 100644 --- a/src/mlpack/core/math/lin_alg_impl.hpp +++ b/src/mlpack/core/math/lin_alg_impl.hpp @@ -35,8 +35,10 @@ inline void VectorPower(arma::vec& vec, const double power) for (size_t i = 0; i < vec.n_elem; ++i) { if (std::abs(vec(i)) > 1e-12) + { vec(i) = (vec(i) > 0) ? std::pow(vec(i), (double) power) : -std::pow(-vec(i), (double) power); + } else vec(i) = 0; } From 0a42f35d6d340d451836e399e94b08e5a38e57d9 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 16 Dec 2021 15:31:41 +0100 Subject: [PATCH 25/50] Fix indentation in src/mlpack/core/math/lin_alg_impl.hpp Co-authored-by: Marcus Edel --- src/mlpack/core/math/lin_alg_impl.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/core/math/lin_alg_impl.hpp b/src/mlpack/core/math/lin_alg_impl.hpp index 76597fd2a5..4c24c8dbee 100644 --- a/src/mlpack/core/math/lin_alg_impl.hpp +++ b/src/mlpack/core/math/lin_alg_impl.hpp @@ -65,8 +65,8 @@ inline void Center(const arma::mat& x, arma::mat& xCentered) * matrix. */ inline void WhitenUsingSVD(const arma::mat& x, - arma::mat& xWhitened, - arma::mat& whiteningMatrix) + arma::mat& xWhitened, + arma::mat& whiteningMatrix) { arma::mat covX, u, v, invSMatrix, temp1; arma::vec sVector; From 75717fe03152b58523291b5aa4657728dc489d5b Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 16 Dec 2021 15:32:00 +0100 Subject: [PATCH 26/50] Fix indentation src/mlpack/core/math/lin_alg_impl.hpp Co-authored-by: Marcus Edel --- src/mlpack/core/math/lin_alg_impl.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/core/math/lin_alg_impl.hpp b/src/mlpack/core/math/lin_alg_impl.hpp index 4c24c8dbee..268f349889 100644 --- a/src/mlpack/core/math/lin_alg_impl.hpp +++ b/src/mlpack/core/math/lin_alg_impl.hpp @@ -148,8 +148,8 @@ inline void Orthogonalize(arma::mat& x) * @param output Matrix to copy non-removed rows into. */ inline void RemoveRows(const arma::mat& input, - const std::vector& rowsToRemove, - arma::mat& output) + const std::vector& rowsToRemove, + arma::mat& output) { const size_t nRemove = rowsToRemove.size(); const size_t nKeep = input.n_rows - nRemove; From 879af347d66fb5e1dd3dce71a7f09f8feb2aa8c6 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 16 Dec 2021 15:32:16 +0100 Subject: [PATCH 27/50] Remove additional line in src/mlpack/core/math/lin_alg_impl.hpp Co-authored-by: Marcus Edel --- src/mlpack/core/math/lin_alg_impl.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/mlpack/core/math/lin_alg_impl.hpp b/src/mlpack/core/math/lin_alg_impl.hpp index 268f349889..e65f7f2a58 100644 --- a/src/mlpack/core/math/lin_alg_impl.hpp +++ b/src/mlpack/core/math/lin_alg_impl.hpp @@ -245,7 +245,6 @@ inline void Smat(const arma::vec& input, arma::mat& output) const size_t n = static_cast (ceil((-1. + sqrt(1. + 8. * input.n_elem))/2.)); - output.zeros(n, n); size_t idx = 0; From c6cb5d33cf1cade5e443b1679b23a18d45d413a2 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 16 Dec 2021 15:32:38 +0100 Subject: [PATCH 28/50] Add spaces in src/mlpack/core/math/lin_alg_impl.hpp Co-authored-by: Marcus Edel --- src/mlpack/core/math/lin_alg_impl.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/core/math/lin_alg_impl.hpp b/src/mlpack/core/math/lin_alg_impl.hpp index e65f7f2a58..7e0d1252e9 100644 --- a/src/mlpack/core/math/lin_alg_impl.hpp +++ b/src/mlpack/core/math/lin_alg_impl.hpp @@ -243,7 +243,7 @@ inline void Svec(const arma::sp_mat& input, arma::sp_vec& output) inline void Smat(const arma::vec& input, arma::mat& output) { const size_t n = static_cast - (ceil((-1. + sqrt(1. + 8. * input.n_elem))/2.)); + (ceil((-1. + sqrt(1. + 8. * input.n_elem)) / 2.)); output.zeros(n, n); From d9a7ac83e142552db616d2d2668b2d2bea9b2595 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 16 Dec 2021 15:32:55 +0100 Subject: [PATCH 29/50] Add forgetten dot in src/mlpack/core/math/random_basis.hpp Co-authored-by: Marcus Edel --- src/mlpack/core/math/random_basis.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/core/math/random_basis.hpp b/src/mlpack/core/math/random_basis.hpp index 75c9b1eef1..3d7faa4a64 100644 --- a/src/mlpack/core/math/random_basis.hpp +++ b/src/mlpack/core/math/random_basis.hpp @@ -29,7 +29,7 @@ inline void RandomBasis(arma::mat& basis, const size_t d); } // namespace math } // namespace mlpack -//! Include the implementation file +//! Include the implementation file. #include "random_basis_impl.hpp" #endif From f658130693c6decd31ca05a69f78ec0133e29825 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 16 Dec 2021 15:33:08 +0100 Subject: [PATCH 30/50] Fix indentation in src/mlpack/core/data/detect_file_type.hpp Co-authored-by: Ryan Curtin --- src/mlpack/core/data/detect_file_type.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/core/data/detect_file_type.hpp b/src/mlpack/core/data/detect_file_type.hpp index 93069c8ce6..668a2c2433 100644 --- a/src/mlpack/core/data/detect_file_type.hpp +++ b/src/mlpack/core/data/detect_file_type.hpp @@ -54,7 +54,7 @@ inline FileType GuessFileType(std::istream& f); * @return The detected file type. arma::file_type_unknown if unknown. */ inline FileType AutoDetect(std::fstream& stream, - const std::string& filename); + const std::string& filename); /** * Return the type based only on the extension. From 5da5c0e34352c1d5c533660e22c251946618baf6 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 16 Dec 2021 15:33:51 +0100 Subject: [PATCH 31/50] Remove additional line in src/mlpack/core/kernels/pspectrum_string_kernel.hpp Co-authored-by: Ryan Curtin --- src/mlpack/core/kernels/pspectrum_string_kernel.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mlpack/core/kernels/pspectrum_string_kernel.hpp b/src/mlpack/core/kernels/pspectrum_string_kernel.hpp index 3fe346b45e..dc5ff24415 100644 --- a/src/mlpack/core/kernels/pspectrum_string_kernel.hpp +++ b/src/mlpack/core/kernels/pspectrum_string_kernel.hpp @@ -129,6 +129,7 @@ class PSpectrumStringKernel Log::Info << "Substring extraction complete." << std::endl; } + /** * Evaluate the kernel for the string indices given. As mentioned in the * class documentation, a and b should be 2-element vectors, where the first From 43440bf4325ecfb952b973e9efe93f81ce54869b Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sun, 16 Jan 2022 12:58:06 +0000 Subject: [PATCH 32/50] Remove mlpack namespace from src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp Co-authored-by: Ryan Curtin --- .../reinforcement_learning/q_networks/simple_dqn.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp b/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp index 167e9f5e42..79af778619 100644 --- a/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp @@ -27,9 +27,9 @@ namespace rl { * @tparam NetworkType The type of network used for simple dqn. */ template< - typename OutputLayerType = mlpack::ann::MeanSquaredError<>, - typename InitType = mlpack::ann::GaussianInitialization, - typename NetworkType = mlpack::ann::FFN + typename OutputLayerType = ann::MeanSquaredError<>, + typename InitType = ann::GaussianInitialization, + typename NetworkType = ann::FFN > class SimpleDQN { From 626024c10f79045167721c5bd384f065758751fa Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sun, 16 Jan 2022 12:58:22 +0000 Subject: [PATCH 33/50] Remove mlpack namespace from src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp Co-authored-by: Ryan Curtin --- .../q_networks/dueling_dqn.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp b/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp index d8c78e8273..06a7e2f9a2 100644 --- a/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp @@ -44,12 +44,12 @@ namespace rl { * @tparam ValueNetworkType The type of network used for value network. */ template < - typename OutputLayerType = mlpack::ann::EmptyLoss<>, - typename InitType = mlpack::ann::GaussianInitialization, - typename CompleteNetworkType = mlpack::ann::FFN, - typename FeatureNetworkType = mlpack::ann::Sequential<>, - typename AdvantageNetworkType = mlpack::ann::Sequential<>, - typename ValueNetworkType = mlpack::ann::Sequential<> + typename OutputLayerType = ann::EmptyLoss<>, + typename InitType = ann::GaussianInitialization, + typename CompleteNetworkType = ann::FFN, + typename FeatureNetworkType = ann::Sequential<>, + typename AdvantageNetworkType = ann::Sequential<>, + typename ValueNetworkType = ann::Sequential<> > class DuelingDQN { From aa385434059a7dc957b3b72defce510a04ff8ce8 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sun, 16 Jan 2022 12:58:38 +0000 Subject: [PATCH 34/50] Remove mlpack namespace from src/mlpack/methods/hmm/hmm_util_impl.hpp Co-authored-by: Ryan Curtin --- src/mlpack/methods/hmm/hmm_util_impl.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/hmm/hmm_util_impl.hpp b/src/mlpack/methods/hmm/hmm_util_impl.hpp index 5352690f93..f207a98fda 100644 --- a/src/mlpack/methods/hmm/hmm_util_impl.hpp +++ b/src/mlpack/methods/hmm/hmm_util_impl.hpp @@ -87,7 +87,7 @@ void LoadHMMAndPerformActionHelper(const std::string& modelFile, case HMMType::GaussianHMM: DeserializeHMMAndPerformAction>(ar, x); + HMM>(ar, x); break; case HMMType::GaussianMixtureModelHMM: From 1dbd10debd33f258ecba691e15f1e7c1db1dcedc Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sun, 16 Jan 2022 12:58:50 +0000 Subject: [PATCH 35/50] remove mlpack namespace from src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp Co-authored-by: Ryan Curtin --- .../reinforcement_learning/q_networks/categorical_dqn.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp b/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp index d6f5374096..917636d87b 100644 --- a/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp @@ -41,9 +41,9 @@ namespace rl { * @tparam NetworkType The type of network used for simple dqn. */ template< - typename OutputLayerType = mlpack::ann::EmptyLoss<>, - typename InitType = mlpack::ann::GaussianInitialization, - typename NetworkType = mlpack::ann::FFN + typename OutputLayerType = ann::EmptyLoss<>, + typename InitType = ann::GaussianInitialization, + typename NetworkType = ann::FFN > class CategoricalDQN { From ed14f10c697219b21b3c8f0b68f2e09c03647c1b Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sun, 16 Jan 2022 12:59:03 +0000 Subject: [PATCH 36/50] Remove mlpack namspace from src/mlpack/methods/hmm/hmm_util_impl.hpp Co-authored-by: Ryan Curtin --- src/mlpack/methods/hmm/hmm_util_impl.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/hmm/hmm_util_impl.hpp b/src/mlpack/methods/hmm/hmm_util_impl.hpp index f207a98fda..75938845e4 100644 --- a/src/mlpack/methods/hmm/hmm_util_impl.hpp +++ b/src/mlpack/methods/hmm/hmm_util_impl.hpp @@ -82,7 +82,7 @@ void LoadHMMAndPerformActionHelper(const std::string& modelFile, { case HMMType::DiscreteHMM: DeserializeHMMAndPerformAction>(ar, x); + HMM>(ar, x); break; case HMMType::GaussianHMM: From accbdbd7cdf9997da77105771f63ac446783d7ed Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sun, 16 Jan 2022 12:59:31 +0000 Subject: [PATCH 37/50] Provide more details for namespace in src/mlpack/methods/det/dtree_impl.hpp Co-authored-by: Ryan Curtin --- src/mlpack/methods/det/dtree_impl.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/det/dtree_impl.hpp b/src/mlpack/methods/det/dtree_impl.hpp index ad8ea873d3..1d217bf1ab 100644 --- a/src/mlpack/methods/det/dtree_impl.hpp +++ b/src/mlpack/methods/det/dtree_impl.hpp @@ -15,8 +15,9 @@ #include #include -namespace details -{ +namespace mlpack { +namespace det { +namespace details { /** * This one sorts and scand the given per-dimension extract and puts all splits From 1892cb4763483b0e460a12678c5fe9ffdcf7f22d Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sun, 16 Jan 2022 16:03:30 +0000 Subject: [PATCH 38/50] Compiling locally, adding all modifications Signed-off-by: Omar Shrit --- src/mlpack/methods/det/dtree_impl.hpp | 87 ++++++++++++++------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/src/mlpack/methods/det/dtree_impl.hpp b/src/mlpack/methods/det/dtree_impl.hpp index 1d217bf1ab..92b1803408 100644 --- a/src/mlpack/methods/det/dtree_impl.hpp +++ b/src/mlpack/methods/det/dtree_impl.hpp @@ -166,7 +166,7 @@ mlpack::det::DTree::DTree() : { /* Nothing to do. */ } template -mlpack::det::DTree::DTree(const DTree& obj) : +DTree::DTree(const DTree& obj) : start(obj.start), end(obj.end), maxVals(obj.maxVals), @@ -188,7 +188,7 @@ mlpack::det::DTree::DTree(const DTree& obj) : } template -mlpack::det::DTree& mlpack::det::DTree::operator=( +DTree& mlpack::det::DTree::operator=( const mlpack::det::DTree& obj) { if (this == &obj) @@ -222,7 +222,7 @@ mlpack::det::DTree& mlpack::det::DTree::oper } template -mlpack::det::DTree::DTree(DTree&& obj): +DTree::DTree(DTree&& obj): start(obj.start), end(obj.end), maxVals(std::move(obj.maxVals)), @@ -258,8 +258,8 @@ mlpack::det::DTree::DTree(DTree&& obj): } template -mlpack::det::DTree& mlpack::det::DTree::operator=( - mlpack::det::DTree&& obj) +DTree& DTree::operator=( + DTree&& obj) { if (this == &obj) return *this; @@ -310,9 +310,9 @@ mlpack::det::DTree& mlpack::det::DTree::oper // Root node initializers. template -mlpack::det::DTree::DTree(const StatType& maxVals, - const StatType& minVals, - const size_t totalPoints) : +DTree::DTree(const StatType& maxVals, + const StatType& minVals, + const size_t totalPoints) : start(0), end(totalPoints), maxVals(maxVals), @@ -354,11 +354,11 @@ mlpack::det::DTree::DTree(MatType & data) : // Non-root node initializers. template -mlpack::det::DTree::DTree(const StatType& maxVals, - const StatType& minVals, - const size_t start, - const size_t end, - const double logNegError) : +DTree::DTree(const StatType& maxVals, + const StatType& minVals, + const size_t start, + const size_t end, + const double logNegError) : start(start), end(end), maxVals(maxVals), @@ -378,11 +378,11 @@ mlpack::det::DTree::DTree(const StatType& maxVals, { /* Nothing to do. */ } template -mlpack::det::DTree::DTree(const StatType& maxVals, - const StatType& minVals, - const size_t totalPoints, - const size_t start, - const size_t end) : +DTree::DTree(const StatType& maxVals, + const StatType& minVals, + const size_t totalPoints, + const size_t start, + const size_t end) : start(start), end(end), maxVals(maxVals), @@ -402,7 +402,7 @@ mlpack::det::DTree::DTree(const StatType& maxVals, { /* Nothing to do. */ } template -mlpack::det::DTree::~DTree() +DTree::~DTree() { delete left; delete right; @@ -411,7 +411,7 @@ mlpack::det::DTree::~DTree() // This function computes the log-l2-negative-error of a given node from the // formula R(t) = log(|t|^2 / (N^2 V_t)). template -double mlpack::det::DTree::LogNegativeError(const size_t totalPoints) const +double DTree::LogNegativeError(const size_t totalPoints) const { // log(-|t|^2 / (N^2 V_t)) = log(-1) + 2 log(|t|) - 2 log(N) - log(V_t). double err = 2 * std::log((double) (end - start)) - @@ -432,12 +432,12 @@ double mlpack::det::DTree::LogNegativeError(const size_t total // all possible splits. The dataset is the full data set but the start and // end are used to obtain the point in this node. template -bool mlpack::det::DTree::FindSplit(const MatType& data, - size_t& splitDim, - ElemType& splitValue, - double& leftError, - double& rightError, - const size_t minLeafSize) const +bool DTree::FindSplit(const MatType& data, + size_t& splitDim, + ElemType& splitValue, + double& leftError, + double& rightError, + const size_t minLeafSize) const { typedef std::pair SplitItem; @@ -550,7 +550,7 @@ bool mlpack::det::DTree::FindSplit(const MatType& data, } template -size_t mlpack::det::DTree::SplitData( +size_t DTree::SplitData( MatType& data, const size_t splitDim, const ElemType splitValue, @@ -586,11 +586,11 @@ size_t mlpack::det::DTree::SplitData( // Greedily expand the tree. template -double mlpack::det::DTree::Grow(MatType& data, - arma::Col& oldFromNew, - const bool useVolReg, - const size_t maxLeafSize, - const size_t minLeafSize) +double DTree::Grow(MatType& data, + arma::Col& oldFromNew, + const bool useVolReg, + const size_t maxLeafSize, + const size_t minLeafSize) { mlpack::Log::Assert(data.n_rows == maxVals.n_elem); mlpack::Log::Assert(data.n_rows == minVals.n_elem); @@ -735,9 +735,9 @@ double mlpack::det::DTree::Grow(MatType& data, template -double mlpack::det::DTree::PruneAndUpdate(const double oldAlpha, - const size_t points, - const bool useVolReg) +double DTree::PruneAndUpdate(const double oldAlpha, + const size_t points, + const bool useVolReg) { // Compute gT. if (subtreeLeaves == 1) // If we are a leaf... @@ -850,7 +850,7 @@ double mlpack::det::DTree::PruneAndUpdate(const double oldAlph // Future improvement: Open up the range with epsilons on both sides where // epsilon depends on the density near the boundary. template -bool mlpack::det::DTree::WithinRange(const VecType& query) const +bool DTree::WithinRange(const VecType& query) const { for (size_t i = 0; i < query.n_elem; ++i) if ((query[i] < minVals[i]) || (query[i] > maxVals[i])) @@ -861,7 +861,7 @@ bool mlpack::det::DTree::WithinRange(const VecType& query) con template -double mlpack::det::DTree::ComputeValue(const VecType& query) const +double DTree::ComputeValue(const VecType& query) const { mlpack::Log::Assert(query.n_elem == maxVals.n_elem); @@ -884,7 +884,7 @@ double mlpack::det::DTree::ComputeValue(const VecType& query) // Index the buckets for possible usage later. template -TagType mlpack::det::DTree::TagTree(const TagType& tag, bool every) +TagType DTree::TagTree(const TagType& tag, bool every) { if (subtreeLeaves == 1) { @@ -906,7 +906,7 @@ TagType mlpack::det::DTree::TagTree(const TagType& tag, bool e } template -TagType mlpack::det::DTree::FindBucket(const VecType& query) const +TagType DTree::FindBucket(const VecType& query) const { mlpack::Log::Assert(query.n_elem == maxVals.n_elem); @@ -932,7 +932,7 @@ TagType mlpack::det::DTree::FindBucket(const VecType& query) c } template -void mlpack::det::DTree::ComputeVariableImportance( +void DTree::ComputeVariableImportance( arma::vec& importances) const { // Clear and set to right size. @@ -961,8 +961,8 @@ void mlpack::det::DTree::ComputeVariableImportance( } template -void mlpack::det::DTree::FillMinMax(const StatType& mins, - const StatType& maxs) +void DTree::FillMinMax(const StatType& mins, + const StatType& maxs) { if (!root) { @@ -1035,3 +1035,6 @@ void mlpack::det::DTree::serialize(Archive& ar, FillMinMax(minVals, maxVals); } } + +} // namespace det +} // namespace mlpack From 39d22fc4fbf321f1cc5f82b24f2c8f8304cdcae5 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sun, 16 Jan 2022 16:21:27 +0000 Subject: [PATCH 39/50] Move constructor to implementation Signed-off-by: Omar Shrit --- .../core/kernels/pspectrum_string_kernel.hpp | 59 +------------------ .../kernels/pspectrum_string_kernel_impl.hpp | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+), 57 deletions(-) diff --git a/src/mlpack/core/kernels/pspectrum_string_kernel.hpp b/src/mlpack/core/kernels/pspectrum_string_kernel.hpp index dc5ff24415..d76acb24a2 100644 --- a/src/mlpack/core/kernels/pspectrum_string_kernel.hpp +++ b/src/mlpack/core/kernels/pspectrum_string_kernel.hpp @@ -72,64 +72,9 @@ class PSpectrumStringKernel * @param datasets Sets of string data. * @param p The length of substrings to search. */ - PSpectrumStringKernel(const std::vector >& datasets, - const size_t p) : p(p) - { - // We have to assemble the counts of substrings. This is not a particularly - // fast operation, unfortunately, but it only needs to be done once. - Log::Info << "Assembling counts of substrings of length " << p << "." - << std::endl; + inline PSpectrumStringKernel(const std::vector >& datasets, + const size_t p); - // Resize for number of datasets. - counts.resize(datasets.size()); - - for (size_t dataset = 0; dataset < datasets.size(); ++dataset) - { - const std::vector& set = datasets[dataset]; - - // Resize for number of strings in dataset. - counts[dataset].resize(set.size()); - - // Inspect each string in the dataset. - for (size_t index = 0; index < set.size(); ++index) - { - // Convenience references. - const std::string& str = set[index]; - std::map& mapping = counts[dataset][index]; - - size_t start = 0; - while ((start + p) <= str.length()) - { - std::string sub = str.substr(start, p); - - // Convert all characters to lowercase. - bool invalid = false; - for (size_t j = 0; j < p; ++j) - { - if (!isalnum(sub[j])) - { - invalid = true; - break; // Only consider substrings with alphanumerics. - } - - sub[j] = tolower(sub[j]); - } - - // Increment position in string. - ++start; - - if (!invalid) - { - // Add to the map. - ++mapping[sub]; - } - } - } - } - - Log::Info << "Substring extraction complete." << std::endl; - } - /** * Evaluate the kernel for the string indices given. As mentioned in the * class documentation, a and b should be 2-element vectors, where the first diff --git a/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp b/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp index cf04b8bdd6..616ae8c498 100644 --- a/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp +++ b/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp @@ -22,6 +22,65 @@ namespace mlpack { namespace kernel { +inline PSpectrumStringKernel::PSpectrumStringKernel( + const std::vector >& datasets, + const size_t p) : p(p) +{ + // We have to assemble the counts of substrings. This is not a particularly + // fast operation, unfortunately, but it only needs to be done once. + Log::Info << "Assembling counts of substrings of length " << p << "." + << std::endl; + + // Resize for number of datasets. + counts.resize(datasets.size()); + + for (size_t dataset = 0; dataset < datasets.size(); ++dataset) + { + const std::vector& set = datasets[dataset]; + + // Resize for number of strings in dataset. + counts[dataset].resize(set.size()); + + // Inspect each string in the dataset. + for (size_t index = 0; index < set.size(); ++index) + { + // Convenience references. + const std::string& str = set[index]; + std::map& mapping = counts[dataset][index]; + + size_t start = 0; + while ((start + p) <= str.length()) + { + std::string sub = str.substr(start, p); + + // Convert all characters to lowercase. + bool invalid = false; + for (size_t j = 0; j < p; ++j) + { + if (!isalnum(sub[j])) + { + invalid = true; + break; // Only consider substrings with alphanumerics. + } + + sub[j] = tolower(sub[j]); + } + + // Increment position in string. + ++start; + + if (!invalid) + { + // Add to the map. + ++mapping[sub]; + } + } + } + } + + Log::Info << "Substring extraction complete." << std::endl; +} + /** * Evaluate the kernel for the string indices given. As mentioned in the class * documentation, a and b should be 2-element vectors, where the first element From 164cafcd8702be52656ba080c000d6af697afabe Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sun, 16 Jan 2022 18:20:04 +0000 Subject: [PATCH 40/50] Commenting all #defines that are causing the problems Signed-off-by: Omar Shrit --- src/mlpack/core/data/load.hpp | 47 +----- src/mlpack/core/data/load_image.hpp | 146 +++++------------- src/mlpack/core/data/load_image_impl.hpp | 116 +++++++++++++- .../kernels/pspectrum_string_kernel_impl.hpp | 1 - 4 files changed, 157 insertions(+), 153 deletions(-) diff --git a/src/mlpack/core/data/load.hpp b/src/mlpack/core/data/load.hpp index d4f35954dd..195838affe 100644 --- a/src/mlpack/core/data/load.hpp +++ b/src/mlpack/core/data/load.hpp @@ -20,10 +20,11 @@ #include "format.hpp" #include "dataset_mapper.hpp" +#include "detect_file_type.hpp" #include "image_info.hpp" #include "load_csv.hpp" #include "load_arff.hpp" -#include "detect_file_type.hpp" +#include "load_image.hpp" namespace mlpack { namespace data /** Functions to load and save matrices and models. */ { @@ -250,46 +251,6 @@ bool Load(const std::string& filename, const bool fatal = false, format f = format::autodetect); -/** - * Image load/save interfaces. - */ - -/** - * Load the image file into the given matrix. - * - * @param filename Name of the image file. - * @param matrix Matrix to load the image into. - * @param info An object of ImageInfo class. - * @param fatal If an error should be reported as fatal (default false). - * @return Boolean value indicating success or failure of load. - */ -template -bool Load(const std::string& filename, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal = false); - -/** - * Load the image file into the given matrix. - * - * @param files A vector consisting of filenames. - * @param matrix Matrix to save the image from. - * @param info An object of ImageInfo class. - * @param fatal If an error should be reported as fatal (default false). - * @return Boolean value indicating success or failure of load. - */ -template -bool Load(const std::vector& files, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal = false); - -// Implementation found in load_image.hpp. -inline bool LoadImage(const std::string& filename, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal = false); - } // namespace data } // namespace mlpack @@ -299,9 +260,5 @@ inline bool LoadImage(const std::string& filename, #include "load_model_impl.hpp" // Include implementation of Load() for vectors. #include "load_vec_impl.hpp" -// Include implementation of Load() for images. -#include "load_image_impl.hpp" -// Include implementation of Load() for images. -#include "load_image.hpp" #endif diff --git a/src/mlpack/core/data/load_image.hpp b/src/mlpack/core/data/load_image.hpp index b8aabf9fcf..19b115670e 100644 --- a/src/mlpack/core/data/load_image.hpp +++ b/src/mlpack/core/data/load_image.hpp @@ -9,121 +9,59 @@ * 3-clause BSD license along with mlpack. If not, see * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ -#include "load.hpp" + +#ifndef MLPACK_CORE_DATA_LOAD_IMAGE_HPP +#define MLPACK_CORE_DATA_LOAD_IMAGE_HPP + #include "image_info.hpp" -#ifdef HAS_STB - -// The definition of STB_IMAGE_IMPLEMENTATION means that the implementation will -// be included here directly. -#define STB_IMAGE_STATIC -#define STB_IMAGE_IMPLEMENTATION - -#include - namespace mlpack { namespace data { +/** + * Image load/save interfaces. + */ + +/** + * Load the image file into the given matrix. + * + * @param filename Name of the image file. + * @param matrix Matrix to load the image into. + * @param info An object of ImageInfo class. + * @param fatal If an error should be reported as fatal (default false). + * @return Boolean value indicating success or failure of load. + */ +template +bool Load(const std::string& filename, + arma::Mat& matrix, + ImageInfo& info, + const bool fatal = false); + +/** + * Load the image file into the given matrix. + * + * @param files A vector consisting of filenames. + * @param matrix Matrix to save the image from. + * @param info An object of ImageInfo class. + * @param fatal If an error should be reported as fatal (default false). + * @return Boolean value indicating success or failure of load. + */ +template +bool Load(const std::vector& files, + arma::Mat& matrix, + ImageInfo& info, + const bool fatal = false); + +// Implementation found in load_image.hpp. inline bool LoadImage(const std::string& filename, arma::Mat& matrix, ImageInfo& info, - const bool fatal) -{ - unsigned char* image; - - if (!ImageFormatSupported(filename)) - { - std::ostringstream oss; - oss << "Load(): file type " << Extension(filename) << " not supported. "; - oss << "Currently it supports: "; - for (auto extension : loadFileTypes) - oss << " " << extension; - oss << "." << std::endl; - - if (fatal) - { - Log::Fatal << oss.str(); - } - else - { - Log::Warn << oss.str(); - } - - return false; - } - - // Temporary variables needed as stb_image.h supports int parameters. - int tempWidth, tempHeight, tempChannels; - - // For grayscale images. - if (info.Channels() == 1) - { - image = stbi_load(filename.c_str(), &tempWidth, &tempHeight, &tempChannels, - STBI_grey); - } - else - { - image = stbi_load(filename.c_str(), &tempWidth, &tempHeight, &tempChannels, - STBI_rgb); - } - - if (!image) - { - if (fatal) - { - Log::Fatal << "Load(): failed to load image '" << filename << "': " - << stbi_failure_reason() << std::endl; - } - else - { - Log::Warn << "Load(): failed to load image '" << filename << "': " - << stbi_failure_reason() << std::endl; - } - - return false; - } - - info.Width() = tempWidth; - info.Height() = tempHeight; - info.Channels() = tempChannels; - - // Copy image into armadillo Mat. - matrix = arma::Mat(image, info.Width() * info.Height() * - info.Channels(), 1, true, true); - - // Free the image pointer. - free(image); - return true; -} + const bool fatal = false); } // namespace data } // namespace mlpack -#else - -namespace mlpack { -namespace data { - -inline bool LoadImage(const std::string& /* filename */, - arma::Mat& /* matrix */, - ImageInfo& /* info */, - const bool fatal) -{ - if (fatal) - { - Log::Fatal << "Load(): mlpack was not compiled with STB support, so images " - << "cannot be loaded!" << std::endl; - } - else - { - Log::Warn << "Load(): mlpack was not compiled with STB support, so images " - << "cannot be loaded!" << std::endl; - } - - return false; -} - -} // namespace data -} // namespace mlpack +// Include implementation of Load() for images. +#include "load_image_impl.hpp" #endif diff --git a/src/mlpack/core/data/load_image_impl.hpp b/src/mlpack/core/data/load_image_impl.hpp index 9a757838b9..2ce3dbffdc 100644 --- a/src/mlpack/core/data/load_image_impl.hpp +++ b/src/mlpack/core/data/load_image_impl.hpp @@ -14,7 +14,7 @@ #define MLPACK_CORE_DATA_LOAD_IMAGE_IMPL_HPP // In case it hasn't been included yet. -#include "load.hpp" +#include "load_image.hpp" namespace mlpack { namespace data { @@ -90,7 +90,117 @@ bool Load(const std::vector& files, return true; } -} // namespace data -} // namespace mlpack +// #ifdef HAS_STB +// // The definition of STB_IMAGE_IMPLEMENTATION means that the implementation will +// // be included here directly. +// #define STB_IMAGE_STATIC +// #define STB_IMAGE_IMPLEMENTATION + +#include + +inline bool LoadImage(const std::string& filename, + arma::Mat& matrix, + ImageInfo& info, + const bool fatal) +{ + unsigned char* image; + + if (!ImageFormatSupported(filename)) + { + std::ostringstream oss; + oss << "Load(): file type " << Extension(filename) << " not supported. "; + oss << "Currently it supports: "; + for (auto extension : loadFileTypes) + oss << " " << extension; + oss << "." << std::endl; + + if (fatal) + { + Log::Fatal << oss.str(); + } + else + { + Log::Warn << oss.str(); + } + + return false; + } + + // Temporary variables needed as stb_image.h supports int parameters. + int tempWidth, tempHeight, tempChannels; + + // For grayscale images. + if (info.Channels() == 1) + { + image = stbi_load(filename.c_str(), &tempWidth, &tempHeight, &tempChannels, + STBI_grey); + } + else + { + image = stbi_load(filename.c_str(), &tempWidth, &tempHeight, &tempChannels, + STBI_rgb); + } + + if (!image) + { + if (fatal) + { + Log::Fatal << "Load(): failed to load image '" << filename << "': " + << stbi_failure_reason() << std::endl; + } + else + { + Log::Warn << "Load(): failed to load image '" << filename << "': " + << stbi_failure_reason() << std::endl; + } + + return false; + } + + info.Width() = tempWidth; + info.Height() = tempHeight; + info.Channels() = tempChannels; + + // Copy image into armadillo Mat. + matrix = arma::Mat(image, info.Width() * info.Height() * + info.Channels(), 1, true, true); + + // Free the image pointer. + free(image); + return true; +} + +} +} + +//#else + +// namespace mlpack { +// namespace data { + + +// inline bool LoadImage(const std::string& /* filename */, +// arma::Mat& /* matrix */, +// ImageInfo& /* info */, +// const bool fatal) +// { +// if (fatal) +// { +// Log::Fatal << "Load(): mlpack was not compiled with STB support, so images " +// << "cannot be loaded!" << std::endl; +// } +// else +// { +// Log::Warn << "Load(): mlpack was not compiled with STB support, so images " +// << "cannot be loaded!" << std::endl; +// } + +// return false; +// } + +// } // namespace data +// } // namespace mlpack + +//#endif #endif diff --git a/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp b/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp index 616ae8c498..b00b7b7731 100644 --- a/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp +++ b/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp @@ -77,7 +77,6 @@ inline PSpectrumStringKernel::PSpectrumStringKernel( } } } - Log::Info << "Substring extraction complete." << std::endl; } From 8087b32684a9b883eb56de40b314dc102baa9cb5 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sun, 23 Jan 2022 19:38:29 +0000 Subject: [PATCH 41/50] Apply @rcurtin patch to fix the STB issue. Signed-off-by: Omar Shrit --- src/mlpack/core/data/image_info_impl.hpp | 28 +++++++--- src/mlpack/core/data/load_image.hpp | 10 ++++ src/mlpack/core/data/load_image_impl.hpp | 68 ++++++++++-------------- src/mlpack/core/data/save_image.hpp | 21 ++++---- 4 files changed, 67 insertions(+), 60 deletions(-) diff --git a/src/mlpack/core/data/image_info_impl.hpp b/src/mlpack/core/data/image_info_impl.hpp index 3040a38415..ea14d6de6c 100644 --- a/src/mlpack/core/data/image_info_impl.hpp +++ b/src/mlpack/core/data/image_info_impl.hpp @@ -13,6 +13,24 @@ #ifndef MLPACK_CORE_DATA_IMAGE_INFO_IMPL_HPP #define MLPACK_CORE_DATA_IMAGE_INFO_IMPL_HPP +namespace mlpack { +namespace data { + +inline const std::vector LoadFileTypes() +{ + return std::vector({"jpg", "png", "tga", + "bmp", "psd", "gif", "hdr", "pic", "pnm", "jpeg"}); +} + +inline const std::vector SaveFileTypes() +{ + return std::vector({"jpg", "png", "tga", + "bmp", "hdr"}); +} + +} +} + #ifdef HAS_STB // Compile this only if stb is present. // In case it hasn't been included yet. @@ -21,18 +39,12 @@ namespace mlpack { namespace data { -static const std::vector loadFileTypes({"jpg", "png", "tga", - "bmp", "psd", "gif", "hdr", "pic", "pnm", "jpeg"}); - -static const std::vector saveFileTypes({"jpg", "png", "tga", - "bmp", "hdr"}); - inline bool ImageFormatSupported(const std::string& fileName, const bool save) { if (save) { // Iterate over all supported file types that can be saved. - for (auto extension : saveFileTypes) + for (auto extension : SaveFileTypes()) { if (extension == Extension(fileName)) return true; @@ -41,7 +53,7 @@ inline bool ImageFormatSupported(const std::string& fileName, const bool save) else { // Iterate over all supported file types that can be loaded. - for (auto extension : loadFileTypes) + for (auto extension : LoadFileTypes()) { if (extension == Extension(fileName)) return true; diff --git a/src/mlpack/core/data/load_image.hpp b/src/mlpack/core/data/load_image.hpp index 19b115670e..fb362ae955 100644 --- a/src/mlpack/core/data/load_image.hpp +++ b/src/mlpack/core/data/load_image.hpp @@ -15,6 +15,16 @@ #include "image_info.hpp" +#ifdef HAS_STB + +// The definition of STB_IMAGE_IMPLEMENTATION means that the implementation will +// be included here directly. +#define STB_IMAGE_STATIC +#define STB_IMAGE_IMPLEMENTATION +#include + +#endif + namespace mlpack { namespace data { diff --git a/src/mlpack/core/data/load_image_impl.hpp b/src/mlpack/core/data/load_image_impl.hpp index 2ce3dbffdc..3ebcade286 100644 --- a/src/mlpack/core/data/load_image_impl.hpp +++ b/src/mlpack/core/data/load_image_impl.hpp @@ -9,12 +9,12 @@ * 3-clause BSD license along with mlpack. If not, see * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ - #ifndef MLPACK_CORE_DATA_LOAD_IMAGE_IMPL_HPP #define MLPACK_CORE_DATA_LOAD_IMAGE_IMPL_HPP // In case it hasn't been included yet. #include "load_image.hpp" +#include "image_info.hpp" namespace mlpack { namespace data { @@ -90,14 +90,7 @@ bool Load(const std::vector& files, return true; } -// #ifdef HAS_STB - -// // The definition of STB_IMAGE_IMPLEMENTATION means that the implementation will -// // be included here directly. -// #define STB_IMAGE_STATIC -// #define STB_IMAGE_IMPLEMENTATION - -#include +#ifdef HAS_STB inline bool LoadImage(const std::string& filename, arma::Mat& matrix, @@ -111,7 +104,8 @@ inline bool LoadImage(const std::string& filename, std::ostringstream oss; oss << "Load(): file type " << Extension(filename) << " not supported. "; oss << "Currently it supports: "; - for (auto extension : loadFileTypes) + auto x = LoadFileTypes(); + for (auto extension : x) oss << " " << extension; oss << "." << std::endl; @@ -171,36 +165,30 @@ inline bool LoadImage(const std::string& filename, return true; } -} +#else // HAS_STB + +inline bool LoadImage(const std::string& /* filename */, + arma::Mat& /* matrix */, + ImageInfo& /* info */, + const bool fatal) +{ + if (fatal) + { + Log::Fatal << "Load(): mlpack was not compiled with STB support, so images " + << "cannot be loaded!" << std::endl; + } + else + { + Log::Warn << "Load(): mlpack was not compiled with STB support, so images " + << "cannot be loaded!" << std::endl; + } + + return false; +} + +#endif + +} } -//#else - -// namespace mlpack { -// namespace data { - - -// inline bool LoadImage(const std::string& /* filename */, -// arma::Mat& /* matrix */, -// ImageInfo& /* info */, -// const bool fatal) -// { -// if (fatal) -// { -// Log::Fatal << "Load(): mlpack was not compiled with STB support, so images " -// << "cannot be loaded!" << std::endl; -// } -// else -// { -// Log::Warn << "Load(): mlpack was not compiled with STB support, so images " -// << "cannot be loaded!" << std::endl; -// } - -// return false; -// } - -// } // namespace data -// } // namespace mlpack - -//#endif #endif diff --git a/src/mlpack/core/data/save_image.hpp b/src/mlpack/core/data/save_image.hpp index 353ee36a7a..c98e38daf3 100644 --- a/src/mlpack/core/data/save_image.hpp +++ b/src/mlpack/core/data/save_image.hpp @@ -9,23 +9,18 @@ * 3-clause BSD license along with mlpack. If not, see * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ +#ifndef MLPACK_CORE_DATA_SAVE_IMAGE_HPP +#define MLPACK_CORE_DATA_SAVE_IMAGE_HPP + #include "save.hpp" #ifdef HAS_STB -// The implementation of the functions is included directly, so we need to make -// sure it doesn't get included twice. This is to work around a bug in old -// versions of STB where not all functions were correctly marked static. +// Include STB functions. Note that we include the implementations, too, and +// all functions will be marked as static. #define STB_IMAGE_WRITE_STATIC -#ifndef STB_IMAGE_WRITE_IMPLEMENTATION - #define STB_IMAGE_WRITE_IMPLEMENTATION -#else - #undef STB_IMAGE_WRITE_IMPLEMENTATION -#endif +#define STB_IMAGE_WRITE_IMPLEMENTATION #include -#ifndef STB_IMAGE_WRITE_IMPLEMENTATION - #define STB_IMAGE_WRITE_IMPLEMENTATION -#endif namespace mlpack { namespace data { @@ -41,7 +36,7 @@ inline bool SaveImage(const std::string& filename, std::ostringstream oss; oss << "Save(): file type " << Extension(filename) << " not supported.\n"; oss << "Currently image saving supports "; - for (auto extension : saveFileTypes) + for (auto extension : SaveFileTypes()) oss << ", " << extension; oss << "." << std::endl; @@ -150,3 +145,5 @@ inline bool SaveImage(const std::string& /* filename */, } // namespace mlpack #endif + +#endif From 6793b9e123955a3f4224816e6c949c7efe768ebd Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 24 Jan 2022 19:51:49 +0100 Subject: [PATCH 42/50] Update src/mlpack/core/data/image_info_impl.hpp Co-authored-by: Ryan Curtin --- src/mlpack/core/data/image_info_impl.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mlpack/core/data/image_info_impl.hpp b/src/mlpack/core/data/image_info_impl.hpp index ea14d6de6c..e9c243662e 100644 --- a/src/mlpack/core/data/image_info_impl.hpp +++ b/src/mlpack/core/data/image_info_impl.hpp @@ -24,8 +24,7 @@ inline const std::vector LoadFileTypes() inline const std::vector SaveFileTypes() { - return std::vector({"jpg", "png", "tga", - "bmp", "hdr"}); + return std::vector({"jpg", "png", "tga", "bmp", "hdr"}); } } From 8d291a47bb31a89002c42b3332ee59e54fb24fb1 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 24 Jan 2022 19:51:55 +0100 Subject: [PATCH 43/50] Update src/mlpack/core/data/image_info_impl.hpp Co-authored-by: Ryan Curtin --- src/mlpack/core/data/image_info_impl.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/core/data/image_info_impl.hpp b/src/mlpack/core/data/image_info_impl.hpp index e9c243662e..f963ae28d8 100644 --- a/src/mlpack/core/data/image_info_impl.hpp +++ b/src/mlpack/core/data/image_info_impl.hpp @@ -18,8 +18,8 @@ namespace data { inline const std::vector LoadFileTypes() { - return std::vector({"jpg", "png", "tga", - "bmp", "psd", "gif", "hdr", "pic", "pnm", "jpeg"}); + return std::vector({"jpg", "png", "tga", "bmp", "psd", "gif", + "hdr", "pic", "pnm", "jpeg"}); } inline const std::vector SaveFileTypes() From 824840914a333499817e2368530fe6628a37e501 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 24 Jan 2022 21:20:12 +0000 Subject: [PATCH 44/50] Adjust namespace of mlpack::Log Signed-off-by: Omar Shrit --- src/mlpack/core/math/lin_alg_impl.hpp | 2 - src/mlpack/methods/det/dtree_impl.hpp | 42 +++++++++---------- .../q_networks/categorical_dqn.hpp | 20 ++++----- 3 files changed, 29 insertions(+), 35 deletions(-) diff --git a/src/mlpack/core/math/lin_alg_impl.hpp b/src/mlpack/core/math/lin_alg_impl.hpp index 7e0d1252e9..76ba56b861 100644 --- a/src/mlpack/core/math/lin_alg_impl.hpp +++ b/src/mlpack/core/math/lin_alg_impl.hpp @@ -89,8 +89,6 @@ inline void WhitenUsingSVD(const arma::mat& x, */ inline void RandVector(arma::vec& v) { - v.zeros(); - for (size_t i = 0; i + 1 < v.n_elem; i += 2) { double a = math::Random(); diff --git a/src/mlpack/methods/det/dtree_impl.hpp b/src/mlpack/methods/det/dtree_impl.hpp index 92b1803408..016bf2df79 100644 --- a/src/mlpack/methods/det/dtree_impl.hpp +++ b/src/mlpack/methods/det/dtree_impl.hpp @@ -93,7 +93,7 @@ void ExtractSplits(std::vector>& splitVec, const size_t minLeafSize) { // It's common sense, but we also use it in a check later. - mlpack::Log::Assert(minLeafSize > 0); + Log::Assert(minLeafSize > 0); typedef std::pair SplitItem; const size_t n_elem = end - start; @@ -116,7 +116,7 @@ void ExtractSplits(std::vector>& splitVec, const ElemType newVal = valsVec[i]; if (lastVal < ElemType(0) && newVal > ElemType(0) && zeroes > 0) { - mlpack::Log::Assert(padding == 0); // We should arrive here once! + Log::Assert(padding == 0); // We should arrive here once! // The minLeafSize > 0 also guarantees we're not entering right at the // start. @@ -148,9 +148,8 @@ void ExtractSplits(std::vector>& splitVec, } // namespace details template -mlpack::det::DTree::DTree() : - start(0), - end(0), +DTree::DTree() : + end(0), splitDim(size_t(-1)), splitValue(std::numeric_limits::max()), logNegError(-DBL_MAX), @@ -188,10 +187,8 @@ DTree::DTree(const DTree& obj) : } template -DTree& mlpack::det::DTree::operator=( - const mlpack::det::DTree& obj) -{ - if (this == &obj) +DTree& DTree::operator=( + const DTree::DTree(const StatType& maxVals, { /* Nothing to do. */ } template -mlpack::det::DTree::DTree(MatType & data) : - start(0), - end(data.n_cols), +DTree::DTree(MatType & data) : + end(data.n_cols), maxVals(arma::max(data, 1)), minVals(arma::min(data, 1)), splitDim(size_t(-1)), @@ -443,8 +439,8 @@ bool DTree::FindSplit(const MatType& data, // Ensure the dimensionality of the data is the same as the dimensionality of // the bounding rectangle. - mlpack::Log::Assert(data.n_rows == maxVals.n_elem); - mlpack::Log::Assert(data.n_rows == minVals.n_elem); + Log::Assert(data.n_rows == maxVals.n_elem); + Log::Assert(data.n_rows == minVals.n_elem); const size_t points = end - start; @@ -503,7 +499,7 @@ bool DTree::FindSplit(const MatType& data, { // Ensure that the right node will have at least the minimum number of // points. - mlpack::Log::Assert((points - position) >= minLeafSize); + Log::Assert((points - position) >= minLeafSize); // Now we have to see if the error will be reduced. Simple manipulation // of the error function gives us the condition we must satisfy: @@ -592,8 +588,8 @@ double DTree::Grow(MatType& data, const size_t maxLeafSize, const size_t minLeafSize) { - mlpack::Log::Assert(data.n_rows == maxVals.n_elem); - mlpack::Log::Assert(data.n_rows == minVals.n_elem); + Log::Assert(data.n_rows == maxVals.n_elem); + Log::Assert(data.n_rows == minVals.n_elem); double leftG, rightG; @@ -667,7 +663,7 @@ double DTree::Grow(MatType& data, else { // We can make this a leaf node. - mlpack::Log::Assert((size_t) (end - start) >= minLeafSize); + Log::Assert((size_t) (end - start) >= minLeafSize); subtreeLeaves = 1; subtreeLeavesLogNegError = logNegError; } @@ -821,7 +817,7 @@ double DTree::PruneAndUpdate(const double oldAlpha, gT = alphaUpper - std::log((double) (subtreeLeaves - 1)); } - mlpack::Log::Assert(gT < std::numeric_limits::max()); + Log::Assert(gT < std::numeric_limits::max()); return std::min((double) gT, std::min(leftG, rightG)); } @@ -863,7 +859,7 @@ bool DTree::WithinRange(const VecType& query) const template double DTree::ComputeValue(const VecType& query) const { - mlpack::Log::Assert(query.n_elem == maxVals.n_elem); + Log::Assert(query.n_elem == maxVals.n_elem); if (root == 1) // If we are the root... { @@ -908,7 +904,7 @@ TagType DTree::TagTree(const TagType& tag, bool every) template TagType DTree::FindBucket(const VecType& query) const { - mlpack::Log::Assert(query.n_elem == maxVals.n_elem); + Log::Assert(query.n_elem == maxVals.n_elem); if (root == 1) // If we are the root... { @@ -985,8 +981,8 @@ void DTree::FillMinMax(const StatType& mins, template template -void mlpack::det::DTree::serialize(Archive& ar, - const uint32_t /* version */) +void DTree::serialize(Archive& ar, + const uint32_t /* version */) { ar(CEREAL_NVP(start)); ar(CEREAL_NVP(end)); diff --git a/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp b/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp index 917636d87b..ba5c1f7abb 100644 --- a/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp @@ -81,21 +81,21 @@ class CategoricalDQN vMax(config.VMax()), isNoisy(isNoisy) { - network.Add(new mlpack::ann::Linear<>(inputDim, h1)); - network.Add(new mlpack::ann::ReLULayer<>()); + network.Add(new ann::Linear<>(inputDim, h1)); + network.Add(new ann::ReLULayer<>()); if (isNoisy) { noisyLayerIndex.push_back(network.Model().size()); - network.Add(new mlpack::ann::NoisyLinear<>(h1, h2)); - network.Add(new mlpack::ann::ReLULayer<>()); + network.Add(new ann::NoisyLinear<>(h1, h2)); + network.Add(new ann::ReLULayer<>()); noisyLayerIndex.push_back(network.Model().size()); - network.Add(new mlpack::ann::NoisyLinear<>(h2, outputDim * atomSize)); + network.Add(new ann::NoisyLinear<>(h2, outputDim * atomSize)); } else { - network.Add(new mlpack::ann::Linear<>(h1, h2)); - network.Add(new mlpack::ann::ReLULayer<>()); - network.Add(new mlpack::ann::Linear<>(h2, outputDim * atomSize)); + network.Add(new ann::Linear<>(h1, h2)); + network.Add(new ann::ReLULayer<>()); + network.Add(new ann::Linear<>(h2, outputDim * atomSize)); } } @@ -181,7 +181,7 @@ class CategoricalDQN { for (size_t i = 0; i < noisyLayerIndex.size(); ++i) { - boost::get*> + boost::get*> (network.Model()[noisyLayerIndex[i]])->ResetNoise(); } } @@ -234,7 +234,7 @@ class CategoricalDQN std::vector noisyLayerIndex; //! Locally-stored softmax activation function. - mlpack::ann::Softmax<> softMax; + ann::Softmax<> softMax; //! Locally-stored activations from softMax. arma::mat activations; From 58ffb75e0c06dbcd8ab0d2e8dec007d05ffb70ce Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 24 Jan 2022 22:15:55 +0000 Subject: [PATCH 45/50] Refactor save_image into save_image_impl finally done!! Signed-off-by: Omar Shrit --- src/mlpack/core/data/CMakeLists.txt | 1 + src/mlpack/core/data/load_image.hpp | 2 +- src/mlpack/core/data/load_image_impl.hpp | 6 +- src/mlpack/core/data/load_impl.hpp | 3 +- src/mlpack/core/data/save.hpp | 41 +---- src/mlpack/core/data/save_image.hpp | 165 +++++-------------- src/mlpack/core/data/save_image_impl.hpp | 195 +++++++++++++++++++++++ src/mlpack/core/data/save_impl.hpp | 60 ------- 8 files changed, 247 insertions(+), 226 deletions(-) create mode 100644 src/mlpack/core/data/save_image_impl.hpp diff --git a/src/mlpack/core/data/CMakeLists.txt b/src/mlpack/core/data/CMakeLists.txt index a5c0438bf7..0b83444c00 100644 --- a/src/mlpack/core/data/CMakeLists.txt +++ b/src/mlpack/core/data/CMakeLists.txt @@ -25,6 +25,7 @@ set(SOURCES save.hpp save_impl.hpp save_image.hpp + save_image_impl.hpp split_data.hpp string_algorithms.hpp imputer.hpp diff --git a/src/mlpack/core/data/load_image.hpp b/src/mlpack/core/data/load_image.hpp index fb362ae955..36e4015302 100644 --- a/src/mlpack/core/data/load_image.hpp +++ b/src/mlpack/core/data/load_image.hpp @@ -23,7 +23,7 @@ #define STB_IMAGE_IMPLEMENTATION #include -#endif +#endif // HAS_STB namespace mlpack { namespace data { diff --git a/src/mlpack/core/data/load_image_impl.hpp b/src/mlpack/core/data/load_image_impl.hpp index 3ebcade286..57b9d7b6fd 100644 --- a/src/mlpack/core/data/load_image_impl.hpp +++ b/src/mlpack/core/data/load_image_impl.hpp @@ -2,7 +2,7 @@ * @file core/data/load_image_impl.hpp * @author Mehul Kumar Nirala * - * An image loading utility implementation. + * An image loading utility implementation via STB. * * 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 @@ -188,7 +188,7 @@ inline bool LoadImage(const std::string& /* filename */, #endif -} -} +} // namespace data +} // namespace mlpack #endif diff --git a/src/mlpack/core/data/load_impl.hpp b/src/mlpack/core/data/load_impl.hpp index 226960a7e0..03993716db 100644 --- a/src/mlpack/core/data/load_impl.hpp +++ b/src/mlpack/core/data/load_impl.hpp @@ -16,13 +16,12 @@ // In case it hasn't already been included. #include "load.hpp" -#include #include +#include #include #include "extension.hpp" #include "detect_file_type.hpp" - #include "string_algorithms.hpp" namespace mlpack { diff --git a/src/mlpack/core/data/save.hpp b/src/mlpack/core/data/save.hpp index 793cf89abe..2faf180618 100644 --- a/src/mlpack/core/data/save.hpp +++ b/src/mlpack/core/data/save.hpp @@ -14,6 +14,7 @@ #ifndef MLPACK_CORE_DATA_SAVE_HPP #define MLPACK_CORE_DATA_SAVE_HPP +#include #include #include // Includes Armadillo. #include @@ -21,6 +22,7 @@ #include "format.hpp" #include "image_info.hpp" #include "detect_file_type.hpp" +#include "save_image.hpp" namespace mlpack { namespace data /** Functions to load and save matrices. */ { @@ -130,49 +132,10 @@ bool Save(const std::string& filename, const bool fatal = false, format f = format::autodetect); -/** - * Save the image file from the given matrix. - * - * @param filename Name of the image file. - * @param matrix Matrix to save the image from. - * @param info An object of ImageInfo class. - * @param fatal If an error should be reported as fatal (default false). - * @return Boolean value indicating success or failure of load. - */ -template -bool Save(const std::string& filename, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal = false); - -/** - * Save the image file from the given matrix. - * - * @param files A vector consisting of filenames. - * @param matrix Matrix to save the image from. - * @param info An object of ImageInfo class. - * @param fatal If an error should be reported as fatal (default false). - * @return Boolean value indicating success or failure of load. - */ -template -bool Save(const std::vector& files, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal = false); - -/** - * Helper function to save files. Implementation in save_image.hpp. - */ -inline bool SaveImage(const std::string& filename, - arma::Mat& image, - ImageInfo& info, - const bool fatal = false); - } // namespace data } // namespace mlpack // Include implementation. #include "save_impl.hpp" -#include "save_image.hpp" #endif diff --git a/src/mlpack/core/data/save_image.hpp b/src/mlpack/core/data/save_image.hpp index c98e38daf3..40a9186cdc 100644 --- a/src/mlpack/core/data/save_image.hpp +++ b/src/mlpack/core/data/save_image.hpp @@ -1,8 +1,8 @@ /** - * @file core/data/save_image.hpp - * @author Mehul Kumar Nirala + * @file core/data/save_image_impl.hpp + * @author Ryan Curtin * - * Implementation of image saving functionality via STB. + * Implementation of save functionality. * * 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 @@ -12,138 +12,61 @@ #ifndef MLPACK_CORE_DATA_SAVE_IMAGE_HPP #define MLPACK_CORE_DATA_SAVE_IMAGE_HPP -#include "save.hpp" +#include "image_info.hpp" #ifdef HAS_STB -// Include STB functions. Note that we include the implementations, too, and -// all functions will be marked as static. #define STB_IMAGE_WRITE_STATIC #define STB_IMAGE_WRITE_IMPLEMENTATION #include +#endif // HAS_STB + namespace mlpack { namespace data { +/** + * Save the image file from the given matrix. + * + * @param filename Name of the image file. + * @param matrix Matrix to save the image from. + * @param info An object of ImageInfo class. + * @param fatal If an error should be reported as fatal (default false). + * @return Boolean value indicating success or failure of load. + */ +template +bool Save(const std::string& filename, + arma::Mat& matrix, + ImageInfo& info, + const bool fatal = false); + +/** + * Save the image file from the given matrix. + * + * @param files A vector consisting of filenames. + * @param matrix Matrix to save the image from. + * @param info An object of ImageInfo class. + * @param fatal If an error should be reported as fatal (default false). + * @return Boolean value indicating success or failure of load. + */ +template +bool Save(const std::vector& files, + arma::Mat& matrix, + ImageInfo& info, + const bool fatal = false); + +/** + * Helper function to save files. Implementation in save_image.hpp. + */ inline bool SaveImage(const std::string& filename, arma::Mat& image, ImageInfo& info, - const bool fatal) -{ - // Check to see if the file type is supported. - if (!ImageFormatSupported(filename, true)) - { - std::ostringstream oss; - oss << "Save(): file type " << Extension(filename) << " not supported.\n"; - oss << "Currently image saving supports "; - for (auto extension : SaveFileTypes()) - oss << ", " << extension; - oss << "." << std::endl; + const bool fatal = false); - if (fatal) - { - Log::Fatal << oss.str(); - } - else - { - Log::Warn << oss.str(); - } +} //namespace data +} //namespace mlpack - return false; - } - - // Ensure the shape of the matrix is correct. - if (image.n_cols > 1) - { - Log::Warn << "Save(): given input image matrix contains more than 1 image." - << std::endl; - Log::Warn << "Only the first image will be saved!" << std::endl; - } - - if (info.Width() * info.Height() * info.Channels() != image.n_elem) - { - Log::Fatal << "data::Save(): The given image dimensions do not match the " - << "dimensions of the matrix to be saved!" << std::endl; - } - - bool status = false; - unsigned char* imageMem = image.memptr(); - - if ("png" == Extension(filename)) - { - status = stbi_write_png(filename.c_str(), info.Width(), info.Height(), - info.Channels(), imageMem, info.Width() * info.Channels()); - } - else if ("bmp" == Extension(filename)) - { - status = stbi_write_bmp(filename.c_str(), info.Width(), info.Height(), - info.Channels(), imageMem); - } - else if ("tga" == Extension(filename)) - { - status = stbi_write_tga(filename.c_str(), info.Width(), info.Height(), - info.Channels(), imageMem); - } - else if ("hdr" == Extension(filename)) - { - // We'll have to convert to float... - arma::fmat tmpImage = arma::conv_to::from(image); - status = stbi_write_hdr(filename.c_str(), info.Width(), info.Height(), - info.Channels(), tmpImage.memptr()); - } - else if ("jpg" == Extension(filename)) - { - status = stbi_write_jpg(filename.c_str(), info.Width(), info.Height(), - info.Channels(), imageMem, info.Quality()); - } - - if (!status) - { - if (fatal) - { - Log::Fatal << "Save(): error saving image to '" << filename << "'." - << std::endl; - } - else - { - Log::Warn << "Save(): error saving image to '" << filename << "'." - << std::endl; - } - } - - return status; -} - -} // namespace data -} // namespace mlpack - -#else - -namespace mlpack { -namespace data { - -inline bool SaveImage(const std::string& /* filename */, - arma::Mat& /* image */, - ImageInfo& /* info */, - const bool fatal) -{ - if (fatal) - { - Log::Fatal << "Save(): mlpack was not compiled with STB support, so images " - << "cannot be saved!" << std::endl; - } - else - { - Log::Warn << "Save(): mlpack was not compiled with STB support, so images " - << "cannot be saved!" << std::endl; - } - - return false; -} - -} // namespace data -} // namespace mlpack - -#endif +// Include implementation of Save() for images. +#include "save_image_impl.hpp" #endif diff --git a/src/mlpack/core/data/save_image_impl.hpp b/src/mlpack/core/data/save_image_impl.hpp new file mode 100644 index 0000000000..3766593418 --- /dev/null +++ b/src/mlpack/core/data/save_image_impl.hpp @@ -0,0 +1,195 @@ +/** + * @file core/data/save_image_impl.hpp + * @author Mehul Kumar Nirala + * + * Implementation of image saving functionality via STB. + * + * 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. + */ +#ifndef MLPACK_CORE_DATA_SAVE_IMAGE_IMPL_HPP +#define MLPACK_CORE_DATA_SAVE_IMAGE_IMPL_HPP + +// In case it hasn't been included yet. +#include "save_image.hpp" +#include "image_info.hpp" + +namespace mlpack { +namespace data { + +/** + * Save the given image to the given filename. + * + * @param filename Filename to save to. + * @param matrix Matrix containing image to be saved. + * @param info Information about the image (width/height/channels/etc.). + * @param fatal Whether an exception should be thrown on save failure. + */ +template +bool Save(const std::string& filename, + arma::Mat& matrix, + ImageInfo& info, + const bool fatal) +{ + arma::Mat tmpMatrix = + arma::conv_to>::from(matrix); + + return SaveImage(filename, tmpMatrix, info, fatal); +} + +// Image saving API for multiple files. +template +bool Save(const std::vector& files, + arma::Mat& matrix, + ImageInfo& info, + const bool fatal) +{ + if (files.size() == 0) + { + if (fatal) + { + Log::Fatal << "Save(): vector of image files is empty; nothing to save." + << std::endl; + } + else + { + Log::Warn << "Save(): vector of image files is empty; nothing to save." + << std::endl; + } + + return false; + } + + arma::Mat img; + bool status = true; + + for (size_t i = 0; i < files.size() ; ++i) + { + arma::Mat colImg(matrix.colptr(i), matrix.n_rows, 1, + false, true); + status &= Save(files[i], colImg, info, fatal); + } + + return status; +} + +#ifdef HAS_STB + +inline bool SaveImage(const std::string& filename, + arma::Mat& image, + ImageInfo& info, + const bool fatal) +{ + // Check to see if the file type is supported. + if (!ImageFormatSupported(filename, true)) + { + std::ostringstream oss; + oss << "Save(): file type " << Extension(filename) << " not supported.\n"; + oss << "Currently image saving supports "; + for (auto extension : SaveFileTypes()) + oss << ", " << extension; + oss << "." << std::endl; + + if (fatal) + { + Log::Fatal << oss.str(); + } + else + { + Log::Warn << oss.str(); + } + + return false; + } + + // Ensure the shape of the matrix is correct. + if (image.n_cols > 1) + { + Log::Warn << "Save(): given input image matrix contains more than 1 image." + << std::endl; + Log::Warn << "Only the first image will be saved!" << std::endl; + } + + if (info.Width() * info.Height() * info.Channels() != image.n_elem) + { + Log::Fatal << "data::Save(): The given image dimensions do not match the " + << "dimensions of the matrix to be saved!" << std::endl; + } + + bool status = false; + unsigned char* imageMem = image.memptr(); + + if ("png" == Extension(filename)) + { + status = stbi_write_png(filename.c_str(), info.Width(), info.Height(), + info.Channels(), imageMem, info.Width() * info.Channels()); + } + else if ("bmp" == Extension(filename)) + { + status = stbi_write_bmp(filename.c_str(), info.Width(), info.Height(), + info.Channels(), imageMem); + } + else if ("tga" == Extension(filename)) + { + status = stbi_write_tga(filename.c_str(), info.Width(), info.Height(), + info.Channels(), imageMem); + } + else if ("hdr" == Extension(filename)) + { + // We'll have to convert to float... + arma::fmat tmpImage = arma::conv_to::from(image); + status = stbi_write_hdr(filename.c_str(), info.Width(), info.Height(), + info.Channels(), tmpImage.memptr()); + } + else if ("jpg" == Extension(filename)) + { + status = stbi_write_jpg(filename.c_str(), info.Width(), info.Height(), + info.Channels(), imageMem, info.Quality()); + } + + if (!status) + { + if (fatal) + { + Log::Fatal << "Save(): error saving image to '" << filename << "'." + << std::endl; + } + else + { + Log::Warn << "Save(): error saving image to '" << filename << "'." + << std::endl; + } + } + + return status; +} + +#else // HAS_STB + +inline bool SaveImage(const std::string& /* filename */, + arma::Mat& /* image */, + ImageInfo& /* info */, + const bool fatal) +{ + if (fatal) + { + Log::Fatal << "Save(): mlpack was not compiled with STB support, so images " + << "cannot be saved!" << std::endl; + } + else + { + Log::Warn << "Save(): mlpack was not compiled with STB support, so images " + << "cannot be saved!" << std::endl; + } + + return false; +} + +#endif + +} // namespace data +} // namespace mlpack + +#endif diff --git a/src/mlpack/core/data/save_impl.hpp b/src/mlpack/core/data/save_impl.hpp index 4542b1c6e7..2d3c92e9ff 100644 --- a/src/mlpack/core/data/save_impl.hpp +++ b/src/mlpack/core/data/save_impl.hpp @@ -16,10 +16,6 @@ #include "save.hpp" #include "extension.hpp" -#include -#include -#include - namespace mlpack { namespace data { @@ -344,62 +340,6 @@ bool Save(const std::string& filename, } } -/** - * Save the given image to the given filename. - * - * @param filename Filename to save to. - * @param matrix Matrix containing image to be saved. - * @param info Information about the image (width/height/channels/etc.). - * @param fatal Whether an exception should be thrown on save failure. - */ -template -bool Save(const std::string& filename, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal) -{ - arma::Mat tmpMatrix = - arma::conv_to>::from(matrix); - - return SaveImage(filename, tmpMatrix, info, fatal); -} - -// Image saving API for multiple files. -template -bool Save(const std::vector& files, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal) -{ - if (files.size() == 0) - { - if (fatal) - { - Log::Fatal << "Save(): vector of image files is empty; nothing to save." - << std::endl; - } - else - { - Log::Warn << "Save(): vector of image files is empty; nothing to save." - << std::endl; - } - - return false; - } - - arma::Mat img; - bool status = true; - - for (size_t i = 0; i < files.size() ; ++i) - { - arma::Mat colImg(matrix.colptr(i), matrix.n_rows, 1, - false, true); - status &= Save(files[i], colImg, info, fatal); - } - - return status; -} - } // namespace data } // namespace mlpack From 453a0174f6ab266b669b64233842488ef7a35410 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 25 Jan 2022 12:09:08 +0000 Subject: [PATCH 46/50] Finish this PR Signed-off-by: Omar Shrit --- src/mlpack/core/data/load_image_impl.hpp | 2 +- src/mlpack/core/data/save_image_impl.hpp | 2 +- src/mlpack/methods/det/dtree_impl.hpp | 4 +- .../q_networks/dueling_dqn.hpp | 60 +++++++++---------- .../q_networks/simple_dqn.hpp | 18 +++--- 5 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/mlpack/core/data/load_image_impl.hpp b/src/mlpack/core/data/load_image_impl.hpp index 57b9d7b6fd..fe7e4c8519 100644 --- a/src/mlpack/core/data/load_image_impl.hpp +++ b/src/mlpack/core/data/load_image_impl.hpp @@ -103,7 +103,7 @@ inline bool LoadImage(const std::string& filename, { std::ostringstream oss; oss << "Load(): file type " << Extension(filename) << " not supported. "; - oss << "Currently it supports: "; + oss << "Currently it supports:"; auto x = LoadFileTypes(); for (auto extension : x) oss << " " << extension; diff --git a/src/mlpack/core/data/save_image_impl.hpp b/src/mlpack/core/data/save_image_impl.hpp index 3766593418..c83a81cc71 100644 --- a/src/mlpack/core/data/save_image_impl.hpp +++ b/src/mlpack/core/data/save_image_impl.hpp @@ -89,7 +89,7 @@ inline bool SaveImage(const std::string& filename, oss << "Save(): file type " << Extension(filename) << " not supported.\n"; oss << "Currently image saving supports "; for (auto extension : SaveFileTypes()) - oss << ", " << extension; + oss << " " << extension; oss << "." << std::endl; if (fatal) diff --git a/src/mlpack/methods/det/dtree_impl.hpp b/src/mlpack/methods/det/dtree_impl.hpp index 016bf2df79..e9a27c793f 100644 --- a/src/mlpack/methods/det/dtree_impl.hpp +++ b/src/mlpack/methods/det/dtree_impl.hpp @@ -188,7 +188,9 @@ DTree::DTree(const DTree& obj) : template DTree& DTree::operator=( - const DTree& obj) +{ + if (this == &obj) return *this; // Copy the values from the other tree. diff --git a/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp b/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp index 06a7e2f9a2..c4b26a353f 100644 --- a/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp @@ -57,14 +57,14 @@ class DuelingDQN //! Default constructor. DuelingDQN() : isNoisy(false) { - featureNetwork = new mlpack::ann::Sequential<>(); - valueNetwork = new mlpack::ann::Sequential<>(); - advantageNetwork = new mlpack::ann::Sequential<>(); - concat = new mlpack::ann::Concat<>(true); + featureNetwork = new ann::Sequential<>(); + valueNetwork = new ann::Sequential<>(); + advantageNetwork = new ann::Sequential<>(); + concat = new ann::Concat<>(true); concat->Add(valueNetwork); concat->Add(advantageNetwork); - completeNetwork.Add(new mlpack::ann::IdentityLayer<>()); + completeNetwork.Add(new ann::IdentityLayer<>()); completeNetwork.Add(featureNetwork); completeNetwork.Add(concat); } @@ -90,42 +90,42 @@ class DuelingDQN completeNetwork(outputLayer, init), isNoisy(isNoisy) { - featureNetwork = new mlpack::ann::Sequential<>(); - featureNetwork->Add(new mlpack::ann::Linear<>(inputDim, h1)); - featureNetwork->Add(new mlpack::ann::ReLULayer<>()); + featureNetwork = new ann::Sequential<>(); + featureNetwork->Add(new ann::Linear<>(inputDim, h1)); + featureNetwork->Add(new ann::ReLULayer<>()); - valueNetwork = new mlpack::ann::Sequential<>(); - advantageNetwork = new mlpack::ann::Sequential<>(); + valueNetwork = new ann::Sequential<>(); + advantageNetwork = new ann::Sequential<>(); if (isNoisy) { noisyLayerIndex.push_back(valueNetwork->Model().size()); - valueNetwork->Add(new mlpack::ann::NoisyLinear<>(h1, h2)); - advantageNetwork->Add(new mlpack::ann::NoisyLinear<>(h1, h2)); + valueNetwork->Add(new ann::NoisyLinear<>(h1, h2)); + advantageNetwork->Add(new ann::NoisyLinear<>(h1, h2)); - valueNetwork->Add(new mlpack::ann::ReLULayer<>()); - advantageNetwork->Add(new mlpack::ann::ReLULayer<>()); + valueNetwork->Add(new ann::ReLULayer<>()); + advantageNetwork->Add(new ann::ReLULayer<>()); noisyLayerIndex.push_back(valueNetwork->Model().size()); - valueNetwork->Add(new mlpack::ann::NoisyLinear<>(h2, 1)); - advantageNetwork->Add(new mlpack::ann::NoisyLinear<>(h2, outputDim)); + valueNetwork->Add(new ann::NoisyLinear<>(h2, 1)); + advantageNetwork->Add(new ann::NoisyLinear<>(h2, outputDim)); } else { - valueNetwork->Add(new mlpack::ann::Linear<>(h1, h2)); - valueNetwork->Add(new mlpack::ann::ReLULayer<>()); - valueNetwork->Add(new mlpack::ann::Linear<>(h2, 1)); + valueNetwork->Add(new ann::Linear<>(h1, h2)); + valueNetwork->Add(new ann::ReLULayer<>()); + valueNetwork->Add(new ann::Linear<>(h2, 1)); - advantageNetwork->Add(new mlpack::ann::Linear<>(h1, h2)); - advantageNetwork->Add(new mlpack::ann::ReLULayer<>()); - advantageNetwork->Add(new mlpack::ann::Linear<>(h2, outputDim)); + advantageNetwork->Add(new ann::Linear<>(h1, h2)); + advantageNetwork->Add(new ann::ReLULayer<>()); + advantageNetwork->Add(new ann::Linear<>(h2, outputDim)); } - concat = new mlpack::ann::Concat<>(true); + concat = new ann::Concat<>(true); concat->Add(valueNetwork); concat->Add(advantageNetwork); - completeNetwork.Add(new mlpack::ann::IdentityLayer<>()); + completeNetwork.Add(new ann::IdentityLayer<>()); completeNetwork.Add(featureNetwork); completeNetwork.Add(concat); this->ResetParameters(); @@ -148,10 +148,10 @@ class DuelingDQN valueNetwork(valueNetwork), isNoisy(isNoisy) { - concat = new mlpack::ann::Concat<>(true); + concat = new ann::Concat<>(true); concat->Add(valueNetwork); concat->Add(advantageNetwork); - completeNetwork.Add(new mlpack::ann::IdentityLayer<>()); + completeNetwork.Add(new ann::IdentityLayer<>()); completeNetwork.Add(featureNetwork); completeNetwork.Add(concat); this->ResetParameters(); @@ -243,9 +243,9 @@ class DuelingDQN { for (size_t i = 0; i < noisyLayerIndex.size(); i++) { - boost::get*> + boost::get*> (valueNetwork->Model()[noisyLayerIndex[i]])->ResetNoise(); - boost::get*> + boost::get*> (advantageNetwork->Model()[noisyLayerIndex[i]])->ResetNoise(); } } @@ -260,7 +260,7 @@ class DuelingDQN CompleteNetworkType completeNetwork; //! Locally-stored concat network. - mlpack::ann::Concat<>* concat; + ann::Concat<>* concat; //! Locally-stored feature network. FeatureNetworkType* featureNetwork; @@ -281,7 +281,7 @@ class DuelingDQN arma::mat actionValues; //! Locally-stored loss function. - mlpack::ann::MeanSquaredError<> lossFunction; + ann::MeanSquaredError<> lossFunction; }; } // namespace rl diff --git a/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp b/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp index 79af778619..54ad55f01e 100644 --- a/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp @@ -61,21 +61,21 @@ class SimpleDQN network(outputLayer, init), isNoisy(isNoisy) { - network.Add(new mlpack::ann::Linear<>(inputDim, h1)); - network.Add(new mlpack::ann::ReLULayer<>()); + network.Add(new ann::Linear<>(inputDim, h1)); + network.Add(new ann::ReLULayer<>()); if (isNoisy) { noisyLayerIndex.push_back(network.Model().size()); - network.Add(new mlpack::ann::NoisyLinear<>(h1, h2)); - network.Add(new mlpack::ann::ReLULayer<>()); + network.Add(new ann::NoisyLinear<>(h1, h2)); + network.Add(new ann::ReLULayer<>()); noisyLayerIndex.push_back(network.Model().size()); - network.Add(new mlpack::ann::NoisyLinear<>(h2, outputDim)); + network.Add(new ann::NoisyLinear<>(h2, outputDim)); } else { - network.Add(new mlpack::ann::Linear<>(h1, h2)); - network.Add(new mlpack::ann::ReLULayer<>()); - network.Add(new mlpack::ann::Linear<>(h2, outputDim)); + network.Add(new ann::Linear<>(h1, h2)); + network.Add(new ann::ReLULayer<>()); + network.Add(new ann::Linear<>(h2, outputDim)); } } @@ -132,7 +132,7 @@ class SimpleDQN { for (size_t i = 0; i < noisyLayerIndex.size(); i++) { - boost::get*> + boost::get*> (network.Model()[noisyLayerIndex[i]])->ResetNoise(); } } From 44d9748dc4726a8a6b05f24757e67180c03bcb8a Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 3 Feb 2022 21:46:40 +0000 Subject: [PATCH 47/50] Adding the missing starts, shitty regexp Signed-off-by: Omar Shrit --- src/mlpack/methods/det/dtree_impl.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/det/dtree_impl.hpp b/src/mlpack/methods/det/dtree_impl.hpp index e9a27c793f..240156ae61 100644 --- a/src/mlpack/methods/det/dtree_impl.hpp +++ b/src/mlpack/methods/det/dtree_impl.hpp @@ -149,7 +149,8 @@ void ExtractSplits(std::vector>& splitVec, template DTree::DTree() : - end(0), + start(0), + end(0), splitDim(size_t(-1)), splitValue(std::numeric_limits::max()), logNegError(-DBL_MAX), @@ -332,7 +333,8 @@ DTree::DTree(const StatType& maxVals, template DTree::DTree(MatType & data) : - end(data.n_cols), + start(0), + end(data.n_cols), maxVals(arma::max(data, 1)), minVals(arma::min(data, 1)), splitDim(size_t(-1)), From 8868c12060263b90795bcb28a79ce1444bb645b2 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sun, 13 Mar 2022 21:28:32 +0000 Subject: [PATCH 48/50] Let us see inital try for STB test Signed-off-by: Omar Shrit --- CMake/TestForSTB.cmake | 37 +++++++++++++++++++++++++++++++++++++ CMake/stb/CMakeLists.txt | 21 +++++++++++++++++++++ CMake/stb/alib.cpp | 8 ++++++++ CMake/stb/alib.hpp | 16 ++++++++++++++++ CMake/stb/blib.cpp | 8 ++++++++ CMake/stb/blib.hpp | 16 ++++++++++++++++ CMake/stb/main.cpp | 11 +++++++++++ 7 files changed, 117 insertions(+) create mode 100644 CMake/TestForSTB.cmake create mode 100644 CMake/stb/CMakeLists.txt create mode 100644 CMake/stb/alib.cpp create mode 100644 CMake/stb/alib.hpp create mode 100644 CMake/stb/blib.cpp create mode 100644 CMake/stb/blib.hpp create mode 100644 CMake/stb/main.cpp diff --git a/CMake/TestForSTB.cmake b/CMake/TestForSTB.cmake new file mode 100644 index 0000000000..25fd8ee240 --- /dev/null +++ b/CMake/TestForSTB.cmake @@ -0,0 +1,37 @@ +# Author: Omar Shrit + +#[=======================================================================[.rst: +TestForSTB +-------------- + +Test to verify if the last version of STB that contains static +functions is available in the system + +check if the compiler supports the standard ANSI sstream header + +:: + + CMAKE_HAS_STATIC_STB - defined by the results +#]=======================================================================] + +if(NOT DEFINED CMAKE_HAS_STATIC_STB) + message(CHECK_START "Check for stb") + try_compile(CMAKE_HAS_STATIC_STB stb/main.cpp + OUTPUT_VARIABLE OUTPUT) + if (CMAKE_HAS_ANSI_STRING_STREAM) + message(CHECK_PASS "found") + set (CMAKE_NO_ANSI_STRING_STREAM 0 CACHE INTERNAL + "Does the compiler support sstream") + file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log + "Determining if the CXX compiler has sstream passed with " + "the following output:\n${OUTPUT}\n\n") + else () + message(CHECK_FAIL "not found") + set (CMAKE_NO_ANSI_STRING_STREAM 1 CACHE INTERNAL + "Does the compiler support sstream") + file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log + "Determining if the CXX compiler has sstream failed with " + "the following output:\n${OUTPUT}\n\n") + endif () +endif() + diff --git a/CMake/stb/CMakeLists.txt b/CMake/stb/CMakeLists.txt new file mode 100644 index 0000000000..0ea752cb61 --- /dev/null +++ b/CMake/stb/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.9) +project(CheckSTB) +include(GNUInstallDirs) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +add_library(alib SHARED alib.hpp alib.cpp) +add_library(blib SHARED blib.hpp blib.cpp) + +#set(alib "alib.so") +#set(blib "libblib.so") + +add_executable(CheckSTB + main.cpp + ) + +target_link_libraries(CheckSTB + alib + blib + ) diff --git a/CMake/stb/alib.cpp b/CMake/stb/alib.cpp new file mode 100644 index 0000000000..e92d7402bc --- /dev/null +++ b/CMake/stb/alib.cpp @@ -0,0 +1,8 @@ + + +#include "alib.hpp" + +void Alib::A() +{ + //Do nothing, just to check if the STB library has the good version. +} diff --git a/CMake/stb/alib.hpp b/CMake/stb/alib.hpp new file mode 100644 index 0000000000..ee36e8474e --- /dev/null +++ b/CMake/stb/alib.hpp @@ -0,0 +1,16 @@ + +#ifndef ALIB_HPP +#define ALIB_HPP + +#define STB_IMAGE_WRITE_STATIC +#define STB_IMAGE_WRITE_IMPLEMENTATION + +#include + +namespace Alib { + +void A(); + +} + +#endif diff --git a/CMake/stb/blib.cpp b/CMake/stb/blib.cpp new file mode 100644 index 0000000000..14b6eb3025 --- /dev/null +++ b/CMake/stb/blib.cpp @@ -0,0 +1,8 @@ + + +#include "blib.hpp" + +void Blib::B() +{ + //Do nothing, just to check if the STB library has the good version. +} diff --git a/CMake/stb/blib.hpp b/CMake/stb/blib.hpp new file mode 100644 index 0000000000..43cc547322 --- /dev/null +++ b/CMake/stb/blib.hpp @@ -0,0 +1,16 @@ + +#ifndef BLIB_HPP +#define BLIB_HPP + +#define STB_IMAGE_STATIC +#define STB_IMAGE_IMPLEMENTATION + +#include + +namespace Blib { + +void B(); + +} + +#endif diff --git a/CMake/stb/main.cpp b/CMake/stb/main.cpp new file mode 100644 index 0000000000..3790075942 --- /dev/null +++ b/CMake/stb/main.cpp @@ -0,0 +1,11 @@ + +#include "alib.hpp" +#include "blib.hpp" + +int main() +{ + + Alib::A(); + + Blib::B(); +} From eefb010434cfa1ff98763df087efb3b7cfcc2ed7 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 16 Apr 2022 16:20:14 +0100 Subject: [PATCH 49/50] Apply @rcurtin modification to check STB version Signed-off-by: Omar Shrit --- CMake/TestForSTB.cmake | 37 ---------------------------------- CMake/TestStaticSTB.cmake | 42 +++++++++++++++++++++++++++++++++++++++ CMake/stb/CMakeLists.txt | 21 -------------------- CMake/stb/a.cpp | 15 ++++++++++++++ CMake/stb/a.hpp | 10 ++++++++++ CMake/stb/alib.cpp | 8 -------- CMake/stb/alib.hpp | 16 --------------- CMake/stb/b.cpp | 15 ++++++++++++++ CMake/stb/b.hpp | 10 ++++++++++ CMake/stb/blib.cpp | 8 -------- CMake/stb/blib.hpp | 16 --------------- CMake/stb/main.cpp | 19 +++++++++++------- CMakeLists.txt | 9 +++++++++ 13 files changed, 113 insertions(+), 113 deletions(-) delete mode 100644 CMake/TestForSTB.cmake create mode 100644 CMake/TestStaticSTB.cmake delete mode 100644 CMake/stb/CMakeLists.txt create mode 100644 CMake/stb/a.cpp create mode 100644 CMake/stb/a.hpp delete mode 100644 CMake/stb/alib.cpp delete mode 100644 CMake/stb/alib.hpp create mode 100644 CMake/stb/b.cpp create mode 100644 CMake/stb/b.hpp delete mode 100644 CMake/stb/blib.cpp delete mode 100644 CMake/stb/blib.hpp diff --git a/CMake/TestForSTB.cmake b/CMake/TestForSTB.cmake deleted file mode 100644 index 25fd8ee240..0000000000 --- a/CMake/TestForSTB.cmake +++ /dev/null @@ -1,37 +0,0 @@ -# Author: Omar Shrit - -#[=======================================================================[.rst: -TestForSTB --------------- - -Test to verify if the last version of STB that contains static -functions is available in the system - -check if the compiler supports the standard ANSI sstream header - -:: - - CMAKE_HAS_STATIC_STB - defined by the results -#]=======================================================================] - -if(NOT DEFINED CMAKE_HAS_STATIC_STB) - message(CHECK_START "Check for stb") - try_compile(CMAKE_HAS_STATIC_STB stb/main.cpp - OUTPUT_VARIABLE OUTPUT) - if (CMAKE_HAS_ANSI_STRING_STREAM) - message(CHECK_PASS "found") - set (CMAKE_NO_ANSI_STRING_STREAM 0 CACHE INTERNAL - "Does the compiler support sstream") - file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log - "Determining if the CXX compiler has sstream passed with " - "the following output:\n${OUTPUT}\n\n") - else () - message(CHECK_FAIL "not found") - set (CMAKE_NO_ANSI_STRING_STREAM 1 CACHE INTERNAL - "Does the compiler support sstream") - file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log - "Determining if the CXX compiler has sstream failed with " - "the following output:\n${OUTPUT}\n\n") - endif () -endif() - diff --git a/CMake/TestStaticSTB.cmake b/CMake/TestStaticSTB.cmake new file mode 100644 index 0000000000..b4b76adc63 --- /dev/null +++ b/CMake/TestStaticSTB.cmake @@ -0,0 +1,42 @@ +# Author: Omar Shrit + +#[=======================================================================[.rst: +TestForSTB +---------- + +Test to verify if the available version of STB contains a working static +implementation that can be used from multiple translation units. + +:: + + CMAKE_HAS_WORKING_STATIC_STB - defined by the results +#]=======================================================================] + +if(NOT DEFINED CMAKE_HAS_WORKING_STATIC_STB) + message(STATUS "Check that STB static implementation mode links correctly...") + try_compile(CMAKE_HAS_WORKING_STATIC_STB + ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/ + SOURCES + ${CMAKE_SOURCE_DIR}/CMake/stb/main.cpp + ${CMAKE_SOURCE_DIR}/CMake/stb/a.cpp + ${CMAKE_SOURCE_DIR}/CMake/stb/b.cpp + CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${STB_IMAGE_INCLUDE_DIR}" + OUTPUT_VARIABLE out) + if (CMAKE_HAS_WORKING_STATIC_STB) + message(STATUS "Check that STB static implementation mode links " + "correctly... success") + set(CMAKE_HAS_WORKING_STATIC_STB 1 CACHE INTERNAL + "Does STB static implementation mode link correctly") + file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log + "Determining if STB's static implementation can link correctly passed " + "with the following output:\n${out}\n\n") + else () + message(STATUS "Check that STB static implementation mode links " + "correctly... fail") + set(CMAKE_HAS_WORKING_STATIC_STB 0 CACHE INTERNAL + "Does STB static implementation mode link correctly") + file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log + "Determining if STB's static implementation can link correctly failed " + "with the following output:\n${out}\n\n") + endif () +endif() diff --git a/CMake/stb/CMakeLists.txt b/CMake/stb/CMakeLists.txt deleted file mode 100644 index 0ea752cb61..0000000000 --- a/CMake/stb/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -cmake_minimum_required(VERSION 3.9) -project(CheckSTB) -include(GNUInstallDirs) - -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -add_library(alib SHARED alib.hpp alib.cpp) -add_library(blib SHARED blib.hpp blib.cpp) - -#set(alib "alib.so") -#set(blib "libblib.so") - -add_executable(CheckSTB - main.cpp - ) - -target_link_libraries(CheckSTB - alib - blib - ) diff --git a/CMake/stb/a.cpp b/CMake/stb/a.cpp new file mode 100644 index 0000000000..4a99350577 --- /dev/null +++ b/CMake/stb/a.cpp @@ -0,0 +1,15 @@ +#include "a.hpp" + +// Include the static implementation of all STB functions. +#define STB_IMAGE_STATIC +#define STB_IMAGE_IMPLEMENTATION +#define STB_IMAGE_WRITE_STATIC +#define STB_IMAGE_WRITE_IMPLEMENTATION + +#include +#include + +void A::A() +{ + // Do nothing, just to check if the STB library is a working version. +} diff --git a/CMake/stb/a.hpp b/CMake/stb/a.hpp new file mode 100644 index 0000000000..be30807b5e --- /dev/null +++ b/CMake/stb/a.hpp @@ -0,0 +1,10 @@ +#ifndef A_HPP +#define A_HPP + +namespace A { + +void A(); + +} + +#endif diff --git a/CMake/stb/alib.cpp b/CMake/stb/alib.cpp deleted file mode 100644 index e92d7402bc..0000000000 --- a/CMake/stb/alib.cpp +++ /dev/null @@ -1,8 +0,0 @@ - - -#include "alib.hpp" - -void Alib::A() -{ - //Do nothing, just to check if the STB library has the good version. -} diff --git a/CMake/stb/alib.hpp b/CMake/stb/alib.hpp deleted file mode 100644 index ee36e8474e..0000000000 --- a/CMake/stb/alib.hpp +++ /dev/null @@ -1,16 +0,0 @@ - -#ifndef ALIB_HPP -#define ALIB_HPP - -#define STB_IMAGE_WRITE_STATIC -#define STB_IMAGE_WRITE_IMPLEMENTATION - -#include - -namespace Alib { - -void A(); - -} - -#endif diff --git a/CMake/stb/b.cpp b/CMake/stb/b.cpp new file mode 100644 index 0000000000..a11a8f15e7 --- /dev/null +++ b/CMake/stb/b.cpp @@ -0,0 +1,15 @@ +#include "b.hpp" + +// Include the static implementation of all STB functions. +#define STB_IMAGE_STATIC +#define STB_IMAGE_IMPLEMENTATION +#define STB_IMAGE_WRITE_STATIC +#define STB_IMAGE_WRITE_IMPLEMENTATION + +#include +#include + +void B::B() +{ + // Do nothing, just to check if the STB library is a working version. +} diff --git a/CMake/stb/b.hpp b/CMake/stb/b.hpp new file mode 100644 index 0000000000..0a287e6b17 --- /dev/null +++ b/CMake/stb/b.hpp @@ -0,0 +1,10 @@ +#ifndef B_HPP +#define B_HPP + +namespace B { + +void B(); + +} + +#endif diff --git a/CMake/stb/blib.cpp b/CMake/stb/blib.cpp deleted file mode 100644 index 14b6eb3025..0000000000 --- a/CMake/stb/blib.cpp +++ /dev/null @@ -1,8 +0,0 @@ - - -#include "blib.hpp" - -void Blib::B() -{ - //Do nothing, just to check if the STB library has the good version. -} diff --git a/CMake/stb/blib.hpp b/CMake/stb/blib.hpp deleted file mode 100644 index 43cc547322..0000000000 --- a/CMake/stb/blib.hpp +++ /dev/null @@ -1,16 +0,0 @@ - -#ifndef BLIB_HPP -#define BLIB_HPP - -#define STB_IMAGE_STATIC -#define STB_IMAGE_IMPLEMENTATION - -#include - -namespace Blib { - -void B(); - -} - -#endif diff --git a/CMake/stb/main.cpp b/CMake/stb/main.cpp index 3790075942..02cf644606 100644 --- a/CMake/stb/main.cpp +++ b/CMake/stb/main.cpp @@ -1,11 +1,16 @@ - -#include "alib.hpp" -#include "blib.hpp" +// The purpose of this file is to include STB's implementation in two separate +// translation units. One is a.cpp, and one is b.cpp. This file simply +// includes both of those, so that when we get to the linking phase, we will +// have to link both translation units. +// +// Some versions of STB fail to correctly define some functions as +// static---which will cause a linking failure. Thus, if this fails to +// compile, then mlpack's use of STB will fail. +#include "a.hpp" +#include "b.hpp" int main() { - - Alib::A(); - - Blib::B(); + A::A(); + B::B(); } diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a04283031..7faa21b8d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -325,6 +325,15 @@ if (STB_IMAGE_FOUND) add_definitions(-DHAS_STB) set(STB_AVAILABLE "1") set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} "${STB_IMAGE_INCLUDE_DIR}") + + # Make sure that we can link STB in multiple translation units. + include(CMake/TestStaticSTB.cmake) + if (NOT CMAKE_HAS_WORKING_STATIC_STB) + message(FATAL_ERROR "STB implementations's static mode cannot link across " + "multiple translation units! Try upgrading your STB implementation, " + "or using the auto-downloader (set DOWNLOAD_DEPENDENCIES=ON in the " + "CMake configuration command.") + endif () endif() # Find ensmallen. From 10f722bc83b26671024544d8f684a506d5e9bb49 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 16 Apr 2022 21:40:15 +0200 Subject: [PATCH 50/50] Update src/mlpack/core/data/save_image.hpp Co-authored-by: Marcus Edel --- src/mlpack/core/data/save_image.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/core/data/save_image.hpp b/src/mlpack/core/data/save_image.hpp index 40a9186cdc..c23ec9a8c3 100644 --- a/src/mlpack/core/data/save_image.hpp +++ b/src/mlpack/core/data/save_image.hpp @@ -1,5 +1,5 @@ /** - * @file core/data/save_image_impl.hpp + * @file core/data/save_image.hpp * @author Ryan Curtin * * Implementation of save functionality.