From dc402b0d106443d91f22ab5b24fff7375578d50c Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Mon, 5 Aug 2019 18:21:57 +0530 Subject: [PATCH 001/265] Added bow encoding policy and tfidf policy (buggy) --- src/mlpack/core/data/string_encoding_impl.hpp | 7 +- .../string_encoding_policies/CMakeLists.txt | 2 + .../bow_encoding_policy.hpp | 142 +++++++++++++++ .../dictionary_encoding_policy.hpp | 10 ++ .../tf_idf_encoding_policy.hpp | 167 ++++++++++++++++++ src/mlpack/tests/string_encoding_test.cpp | 150 ++++++++++++++++ 6 files changed, 474 insertions(+), 4 deletions(-) create mode 100644 src/mlpack/core/data/string_encoding_policies/bow_encoding_policy.hpp create mode 100644 src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp diff --git a/src/mlpack/core/data/string_encoding_impl.hpp b/src/mlpack/core/data/string_encoding_impl.hpp index b84781c26f..959d5b7b91 100644 --- a/src/mlpack/core/data/string_encoding_impl.hpp +++ b/src/mlpack/core/data/string_encoding_impl.hpp @@ -110,9 +110,9 @@ EncodeHelper(const std::vector& input, { size_t numColumns = 0; - for (const std::string& line : input) + for (size_t i = 0; i < input.size(); i++) { - boost::string_view strView(line); + boost::string_view strView(input[i]); auto token = tokenizer(strView); static_assert( @@ -128,7 +128,7 @@ EncodeHelper(const std::vector& input, { if (!dictionary.HasToken(token)) dictionary.AddToken(token); - + policy.PreprocessToken(i, numTokens, dictionary.Value(token)); token = tokenizer(strView); numTokens++; } @@ -136,7 +136,6 @@ EncodeHelper(const std::vector& input, } policy.InitMatrix(output, input.size(), numColumns, dictionary.Size()); - for (size_t i = 0; i < input.size(); i++) { boost::string_view strView(input[i]); diff --git a/src/mlpack/core/data/string_encoding_policies/CMakeLists.txt b/src/mlpack/core/data/string_encoding_policies/CMakeLists.txt index 8a8c7ab65c..ad88e9c0fc 100644 --- a/src/mlpack/core/data/string_encoding_policies/CMakeLists.txt +++ b/src/mlpack/core/data/string_encoding_policies/CMakeLists.txt @@ -2,6 +2,8 @@ # Anything not in this list will not be compiled into mlpack. set(SOURCES dictionary_encoding_policy.hpp + bow_encoding_policy.hpp + tf_idf_encoding_policy.hpp policy_traits.hpp ) diff --git a/src/mlpack/core/data/string_encoding_policies/bow_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bow_encoding_policy.hpp new file mode 100644 index 0000000000..14c8566c94 --- /dev/null +++ b/src/mlpack/core/data/string_encoding_policies/bow_encoding_policy.hpp @@ -0,0 +1,142 @@ +/** + * @file bow_encoding_policy.hpp + * @author Jeffin Sam + * + * Definition of the BagOfWordsEncodingPolicy class. + * + * 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_BAG_OF_WORDS_ENCODING_POLICY_HPP +#define MLPACK_CORE_DATA_BAG_OF_WORDS_ENCODING_POLICY_HPP + +#include +#include +#include + +namespace mlpack { +namespace data { +/** + * Definition of the BagOfWordsEncodingPolicy class. + * + * DicitonaryEnocding is used as a helper class for StringEncoding. + * The encoder assigns a positive integer number to each unique token and treat + * the dataset as categorical. The numbers are assigned sequentially starting + * from one. The tokens are labeled in the order of their occurrence + * in the input dataset. + */ +class BagOfWordsEncodingPolicy +{ + public: + /** + * The function initializes the output matrix. + * + * @param output Output matrix to store the encoded results (sp_mat or mat). + * @param datasetSize The number of strings in the input dataset. + * @param maxNumTokens The maximum number of tokens in the strings of the + input dataset (not used). + * @param dictionarySize The size of the dictionary. + */ + template + static void InitMatrix(MatType& output, + size_t datasetSize, + size_t /*maxNumTokens*/, + size_t dictionarySize) + { + output.zeros(datasetSize, dictionarySize); + } + + /** + * The function initializes the output matrix. + * Overloaded function to store result in vector> + * + * @param output Output matrix to store the encoded results. + * @param datasetSize The number of strings in the input dataset. + * @param maxNumTokens The maximum number of tokens in the strings of the + input dataset. + * @param dictionarySize The size of the dictionary (not used). + */ + static void InitMatrix(std::vector >& output, + size_t datasetSize, + size_t /*maxNumTokens*/, + size_t dictionarySize) + { + output.resize(datasetSize, std::vector (dictionarySize,0)); + } + + /** + * The function performs the bagofwords encoding algorithm i.e. it writes + * the encoded token to the ouput. + * + * @param output Output matrix to store the encoded results (sp_mat or mat). + * @param value The encoded token. + * @param row The row number at which the encoding is performed. + * @param col The row token number at which the encoding is performed. + */ + template + static void Encode(MatType& output, size_t value, size_t row, size_t /*col*/) + { + // Important since Mapping starts from 1 whereas allowed column value is 0. + output(row, value-1) = 1; + } + + /** + * The function performs the bagofwords encoding algorithm i.e. it writes + * the encoded token to the ouput. + * Overload function to accepted vector> as output type. + * + * @param output Output matrix to store the encoded results. + * @param value The encoded token. + * @param row The row number at which the encoding is performed. + * @param col The row token number at which the encoding is performed. + */ + static void Encode(std::vector >& output, size_t value, + size_t row, size_t /*col*/) + { + // Important since Mapping starts from 1 whereas allowed column value is 0. + output[row][value-1] = 1; + } + + /** + * Serialize the class to the given archive. + */ + template + void serialize(Archive& /* ar */, const unsigned int /* version */) + { + // Nothing to serialize. + } + + /** + * Empty function, Important for tf-idf encoding policy + * + * @param row The row number at which the encoding is performed. + * @param numToken The count of token parsed till now. + * @param value The encoded token. + */ + static void PreprocessToken(size_t /*row*/, size_t /*numTokens*/, + size_t /*value*/) { } +}; + +/** + * The specialization provides some information about the dictionary encoding + * policy. + */ +template<> +struct StringEncodingPolicyTraits +{ + /** + * Indicates if the policy is able to encode the token at once without + * any information about other tokens as well as the total tokens count. + */ + static const bool onePassEncoding = false; +}; + +template +using BowEncoding = StringEncoding>; +} // namespace data +} // namespace mlpack + +#endif \ No newline at end of file diff --git a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp index 10239d7fbb..18b96b24a3 100644 --- a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp @@ -76,6 +76,16 @@ class DictionaryEncodingPolicy output.push_back(value); } + /** + * Empty function, Important for tf-idf encoding policy + * + * @param row The row number at which the encoding is performed. + * @param numToken The count of token parsed till now. + * @param value The encoded token. + */ + static void PreprocessToken(size_t /*row*/, size_t /*numTokens*/, + size_t /*value*/) { } + /** * Serialize the class to the given archive. */ diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp new file mode 100644 index 0000000000..1c9565b8cb --- /dev/null +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -0,0 +1,167 @@ +/** + * @file td_idf_encoding_policy.hpp + * @author Jeffin Sam + * + * Definition of the TfIdfEncodingPolicy class. + * + * 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_TF_IDF_ENCODING_POLICY_HPP +#define MLPACK_CORE_DATA_TF_IDF_ENCODING_POLICY_HPP + +#include +#include +#include +namespace mlpack { +namespace data { +/** + * Definition of the TfIdfEncodingPolicy class. + * + * DicitonaryEnocding is used as a helper class for StringEncoding. + * The encoder assigns a positive integer number to each unique token and treat + * the dataset as categorical. The numbers are assigned sequentially starting + * from one. The tokens are labeled in the order of their occurrence + * in the input dataset. + */ +class TfIdfEncodingPolicy +{ + public: + + /** + * The function initializes the output matrix. + * + * @param output Output matrix to store the encoded results (sp_mat or mat). + * @param datasetSize The number of strings in the input dataset. + * @param maxNumTokens The maximum number of tokens in the strings of the + input dataset. + * @param dictionarySize The size of the dictionary (not used). + */ + template + static void InitMatrix(MatType& output, + size_t datasetSize, + size_t /*maxNumTokens*/, + size_t dictionarySize) + { + output.zeros(datasetSize, dictionarySize); + std::cout<<"Init matrix "<> + * + * @param output Output matrix to store the encoded results. + * @param datasetSize The number of strings in the input dataset. + * @param maxNumTokens The maximum number of tokens in the strings of the + input dataset. + * @param dictionarySize The size of the dictionary (not used). + */ + static void InitMatrix(std::vector >& output, + size_t datasetSize, + size_t /*maxNumTokens*/, + size_t dictionarySize) + { + output.resize(datasetSize, std::vector (dictionarySize,0)); + } + + /** + * The function performs the TfIdf encoding algorithm i.e. it writes + * the encoded token to the ouput. + * + * @param output Output matrix to store the encoded results (sp_mat or mat). + * @param value The encoded token. + * @param row The row number at which the encoding is performed. + * @param col The row token number at which the encoding is performed. + */ + template + static void Encode(MatType& output, size_t value, size_t row, size_t /*col*/) + { + // Important since Mapping starts from 1 whereas allowed column value is 0. + std::cout<<"tokencoutn for "<> as output type. + * + * @param output Output matrix to store the encoded results (sp_mat or mat). + * @param value The encoded token. + * @param row The row number at which the encoding is performed. + * @param col The row token number at which the encoding is performed. + */ + static void Encode(std::vector >& output, size_t value, + size_t row, size_t /*col*/) + { + // Important since Mapping starts from 1 whereas allowed column value is 0. + output[row][value-1] = (tokenCount[row][value - 1] / (row_size[row]-1)) * + std::log10(output.size() / idfdict[value-1]); + } + + /** + * Serialize the class to the given archive. + */ + template + void serialize(Archive& /* ar */, const unsigned int /* version */) + { + // Nothing to serialize. + } + + /* + * The function is used to create the datastrcutre will be important to find + * out idfvalue of words, and then wrtiting the output based on their count. + * + * @param row The row number at which the encoding is performed. + * @param numToken The count of token parsed till now. + * @param value The encoded token. + */ + static void PreprocessToken(size_t row, size_t numTokens, + size_t value) + { + if(row>=tokenCount.size()) + { + row_size.push_back(0); + tokenCount.push_back(std::unordered_map()); + } + tokenCount.back()[value-1]++; + if(tokenCount.back()[value-1]==1) + idfdict[value-1]++; + row_size.back()++; + } + private: + static std::vector> tokenCount; + static std::unordered_map idfdict; + static std::vector row_size; +}; + +std::vector TfIdfEncodingPolicy::row_size = {}; +std::unordered_map TfIdfEncodingPolicy::idfdict = {}; +std::vector> TfIdfEncodingPolicy::tokenCount = {}; + +/** + * The specialization provides some information about the dictionary encoding + * policy. + */ +template<> +struct StringEncodingPolicyTraits +{ + /** + * Indicates if the policy is able to encode the token at once without + * any information about other tokens as well as the total tokens count. + */ + static const bool onePassEncoding = false; +}; + +template +using TfIdfEncoding = StringEncoding>; +} // namespace data +} // namespace mlpack + +#endif \ No newline at end of file diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index a819ef4328..f7f614e099 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #include #include "test_tools.hpp" @@ -37,6 +39,11 @@ static vector stringEncodingInput = { "command-line programs and Python bindings." }; +static vector stringEncodingInputSmall = { + "hello how are you", + "i am good", + "Good how are you", +}; /** * Test the dictionary encoding algorithm. @@ -401,5 +408,148 @@ BOOST_AUTO_TEST_CASE(CharExtractDictionaryEncodingSerialization) CheckMatrices(output, xmlOutput, textOutput, binaryOutput); } +BOOST_AUTO_TEST_CASE(BowEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; + + arma::mat output; + BowEncoding encoder; + SplitByAnyOf tokenizer(" "); + + encoder.Encode(stringEncodingInputSmall, output, tokenizer); + + const DictionaryType& dictionary = encoder.Dictionary(); + + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } + arma::mat expected = { + { 1, 1, 1, 1, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 1, 1, 1, 0 }, + { 0, 1, 1, 1, 0, 0, 0, 1 } + }; + CheckMatrices(output, expected); +} + +/** + * Test the one pass modification of the Bag of Words encoding algorithm. + */ +BOOST_AUTO_TEST_CASE(OnePassBowEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; + + vector> output; + BowEncoding encoder( + (BagOfWordsEncodingPolicy())); + SplitByAnyOf tokenizer(" "); + + encoder.Encode(stringEncodingInputSmall, output, tokenizer); + + const DictionaryType& dictionary = encoder.Dictionary(); + + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } + + vector> expected = { + { 1, 1, 1, 1, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 1, 1, 1, 0 }, + { 0, 1, 1, 1, 0, 0, 0, 1 } + }; + + BOOST_REQUIRE(output == expected); +} + +/** +* Test Bag of Words encoding for characters using lamda function. +*/ +BOOST_AUTO_TEST_CASE(BowEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + arma::mat output; + BowEncoding encoder; + + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1, 1, 1, 0, 0 }, + { 0, 1, 1, 1, 1 }, + { 1, 1, 0, 1, 0 } + }; + + CheckMatrices(output, target); +} + +/** + * Test the one pass modification of the Bag of Words encoding algorithm + * in case of individual character encoding. + */ +BOOST_AUTO_TEST_CASE(OnePassBowEncodingIndividualCharactersTest) +{ + std::vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + vector> output; + BowEncoding encoder; + + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); + + vector> expected = { + { 1, 1, 1, 0, 0 }, + { 0, 1, 1, 1, 1 }, + { 1, 1, 0, 1, 0 } + }; + + BOOST_REQUIRE(output == expected); +} + +BOOST_AUTO_TEST_CASE(TfIdfEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; + + arma::mat output; + TfIdfEncoding encoder; + SplitByAnyOf tokenizer(" "); + + encoder.Encode(stringEncodingInputSmall, output, tokenizer); + + const DictionaryType& dictionary = encoder.Dictionary(); + + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } + std::cout<<"inside \n"< Date: Mon, 5 Aug 2019 21:42:19 +0530 Subject: [PATCH 002/265] solved the bug in Tf Idf Encoding Algorithim --- .../tf_idf_encoding_policy.hpp | 7 ++---- src/mlpack/tests/string_encoding_test.cpp | 25 ++++++++++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 1c9565b8cb..4fcd659b37 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -46,7 +46,6 @@ class TfIdfEncodingPolicy size_t dictionarySize) { output.zeros(datasetSize, dictionarySize); - std::cout<<"Init matrix "< input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + arma::mat output; + TfIdfEncoding encoder; + + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 0.0440, 0.0440, 0.0440, 0, 0 }, + { 0, 0, 0, 0, 0.0587 }, + { 0.0185, 0.0556, 0, 0.0371, 0 } + }; + CheckMatrices(output, target, 1e-01); +} + BOOST_AUTO_TEST_SUITE_END(); From 1935a9efdcb8430bef7aba8ca9bf6c75dc210586 Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Mon, 5 Aug 2019 22:26:44 +0530 Subject: [PATCH 003/265] add support for vector> for tfidf --- src/mlpack/core/data/string_encoding.hpp | 5 +- src/mlpack/core/data/string_encoding_impl.hpp | 5 +- .../bow_encoding_policy.hpp | 8 +-- .../dictionary_encoding_policy.hpp | 3 +- .../tf_idf_encoding_policy.hpp | 18 +++++-- src/mlpack/tests/string_encoding_test.cpp | 50 +++++++++++++++++++ 6 files changed, 77 insertions(+), 12 deletions(-) diff --git a/src/mlpack/core/data/string_encoding.hpp b/src/mlpack/core/data/string_encoding.hpp index 54356bd06b..3377f29abb 100644 --- a/src/mlpack/core/data/string_encoding.hpp +++ b/src/mlpack/core/data/string_encoding.hpp @@ -172,9 +172,10 @@ class StringEncoding * and the IsTokenEmpty() method that accepts the given token and returns true * if the given token is empty. */ - template + template void EncodeHelper(const std::vector& input, - std::vector>& output, + std::vector>& output, const TokenizerType& tokenizer, PolicyType& policy, typename std::enable_if& input, } template -template +template void StringEncoding:: EncodeHelper(const std::vector& input, - std::vector>& output, + std::vector>& output, const TokenizerType& tokenizer, PolicyType& policy, typename std::enable_if >& output, + template + static void InitMatrix(std::vector >& output, size_t datasetSize, size_t /*maxNumTokens*/, size_t dictionarySize) { - output.resize(datasetSize, std::vector (dictionarySize,0)); + output.resize(datasetSize, std::vector (dictionarySize,0)); } /** @@ -92,7 +93,8 @@ class BagOfWordsEncodingPolicy * @param row The row number at which the encoding is performed. * @param col The row token number at which the encoding is performed. */ - static void Encode(std::vector >& output, size_t value, + template + static void Encode(std::vector >& output, size_t value, size_t row, size_t /*col*/) { // Important since Mapping starts from 1 whereas allowed column value is 0. diff --git a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp index 18b96b24a3..f11e7bb566 100644 --- a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp @@ -71,7 +71,8 @@ class DictionaryEncodingPolicy * @param output Output vector to store the encoded results. * @param value The encoded token. */ - static void Encode(std::vector& output, size_t value) + template + static void Encode(std::vector& output, size_t value) { output.push_back(value); } diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 4fcd659b37..6d8e49f4e3 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -58,12 +58,13 @@ class TfIdfEncodingPolicy input dataset. * @param dictionarySize The size of the dictionary (not used). */ - static void InitMatrix(std::vector >& output, + template + static void InitMatrix(std::vector >& output, size_t datasetSize, size_t /*maxNumTokens*/, size_t dictionarySize) { - output.resize(datasetSize, std::vector (dictionarySize,0)); + output.resize(datasetSize, std::vector (dictionarySize,0)); } /** @@ -79,8 +80,12 @@ class TfIdfEncodingPolicy static void Encode(MatType& output, size_t value, size_t row, size_t /*col*/) { // Important since Mapping starts from 1 whereas allowed column value is 0. + // std::cout<<"divide "<<(tokenCount[row][value - 1] / row_size[row])<<"\n"; + // std::cout<<"output size "< >& output, size_t value, + template + static void Encode(std::vector >& output, size_t value, size_t row, size_t /*col*/) { // Important since Mapping starts from 1 whereas allowed column value is 0. + // std::cout<<"divide "<<(tokenCount[row][value - 1] / row_size[row])<<"\n"; + // std::cout<<"output size "<=tokenCount.size()) diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 9eeadd4ae2..38f306a4ca 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -550,6 +550,56 @@ BOOST_AUTO_TEST_CASE(TfIdfEncodingTest) CheckMatrices(output, expected, 1e-01); } +/** + * Test the one pass modification of the TfIdf encoding algorithm. + */ +BOOST_AUTO_TEST_CASE(OnePassTfIdfEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; + + vector> output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy())); + SplitByAnyOf tokenizer(" "); + + encoder.Encode(stringEncodingInputSmall, output, tokenizer); + + const DictionaryType& dictionary = encoder.Dictionary(); + + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } + std::cout<<"print output "<> expected = { + { 0.1193, 0.0440, 0.0440, 0.0440, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0.1590, 0.1590, 0.1590, 0 }, + { 0, 0.0440, 0.0440, 0.0440, 0, 0, 0, 0.1193 } + }; + std::cout<<"normal expected"< Date: Mon, 5 Aug 2019 22:43:28 +0530 Subject: [PATCH 004/265] Y are tfidf values different? --- .../tf_idf_encoding_policy.hpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 6d8e49f4e3..be34671af3 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -46,6 +46,13 @@ class TfIdfEncodingPolicy size_t dictionarySize) { output.zeros(datasetSize, dictionarySize); + std::cout<<"arma::mat;\n"; + for(auto it=idfdict.begin();it!=idfdict.end();it++) + { + std::cout<first<<" "<second<<"\n "; + } + std::cout<<"\n"; + } /** @@ -65,6 +72,12 @@ class TfIdfEncodingPolicy size_t dictionarySize) { output.resize(datasetSize, std::vector (dictionarySize,0)); + std::cout<<"std::vector v;\n"; + for(auto it=idfdict.begin();it!=idfdict.end();it++) + { + std::cout<first<<" "<second<<"\n"; + } + std::cout<<"\n"; } /** From 22362cb03ae7bd1a643ea8c5541cea2fd1e25ede Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Mon, 5 Aug 2019 23:18:04 +0530 Subject: [PATCH 005/265] completed with TF IDF Policy --- .../tf_idf_encoding_policy.hpp | 37 ++--------- src/mlpack/tests/string_encoding_test.cpp | 62 +++++++++++-------- 2 files changed, 43 insertions(+), 56 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index be34671af3..a3a76b2128 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -46,13 +46,6 @@ class TfIdfEncodingPolicy size_t dictionarySize) { output.zeros(datasetSize, dictionarySize); - std::cout<<"arma::mat;\n"; - for(auto it=idfdict.begin();it!=idfdict.end();it++) - { - std::cout<first<<" "<second<<"\n "; - } - std::cout<<"\n"; - } /** @@ -72,12 +65,6 @@ class TfIdfEncodingPolicy size_t dictionarySize) { output.resize(datasetSize, std::vector (dictionarySize,0)); - std::cout<<"std::vector v;\n"; - for(auto it=idfdict.begin();it!=idfdict.end();it++) - { - std::cout<first<<" "<second<<"\n"; - } - std::cout<<"\n"; } /** @@ -90,15 +77,11 @@ class TfIdfEncodingPolicy * @param col The row token number at which the encoding is performed. */ template - static void Encode(MatType& output, size_t value, size_t row, size_t /*col*/) + void Encode(MatType& output, size_t value, size_t row, size_t /*col*/) { // Important since Mapping starts from 1 whereas allowed column value is 0. - // std::cout<<"divide "<<(tokenCount[row][value - 1] / row_size[row])<<"\n"; - // std::cout<<"output size "< - static void Encode(std::vector >& output, size_t value, + void Encode(std::vector >& output, size_t value, size_t row, size_t /*col*/) { // Important since Mapping starts from 1 whereas allowed column value is 0. - // std::cout<<"divide "<<(tokenCount[row][value - 1] / row_size[row])<<"\n"; - // std::cout<<"output size "<=tokenCount.size()) @@ -155,15 +134,11 @@ class TfIdfEncodingPolicy row_size.back()++; } private: - static std::vector> tokenCount; - static std::unordered_map idfdict; - static std::vector row_size; + std::vector> tokenCount; + std::unordered_map idfdict; + std::vector row_size; }; -std::vector TfIdfEncodingPolicy::row_size = {}; -std::unordered_map TfIdfEncodingPolicy::idfdict = {}; -std::vector> TfIdfEncodingPolicy::tokenCount = {}; - /** * The specialization provides some information about the dictionary encoding * policy. diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 38f306a4ca..4ec1ab4b7c 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -574,30 +574,15 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfEncodingTest) // Every token should be mapped only once BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } - std::cout<<"print output "<> expected = { - { 0.1193, 0.0440, 0.0440, 0.0440, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0.1590, 0.1590, 0.1590, 0 }, - { 0, 0.0440, 0.0440, 0.0440, 0, 0, 0, 0.1193 } + { 0.11928, 0.0440228, 0.0440228, 0.0440228, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0.15904, 0.15904, 0.15904, 0 }, + { 0, 0.044028, 0.0440228, 0.0440228, 0, 0, 0, 0.11928 } }; - std::cout<<"normal expected"< input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + vector> output; + TfIdfEncoding encoder; + + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); + vector> expected = { + { 0.0352, 0, 0.0704, 0, 0 }, + { 0, 0, 0.0503, 0.0503, 0.0682 }, + { 0.0587, 0, 0, 0.0587, 0 } + }; + for(size_t i=0;i Date: Tue, 6 Aug 2019 16:16:57 +0530 Subject: [PATCH 006/265] resolve comments about header and eps --- .../bow_encoding_policy.hpp | 12 ++--- .../tf_idf_encoding_policy.hpp | 6 +-- src/mlpack/tests/string_encoding_test.cpp | 48 +++++++++---------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/bow_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bow_encoding_policy.hpp index 377687dcdb..9660c939e9 100644 --- a/src/mlpack/core/data/string_encoding_policies/bow_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bow_encoding_policy.hpp @@ -9,8 +9,8 @@ * 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_BAG_OF_WORDS_ENCODING_POLICY_HPP -#define MLPACK_CORE_DATA_BAG_OF_WORDS_ENCODING_POLICY_HPP +#ifndef MLPACK_CORE_DATA_ENCODING_POLICIES_BAG_OF_WORDS_ENCODING_POLICY_HPP +#define MLPACK_CORE_DATA_ENCODING_POLICIES_BAG_OF_WORDS_ENCODING_POLICY_HPP #include #include @@ -64,11 +64,11 @@ class BagOfWordsEncodingPolicy size_t /*maxNumTokens*/, size_t dictionarySize) { - output.resize(datasetSize, std::vector (dictionarySize,0)); + output.resize(datasetSize, std::vector (dictionarySize, 0)); } /** - * The function performs the bagofwords encoding algorithm i.e. it writes + * The function performs the bag of words encoding algorithm i.e. it writes * the encoded token to the ouput. * * @param output Output matrix to store the encoded results (sp_mat or mat). @@ -84,7 +84,7 @@ class BagOfWordsEncodingPolicy } /** - * The function performs the bagofwords encoding algorithm i.e. it writes + * The function performs the bag of words encoding algorithm i.e. it writess * the encoded token to the ouput. * Overload function to accepted vector> as output type. * @@ -136,7 +136,7 @@ struct StringEncodingPolicyTraits }; template -using BowEncoding = StringEncoding>; } // namespace data } // namespace mlpack diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index a3a76b2128..16b4cbbc29 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -9,8 +9,8 @@ * 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_TF_IDF_ENCODING_POLICY_HPP -#define MLPACK_CORE_DATA_TF_IDF_ENCODING_POLICY_HPP +#ifndef MLPACK_CORE_DATA_ENCODING_POLICIES_TF_IDF_ENCODING_POLICY_HPP +#define MLPACK_CORE_DATA_ENCODING_POLICIES_TF_IDF_ENCODING_POLICY_HPP #include #include @@ -64,7 +64,7 @@ class TfIdfEncodingPolicy size_t /*maxNumTokens*/, size_t dictionarySize) { - output.resize(datasetSize, std::vector (dictionarySize,0)); + output.resize(datasetSize, std::vector (dictionarySize, 0)); } /** diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 4ec1ab4b7c..74cc5b99ba 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -408,12 +408,12 @@ BOOST_AUTO_TEST_CASE(CharExtractDictionaryEncodingSerialization) CheckMatrices(output, xmlOutput, textOutput, binaryOutput); } -BOOST_AUTO_TEST_CASE(BowEncodingTest) +BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) { using DictionaryType = StringEncodingDictionary; arma::mat output; - BowEncoding encoder; + BagOfWordsEncoding encoder; SplitByAnyOf tokenizer(" "); encoder.Encode(stringEncodingInputSmall, output, tokenizer); @@ -439,12 +439,12 @@ BOOST_AUTO_TEST_CASE(BowEncodingTest) /** * Test the one pass modification of the Bag of Words encoding algorithm. */ -BOOST_AUTO_TEST_CASE(OnePassBowEncodingTest) +BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingTest) { using DictionaryType = StringEncodingDictionary; vector> output; - BowEncoding encoder( + BagOfWordsEncoding encoder( (BagOfWordsEncodingPolicy())); SplitByAnyOf tokenizer(" "); @@ -473,7 +473,7 @@ BOOST_AUTO_TEST_CASE(OnePassBowEncodingTest) /** * Test Bag of Words encoding for characters using lamda function. */ -BOOST_AUTO_TEST_CASE(BowEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) { vector input = { "GACCA", @@ -482,7 +482,7 @@ BOOST_AUTO_TEST_CASE(BowEncodingIndividualCharactersTest) }; arma::mat output; - BowEncoding encoder; + BagOfWordsEncoding encoder; // Passing a empty string to encode characters encoder.Encode(input, output, CharExtract()); @@ -499,7 +499,7 @@ BOOST_AUTO_TEST_CASE(BowEncodingIndividualCharactersTest) * Test the one pass modification of the Bag of Words encoding algorithm * in case of individual character encoding. */ -BOOST_AUTO_TEST_CASE(OnePassBowEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingIndividualCharactersTest) { std::vector input = { "GACCA", @@ -508,7 +508,7 @@ BOOST_AUTO_TEST_CASE(OnePassBowEncodingIndividualCharactersTest) }; vector> output; - BowEncoding encoder; + BagOfWordsEncoding encoder; // Passing a empty string to encode characters encoder.Encode(input, output, CharExtract()); @@ -543,11 +543,11 @@ BOOST_AUTO_TEST_CASE(TfIdfEncodingTest) BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } arma::mat expected = { - { 0.1193, 0.0440, 0.0440, 0.0440, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0.1590, 0.1590, 0.1590, 0 }, - { 0, 0.0440, 0.0440, 0.0440, 0, 0, 0, 0.1193 } + { 0.1192803136799, 0.0440228147639, 0.0440228147639, 0.0440228147639, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0.1590404182399, 0.1590404182399, 0.1590404182399, 0 }, + { 0, 0.0440228147639 , 0.0440228147639 , 0.0440228147639 , 0, 0, 0, 0.1192803136799 } }; - CheckMatrices(output, expected, 1e-01); + CheckMatrices(output, expected, 1e-10); } /** @@ -576,13 +576,13 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfEncodingTest) } vector> expected = { - { 0.11928, 0.0440228, 0.0440228, 0.0440228, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0.15904, 0.15904, 0.15904, 0 }, - { 0, 0.044028, 0.0440228, 0.0440228, 0, 0, 0, 0.11928 } + { 0.1192803136799, 0.0440228147639, 0.0440228147639, 0.0440228147639, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0.1590404182399, 0.1590404182399, 0.1590404182399, 0 }, + { 0, 0.0440228147639 , 0.0440228147639 , 0.0440228147639 , 0, 0, 0, 0.1192803136799 } }; for(size_t i=0;i> expected = { - { 0.0352, 0, 0.0704, 0, 0 }, - { 0, 0, 0.0503, 0.0503, 0.0682 }, - { 0.0587, 0, 0, 0.0587, 0 } + { 0.0352182518111, 0, 0.0704365036223, 0, 0 }, + { 0, 0, 0.0503117883016, 0.0503117883016, 0.0681601792457 }, + { 0.0586970863519, 0, 0, 0.0586970863519, 0 } }; for(size_t i=0;i Date: Tue, 6 Aug 2019 18:44:00 +0530 Subject: [PATCH 007/265] rewriting test for long inputs as accuracy as eps = 1e-12 --- src/mlpack/tests/string_encoding_test.cpp | 103 +++++++++++++++------- 1 file changed, 70 insertions(+), 33 deletions(-) diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 74cc5b99ba..185e8820ef 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -39,12 +39,6 @@ static vector stringEncodingInput = { "command-line programs and Python bindings." }; -static vector stringEncodingInputSmall = { - "hello how are you", - "i am good", - "Good how are you", -}; - /** * Test the dictionary encoding algorithm. */ @@ -416,8 +410,7 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) BagOfWordsEncoding encoder; SplitByAnyOf tokenizer(" "); - encoder.Encode(stringEncodingInputSmall, output, tokenizer); - + encoder.Encode(stringEncodingInput, output, tokenizer); const DictionaryType& dictionary = encoder.Dictionary(); // Checking that everything is mapped to different numbers @@ -429,9 +422,12 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } arma::mat expected = { - { 1, 1, 1, 1, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 1, 1, 1, 0 }, - { 0, 1, 1, 1, 0, 0, 0, 1 } + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; CheckMatrices(output, expected); } @@ -448,7 +444,7 @@ BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingTest) (BagOfWordsEncodingPolicy())); SplitByAnyOf tokenizer(" "); - encoder.Encode(stringEncodingInputSmall, output, tokenizer); + encoder.Encode(stringEncodingInput, output, tokenizer); const DictionaryType& dictionary = encoder.Dictionary(); @@ -462,9 +458,12 @@ BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingTest) } vector> expected = { - { 1, 1, 1, 1, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 1, 1, 1, 0 }, - { 0, 1, 1, 1, 0, 0, 0, 1 } + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; BOOST_REQUIRE(output == expected); @@ -530,7 +529,7 @@ BOOST_AUTO_TEST_CASE(TfIdfEncodingTest) TfIdfEncoding encoder; SplitByAnyOf tokenizer(" "); - encoder.Encode(stringEncodingInputSmall, output, tokenizer); + encoder.Encode(stringEncodingInput, output, tokenizer); const DictionaryType& dictionary = encoder.Dictionary(); @@ -543,11 +542,29 @@ BOOST_AUTO_TEST_CASE(TfIdfEncodingTest) BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } arma::mat expected = { - { 0.1192803136799, 0.0440228147639, 0.0440228147639, 0.0440228147639, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0.1590404182399, 0.1590404182399, 0.1590404182399, 0 }, - { 0, 0.0440228147639 , 0.0440228147639 , 0.0440228147639 , 0, 0, 0, 0.1192803136799 } + { 0.01100570369098, 0.01100570369098, 0.029820078419979, + 0.029820078419979, 0.029820078419979, 0, 0.029820078419979, + 0.01100570369098, 0.01100570369098, 0.01100570369098, 0.029820078419979, + 0.029820078419979, 0.029820078419979, 0, 0.029820078419979, + 0.029820078419979, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0.0053360987592630683, 0, 0, 0, 0, 0, 0, 0.016008296277789203, + 0.016008296277789203, 0, 0, 0, 0, 0, 0, 0.014458219839989772, + 0.014458219839989772, 0.014458219839989772, 0.043374659519969, + 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, + 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, + 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, + 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, + 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0.011739417270378749, 0, 0, 0, 0, 0, 0, 0.011739417270378749, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0.031808083647977492, 0.031808083647977492, 0.031808083647977492, + 0.031808083647977492, 0.031808083647977492, 0.031808083647977492, + 0.031808083647977492, 0.031808083647977492, 0.031808083647977492, + 0.031808083647977492, 0.031808083647977492 } }; - CheckMatrices(output, expected, 1e-10); + CheckMatrices(output, expected, 1e-12); } /** @@ -562,7 +579,7 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfEncodingTest) (TfIdfEncodingPolicy())); SplitByAnyOf tokenizer(" "); - encoder.Encode(stringEncodingInputSmall, output, tokenizer); + encoder.Encode(stringEncodingInput, output, tokenizer); const DictionaryType& dictionary = encoder.Dictionary(); @@ -576,13 +593,31 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfEncodingTest) } vector> expected = { - { 0.1192803136799, 0.0440228147639, 0.0440228147639, 0.0440228147639, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0.1590404182399, 0.1590404182399, 0.1590404182399, 0 }, - { 0, 0.0440228147639 , 0.0440228147639 , 0.0440228147639 , 0, 0, 0, 0.1192803136799 } + { 0.01100570369098, 0.01100570369098, 0.029820078419979, + 0.029820078419979, 0.029820078419979, 0, 0.029820078419979, + 0.01100570369098, 0.01100570369098, 0.01100570369098, 0.029820078419979, + 0.029820078419979, 0.029820078419979, 0, 0.029820078419979, + 0.029820078419979, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0.0053360987592630683, 0, 0, 0, 0, 0, 0, 0.016008296277789203, + 0.016008296277789203, 0, 0, 0, 0, 0, 0, 0.014458219839989772, + 0.014458219839989772, 0.014458219839989772, 0.043374659519969, + 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, + 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, + 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, + 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, + 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0.011739417270378749, 0, 0, 0, 0, 0, 0, 0.011739417270378749, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0.031808083647977492, 0.031808083647977492, 0.031808083647977492, + 0.031808083647977492, 0.031808083647977492, 0.031808083647977492, + 0.031808083647977492, 0.031808083647977492, 0.031808083647977492, + 0.031808083647977492, 0.031808083647977492 } }; for(size_t i=0;i> expected = { - { 0.0352182518111, 0, 0.0704365036223, 0, 0 }, - { 0, 0, 0.0503117883016, 0.0503117883016, 0.0681601792457 }, - { 0.0586970863519, 0, 0, 0.0586970863519, 0 } + { 0.0352182518111362474755310, 0, 0.0704365036222724949510621, 0, 0 }, + { 0, 0, 0.0503117883016232086967889, 0.0503117883016232086967889, + 0.0681601792456660582342209 }, + { 0.0586970863518937457925517, 0, 0, 0.0586970863518937457925517, 0 } }; for(size_t i=0;i Date: Wed, 7 Aug 2019 01:01:35 +0530 Subject: [PATCH 008/265] Added some different implimentation of tf-idf along with test --- .../tf_idf_encoding_policy.hpp | 42 ++- src/mlpack/tests/string_encoding_test.cpp | 298 ++++++++++++++---- 2 files changed, 283 insertions(+), 57 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 16b4cbbc29..aded2bf404 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -30,6 +30,11 @@ class TfIdfEncodingPolicy { public: + TfIdfEncodingPolicy(bool binary = false, bool smooth_idf = true, + bool sublinear_tf = false, bool raw_count = true, + bool term_frequency = false) : binary (binary), + smooth_idf (smooth_idf), sublinear_tf (sublinear_tf), + raw_count (raw_count), term_frequency (term_frequency) {} /** * The function initializes the output matrix. * @@ -80,8 +85,20 @@ class TfIdfEncodingPolicy void Encode(MatType& output, size_t value, size_t row, size_t /*col*/) { // Important since Mapping starts from 1 whereas allowed column value is 0. - output(row, value-1) = (tokenCount[row][value - 1] / row_size[row]) * - std::log10(output.n_rows / idfdict[value-1]); + double idf, tf; + if(smooth_idf) + idf = std::log((output.n_rows + 1) / (1 + idfdict[value-1])) + 1; + else + idf = std::log(output.n_rows / idfdict[value - 1]) + 1; + if (sublinear_tf) + tf = tokenCount[row][value - 1] / row_size[row]; + else if (term_frequency) + tf = std::log(tokenCount[row][value - 1]) + 1; + else if (binary) + tf = tokenCount[row][value - 1] > 0 ? 1 : 0; + else + tf = tokenCount[row][value - 1]; + output(row, value-1) = tf * idf; } /** @@ -99,8 +116,20 @@ class TfIdfEncodingPolicy size_t row, size_t /*col*/) { // Important since Mapping starts from 1 whereas allowed column value is 0. - output[row][value-1] = (tokenCount[row][value - 1] / row_size[row]) * - std::log10(output.size() / idfdict[value-1]); + double idf, tf; + if(smooth_idf) + idf = std::log((output.size() + 1) / (1 + idfdict[value-1])) + 1; + else + idf = std::log(output.size() / idfdict[value - 1]) + 1; + if (sublinear_tf) + tf = tokenCount[row][value - 1] / row_size[row]; + else if (term_frequency) + tf = std::log(tokenCount[row][value - 1]) + 1; + else if (binary) + tf = tokenCount[row][value - 1] > 0 ? 1 : 0; + else + tf = tokenCount[row][value - 1]; + output[row][value-1] = tf * idf; } /** @@ -137,6 +166,11 @@ class TfIdfEncodingPolicy std::vector> tokenCount; std::unordered_map idfdict; std::vector row_size; + bool binary; + bool smooth_idf; + bool sublinear_tf; + bool raw_count; + bool term_frequency; }; /** diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 185e8820ef..1075eace4a 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -521,7 +521,7 @@ BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingIndividualCharactersTest) BOOST_REQUIRE(output == expected); } -BOOST_AUTO_TEST_CASE(TfIdfEncodingTest) +BOOST_AUTO_TEST_CASE(TfIdfrawcountsmoothidftrueEncodingTest) { using DictionaryType = StringEncodingDictionary; @@ -542,27 +542,24 @@ BOOST_AUTO_TEST_CASE(TfIdfEncodingTest) BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } arma::mat expected = { - { 0.01100570369098, 0.01100570369098, 0.029820078419979, - 0.029820078419979, 0.029820078419979, 0, 0.029820078419979, - 0.01100570369098, 0.01100570369098, 0.01100570369098, 0.029820078419979, - 0.029820078419979, 0.029820078419979, 0, 0.029820078419979, - 0.029820078419979, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0.0053360987592630683, 0, 0, 0, 0, 0, 0, 0.016008296277789203, - 0.016008296277789203, 0, 0, 0, 0, 0, 0, 0.014458219839989772, - 0.014458219839989772, 0.014458219839989772, 0.043374659519969, - 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, - 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, - 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, - 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, - 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0.011739417270378749, 0, 0, 0, 0, 0, 0, 0.011739417270378749, 0, 0, 0, + { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, + 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0.031808083647977492, 0.031808083647977492, 0.031808083647977492, - 0.031808083647977492, 0.031808083647977492, 0.031808083647977492, - 0.031808083647977492, 0.031808083647977492, 0.031808083647977492, - 0.031808083647977492, 0.031808083647977492 } + 0 }, + { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, + 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995 } }; CheckMatrices(output, expected, 1e-12); } @@ -570,7 +567,7 @@ BOOST_AUTO_TEST_CASE(TfIdfEncodingTest) /** * Test the one pass modification of the TfIdf encoding algorithm. */ -BOOST_AUTO_TEST_CASE(OnePassTfIdfEncodingTest) +BOOST_AUTO_TEST_CASE(OnePassTfIdfrawcountsmoothidftrueEncodingTest) { using DictionaryType = StringEncodingDictionary; @@ -593,27 +590,24 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfEncodingTest) } vector> expected = { - { 0.01100570369098, 0.01100570369098, 0.029820078419979, - 0.029820078419979, 0.029820078419979, 0, 0.029820078419979, - 0.01100570369098, 0.01100570369098, 0.01100570369098, 0.029820078419979, - 0.029820078419979, 0.029820078419979, 0, 0.029820078419979, - 0.029820078419979, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0.0053360987592630683, 0, 0, 0, 0, 0, 0, 0.016008296277789203, - 0.016008296277789203, 0, 0, 0, 0, 0, 0, 0.014458219839989772, - 0.014458219839989772, 0.014458219839989772, 0.043374659519969, - 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, - 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, - 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, - 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, - 0.014458219839989772, 0.014458219839989772, 0.014458219839989772, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0.011739417270378749, 0, 0, 0, 0, 0, 0, 0.011739417270378749, 0, 0, 0, + { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, + 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0.031808083647977492, 0.031808083647977492, 0.031808083647977492, - 0.031808083647977492, 0.031808083647977492, 0.031808083647977492, - 0.031808083647977492, 0.031808083647977492, 0.031808083647977492, - 0.031808083647977492, 0.031808083647977492 } + 0 }, + { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, + 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995 } }; for(size_t i=0;i input = { "GACCA", @@ -634,13 +628,12 @@ BOOST_AUTO_TEST_CASE(TfIdfEncodingIndividualCharactersTest) arma::mat output; TfIdfEncoding encoder; - // Passing a empty string to encode characters + // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); arma::mat target = { - { 0.0352182518111362474755310, 0, 0.0704365036222724949510621, 0, 0 }, - { 0, 0, 0.0503117883016232086967889, 0.0503117883016232086967889, - 0.0681601792456660582342209 }, - { 0.0586970863518937457925517, 0, 0, 0.0586970863518937457925517, 0 } + { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, + { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, + { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } }; CheckMatrices(output, target, 1e-12); } @@ -649,7 +642,7 @@ BOOST_AUTO_TEST_CASE(TfIdfEncodingIndividualCharactersTest) * Test the one pass modification of the Bag of Words encoding algorithm * in case of individual character encoding. */ -BOOST_AUTO_TEST_CASE(OnePassTfIdfEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(OnePassTfIdfrawcountsmoothidftrueEncodingIndividualCharactersTest) { std::vector input = { "GACCA", @@ -663,15 +656,214 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfEncodingIndividualCharactersTest) // Passing a empty string to encode characters encoder.Encode(input, output, CharExtract()); vector> expected = { - { 0.0352182518111362474755310, 0, 0.0704365036222724949510621, 0, 0 }, - { 0, 0, 0.0503117883016232086967889, 0.0503117883016232086967889, - 0.0681601792456660582342209 }, - { 0.0586970863518937457925517, 0, 0, 0.0586970863518937457925517, 0 } + { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, + { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, + { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } }; for(size_t i=0;i; + + arma::mat output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy(false, false))); + SplitByAnyOf tokenizer(" "); + + encoder.Encode(stringEncodingInput, output, tokenizer); + + const DictionaryType& dictionary = encoder.Dictionary(); + + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } + + arma::mat expected = { + { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, + 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, + 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811 } + }; + CheckMatrices(output, expected, 1e-12); +} + +/** + * Test the one pass modification of the TfIdf encoding algorithm. + */ +BOOST_AUTO_TEST_CASE(OnePassTfIdfrawcountsmoothidffalseEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; + + vector> output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy(false, false))); + SplitByAnyOf tokenizer(" "); + + encoder.Encode(stringEncodingInput, output, tokenizer); + + const DictionaryType& dictionary = encoder.Dictionary(); + + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } + + vector> expected = { + { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, + 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, + 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811 } + }; + for(size_t i=0;i input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + arma::mat output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy(false, false))); + + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, + { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, + { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } + }; + CheckMatrices(output, target, 1e-12); +} + +/** + * Test the one pass modification of the Bag of Words encoding algorithm + * in case of individual character encoding. + */ +BOOST_AUTO_TEST_CASE(OnePassTfIdfrawcountsmoothidfalseEncodingIndividualCharactersTest) +{ + std::vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + vector> output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy(false, false))); + + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); + vector> expected = { + { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, + { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, + { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } + }; + for(size_t i=0;i input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + arma::mat output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy(true))); + + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, + { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, + { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + }; + CheckMatrices(output, target, 1e-12); +} + +/** + * Test the one pass modification of the Bag of Words encoding algorithm + * in case of individual character encoding. + */ +BOOST_AUTO_TEST_CASE(OnePassTfIdfbnarysmoothidtrueEncodingIndividualCharactersTest) +{ + std::vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + vector> output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy(true))); + + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); + vector> expected = { + { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, + { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, + { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + }; + for(size_t i=0;i Date: Wed, 7 Aug 2019 01:14:30 +0530 Subject: [PATCH 009/265] renamed file bow_encoding_policy.hpp to bag_of_words_encoding_policy.hpp --- src/mlpack/core/data/string_encoding_policies/CMakeLists.txt | 2 +- ...bow_encoding_policy.hpp => bag_of_words_encoding_policy.hpp} | 2 +- src/mlpack/tests/string_encoding_test.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/mlpack/core/data/string_encoding_policies/{bow_encoding_policy.hpp => bag_of_words_encoding_policy.hpp} (99%) diff --git a/src/mlpack/core/data/string_encoding_policies/CMakeLists.txt b/src/mlpack/core/data/string_encoding_policies/CMakeLists.txt index ad88e9c0fc..b78b409453 100644 --- a/src/mlpack/core/data/string_encoding_policies/CMakeLists.txt +++ b/src/mlpack/core/data/string_encoding_policies/CMakeLists.txt @@ -2,7 +2,7 @@ # Anything not in this list will not be compiled into mlpack. set(SOURCES dictionary_encoding_policy.hpp - bow_encoding_policy.hpp + bag_of_words_encoding_policy.hpp tf_idf_encoding_policy.hpp policy_traits.hpp ) diff --git a/src/mlpack/core/data/string_encoding_policies/bow_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp similarity index 99% rename from src/mlpack/core/data/string_encoding_policies/bow_encoding_policy.hpp rename to src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index 9660c939e9..2ba40ede27 100644 --- a/src/mlpack/core/data/string_encoding_policies/bow_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -1,5 +1,5 @@ /** - * @file bow_encoding_policy.hpp + * @file bag_of_words_encoding_policy.hpp * @author Jeffin Sam * * Definition of the BagOfWordsEncodingPolicy class. diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 1075eace4a..a3c0f5e392 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include From 195461fe172ef454b46579821d8fd276a0eaf00a Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Wed, 7 Aug 2019 07:48:11 +0530 Subject: [PATCH 010/265] use enum for tfTypes instead of bool values --- .../tf_idf_encoding_policy.hpp | 32 ++-- src/mlpack/tests/string_encoding_test.cpp | 160 ++++++++++++++++-- 2 files changed, 162 insertions(+), 30 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index aded2bf404..90e5e2ecc9 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -29,12 +29,17 @@ namespace data { class TfIdfEncodingPolicy { public: + + enum tfTypes + { + rawCount, + binary, + sublinearTf, + termFrequency, + }; - TfIdfEncodingPolicy(bool binary = false, bool smooth_idf = true, - bool sublinear_tf = false, bool raw_count = true, - bool term_frequency = false) : binary (binary), - smooth_idf (smooth_idf), sublinear_tf (sublinear_tf), - raw_count (raw_count), term_frequency (term_frequency) {} + TfIdfEncodingPolicy(size_t tfType = 0, bool smooth_idf = true) : + tfType (tfType), smooth_idf (smooth_idf) {} /** * The function initializes the output matrix. * @@ -90,11 +95,11 @@ class TfIdfEncodingPolicy idf = std::log((output.n_rows + 1) / (1 + idfdict[value-1])) + 1; else idf = std::log(output.n_rows / idfdict[value - 1]) + 1; - if (sublinear_tf) + if (tfType == tfTypes::termFrequency) tf = tokenCount[row][value - 1] / row_size[row]; - else if (term_frequency) + else if (tfType == tfTypes::sublinearTf) tf = std::log(tokenCount[row][value - 1]) + 1; - else if (binary) + else if (tfType == tfTypes::binary) tf = tokenCount[row][value - 1] > 0 ? 1 : 0; else tf = tokenCount[row][value - 1]; @@ -121,11 +126,11 @@ class TfIdfEncodingPolicy idf = std::log((output.size() + 1) / (1 + idfdict[value-1])) + 1; else idf = std::log(output.size() / idfdict[value - 1]) + 1; - if (sublinear_tf) + if (tfType == tfTypes::termFrequency) tf = tokenCount[row][value - 1] / row_size[row]; - else if (term_frequency) + else if (tfType == tfTypes::sublinearTf) tf = std::log(tokenCount[row][value - 1]) + 1; - else if (binary) + else if (tfType == tfTypes::binary) tf = tokenCount[row][value - 1] > 0 ? 1 : 0; else tf = tokenCount[row][value - 1]; @@ -166,11 +171,8 @@ class TfIdfEncodingPolicy std::vector> tokenCount; std::unordered_map idfdict; std::vector row_size; - bool binary; bool smooth_idf; - bool sublinear_tf; - bool raw_count; - bool term_frequency; + size_t tfType; }; /** diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index a3c0f5e392..97cd82ba7c 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -521,7 +521,7 @@ BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingIndividualCharactersTest) BOOST_REQUIRE(output == expected); } -BOOST_AUTO_TEST_CASE(TfIdfrawcountsmoothidftrueEncodingTest) +BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) { using DictionaryType = StringEncodingDictionary; @@ -567,7 +567,7 @@ BOOST_AUTO_TEST_CASE(TfIdfrawcountsmoothidftrueEncodingTest) /** * Test the one pass modification of the TfIdf encoding algorithm. */ -BOOST_AUTO_TEST_CASE(OnePassTfIdfrawcountsmoothidftrueEncodingTest) +BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingTest) { using DictionaryType = StringEncodingDictionary; @@ -617,7 +617,7 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfrawcountsmoothidftrueEncodingTest) /** * Test TFIDF encoding for characters using lamda function. */ -BOOST_AUTO_TEST_CASE(TfIdfrawcountsmoothidftrueEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) { vector input = { "GACCA", @@ -642,7 +642,7 @@ BOOST_AUTO_TEST_CASE(TfIdfrawcountsmoothidftrueEncodingIndividualCharactersTest) * Test the one pass modification of the Bag of Words encoding algorithm * in case of individual character encoding. */ -BOOST_AUTO_TEST_CASE(OnePassTfIdfrawcountsmoothidftrueEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingIndividualCharactersTest) { std::vector input = { "GACCA", @@ -665,13 +665,13 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfrawcountsmoothidftrueEncodingIndividualCharacte BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); } -BOOST_AUTO_TEST_CASE(TfIdfrawcountsmoothidffalseEncodingTest) +BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) { using DictionaryType = StringEncodingDictionary; arma::mat output; TfIdfEncoding encoder( - (TfIdfEncodingPolicy(false, false))); + (TfIdfEncodingPolicy(0, false))); SplitByAnyOf tokenizer(" "); encoder.Encode(stringEncodingInput, output, tokenizer); @@ -713,13 +713,13 @@ BOOST_AUTO_TEST_CASE(TfIdfrawcountsmoothidffalseEncodingTest) /** * Test the one pass modification of the TfIdf encoding algorithm. */ -BOOST_AUTO_TEST_CASE(OnePassTfIdfrawcountsmoothidffalseEncodingTest) +BOOST_AUTO_TEST_CASE(OnePassTfIdfRawCountEncodingTest) { using DictionaryType = StringEncodingDictionary; vector> output; TfIdfEncoding encoder( - (TfIdfEncodingPolicy(false, false))); + (TfIdfEncodingPolicy(0, false))); SplitByAnyOf tokenizer(" "); encoder.Encode(stringEncodingInput, output, tokenizer); @@ -763,7 +763,7 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfrawcountsmoothidffalseEncodingTest) /** * Test TFIDF encoding for characters using lamda function. */ -BOOST_AUTO_TEST_CASE(TfIdfrawcountsmoothidffalseEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) { vector input = { "GACCA", @@ -773,7 +773,7 @@ BOOST_AUTO_TEST_CASE(TfIdfrawcountsmoothidffalseEncodingIndividualCharactersTest arma::mat output; TfIdfEncoding encoder( - (TfIdfEncodingPolicy(false, false))); + (TfIdfEncodingPolicy(0, false))); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -789,7 +789,7 @@ BOOST_AUTO_TEST_CASE(TfIdfrawcountsmoothidffalseEncodingIndividualCharactersTest * Test the one pass modification of the Bag of Words encoding algorithm * in case of individual character encoding. */ -BOOST_AUTO_TEST_CASE(OnePassTfIdfrawcountsmoothidfalseEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(OnePassRawcountEncodingIndividualCharactersTest) { std::vector input = { "GACCA", @@ -816,7 +816,7 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfrawcountsmoothidfalseEncodingIndividualCharacte /** * Test TFIDF encoding for characters using lamda function. */ -BOOST_AUTO_TEST_CASE(TfIdfbinarysmoothidftrueEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) { vector input = { "GACCA", @@ -826,7 +826,7 @@ BOOST_AUTO_TEST_CASE(TfIdfbinarysmoothidftrueEncodingIndividualCharactersTest) arma::mat output; TfIdfEncoding encoder( - (TfIdfEncodingPolicy(true))); + (TfIdfEncodingPolicy(1, true))); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -842,7 +842,7 @@ BOOST_AUTO_TEST_CASE(TfIdfbinarysmoothidftrueEncodingIndividualCharactersTest) * Test the one pass modification of the Bag of Words encoding algorithm * in case of individual character encoding. */ -BOOST_AUTO_TEST_CASE(OnePassTfIdfbnarysmoothidtrueEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(OnePassBnarySmoothIdfEncodingIndividualCharactersTest) { std::vector input = { "GACCA", @@ -852,7 +852,7 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfbnarysmoothidtrueEncodingIndividualCharactersTe vector> output; TfIdfEncoding encoder( - (TfIdfEncodingPolicy(true))); + (TfIdfEncodingPolicy(1, true))); // Passing a empty string to encode characters encoder.Encode(input, output, CharExtract()); @@ -865,5 +865,135 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfbnarysmoothidtrueEncodingIndividualCharactersTe for(size_t j=0;j input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + arma::mat output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy(1, false))); + + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1.4054651081081644, 1, 1.4054651081081644, 0, 0 }, + { 0, 1, 1.4054651081081644, 1.4054651081081644, 2.0986122886681100 }, + { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } + }; + CheckMatrices(output, target, 1e-12); +} + +/** +* Test TFIDF encoding for characters using lamda function. +*/ +BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + arma::mat output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy(2, true))); + + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1.2876820724517808, 1.6931471805599454, 2.1802352704293200, 0, 0 }, + { 0, 1.6931471805599454, 2.1802352704293200, 2.1802352704293200, + 1.6931471805599454 }, + { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + }; + CheckMatrices(output, target, 1e-12); +} + +/** +* Test TFIDF encoding for characters using lamda function. +*/ +BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + arma::mat output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy(2, false))); + + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1.4054651081081644, 1.6931471805599454, 2.3796592851687173, 0, 0 }, + { 0, 1.6931471805599454, 2.3796592851687173, 2.3796592851687173, + 2.0986122886681100 }, + { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } + }; + CheckMatrices(output, target, 1e-12); +} + +/** +* Test TFIDF encoding for characters using lamda function. +*/ +BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + arma::mat output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy(3, true))); + + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 0.2575364144903562, 0.4, 0.5150728289807124, 0, 0 }, + { 0, 0.2857142857142857, 0.3679091635576516, 0.3679091635576516, + 0.2418781686514208 }, + { 0.4292273574839269, 0.3333333333333333, 0, 0.4292273574839269, 0 } + }; + CheckMatrices(output, target, 1e-12); +} + +/** +* Test TFIDF encoding for characters using lamda function. +*/ +BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; + + arma::mat output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy(3, false))); + + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 0.2810930216216329, 0.4, 0.5621860432432658, 0, 0 }, + { 0, 0.2857142857142857, 0.4015614594594755, 0.4015614594594755, + 0.2998017555240157 }, + { 0.4684883693693881, 0.3333333333333333, 0, 0.4684883693693881, 0 } + }; + CheckMatrices(output, target, 1e-12); +} + BOOST_AUTO_TEST_SUITE_END(); From 2e615024d3e2af066c48d040005327ddb1dc1c78 Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Wed, 7 Aug 2019 11:23:56 +0530 Subject: [PATCH 011/265] Done with the documetnation and adapted test accordingly --- .../bag_of_words_encoding_policy.hpp | 25 ++++--- .../dictionary_encoding_policy.hpp | 10 ++- .../tf_idf_encoding_policy.hpp | 56 ++++++++++++---- src/mlpack/tests/string_encoding_test.cpp | 66 ++++++++++++------- 4 files changed, 106 insertions(+), 51 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index 2ba40ede27..e692cfce1b 100644 --- a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -21,11 +21,10 @@ namespace data { /** * Definition of the BagOfWordsEncodingPolicy class. * - * DicitonaryEnocding is used as a helper class for StringEncoding. - * The encoder assigns a positive integer number to each unique token and treat - * the dataset as categorical. The numbers are assigned sequentially starting - * from one. The tokens are labeled in the order of their occurrence - * in the input dataset. + * BagOfWords is used as a helper class for StringEncoding. + * The encoder create a vector of all the unique token and then assigns + * 1 if the token is present in the document, 0 if not present. The tokens + * are labeled in the order of their occurrence in the input dataset. */ class BagOfWordsEncodingPolicy { @@ -77,7 +76,10 @@ class BagOfWordsEncodingPolicy * @param col The row token number at which the encoding is performed. */ template - static void Encode(MatType& output, size_t value, size_t row, size_t /*col*/) + static void Encode(MatType& output, + size_t value, + size_t row, + size_t /*col*/) { // Important since Mapping starts from 1 whereas allowed column value is 0. output(row, value-1) = 1; @@ -94,8 +96,10 @@ class BagOfWordsEncodingPolicy * @param col The row token number at which the encoding is performed. */ template - static void Encode(std::vector >& output, size_t value, - size_t row, size_t /*col*/) + static void Encode(std::vector >& output, + size_t value, + size_t row, + size_t /*col*/) { // Important since Mapping starts from 1 whereas allowed column value is 0. output[row][value-1] = 1; @@ -117,8 +121,9 @@ class BagOfWordsEncodingPolicy * @param numToken The count of token parsed till now. * @param value The encoded token. */ - static void PreprocessToken(size_t /*row*/, size_t /*numTokens*/, - size_t /*value*/) { } + static void PreprocessToken(size_t /*row*/, + size_t /*numTokens*/, + size_t /*value*/) { } }; /** diff --git a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp index f11e7bb566..9c972fd856 100644 --- a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp @@ -58,7 +58,10 @@ class DictionaryEncodingPolicy * @param col The row token number at which the encoding is performed. */ template - static void Encode(MatType& output, size_t value, size_t row, size_t col) + static void Encode(MatType& output, + size_t value, + size_t row, + size_t col) { output(row, col) = value; } @@ -84,8 +87,9 @@ class DictionaryEncodingPolicy * @param numToken The count of token parsed till now. * @param value The encoded token. */ - static void PreprocessToken(size_t /*row*/, size_t /*numTokens*/, - size_t /*value*/) { } + static void PreprocessToken(size_t /*row*/, + size_t /*numTokens*/, + size_t /*value*/) {} /** * Serialize the class to the given archive. diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 90e5e2ecc9..4cdf8095e7 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -20,16 +20,33 @@ namespace data { /** * Definition of the TfIdfEncodingPolicy class. * - * DicitonaryEnocding is used as a helper class for StringEncoding. - * The encoder assigns a positive integer number to each unique token and treat - * the dataset as categorical. The numbers are assigned sequentially starting - * from one. The tokens are labeled in the order of their occurrence - * in the input dataset. + * Tf means term-frequency while tf-idf means term-frequency times inverse + * document-frequency. This is a common term weighting scheme in information + * retrieval, that has also found good use in document classification. + * The goal of using tf-idf instead of the raw frequencies of occurrence of a + * token in a given document is to scale down the impact of tokens that occur + * very frequently in a given corpus and that are hence empirically less + * informative than features that occur in a small fraction of the training + * corpus. + * TfIdfEncodingPolicy is used as a helper class for StringEncoding. + * The encoder assigns a tf-idf number to each unique token and treat + * the dataset as categorical. The tokens are labeled in the order of their + * occurrence in the input dataset. */ class TfIdfEncodingPolicy { public: + /* + * Enum Class used to identify the type of tf encoding + * + * Follwing are the defination of the types + * binary : binary weighting scheme (0,1) + * rawCount : raw count weighting scheme (count of token for every row) + * termFrequency : term frequency weighting scheme (count / length(row)) + * subinerTf : logarthimic weighting scheme (log(tf) + 1) + * + */ enum tfTypes { rawCount, @@ -87,12 +104,15 @@ class TfIdfEncodingPolicy * @param col The row token number at which the encoding is performed. */ template - void Encode(MatType& output, size_t value, size_t row, size_t /*col*/) + void Encode(MatType& output, + size_t value, + size_t row, + size_t /*col*/) { // Important since Mapping starts from 1 whereas allowed column value is 0. double idf, tf; if(smooth_idf) - idf = std::log((output.n_rows + 1) / (1 + idfdict[value-1])) + 1; + idf = std::log((output.n_rows + 1) / (1 + idfdict[value - 1])) + 1; else idf = std::log(output.n_rows / idfdict[value - 1]) + 1; if (tfType == tfTypes::termFrequency) @@ -117,13 +137,15 @@ class TfIdfEncodingPolicy * @param col The row token number at which the encoding is performed. */ template - void Encode(std::vector >& output, size_t value, - size_t row, size_t /*col*/) + void Encode(std::vector >& output, + size_t value, + size_t row, + size_t /*col*/) { // Important since Mapping starts from 1 whereas allowed column value is 0. double idf, tf; if(smooth_idf) - idf = std::log((output.size() + 1) / (1 + idfdict[value-1])) + 1; + idf = std::log((output.size() + 1) / (1 + idfdict[value - 1])) + 1; else idf = std::log(output.size() / idfdict[value - 1]) + 1; if (tfType == tfTypes::termFrequency) @@ -154,8 +176,9 @@ class TfIdfEncodingPolicy * @param numToken The count of token parsed till now. * @param value The encoded token. */ - void PreprocessToken(size_t row, size_t /*numTokens*/, - size_t value) + void PreprocessToken(size_t row, + size_t /*numTokens*/, + size_t value) { if(row>=tokenCount.size()) { @@ -163,15 +186,20 @@ class TfIdfEncodingPolicy tokenCount.push_back(std::unordered_map()); } tokenCount.back()[value-1]++; - if(tokenCount.back()[value-1]==1) - idfdict[value-1]++; + if(tokenCount.back()[value - 1]==1) + idfdict[value - 1]++; row_size.back()++; } private: + // Used to store the count of token for each row. std::vector> tokenCount; + // Used to store the idf values. std::unordered_map idfdict; + // Used to store the number of tokens in each row. std::vector row_size; + // smooth_idf variable to indicate smoothining. bool smooth_idf; + // Type of Term Frequency to use. size_t tfType; }; diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 97cd82ba7c..544e8fe160 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -521,6 +521,10 @@ BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingIndividualCharactersTest) BOOST_REQUIRE(output == expected); } +/** + * Test the Tf-Idf Encoding using rawcount type and smoothidf as true, + * which is the deafult values used for algorithim. + */ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) { using DictionaryType = StringEncodingDictionary; @@ -565,7 +569,8 @@ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) } /** - * Test the one pass modification of the TfIdf encoding algorithm. + * Test the one pass modification of the TfIdf encoding algorithm, using rawcount + * as type of tf and smoothIdf as true. */ BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingTest) { @@ -615,8 +620,9 @@ BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingTest) } /** -* Test TFIDF encoding for characters using lamda function. -*/ + * Test TFIDF encoding for characters using lamda function, using rawcount as tf + * type and smoothidf as true. + */ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) { vector input = { @@ -639,8 +645,8 @@ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) } /** - * Test the one pass modification of the Bag of Words encoding algorithm - * in case of individual character encoding. + * Test the one pass modification of the Tf-Idf encoding algorithm + * in case of individual character encoding using default values. */ BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingIndividualCharactersTest) { @@ -665,6 +671,9 @@ BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingIndividualCharactersTest) BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); } +/** + * Test the Tf-Idf Encoding using rawcount type and smoothidf as false. + */ BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) { using DictionaryType = StringEncodingDictionary; @@ -711,7 +720,8 @@ BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) } /** - * Test the one pass modification of the TfIdf encoding algorithm. + * Test the one pass modification of the TfIdf encoding algorithm, with rawcount + * as type, but with smoothidf as false. */ BOOST_AUTO_TEST_CASE(OnePassTfIdfRawCountEncodingTest) { @@ -761,8 +771,9 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfRawCountEncodingTest) } /** -* Test TFIDF encoding for characters using lamda function. -*/ + * Test TFIDF encoding for characters using lamda function, using rawcount as + * tf type and smoothidf as false. + */ BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) { vector input = { @@ -786,8 +797,9 @@ BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) } /** - * Test the one pass modification of the Bag of Words encoding algorithm - * in case of individual character encoding. + * Test the one pass modification of the Tf Idf encoding algorithm + * in case of individual character encoding, using raw count as type, + * and smoothidf as false. */ BOOST_AUTO_TEST_CASE(OnePassRawcountEncodingIndividualCharactersTest) { @@ -814,8 +826,9 @@ BOOST_AUTO_TEST_CASE(OnePassRawcountEncodingIndividualCharactersTest) } /** -* Test TFIDF encoding for characters using lamda function. -*/ + * Test TFIDF encoding for characters using lamda function, using binary + * weighting scheme for tf and smoothidf as true. + */ BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) { vector input = { @@ -839,8 +852,8 @@ BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) } /** - * Test the one pass modification of the Bag of Words encoding algorithm - * in case of individual character encoding. + * Test TFIDF encoding for characters using lamda function, using binary + * weighting scheme for tf and smoothidf as true. */ BOOST_AUTO_TEST_CASE(OnePassBnarySmoothIdfEncodingIndividualCharactersTest) { @@ -867,8 +880,9 @@ BOOST_AUTO_TEST_CASE(OnePassBnarySmoothIdfEncodingIndividualCharactersTest) } /** -* Test TFIDF encoding for characters using lamda function. -*/ + * Test TFIDF encoding for characters using lamda function, using binary + * as weighting scheme and smoothidf as false. + */ BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) { vector input = { @@ -892,8 +906,9 @@ BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) } /** -* Test TFIDF encoding for characters using lamda function. -*/ + * Test TFIDF encoding for characters using lamda function, using sublinear + * as weighting scheme and smoothidf as true. + */ BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) { vector input = { @@ -918,8 +933,9 @@ BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) } /** -* Test TFIDF encoding for characters using lamda function. -*/ + * Test TFIDF encoding for characters using lamda function, using sublinear + * as weighting scheme and smoothidf as false. + */ BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) { vector input = { @@ -944,8 +960,9 @@ BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) } /** -* Test TFIDF encoding for characters using lamda function. -*/ + * Test TFIDF encoding for characters using lamda function, using term + * Frequency as weighting scheme and smoothidf as true. + */ BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) { vector input = { @@ -970,8 +987,9 @@ BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) } /** -* Test TFIDF encoding for characters using lamda function. -*/ + * Test TFIDF encoding for characters using lamda function, using Term + * Frequency as weighting scheme and smoothidf as false. + */ BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) { vector input = { From efdd833b0aca76394eaa4ac70d8f9f848644e48b Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Wed, 7 Aug 2019 11:38:54 +0530 Subject: [PATCH 012/265] Style fixes --- .../bag_of_words_encoding_policy.hpp | 2 +- .../tf_idf_encoding_policy.hpp | 16 ++++++++------ src/mlpack/tests/string_encoding_test.cpp | 22 +++++++++---------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index e692cfce1b..fdfc26bab9 100644 --- a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -146,4 +146,4 @@ using BagOfWordsEncoding = StringEncoding=tokenCount.size()) + if (row >= tokenCount.size()) { row_size.push_back(0); tokenCount.push_back(std::unordered_map()); } tokenCount.back()[value-1]++; - if(tokenCount.back()[value - 1]==1) + if (tokenCount.back()[value - 1] == 1) idfdict[value - 1]++; row_size.back()++; } @@ -223,4 +225,4 @@ using TfIdfEncoding = StringEncoding Date: Wed, 7 Aug 2019 11:47:16 +0530 Subject: [PATCH 013/265] style fixes part2 --- .../bag_of_words_encoding_policy.hpp | 2 +- .../tf_idf_encoding_policy.hpp | 11 +++++------ src/mlpack/tests/string_encoding_test.cpp | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index fdfc26bab9..87656a1c4c 100644 --- a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -146,4 +146,4 @@ using BagOfWordsEncoding = StringEncoding= tokenCount.size()) { @@ -225,4 +224,4 @@ using TfIdfEncoding = StringEncoding Date: Thu, 8 Aug 2019 19:05:23 +0530 Subject: [PATCH 014/265] resolve comments regarding test, have to add test for serialisation --- .../tf_idf_encoding_policy.hpp | 44 +++++++++++-------- src/mlpack/tests/string_encoding_test.cpp | 30 +++++-------- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index dc10f29e5d..83e4062bda 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -48,14 +48,14 @@ class TfIdfEncodingPolicy */ enum tfTypes { - rawCount, - binary, - sublinearTf, - termFrequency, + RAW_COUNT, + BINARY, + SUBLINEAR_TF, + TERM_FREQUENCY, }; - TfIdfEncodingPolicy(size_t tfType = 0, bool smooth_idf = true) : - tfType(tfType), smooth_idf(smooth_idf) + TfIdfEncodingPolicy(size_t tfType = 0, bool smoothIdf = true) : + tfType(tfType), smoothIdf(smoothIdf) { } /** @@ -112,18 +112,20 @@ class TfIdfEncodingPolicy { // Important since Mapping starts from 1 whereas allowed column value is 0. double idf, tf; - if (smooth_idf) + if (smoothIdf) idf = std::log((output.n_rows + 1) / (1 + idfdict[value - 1])) + 1; else idf = std::log(output.n_rows / idfdict[value - 1]) + 1; - if (tfType == tfTypes::termFrequency) + + if (tfType == tfTypes::TERM_FREQUENCY) tf = tokenCount[row][value - 1] / row_size[row]; - else if (tfType == tfTypes::sublinearTf) + else if (tfType == tfTypes::SUBLINEAR_TF) tf = std::log(tokenCount[row][value - 1]) + 1; - else if (tfType == tfTypes::binary) + else if (tfType == tfTypes::BINARY) tf = tokenCount[row][value - 1] > 0 ? 1 : 0; else tf = tokenCount[row][value - 1]; + output(row, value-1) = tf * idf; } @@ -145,18 +147,20 @@ class TfIdfEncodingPolicy { // Important since Mapping starts from 1 whereas allowed column value is 0. double idf, tf; - if (smooth_idf) + if (smoothIdf) idf = std::log((output.size() + 1) / (1 + idfdict[value - 1])) + 1; else idf = std::log(output.size() / idfdict[value - 1]) + 1; - if (tfType == tfTypes::termFrequency) + + if (tfType == tfTypes::TERM_FREQUENCY) tf = tokenCount[row][value - 1] / row_size[row]; - else if (tfType == tfTypes::sublinearTf) + else if (tfType == tfTypes::SUBLINEAR_TF) tf = std::log(tokenCount[row][value - 1]) + 1; - else if (tfType == tfTypes::binary) + else if (tfType == tfTypes::BINARY) tf = tokenCount[row][value - 1] > 0 ? 1 : 0; else tf = tokenCount[row][value - 1]; + output[row][value-1] = tf * idf; } @@ -164,9 +168,13 @@ class TfIdfEncodingPolicy * Serialize the class to the given archive. */ template - void serialize(Archive& /* ar */, const unsigned int /* version */) + void serialize(Archive& ar , const unsigned int /* version */) { - // Nothing to serialize. + ar & BOOST_SERIALIZATION_NVP(tfType); + ar & BOOST_SERIALIZATION_NVP(tokenCount); + ar & BOOST_SERIALIZATION_NVP(idfdict); + ar & BOOST_SERIALIZATION_NVP(smoothIdf); + ar & BOOST_SERIALIZATION_NVP(row_size); } /* @@ -198,8 +206,8 @@ class TfIdfEncodingPolicy std::unordered_map idfdict; // Used to store the number of tokens in each row. std::vector row_size; - // smooth_idf variable to indicate smoothining. - bool smooth_idf; + // smoothIdf variable to indicate smoothining. + bool smoothIdf; // Type of Term Frequency to use. size_t tfType; }; diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 2cb1201097..87c358ed81 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -728,8 +728,7 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfRawCountEncodingTest) using DictionaryType = StringEncodingDictionary; vector> output; - TfIdfEncoding encoder( - (TfIdfEncodingPolicy(0, false))); + TfIdfEncoding encoder(0, false); SplitByAnyOf tokenizer(" "); encoder.Encode(stringEncodingInput, output, tokenizer); @@ -783,8 +782,7 @@ BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder( - (TfIdfEncodingPolicy(0, false))); + TfIdfEncoding encoder(0, false); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -810,8 +808,7 @@ BOOST_AUTO_TEST_CASE(OnePassRawcountEncodingIndividualCharactersTest) }; vector> output; - TfIdfEncoding encoder( - (TfIdfEncodingPolicy(false, false))); + TfIdfEncoding encoder(0, false); // Passing a empty string to encode characters encoder.Encode(input, output, CharExtract()); @@ -838,8 +835,7 @@ BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder( - (TfIdfEncodingPolicy(1, true))); + TfIdfEncoding encoder(1, true); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -864,8 +860,7 @@ BOOST_AUTO_TEST_CASE(OnePassBnarySmoothIdfEncodingIndividualCharactersTest) }; vector> output; - TfIdfEncoding encoder( - (TfIdfEncodingPolicy(1, true))); + TfIdfEncoding encoder(1, true); // Passing a empty string to encode characters encoder.Encode(input, output, CharExtract()); @@ -892,8 +887,7 @@ BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder( - (TfIdfEncodingPolicy(1, false))); + TfIdfEncoding encoder(1, false); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -918,8 +912,7 @@ BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder( - (TfIdfEncodingPolicy(2, true))); + TfIdfEncoding encoder(2, true); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -945,8 +938,7 @@ BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder( - (TfIdfEncodingPolicy(2, false))); + TfIdfEncoding encoder(2, false); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -972,8 +964,7 @@ BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder( - (TfIdfEncodingPolicy(3, true))); + TfIdfEncoding encoder(3, true); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -999,8 +990,7 @@ BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder( - (TfIdfEncodingPolicy(3, false))); + TfIdfEncoding encoder(3, false); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); From 818c4269739c6fd77a44821c9f721d0cd092d208 Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Sat, 10 Aug 2019 11:48:58 +0530 Subject: [PATCH 015/265] temp commit to seek help --- src/mlpack/core/data/string_encoding_impl.hpp | 2 +- .../bag_of_words_encoding_policy.hpp | 7 +- .../tf_idf_encoding_policy.hpp | 25 +- src/mlpack/tests/string_encoding_test.cpp | 1618 +++++++++-------- 4 files changed, 850 insertions(+), 802 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_impl.hpp b/src/mlpack/core/data/string_encoding_impl.hpp index a621ba0982..058539dc79 100644 --- a/src/mlpack/core/data/string_encoding_impl.hpp +++ b/src/mlpack/core/data/string_encoding_impl.hpp @@ -134,7 +134,7 @@ EncodeHelper(const std::vector& input, } numColumns = std::max(numColumns, numTokens); } - + std::cout<<"dictionary size is "< struct StringEncodingPolicyTraits diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 83e4062bda..42b16aa3b3 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -46,7 +46,7 @@ class TfIdfEncodingPolicy * subinerTf : logarthimic weighting scheme (log(tf) + 1) * */ - enum tfTypes + enum class TfTypes { RAW_COUNT, BINARY, @@ -55,7 +55,8 @@ class TfIdfEncodingPolicy }; TfIdfEncodingPolicy(size_t tfType = 0, bool smoothIdf = true) : - tfType(tfType), smoothIdf(smoothIdf) + tfType(tfType), + smoothIdf(smoothIdf) { } /** @@ -73,6 +74,7 @@ class TfIdfEncodingPolicy size_t /*maxNumTokens*/, size_t dictionarySize) { + std::cout<<"dataset "<(TfTypes::TERM_FREQUENCY)) tf = tokenCount[row][value - 1] / row_size[row]; - else if (tfType == tfTypes::SUBLINEAR_TF) + else if (tfType == static_cast(TfTypes::SUBLINEAR_TF)) tf = std::log(tokenCount[row][value - 1]) + 1; - else if (tfType == tfTypes::BINARY) + else if (tfType == static_cast(TfTypes::BINARY)) tf = tokenCount[row][value - 1] > 0 ? 1 : 0; else tf = tokenCount[row][value - 1]; @@ -152,11 +154,11 @@ class TfIdfEncodingPolicy else idf = std::log(output.size() / idfdict[value - 1]) + 1; - if (tfType == tfTypes::TERM_FREQUENCY) + if (tfType == static_cast(TfTypes::TERM_FREQUENCY)) tf = tokenCount[row][value - 1] / row_size[row]; - else if (tfType == tfTypes::SUBLINEAR_TF) + else if (tfType == static_cast(TfTypes::SUBLINEAR_TF)) tf = std::log(tokenCount[row][value - 1]) + 1; - else if (tfType == tfTypes::BINARY) + else if (tfType == static_cast(TfTypes::BINARY)) tf = tokenCount[row][value - 1] > 0 ? 1 : 0; else tf = tokenCount[row][value - 1]; @@ -174,7 +176,8 @@ class TfIdfEncodingPolicy ar & BOOST_SERIALIZATION_NVP(tokenCount); ar & BOOST_SERIALIZATION_NVP(idfdict); ar & BOOST_SERIALIZATION_NVP(smoothIdf); - ar & BOOST_SERIALIZATION_NVP(row_size); + ar & BOOST_SERIALIZATION_NVP(row_size); + std::cout<<"hello world"<= tokenCount.size()) { row_size.push_back(0); tokenCount.push_back(std::unordered_map()); } + std::cout<<"error"< stringEncodingInput = { "command-line programs and Python bindings." }; -/** - * Test the dictionary encoding algorithm. - */ -BOOST_AUTO_TEST_CASE(DictionaryEncodingTest) -{ - using DictionaryType = StringEncodingDictionary; +// /** +// * Test the dictionary encoding algorithm. +// */ +// BOOST_AUTO_TEST_CASE(DictionaryEncodingTest) +// { +// using DictionaryType = StringEncodingDictionary; - arma::mat output; - DictionaryEncoding encoder; - SplitByAnyOf tokenizer(" .,\""); +// arma::mat output; +// DictionaryEncoding encoder; +// SplitByAnyOf tokenizer(" .,\""); - encoder.Encode(stringEncodingInput, output, tokenizer); +// encoder.Encode(stringEncodingInput, output, tokenizer); - const DictionaryType& dictionary = encoder.Dictionary(); +// const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers - std::unordered_map keysCount; - for (auto& keyValue : dictionary.Mapping()) - { - keysCount[keyValue.second]++; - // Every token should be mapped only once - BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); - } +// // Checking that everything is mapped to different numbers +// std::unordered_map keysCount; +// for (auto& keyValue : dictionary.Mapping()) +// { +// keysCount[keyValue.second]++; +// // Every token should be mapped only once +// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); +// } - arma::mat expected = { - { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 17, 2, 18, 14, 19, 20, 9, 10, 21, 14, 22, 6, 23, 14, 24, 20, 25, - 26, 27, 9, 10, 28, 6, 29, 30, 20, 31, 32, 33, 34, 9, 10, 35 }, - { 36, 37, 14, 38, 39, 8, 40, 1, 41, 42, 43, 44, 6, 45, 13, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } - }; +// arma::mat expected = { +// { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, +// { 17, 2, 18, 14, 19, 20, 9, 10, 21, 14, 22, 6, 23, 14, 24, 20, 25, +// 26, 27, 9, 10, 28, 6, 29, 30, 20, 31, 32, 33, 34, 9, 10, 35 }, +// { 36, 37, 14, 38, 39, 8, 40, 1, 41, 42, 43, 44, 6, 45, 13, 0, 0, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } +// }; - CheckMatrices(output, expected); -} +// CheckMatrices(output, expected); +// } -/** - * Test the one pass modification of the dictionary encoding algorithm. - */ -BOOST_AUTO_TEST_CASE(OnePassDictionaryEncodingTest) -{ - using DictionaryType = StringEncodingDictionary; +// /** +// * Test the one pass modification of the dictionary encoding algorithm. +// */ +// BOOST_AUTO_TEST_CASE(OnePassDictionaryEncodingTest) +// { +// using DictionaryType = StringEncodingDictionary; - vector> output; - DictionaryEncoding encoder( - (DictionaryEncodingPolicy())); - SplitByAnyOf tokenizer(" .,\""); +// vector> output; +// DictionaryEncoding encoder( +// (DictionaryEncodingPolicy())); +// SplitByAnyOf tokenizer(" .,\""); - encoder.Encode(stringEncodingInput, output, tokenizer); +// encoder.Encode(stringEncodingInput, output, tokenizer); - const DictionaryType& dictionary = encoder.Dictionary(); +// const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers - std::unordered_map keysCount; - for (auto& keyValue : dictionary.Mapping()) - { - keysCount[keyValue.second]++; - // Every token should be mapped only once - BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); - } +// // Checking that everything is mapped to different numbers +// std::unordered_map keysCount; +// for (auto& keyValue : dictionary.Mapping()) +// { +// keysCount[keyValue.second]++; +// // Every token should be mapped only once +// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); +// } - vector> expected = { - { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, - { 17, 2, 18, 14, 19, 20, 9, 10, 21, 14, 22, 6, 23, 14, 24, 20, 25, - 26, 27, 9, 10, 28, 6, 29, 30, 20, 31, 32, 33, 34, 9, 10, 35 }, - { 36, 37, 14, 38, 39, 8, 40, 1, 41, 42, 43, 44, 6, 45, 13 } - }; +// vector> expected = { +// { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, +// { 17, 2, 18, 14, 19, 20, 9, 10, 21, 14, 22, 6, 23, 14, 24, 20, 25, +// 26, 27, 9, 10, 28, 6, 29, 30, 20, 31, 32, 33, 34, 9, 10, 35 }, +// { 36, 37, 14, 38, 39, 8, 40, 1, 41, 42, 43, 44, 6, 45, 13 } +// }; - BOOST_REQUIRE(output == expected); -} +// BOOST_REQUIRE(output == expected); +// } -/** - * Test for the SplitByAnyOf tokenizer. - */ -BOOST_AUTO_TEST_CASE(SplitByAnyOfTokenizerTest) -{ - std::vector tokens; - boost::string_view line(stringEncodingInput[0]); - SplitByAnyOf tokenizer(" ,."); - boost::string_view token = tokenizer(line); +// /** +// * Test for the SplitByAnyOf tokenizer. +// */ +// BOOST_AUTO_TEST_CASE(SplitByAnyOfTokenizerTest) +// { +// std::vector tokens; +// boost::string_view line(stringEncodingInput[0]); +// SplitByAnyOf tokenizer(" ,."); +// boost::string_view token = tokenizer(line); - while (!token.empty()) - { - tokens.push_back(token); - token = tokenizer(line); - } +// while (!token.empty()) +// { +// tokens.push_back(token); +// token = tokenizer(line); +// } - vector expected = { "mlpack", "is", "an", "intuitive", "fast", - "and", "flexible", "C++", "machine", "learning", "library", "with", - "bindings", "to", "other", "languages" - }; +// vector expected = { "mlpack", "is", "an", "intuitive", "fast", +// "and", "flexible", "C++", "machine", "learning", "library", "with", +// "bindings", "to", "other", "languages" +// }; - BOOST_REQUIRE_EQUAL(tokens.size(), expected.size()); +// BOOST_REQUIRE_EQUAL(tokens.size(), expected.size()); - for (size_t i = 0; i < tokens.size(); i++) - BOOST_REQUIRE_EQUAL(tokens[i], expected[i]); -} +// for (size_t i = 0; i < tokens.size(); i++) +// BOOST_REQUIRE_EQUAL(tokens[i], expected[i]); +// } -/** -* Test Dictionary encoding for characters using lamda function. -*/ -BOOST_AUTO_TEST_CASE(DictionaryEncodingIndividualCharactersTest) -{ - vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; +// /** +// * Test Dictionary encoding for characters using lamda function. +// */ +// BOOST_AUTO_TEST_CASE(DictionaryEncodingIndividualCharactersTest) +// { +// vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; - arma::mat output; - DictionaryEncoding encoder; +// arma::mat output; +// DictionaryEncoding encoder; - // Passing a empty string to encode characters - encoder.Encode(input, output, CharExtract()); +// // Passing a empty string to encode characters +// encoder.Encode(input, output, CharExtract()); - arma::mat target = { - { 1, 2, 3, 3, 2, 0, 0 }, - { 2, 4, 3, 2, 4, 3, 5 }, - { 1, 2, 4, 0, 0, 0, 0 } - }; - CheckMatrices(output, target); -} +// arma::mat target = { +// { 1, 2, 3, 3, 2, 0, 0 }, +// { 2, 4, 3, 2, 4, 3, 5 }, +// { 1, 2, 4, 0, 0, 0, 0 } +// }; +// CheckMatrices(output, target); +// } -/** - * Test the one pass modification of the dictionary encoding algorithm - * in case of individual character encoding. - */ -BOOST_AUTO_TEST_CASE(OnePassDictionaryEncodingIndividualCharactersTest) -{ - std::vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; +// /** +// * Test the one pass modification of the dictionary encoding algorithm +// * in case of individual character encoding. +// */ +// BOOST_AUTO_TEST_CASE(OnePassDictionaryEncodingIndividualCharactersTest) +// { +// std::vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; - vector> output; - DictionaryEncoding encoder; +// vector> output; +// DictionaryEncoding encoder; - // Passing a empty string to encode characters - encoder.Encode(input, output, CharExtract()); +// // Passing a empty string to encode characters +// encoder.Encode(input, output, CharExtract()); - vector> expected = { - { 1, 2, 3, 3, 2 }, - { 2, 4, 3, 2, 4, 3, 5 }, - { 1, 2, 4 } - }; +// vector> expected = { +// { 1, 2, 3, 3, 2 }, +// { 2, 4, 3, 2, 4, 3, 5 }, +// { 1, 2, 4 } +// }; - BOOST_REQUIRE(output == expected); -} +// BOOST_REQUIRE(output == expected); +// } -/** - * Test the functionality of copy constructor. - */ -BOOST_AUTO_TEST_CASE(StringEncodingCopyTest) -{ - using DictionaryType = StringEncodingDictionary; - arma::sp_mat output; - DictionaryEncoding encoderCopy; - SplitByAnyOf tokenizer(" ,."); +// /** +// * Test the functionality of copy constructor. +// */ +// BOOST_AUTO_TEST_CASE(StringEncodingCopyTest) +// { +// using DictionaryType = StringEncodingDictionary; +// arma::sp_mat output; +// DictionaryEncoding encoderCopy; +// SplitByAnyOf tokenizer(" ,."); - vector> naiveDictionary; +// vector> naiveDictionary; - { - DictionaryEncoding encoder; - encoder.Encode(stringEncodingInput, output, tokenizer); +// { +// DictionaryEncoding encoder; +// encoder.Encode(stringEncodingInput, output, tokenizer); - for (const string& token : encoder.Dictionary().Tokens()) - { - naiveDictionary.emplace_back(token, encoder.Dictionary().Value(token)); - } +// for (const string& token : encoder.Dictionary().Tokens()) +// { +// naiveDictionary.emplace_back(token, encoder.Dictionary().Value(token)); +// } - encoderCopy = DictionaryEncoding(encoder); - } +// encoderCopy = DictionaryEncoding(encoder); +// } - const DictionaryType& copiedDictionary = encoderCopy.Dictionary(); +// const DictionaryType& copiedDictionary = encoderCopy.Dictionary(); - BOOST_REQUIRE_EQUAL(naiveDictionary.size(), copiedDictionary.Size()); +// BOOST_REQUIRE_EQUAL(naiveDictionary.size(), copiedDictionary.Size()); - for (const pair& keyValue : naiveDictionary) - { - BOOST_REQUIRE(copiedDictionary.HasToken(keyValue.first)); - BOOST_REQUIRE_EQUAL(copiedDictionary.Value(keyValue.first), - keyValue.second); - } -} +// for (const pair& keyValue : naiveDictionary) +// { +// BOOST_REQUIRE(copiedDictionary.HasToken(keyValue.first)); +// BOOST_REQUIRE_EQUAL(copiedDictionary.Value(keyValue.first), +// keyValue.second); +// } +// } -/** - * Test the move assignment operator. - */ -BOOST_AUTO_TEST_CASE(StringEncodingMoveTest) -{ - using DictionaryType = StringEncodingDictionary; - arma::sp_mat output; - DictionaryEncoding encoderCopy; - SplitByAnyOf tokenizer(" ,."); +// /** +// * Test the move assignment operator. +// */ +// BOOST_AUTO_TEST_CASE(StringEncodingMoveTest) +// { +// using DictionaryType = StringEncodingDictionary; +// arma::sp_mat output; +// DictionaryEncoding encoderCopy; +// SplitByAnyOf tokenizer(" ,."); - vector> naiveDictionary; +// vector> naiveDictionary; - { - DictionaryEncoding encoder; - encoder.Encode(stringEncodingInput, output, tokenizer); +// { +// DictionaryEncoding encoder; +// encoder.Encode(stringEncodingInput, output, tokenizer); - for (const string& token : encoder.Dictionary().Tokens()) - { - naiveDictionary.emplace_back(token, encoder.Dictionary().Value(token)); - } +// for (const string& token : encoder.Dictionary().Tokens()) +// { +// naiveDictionary.emplace_back(token, encoder.Dictionary().Value(token)); +// } - encoderCopy = std::move(encoder); - } +// encoderCopy = std::move(encoder); +// } - const DictionaryType& copiedDictionary = encoderCopy.Dictionary(); +// const DictionaryType& copiedDictionary = encoderCopy.Dictionary(); - BOOST_REQUIRE_EQUAL(naiveDictionary.size(), copiedDictionary.Size()); +// BOOST_REQUIRE_EQUAL(naiveDictionary.size(), copiedDictionary.Size()); - for (const pair& keyValue : naiveDictionary) - { - BOOST_REQUIRE(copiedDictionary.HasToken(keyValue.first)); - BOOST_REQUIRE_EQUAL(copiedDictionary.Value(keyValue.first), - keyValue.second); - } -} +// for (const pair& keyValue : naiveDictionary) +// { +// BOOST_REQUIRE(copiedDictionary.HasToken(keyValue.first)); +// BOOST_REQUIRE_EQUAL(copiedDictionary.Value(keyValue.first), +// keyValue.second); +// } +// } /** * The function checks that the given dictionaries contain the same data. @@ -342,35 +342,35 @@ void CheckDictionaries(const StringEncodingDictionary& expected, } } -/** - * Serialization test for the dictionary encoding algorithm with - * the SplitByAnyOf tokenizer. - */ -BOOST_AUTO_TEST_CASE(SplitByAnyOfDictionaryEncodingSerialization) -{ - using EncoderType = DictionaryEncoding; +// /** +// * Serialization test for the dictionary encoding algorithm with +// * the SplitByAnyOf tokenizer. +// */ +// BOOST_AUTO_TEST_CASE(SplitByAnyOfDictionaryEncodingSerialization) +// { +// using EncoderType = DictionaryEncoding; - EncoderType encoder; - SplitByAnyOf tokenizer(" ,."); - arma::mat output; +// EncoderType encoder; +// SplitByAnyOf tokenizer(" ,."); +// arma::mat output; - encoder.Encode(stringEncodingInput, output, tokenizer); +// encoder.Encode(stringEncodingInput, output, tokenizer); - EncoderType xmlEncoder, textEncoder, binaryEncoder; - arma::mat xmlOutput, textOutput, binaryOutput; +// EncoderType xmlEncoder, textEncoder, binaryEncoder; +// arma::mat xmlOutput, textOutput, binaryOutput; - SerializeObjectAll(encoder, xmlEncoder, textEncoder, binaryEncoder); +// SerializeObjectAll(encoder, xmlEncoder, textEncoder, binaryEncoder); - CheckDictionaries(encoder.Dictionary(), xmlEncoder.Dictionary()); - CheckDictionaries(encoder.Dictionary(), textEncoder.Dictionary()); - CheckDictionaries(encoder.Dictionary(), binaryEncoder.Dictionary()); +// CheckDictionaries(encoder.Dictionary(), xmlEncoder.Dictionary()); +// CheckDictionaries(encoder.Dictionary(), textEncoder.Dictionary()); +// CheckDictionaries(encoder.Dictionary(), binaryEncoder.Dictionary()); - xmlEncoder.Encode(stringEncodingInput, xmlOutput, tokenizer); - textEncoder.Encode(stringEncodingInput, textOutput, tokenizer); - binaryEncoder.Encode(stringEncodingInput, binaryOutput, tokenizer); +// xmlEncoder.Encode(stringEncodingInput, xmlOutput, tokenizer); +// textEncoder.Encode(stringEncodingInput, textOutput, tokenizer); +// binaryEncoder.Encode(stringEncodingInput, binaryOutput, tokenizer); - CheckMatrices(output, xmlOutput, textOutput, binaryOutput); -} +// CheckMatrices(output, xmlOutput, textOutput, binaryOutput); +// } /** * Serialization test for the dictionary encoding algorithm with @@ -378,13 +378,17 @@ BOOST_AUTO_TEST_CASE(SplitByAnyOfDictionaryEncodingSerialization) */ BOOST_AUTO_TEST_CASE(CharExtractDictionaryEncodingSerialization) { - using EncoderType = DictionaryEncoding; + using EncoderType = BagOfWordsEncoding; EncoderType encoder; CharExtract tokenizer; arma::mat output; - - encoder.Encode(stringEncodingInput, output, tokenizer); + std::vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; + encoder.Encode(input, output, tokenizer); EncoderType xmlEncoder, textEncoder, binaryEncoder; arma::mat xmlOutput, textOutput, binaryOutput; @@ -394,613 +398,649 @@ BOOST_AUTO_TEST_CASE(CharExtractDictionaryEncodingSerialization) CheckDictionaries(encoder.Dictionary(), xmlEncoder.Dictionary()); CheckDictionaries(encoder.Dictionary(), textEncoder.Dictionary()); CheckDictionaries(encoder.Dictionary(), binaryEncoder.Dictionary()); - - xmlEncoder.Encode(stringEncodingInput, xmlOutput, tokenizer); - textEncoder.Encode(stringEncodingInput, textOutput, tokenizer); - binaryEncoder.Encode(stringEncodingInput, binaryOutput, tokenizer); + std::cout<<"Calling the actual object"<; +// BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) +// { +// using DictionaryType = StringEncodingDictionary; - arma::mat output; - BagOfWordsEncoding encoder; - SplitByAnyOf tokenizer(" "); +// arma::mat output; +// BagOfWordsEncoding encoder; +// SplitByAnyOf tokenizer(" "); - encoder.Encode(stringEncodingInput, output, tokenizer); - const DictionaryType& dictionary = encoder.Dictionary(); +// encoder.Encode(stringEncodingInput, output, tokenizer); +// const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers - std::unordered_map keysCount; - for (auto& keyValue : dictionary.Mapping()) - { - keysCount[keyValue.second]++; - // Every token should be mapped only once - BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); - } - arma::mat expected = { - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } - }; - CheckMatrices(output, expected); -} +// // Checking that everything is mapped to different numbers +// std::unordered_map keysCount; +// for (auto& keyValue : dictionary.Mapping()) +// { +// keysCount[keyValue.second]++; +// // Every token should be mapped only once +// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); +// } +// arma::mat expected = { +// { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, +// { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, +// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, +// { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } +// }; +// CheckMatrices(output, expected); +// } + +// * +// * Test the one pass modification of the Bag of Words encoding algorithm. + +// BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingTest) +// { +// using DictionaryType = StringEncodingDictionary; + +// vector> output; +// BagOfWordsEncoding encoder( +// (BagOfWordsEncodingPolicy())); +// SplitByAnyOf tokenizer(" "); + +// encoder.Encode(stringEncodingInput, output, tokenizer); + +// const DictionaryType& dictionary = encoder.Dictionary(); + +// // Checking that everything is mapped to different numbers +// std::unordered_map keysCount; +// for (auto& keyValue : dictionary.Mapping()) +// { +// keysCount[keyValue.second]++; +// // Every token should be mapped only once +// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); +// } + +// vector> expected = { +// { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, +// { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, +// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, +// { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } +// }; + +// BOOST_REQUIRE(output == expected); +// } + +// /** +// * Test Bag of Words encoding for characters using lamda function. +// */ +// BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) +// { +// vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// arma::mat output; +// BagOfWordsEncoding encoder; + +// // Passing a empty string to encode characters +// encoder.Encode(input, output, CharExtract()); +// arma::mat target = { +// { 1, 1, 1, 0, 0 }, +// { 0, 1, 1, 1, 1 }, +// { 1, 1, 0, 1, 0 } +// }; + +// CheckMatrices(output, target); +// } + +// /** +// * Test the one pass modification of the Bag of Words encoding algorithm +// * in case of individual character encoding. +// */ +// BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingIndividualCharactersTest) +// { +// std::vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// vector> output; +// BagOfWordsEncoding encoder; + +// // Passing a empty string to encode characters +// encoder.Encode(input, output, CharExtract()); + +// vector> expected = { +// { 1, 1, 1, 0, 0 }, +// { 0, 1, 1, 1, 1 }, +// { 1, 1, 0, 1, 0 } +// }; + +// BOOST_REQUIRE(output == expected); +// } + +// /** +// * Test the Tf-Idf Encoding using rawcount type and smoothidf as true, +// * which is the deafult values used for algorithim. +// */ +// BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) +// { +// using DictionaryType = StringEncodingDictionary; + +// arma::mat output; +// TfIdfEncoding encoder; +// SplitByAnyOf tokenizer(" "); + +// encoder.Encode(stringEncodingInput, output, tokenizer); + +// const DictionaryType& dictionary = encoder.Dictionary(); + +// // Checking that everything is mapped to different numbers +// std::unordered_map keysCount; +// for (auto& keyValue : dictionary.Mapping()) +// { +// keysCount[keyValue.second]++; +// // Every token should be mapped only once +// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); +// } +// arma::mat expected = { +// { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, +// 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +// 0 }, +// { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, +// 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, +// { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, 0, 1, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1.69314718055995, 1.69314718055995 } +// }; +// CheckMatrices(output, expected, 1e-12); +// } + +// /** +// * Test the one pass modification of the TfIdf encoding algorithm, using rawcount +// * as type of tf and smoothIdf as true. +// */ +// BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingTest) +// { +// using DictionaryType = StringEncodingDictionary; + +// vector> output; +// TfIdfEncoding encoder( +// (TfIdfEncodingPolicy())); +// SplitByAnyOf tokenizer(" "); + +// encoder.Encode(stringEncodingInput, output, tokenizer); + +// const DictionaryType& dictionary = encoder.Dictionary(); + +// // Checking that everything is mapped to different numbers +// std::unordered_map keysCount; +// for (auto& keyValue : dictionary.Mapping()) +// { +// keysCount[keyValue.second]++; +// // Every token should be mapped only once +// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); +// } + +// vector> expected = { +// { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, +// 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +// 0 }, +// { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, +// 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, +// { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, 0, 1, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, +// 1.69314718055995, 1.69314718055995, 1.69314718055995 } +// }; +// for (size_t i = 0; i < expected.size(); i++) +// for (size_t j = 0; j < expected[i].size(); j++) +// BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); +// } + +// /** +// * Test TFIDF encoding for characters using lamda function, using rawcount as tf +// * type and smoothidf as true. +// */ +// BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) +// { +// vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// arma::mat output; +// TfIdfEncoding encoder; + +// // Passing a empty string to encode charactersrawcountsmoothidftrue +// encoder.Encode(input, output, CharExtract()); +// arma::mat target = { +// { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, +// { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, +// { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } +// }; +// CheckMatrices(output, target, 1e-12); +// } + +// /** +// * Test the one pass modification of the Tf-Idf encoding algorithm +// * in case of individual character encoding using default values. +// */ +// BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingIndividualCharactersTest) +// { +// std::vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// vector> output; +// TfIdfEncoding encoder; + +// // Passing a empty string to encode characters +// encoder.Encode(input, output, CharExtract()); +// vector> expected = { +// { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, +// { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, +// { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } +// }; +// for (size_t i = 0; i < expected.size(); i++) +// for (size_t j = 0; j < expected[i].size(); j++) +// BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); +// } + +// /** +// * Test the Tf-Idf Encoding using rawcount type and smoothidf as false. +// */ +// BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) +// { +// using DictionaryType = StringEncodingDictionary; + +// arma::mat output; +// TfIdfEncoding encoder( +// (TfIdfEncodingPolicy(0, false))); +// SplitByAnyOf tokenizer(" "); + +// encoder.Encode(stringEncodingInput, output, tokenizer); + +// const DictionaryType& dictionary = encoder.Dictionary(); + +// // Checking that everything is mapped to different numbers +// std::unordered_map keysCount; +// for (auto& keyValue : dictionary.Mapping()) +// { +// keysCount[keyValue.second]++; +// // Every token should be mapped only once +// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); +// } + +// arma::mat expected = { +// { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, +// 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +// 0 }, +// { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, +// 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, +// { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, 0, 1, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 2.09861228866811, 2.09861228866811 } +// }; +// CheckMatrices(output, expected, 1e-12); +// } + +// * +// * Test the one pass modification of the TfIdf encoding algorithm, with rawcount +// * as type, but with smoothidf as false. + +// BOOST_AUTO_TEST_CASE(OnePassTfIdfRawCountEncodingTest) +// { +// using DictionaryType = StringEncodingDictionary; + +// vector> output; +// TfIdfEncoding encoder(0, false); +// SplitByAnyOf tokenizer(" "); + +// encoder.Encode(stringEncodingInput, output, tokenizer); + +// const DictionaryType& dictionary = encoder.Dictionary(); + +// // Checking that everything is mapped to different numbers +// std::unordered_map keysCount; +// for (auto& keyValue : dictionary.Mapping()) +// { +// keysCount[keyValue.second]++; +// // Every token should be mapped only once +// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); +// } + +// vector> expected = { +// { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, +// 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +// 0 }, +// { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, +// 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, +// { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, 0, 1, +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, +// 2.09861228866811, 2.09861228866811, 2.09861228866811 } +// }; +// for (size_t i = 0; i < expected.size(); i++) +// for (size_t j = 0; j < expected[i].size(); j++) +// BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); +// } + +// /** +// * Test TFIDF encoding for characters using lamda function, using rawcount as +// * tf type and smoothidf as false. +// */ +// BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) +// { +// vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// arma::mat output; +// TfIdfEncoding encoder(0, false); + +// // Passing a empty string to encode charactersrawcountsmoothidftrue +// encoder.Encode(input, output, CharExtract()); +// arma::mat target = { +// { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, +// { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, +// { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } +// }; +// CheckMatrices(output, target, 1e-12); +// } + +// /** +// * Test the one pass modification of the Tf Idf encoding algorithm +// * in case of individual character encoding, using raw count as type, +// * and smoothidf as false. +// */ +// BOOST_AUTO_TEST_CASE(OnePassRawcountEncodingIndividualCharactersTest) +// { +// std::vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// vector> output; +// TfIdfEncoding encoder(0, false); + +// // Passing a empty string to encode characters +// encoder.Encode(input, output, CharExtract()); +// vector> expected = { +// { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, +// { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, +// { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } +// }; +// for (size_t i = 0; i < expected.size(); i++) +// for (size_t j = 0; j < expected[i].size(); j++) +// BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); +// } + +// /** +// * Test TFIDF encoding for characters using lamda function, using binary +// * weighting scheme for tf and smoothidf as true. +// */ +// BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) +// { +// vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// arma::mat output; +// TfIdfEncoding encoder(1, true); + +// // Passing a empty string to encode charactersrawcountsmoothidftrue +// encoder.Encode(input, output, CharExtract()); +// arma::mat target = { +// { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, +// { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, +// { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } +// }; +// CheckMatrices(output, target, 1e-12); +// } + +// /** +// * Test TFIDF encoding for characters using lamda function, using binary +// * weighting scheme for tf and smoothidf as true. +// */ +// BOOST_AUTO_TEST_CASE(OnePassBnarySmoothIdfEncodingIndividualCharactersTest) +// { +// std::vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// vector> output; +// TfIdfEncoding encoder(1, true); + +// // Passing a empty string to encode characters +// encoder.Encode(input, output, CharExtract()); +// vector> expected = { +// { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, +// { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, +// { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } +// }; +// for (size_t i = 0; i < expected.size(); i++) +// for (size_t j = 0; j < expected[i].size(); j++) +// BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); +// } + +// /** +// * Test TFIDF encoding for characters using lamda function, using binary +// * as weighting scheme and smoothidf as false. +// */ +// BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) +// { +// vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// arma::mat output; +// TfIdfEncoding encoder(1, false); + +// // Passing a empty string to encode charactersrawcountsmoothidftrue +// encoder.Encode(input, output, CharExtract()); +// arma::mat target = { +// { 1.4054651081081644, 1, 1.4054651081081644, 0, 0 }, +// { 0, 1, 1.4054651081081644, 1.4054651081081644, 2.0986122886681100 }, +// { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } +// }; +// CheckMatrices(output, target, 1e-12); +// } + +// /** +// * Test TFIDF encoding for characters using lamda function, using sublinear +// * as weighting scheme and smoothidf as true. +// */ +// BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) +// { +// vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// arma::mat output; +// TfIdfEncoding encoder(2, true); + +// // Passing a empty string to encode charactersrawcountsmoothidftrue +// encoder.Encode(input, output, CharExtract()); +// arma::mat target = { +// { 1.2876820724517808, 1.6931471805599454, 2.1802352704293200, 0, 0 }, +// { 0, 1.6931471805599454, 2.1802352704293200, 2.1802352704293200, +// 1.6931471805599454 }, +// { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } +// }; +// CheckMatrices(output, target, 1e-12); +// } + +// /** +// * Test TFIDF encoding for characters using lamda function, using sublinear +// * as weighting scheme and smoothidf as false. +// */ +// BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) +// { +// vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// arma::mat output; +// TfIdfEncoding encoder(2, false); + +// // Passing a empty string to encode charactersrawcountsmoothidftrue +// encoder.Encode(input, output, CharExtract()); +// arma::mat target = { +// { 1.4054651081081644, 1.6931471805599454, 2.3796592851687173, 0, 0 }, +// { 0, 1.6931471805599454, 2.3796592851687173, 2.3796592851687173, +// 2.0986122886681100 }, +// { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } +// }; +// CheckMatrices(output, target, 1e-12); +// } + +// /** +// * Test TFIDF encoding for characters using lamda function, using term +// * Frequency as weighting scheme and smoothidf as true. +// */ +// BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) +// { +// vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// arma::mat output; +// TfIdfEncoding encoder(3, true); + +// // Passing a empty string to encode charactersrawcountsmoothidftrue +// encoder.Encode(input, output, CharExtract()); +// arma::mat target = { +// { 0.2575364144903562, 0.4, 0.5150728289807124, 0, 0 }, +// { 0, 0.2857142857142857, 0.3679091635576516, 0.3679091635576516, +// 0.2418781686514208 }, +// { 0.4292273574839269, 0.3333333333333333, 0, 0.4292273574839269, 0 } +// }; +// CheckMatrices(output, target, 1e-12); +// } + +// /** +// * Test TFIDF encoding for characters using lamda function, using Term +// * Frequency as weighting scheme and smoothidf as false. +// */ +// BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) +// { +// vector input = { +// "GACCA", +// "ABCABCD", +// "GAB" +// }; + +// arma::mat output; +// TfIdfEncoding encoder(3, false); + +// // Passing a empty string to encode charactersrawcountsmoothidftrue +// encoder.Encode(input, output, CharExtract()); +// arma::mat target = { +// { 0.2810930216216329, 0.4, 0.5621860432432658, 0, 0 }, +// { 0, 0.2857142857142857, 0.4015614594594755, 0.4015614594594755, +// 0.2998017555240157 }, +// { 0.4684883693693881, 0.3333333333333333, 0, 0.4684883693693881, 0 } +// }; +// CheckMatrices(output, target, 1e-12); +// } /** - * Test the one pass modification of the Bag of Words encoding algorithm. + * Serialization test for the dictionary encoding algorithm with + * the CharExtract tokenizer. */ -BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingTest) +BOOST_AUTO_TEST_CASE(CharExtractBagOfWordsEncodingSerialization) { - using DictionaryType = StringEncodingDictionary; + using EncoderType = TfIdfEncoding; - vector> output; - BagOfWordsEncoding encoder( - (BagOfWordsEncodingPolicy())); - SplitByAnyOf tokenizer(" "); - - encoder.Encode(stringEncodingInput, output, tokenizer); - - const DictionaryType& dictionary = encoder.Dictionary(); - - // Checking that everything is mapped to different numbers - std::unordered_map keysCount; - for (auto& keyValue : dictionary.Mapping()) - { - keysCount[keyValue.second]++; - // Every token should be mapped only once - BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); - } - - vector> expected = { - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } - }; - - BOOST_REQUIRE(output == expected); -} - -/** -* Test Bag of Words encoding for characters using lamda function. -*/ -BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) -{ + EncoderType encoder; + CharExtract tokenizer; + arma::mat output; vector input = { "GACCA", "ABCABCD", "GAB" }; + encoder.Encode(input, output, tokenizer); - arma::mat output; - BagOfWordsEncoding encoder; + EncoderType xmlEncoder, textEncoder, binaryEncoder; + arma::mat xmlOutput, textOutput, binaryOutput; - // Passing a empty string to encode characters - encoder.Encode(input, output, CharExtract()); - arma::mat target = { - { 1, 1, 1, 0, 0 }, - { 0, 1, 1, 1, 1 }, - { 1, 1, 0, 1, 0 } - }; + SerializeObjectAll(encoder, xmlEncoder, textEncoder, binaryEncoder); + std::cout<<"HHHHHHHHHHHHHH\n"; + CheckDictionaries(encoder.Dictionary(), xmlEncoder.Dictionary()); + CheckDictionaries(encoder.Dictionary(), textEncoder.Dictionary()); + CheckDictionaries(encoder.Dictionary(), binaryEncoder.Dictionary()); + std::cout<<"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFSDDDDDDDDDDDDDDDD\n"; + xmlEncoder.Encode(input, xmlOutput, tokenizer); + std::cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"; - CheckMatrices(output, target); -} + textEncoder.Encode(input, textOutput, tokenizer); + binaryEncoder.Encode(input, binaryOutput, tokenizer); -/** - * Test the one pass modification of the Bag of Words encoding algorithm - * in case of individual character encoding. - */ -BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingIndividualCharactersTest) -{ - std::vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - - vector> output; - BagOfWordsEncoding encoder; - - // Passing a empty string to encode characters - encoder.Encode(input, output, CharExtract()); - - vector> expected = { - { 1, 1, 1, 0, 0 }, - { 0, 1, 1, 1, 1 }, - { 1, 1, 0, 1, 0 } - }; - - BOOST_REQUIRE(output == expected); -} - -/** - * Test the Tf-Idf Encoding using rawcount type and smoothidf as true, - * which is the deafult values used for algorithim. - */ -BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) -{ - using DictionaryType = StringEncodingDictionary; - - arma::mat output; - TfIdfEncoding encoder; - SplitByAnyOf tokenizer(" "); - - encoder.Encode(stringEncodingInput, output, tokenizer); - - const DictionaryType& dictionary = encoder.Dictionary(); - - // Checking that everything is mapped to different numbers - std::unordered_map keysCount; - for (auto& keyValue : dictionary.Mapping()) - { - keysCount[keyValue.second]++; - // Every token should be mapped only once - BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); - } - arma::mat expected = { - { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, - 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0 }, - { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, - 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, 0, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995 } - }; - CheckMatrices(output, expected, 1e-12); -} - -/** - * Test the one pass modification of the TfIdf encoding algorithm, using rawcount - * as type of tf and smoothIdf as true. - */ -BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingTest) -{ - using DictionaryType = StringEncodingDictionary; - - vector> output; - TfIdfEncoding encoder( - (TfIdfEncodingPolicy())); - SplitByAnyOf tokenizer(" "); - - encoder.Encode(stringEncodingInput, output, tokenizer); - - const DictionaryType& dictionary = encoder.Dictionary(); - - // Checking that everything is mapped to different numbers - std::unordered_map keysCount; - for (auto& keyValue : dictionary.Mapping()) - { - keysCount[keyValue.second]++; - // Every token should be mapped only once - BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); - } - - vector> expected = { - { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, - 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0 }, - { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, - 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, 0, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995 } - }; - for (size_t i = 0; i < expected.size(); i++) - for (size_t j = 0; j < expected[i].size(); j++) - BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); -} - -/** - * Test TFIDF encoding for characters using lamda function, using rawcount as tf - * type and smoothidf as true. - */ -BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) -{ - vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - - arma::mat output; - TfIdfEncoding encoder; - - // Passing a empty string to encode charactersrawcountsmoothidftrue - encoder.Encode(input, output, CharExtract()); - arma::mat target = { - { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, - { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, - { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } - }; - CheckMatrices(output, target, 1e-12); -} - -/** - * Test the one pass modification of the Tf-Idf encoding algorithm - * in case of individual character encoding using default values. - */ -BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingIndividualCharactersTest) -{ - std::vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - - vector> output; - TfIdfEncoding encoder; - - // Passing a empty string to encode characters - encoder.Encode(input, output, CharExtract()); - vector> expected = { - { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, - { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, - { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } - }; - for (size_t i = 0; i < expected.size(); i++) - for (size_t j = 0; j < expected[i].size(); j++) - BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); -} - -/** - * Test the Tf-Idf Encoding using rawcount type and smoothidf as false. - */ -BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) -{ - using DictionaryType = StringEncodingDictionary; - - arma::mat output; - TfIdfEncoding encoder( - (TfIdfEncodingPolicy(0, false))); - SplitByAnyOf tokenizer(" "); - - encoder.Encode(stringEncodingInput, output, tokenizer); - - const DictionaryType& dictionary = encoder.Dictionary(); - - // Checking that everything is mapped to different numbers - std::unordered_map keysCount; - for (auto& keyValue : dictionary.Mapping()) - { - keysCount[keyValue.second]++; - // Every token should be mapped only once - BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); - } - - arma::mat expected = { - { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, - 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0 }, - { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, - 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, 0, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811 } - }; - CheckMatrices(output, expected, 1e-12); -} - -/** - * Test the one pass modification of the TfIdf encoding algorithm, with rawcount - * as type, but with smoothidf as false. - */ -BOOST_AUTO_TEST_CASE(OnePassTfIdfRawCountEncodingTest) -{ - using DictionaryType = StringEncodingDictionary; - - vector> output; - TfIdfEncoding encoder(0, false); - SplitByAnyOf tokenizer(" "); - - encoder.Encode(stringEncodingInput, output, tokenizer); - - const DictionaryType& dictionary = encoder.Dictionary(); - - // Checking that everything is mapped to different numbers - std::unordered_map keysCount; - for (auto& keyValue : dictionary.Mapping()) - { - keysCount[keyValue.second]++; - // Every token should be mapped only once - BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); - } - - vector> expected = { - { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, - 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0 }, - { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, - 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, 0, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811 } - }; - for (size_t i = 0; i < expected.size(); i++) - for (size_t j = 0; j < expected[i].size(); j++) - BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); -} - -/** - * Test TFIDF encoding for characters using lamda function, using rawcount as - * tf type and smoothidf as false. - */ -BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) -{ - vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - - arma::mat output; - TfIdfEncoding encoder(0, false); - - // Passing a empty string to encode charactersrawcountsmoothidftrue - encoder.Encode(input, output, CharExtract()); - arma::mat target = { - { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, - { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, - { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } - }; - CheckMatrices(output, target, 1e-12); -} - -/** - * Test the one pass modification of the Tf Idf encoding algorithm - * in case of individual character encoding, using raw count as type, - * and smoothidf as false. - */ -BOOST_AUTO_TEST_CASE(OnePassRawcountEncodingIndividualCharactersTest) -{ - std::vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - - vector> output; - TfIdfEncoding encoder(0, false); - - // Passing a empty string to encode characters - encoder.Encode(input, output, CharExtract()); - vector> expected = { - { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, - { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, - { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } - }; - for (size_t i = 0; i < expected.size(); i++) - for (size_t j = 0; j < expected[i].size(); j++) - BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); -} - -/** - * Test TFIDF encoding for characters using lamda function, using binary - * weighting scheme for tf and smoothidf as true. - */ -BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) -{ - vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - - arma::mat output; - TfIdfEncoding encoder(1, true); - - // Passing a empty string to encode charactersrawcountsmoothidftrue - encoder.Encode(input, output, CharExtract()); - arma::mat target = { - { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, - { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, - { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } - }; - CheckMatrices(output, target, 1e-12); -} - -/** - * Test TFIDF encoding for characters using lamda function, using binary - * weighting scheme for tf and smoothidf as true. - */ -BOOST_AUTO_TEST_CASE(OnePassBnarySmoothIdfEncodingIndividualCharactersTest) -{ - std::vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - - vector> output; - TfIdfEncoding encoder(1, true); - - // Passing a empty string to encode characters - encoder.Encode(input, output, CharExtract()); - vector> expected = { - { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, - { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, - { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } - }; - for (size_t i = 0; i < expected.size(); i++) - for (size_t j = 0; j < expected[i].size(); j++) - BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); -} - -/** - * Test TFIDF encoding for characters using lamda function, using binary - * as weighting scheme and smoothidf as false. - */ -BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) -{ - vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - - arma::mat output; - TfIdfEncoding encoder(1, false); - - // Passing a empty string to encode charactersrawcountsmoothidftrue - encoder.Encode(input, output, CharExtract()); - arma::mat target = { - { 1.4054651081081644, 1, 1.4054651081081644, 0, 0 }, - { 0, 1, 1.4054651081081644, 1.4054651081081644, 2.0986122886681100 }, - { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } - }; - CheckMatrices(output, target, 1e-12); -} - -/** - * Test TFIDF encoding for characters using lamda function, using sublinear - * as weighting scheme and smoothidf as true. - */ -BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) -{ - vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - - arma::mat output; - TfIdfEncoding encoder(2, true); - - // Passing a empty string to encode charactersrawcountsmoothidftrue - encoder.Encode(input, output, CharExtract()); - arma::mat target = { - { 1.2876820724517808, 1.6931471805599454, 2.1802352704293200, 0, 0 }, - { 0, 1.6931471805599454, 2.1802352704293200, 2.1802352704293200, - 1.6931471805599454 }, - { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } - }; - CheckMatrices(output, target, 1e-12); -} - -/** - * Test TFIDF encoding for characters using lamda function, using sublinear - * as weighting scheme and smoothidf as false. - */ -BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) -{ - vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - - arma::mat output; - TfIdfEncoding encoder(2, false); - - // Passing a empty string to encode charactersrawcountsmoothidftrue - encoder.Encode(input, output, CharExtract()); - arma::mat target = { - { 1.4054651081081644, 1.6931471805599454, 2.3796592851687173, 0, 0 }, - { 0, 1.6931471805599454, 2.3796592851687173, 2.3796592851687173, - 2.0986122886681100 }, - { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } - }; - CheckMatrices(output, target, 1e-12); -} - -/** - * Test TFIDF encoding for characters using lamda function, using term - * Frequency as weighting scheme and smoothidf as true. - */ -BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) -{ - vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - - arma::mat output; - TfIdfEncoding encoder(3, true); - - // Passing a empty string to encode charactersrawcountsmoothidftrue - encoder.Encode(input, output, CharExtract()); - arma::mat target = { - { 0.2575364144903562, 0.4, 0.5150728289807124, 0, 0 }, - { 0, 0.2857142857142857, 0.3679091635576516, 0.3679091635576516, - 0.2418781686514208 }, - { 0.4292273574839269, 0.3333333333333333, 0, 0.4292273574839269, 0 } - }; - CheckMatrices(output, target, 1e-12); -} - -/** - * Test TFIDF encoding for characters using lamda function, using Term - * Frequency as weighting scheme and smoothidf as false. - */ -BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) -{ - vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - - arma::mat output; - TfIdfEncoding encoder(3, false); - - // Passing a empty string to encode charactersrawcountsmoothidftrue - encoder.Encode(input, output, CharExtract()); - arma::mat target = { - { 0.2810930216216329, 0.4, 0.5621860432432658, 0, 0 }, - { 0, 0.2857142857142857, 0.4015614594594755, 0.4015614594594755, - 0.2998017555240157 }, - { 0.4684883693693881, 0.3333333333333333, 0, 0.4684883693693881, 0 } - }; - CheckMatrices(output, target, 1e-12); + CheckMatrices(output, xmlOutput, textOutput, binaryOutput); } BOOST_AUTO_TEST_SUITE_END(); From b663dd83d7232508698cb85b9352a0b747c71605 Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Sat, 10 Aug 2019 12:18:04 +0530 Subject: [PATCH 016/265] resolved serialization issue with dictionary class --- .../core/data/string_encoding_dictionary.hpp | 1 + src/mlpack/core/data/string_encoding_impl.hpp | 1 - .../bag_of_words_encoding_policy.hpp | 1 - src/mlpack/tests/string_encoding_test.cpp | 639 +++++++++--------- 4 files changed, 318 insertions(+), 324 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_dictionary.hpp b/src/mlpack/core/data/string_encoding_dictionary.hpp index db5943f61a..2ccec8fb89 100644 --- a/src/mlpack/core/data/string_encoding_dictionary.hpp +++ b/src/mlpack/core/data/string_encoding_dictionary.hpp @@ -318,6 +318,7 @@ class StringEncodingDictionary void serialize(Archive& ar, const unsigned int /* version */) { ar & BOOST_SERIALIZATION_NVP(mapping); + ar & BOOST_SERIALIZATION_NVP(size); } private: diff --git a/src/mlpack/core/data/string_encoding_impl.hpp b/src/mlpack/core/data/string_encoding_impl.hpp index 058539dc79..11fd3644d1 100644 --- a/src/mlpack/core/data/string_encoding_impl.hpp +++ b/src/mlpack/core/data/string_encoding_impl.hpp @@ -134,7 +134,6 @@ EncodeHelper(const std::vector& input, } numColumns = std::max(numColumns, numTokens); } - std::cout<<"dictionary size is "< stringEncodingInput = { "command-line programs and Python bindings." }; -// /** -// * Test the dictionary encoding algorithm. -// */ -// BOOST_AUTO_TEST_CASE(DictionaryEncodingTest) -// { -// using DictionaryType = StringEncodingDictionary; +/** + * Test the dictionary encoding algorithm. + */ +BOOST_AUTO_TEST_CASE(DictionaryEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; -// arma::mat output; -// DictionaryEncoding encoder; -// SplitByAnyOf tokenizer(" .,\""); + arma::mat output; + DictionaryEncoding encoder; + SplitByAnyOf tokenizer(" .,\""); -// encoder.Encode(stringEncodingInput, output, tokenizer); + encoder.Encode(stringEncodingInput, output, tokenizer); -// const DictionaryType& dictionary = encoder.Dictionary(); + const DictionaryType& dictionary = encoder.Dictionary(); -// // Checking that everything is mapped to different numbers -// std::unordered_map keysCount; -// for (auto& keyValue : dictionary.Mapping()) -// { -// keysCount[keyValue.second]++; -// // Every token should be mapped only once -// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); -// } + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } -// arma::mat expected = { -// { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, -// { 17, 2, 18, 14, 19, 20, 9, 10, 21, 14, 22, 6, 23, 14, 24, 20, 25, -// 26, 27, 9, 10, 28, 6, 29, 30, 20, 31, 32, 33, 34, 9, 10, 35 }, -// { 36, 37, 14, 38, 39, 8, 40, 1, 41, 42, 43, 44, 6, 45, 13, 0, 0, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } -// }; + arma::mat expected = { + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 17, 2, 18, 14, 19, 20, 9, 10, 21, 14, 22, 6, 23, 14, 24, 20, 25, + 26, 27, 9, 10, 28, 6, 29, 30, 20, 31, 32, 33, 34, 9, 10, 35 }, + { 36, 37, 14, 38, 39, 8, 40, 1, 41, 42, 43, 44, 6, 45, 13, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } + }; -// CheckMatrices(output, expected); -// } + CheckMatrices(output, expected); +} -// /** -// * Test the one pass modification of the dictionary encoding algorithm. -// */ -// BOOST_AUTO_TEST_CASE(OnePassDictionaryEncodingTest) -// { -// using DictionaryType = StringEncodingDictionary; +/** + * Test the one pass modification of the dictionary encoding algorithm. + */ +BOOST_AUTO_TEST_CASE(OnePassDictionaryEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; -// vector> output; -// DictionaryEncoding encoder( -// (DictionaryEncodingPolicy())); -// SplitByAnyOf tokenizer(" .,\""); + vector> output; + DictionaryEncoding encoder( + (DictionaryEncodingPolicy())); + SplitByAnyOf tokenizer(" .,\""); -// encoder.Encode(stringEncodingInput, output, tokenizer); + encoder.Encode(stringEncodingInput, output, tokenizer); -// const DictionaryType& dictionary = encoder.Dictionary(); + const DictionaryType& dictionary = encoder.Dictionary(); -// // Checking that everything is mapped to different numbers -// std::unordered_map keysCount; -// for (auto& keyValue : dictionary.Mapping()) -// { -// keysCount[keyValue.second]++; -// // Every token should be mapped only once -// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); -// } + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } -// vector> expected = { -// { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, -// { 17, 2, 18, 14, 19, 20, 9, 10, 21, 14, 22, 6, 23, 14, 24, 20, 25, -// 26, 27, 9, 10, 28, 6, 29, 30, 20, 31, 32, 33, 34, 9, 10, 35 }, -// { 36, 37, 14, 38, 39, 8, 40, 1, 41, 42, 43, 44, 6, 45, 13 } -// }; + vector> expected = { + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, + { 17, 2, 18, 14, 19, 20, 9, 10, 21, 14, 22, 6, 23, 14, 24, 20, 25, + 26, 27, 9, 10, 28, 6, 29, 30, 20, 31, 32, 33, 34, 9, 10, 35 }, + { 36, 37, 14, 38, 39, 8, 40, 1, 41, 42, 43, 44, 6, 45, 13 } + }; -// BOOST_REQUIRE(output == expected); -// } + BOOST_REQUIRE(output == expected); +} -// /** -// * Test for the SplitByAnyOf tokenizer. -// */ -// BOOST_AUTO_TEST_CASE(SplitByAnyOfTokenizerTest) -// { -// std::vector tokens; -// boost::string_view line(stringEncodingInput[0]); -// SplitByAnyOf tokenizer(" ,."); -// boost::string_view token = tokenizer(line); +/** + * Test for the SplitByAnyOf tokenizer. + */ +BOOST_AUTO_TEST_CASE(SplitByAnyOfTokenizerTest) +{ + std::vector tokens; + boost::string_view line(stringEncodingInput[0]); + SplitByAnyOf tokenizer(" ,."); + boost::string_view token = tokenizer(line); -// while (!token.empty()) -// { -// tokens.push_back(token); -// token = tokenizer(line); -// } + while (!token.empty()) + { + tokens.push_back(token); + token = tokenizer(line); + } -// vector expected = { "mlpack", "is", "an", "intuitive", "fast", -// "and", "flexible", "C++", "machine", "learning", "library", "with", -// "bindings", "to", "other", "languages" -// }; + vector expected = { "mlpack", "is", "an", "intuitive", "fast", + "and", "flexible", "C++", "machine", "learning", "library", "with", + "bindings", "to", "other", "languages" + }; -// BOOST_REQUIRE_EQUAL(tokens.size(), expected.size()); + BOOST_REQUIRE_EQUAL(tokens.size(), expected.size()); -// for (size_t i = 0; i < tokens.size(); i++) -// BOOST_REQUIRE_EQUAL(tokens[i], expected[i]); -// } + for (size_t i = 0; i < tokens.size(); i++) + BOOST_REQUIRE_EQUAL(tokens[i], expected[i]); +} -// /** -// * Test Dictionary encoding for characters using lamda function. -// */ -// BOOST_AUTO_TEST_CASE(DictionaryEncodingIndividualCharactersTest) -// { -// vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** +* Test Dictionary encoding for characters using lamda function. +*/ +BOOST_AUTO_TEST_CASE(DictionaryEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// arma::mat output; -// DictionaryEncoding encoder; + arma::mat output; + DictionaryEncoding encoder; -// // Passing a empty string to encode characters -// encoder.Encode(input, output, CharExtract()); + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); -// arma::mat target = { -// { 1, 2, 3, 3, 2, 0, 0 }, -// { 2, 4, 3, 2, 4, 3, 5 }, -// { 1, 2, 4, 0, 0, 0, 0 } -// }; -// CheckMatrices(output, target); -// } + arma::mat target = { + { 1, 2, 3, 3, 2, 0, 0 }, + { 2, 4, 3, 2, 4, 3, 5 }, + { 1, 2, 4, 0, 0, 0, 0 } + }; + CheckMatrices(output, target); +} -// /** -// * Test the one pass modification of the dictionary encoding algorithm -// * in case of individual character encoding. -// */ -// BOOST_AUTO_TEST_CASE(OnePassDictionaryEncodingIndividualCharactersTest) -// { -// std::vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test the one pass modification of the dictionary encoding algorithm + * in case of individual character encoding. + */ +BOOST_AUTO_TEST_CASE(OnePassDictionaryEncodingIndividualCharactersTest) +{ + std::vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// vector> output; -// DictionaryEncoding encoder; + vector> output; + DictionaryEncoding encoder; -// // Passing a empty string to encode characters -// encoder.Encode(input, output, CharExtract()); + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); -// vector> expected = { -// { 1, 2, 3, 3, 2 }, -// { 2, 4, 3, 2, 4, 3, 5 }, -// { 1, 2, 4 } -// }; + vector> expected = { + { 1, 2, 3, 3, 2 }, + { 2, 4, 3, 2, 4, 3, 5 }, + { 1, 2, 4 } + }; -// BOOST_REQUIRE(output == expected); -// } + BOOST_REQUIRE(output == expected); +} -// /** -// * Test the functionality of copy constructor. -// */ -// BOOST_AUTO_TEST_CASE(StringEncodingCopyTest) -// { -// using DictionaryType = StringEncodingDictionary; -// arma::sp_mat output; -// DictionaryEncoding encoderCopy; -// SplitByAnyOf tokenizer(" ,."); +/** + * Test the functionality of copy constructor. + */ +BOOST_AUTO_TEST_CASE(StringEncodingCopyTest) +{ + using DictionaryType = StringEncodingDictionary; + arma::sp_mat output; + DictionaryEncoding encoderCopy; + SplitByAnyOf tokenizer(" ,."); -// vector> naiveDictionary; + vector> naiveDictionary; -// { -// DictionaryEncoding encoder; -// encoder.Encode(stringEncodingInput, output, tokenizer); + { + DictionaryEncoding encoder; + encoder.Encode(stringEncodingInput, output, tokenizer); -// for (const string& token : encoder.Dictionary().Tokens()) -// { -// naiveDictionary.emplace_back(token, encoder.Dictionary().Value(token)); -// } + for (const string& token : encoder.Dictionary().Tokens()) + { + naiveDictionary.emplace_back(token, encoder.Dictionary().Value(token)); + } -// encoderCopy = DictionaryEncoding(encoder); -// } + encoderCopy = DictionaryEncoding(encoder); + } -// const DictionaryType& copiedDictionary = encoderCopy.Dictionary(); + const DictionaryType& copiedDictionary = encoderCopy.Dictionary(); -// BOOST_REQUIRE_EQUAL(naiveDictionary.size(), copiedDictionary.Size()); + BOOST_REQUIRE_EQUAL(naiveDictionary.size(), copiedDictionary.Size()); -// for (const pair& keyValue : naiveDictionary) -// { -// BOOST_REQUIRE(copiedDictionary.HasToken(keyValue.first)); -// BOOST_REQUIRE_EQUAL(copiedDictionary.Value(keyValue.first), -// keyValue.second); -// } -// } + for (const pair& keyValue : naiveDictionary) + { + BOOST_REQUIRE(copiedDictionary.HasToken(keyValue.first)); + BOOST_REQUIRE_EQUAL(copiedDictionary.Value(keyValue.first), + keyValue.second); + } +} -// /** -// * Test the move assignment operator. -// */ -// BOOST_AUTO_TEST_CASE(StringEncodingMoveTest) -// { -// using DictionaryType = StringEncodingDictionary; -// arma::sp_mat output; -// DictionaryEncoding encoderCopy; -// SplitByAnyOf tokenizer(" ,."); +/** + * Test the move assignment operator. + */ +BOOST_AUTO_TEST_CASE(StringEncodingMoveTest) +{ + using DictionaryType = StringEncodingDictionary; + arma::sp_mat output; + DictionaryEncoding encoderCopy; + SplitByAnyOf tokenizer(" ,."); -// vector> naiveDictionary; + vector> naiveDictionary; -// { -// DictionaryEncoding encoder; -// encoder.Encode(stringEncodingInput, output, tokenizer); + { + DictionaryEncoding encoder; + encoder.Encode(stringEncodingInput, output, tokenizer); -// for (const string& token : encoder.Dictionary().Tokens()) -// { -// naiveDictionary.emplace_back(token, encoder.Dictionary().Value(token)); -// } + for (const string& token : encoder.Dictionary().Tokens()) + { + naiveDictionary.emplace_back(token, encoder.Dictionary().Value(token)); + } -// encoderCopy = std::move(encoder); -// } + encoderCopy = std::move(encoder); + } -// const DictionaryType& copiedDictionary = encoderCopy.Dictionary(); + const DictionaryType& copiedDictionary = encoderCopy.Dictionary(); -// BOOST_REQUIRE_EQUAL(naiveDictionary.size(), copiedDictionary.Size()); + BOOST_REQUIRE_EQUAL(naiveDictionary.size(), copiedDictionary.Size()); -// for (const pair& keyValue : naiveDictionary) -// { -// BOOST_REQUIRE(copiedDictionary.HasToken(keyValue.first)); -// BOOST_REQUIRE_EQUAL(copiedDictionary.Value(keyValue.first), -// keyValue.second); -// } -// } + for (const pair& keyValue : naiveDictionary) + { + BOOST_REQUIRE(copiedDictionary.HasToken(keyValue.first)); + BOOST_REQUIRE_EQUAL(copiedDictionary.Value(keyValue.first), + keyValue.second); + } +} /** * The function checks that the given dictionaries contain the same data. @@ -342,53 +342,19 @@ void CheckDictionaries(const StringEncodingDictionary& expected, } } -// /** -// * Serialization test for the dictionary encoding algorithm with -// * the SplitByAnyOf tokenizer. -// */ -// BOOST_AUTO_TEST_CASE(SplitByAnyOfDictionaryEncodingSerialization) -// { -// using EncoderType = DictionaryEncoding; - -// EncoderType encoder; -// SplitByAnyOf tokenizer(" ,."); -// arma::mat output; - -// encoder.Encode(stringEncodingInput, output, tokenizer); - -// EncoderType xmlEncoder, textEncoder, binaryEncoder; -// arma::mat xmlOutput, textOutput, binaryOutput; - -// SerializeObjectAll(encoder, xmlEncoder, textEncoder, binaryEncoder); - -// CheckDictionaries(encoder.Dictionary(), xmlEncoder.Dictionary()); -// CheckDictionaries(encoder.Dictionary(), textEncoder.Dictionary()); -// CheckDictionaries(encoder.Dictionary(), binaryEncoder.Dictionary()); - -// xmlEncoder.Encode(stringEncodingInput, xmlOutput, tokenizer); -// textEncoder.Encode(stringEncodingInput, textOutput, tokenizer); -// binaryEncoder.Encode(stringEncodingInput, binaryOutput, tokenizer); - -// CheckMatrices(output, xmlOutput, textOutput, binaryOutput); -// } - /** * Serialization test for the dictionary encoding algorithm with - * the CharExtract tokenizer. + * the SplitByAnyOf tokenizer. */ -BOOST_AUTO_TEST_CASE(CharExtractDictionaryEncodingSerialization) +BOOST_AUTO_TEST_CASE(SplitByAnyOfDictionaryEncodingSerialization) { - using EncoderType = BagOfWordsEncoding; + using EncoderType = DictionaryEncoding; EncoderType encoder; - CharExtract tokenizer; + SplitByAnyOf tokenizer(" ,."); arma::mat output; - std::vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - encoder.Encode(input, output, tokenizer); + + encoder.Encode(stringEncodingInput, output, tokenizer); EncoderType xmlEncoder, textEncoder, binaryEncoder; arma::mat xmlOutput, textOutput, binaryOutput; @@ -398,132 +364,161 @@ BOOST_AUTO_TEST_CASE(CharExtractDictionaryEncodingSerialization) CheckDictionaries(encoder.Dictionary(), xmlEncoder.Dictionary()); CheckDictionaries(encoder.Dictionary(), textEncoder.Dictionary()); CheckDictionaries(encoder.Dictionary(), binaryEncoder.Dictionary()); - std::cout<<"Calling the actual object"<; +/** + * Serialization test for the Bag Of Words encoding algorithm with + * the CharExtract tokenizer. + */ +BOOST_AUTO_TEST_CASE(CharExtractBagOfWordsEncodingSerialization) +{ + using EncoderType = BagOfWordsEncoding; -// arma::mat output; -// BagOfWordsEncoding encoder; -// SplitByAnyOf tokenizer(" "); + EncoderType encoder; + CharExtract tokenizer; + arma::mat output; + encoder.Encode(stringEncodingInput, output, tokenizer); -// encoder.Encode(stringEncodingInput, output, tokenizer); -// const DictionaryType& dictionary = encoder.Dictionary(); + EncoderType xmlEncoder, textEncoder, binaryEncoder; + arma::mat xmlOutput, textOutput, binaryOutput; -// // Checking that everything is mapped to different numbers -// std::unordered_map keysCount; -// for (auto& keyValue : dictionary.Mapping()) -// { -// keysCount[keyValue.second]++; -// // Every token should be mapped only once -// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); -// } -// arma::mat expected = { -// { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, -// { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, -// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, -// { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } -// }; -// CheckMatrices(output, expected); -// } + SerializeObjectAll(encoder, xmlEncoder, textEncoder, binaryEncoder); -// * -// * Test the one pass modification of the Bag of Words encoding algorithm. + CheckDictionaries(encoder.Dictionary(), xmlEncoder.Dictionary()); + CheckDictionaries(encoder.Dictionary(), textEncoder.Dictionary()); + CheckDictionaries(encoder.Dictionary(), binaryEncoder.Dictionary()); + + xmlEncoder.Encode(stringEncodingInput, xmlOutput, tokenizer); + textEncoder.Encode(stringEncodingInput, textOutput, tokenizer); + binaryEncoder.Encode(stringEncodingInput, binaryOutput, tokenizer); + + CheckMatrices(output, xmlOutput, textOutput, binaryOutput); +} + +BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; + + arma::mat output; + BagOfWordsEncoding encoder; + SplitByAnyOf tokenizer(" "); + + encoder.Encode(stringEncodingInput, output, tokenizer); + const DictionaryType& dictionary = encoder.Dictionary(); + + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } + arma::mat expected = { + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } + }; + CheckMatrices(output, expected); +} + +* + * Test the one pass modification of the Bag of Words encoding algorithm. -// BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingTest) -// { -// using DictionaryType = StringEncodingDictionary; +BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; -// vector> output; -// BagOfWordsEncoding encoder( -// (BagOfWordsEncodingPolicy())); -// SplitByAnyOf tokenizer(" "); + vector> output; + BagOfWordsEncoding encoder( + (BagOfWordsEncodingPolicy())); + SplitByAnyOf tokenizer(" "); -// encoder.Encode(stringEncodingInput, output, tokenizer); + encoder.Encode(stringEncodingInput, output, tokenizer); -// const DictionaryType& dictionary = encoder.Dictionary(); + const DictionaryType& dictionary = encoder.Dictionary(); -// // Checking that everything is mapped to different numbers -// std::unordered_map keysCount; -// for (auto& keyValue : dictionary.Mapping()) -// { -// keysCount[keyValue.second]++; -// // Every token should be mapped only once -// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); -// } + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } -// vector> expected = { -// { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, -// { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, -// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, -// { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } -// }; + vector> expected = { + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } + }; -// BOOST_REQUIRE(output == expected); -// } + BOOST_REQUIRE(output == expected); +} -// /** -// * Test Bag of Words encoding for characters using lamda function. -// */ -// BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) -// { -// vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** +* Test Bag of Words encoding for characters using lamda function. +*/ +BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// arma::mat output; -// BagOfWordsEncoding encoder; + arma::mat output; + BagOfWordsEncoding encoder; -// // Passing a empty string to encode characters -// encoder.Encode(input, output, CharExtract()); -// arma::mat target = { -// { 1, 1, 1, 0, 0 }, -// { 0, 1, 1, 1, 1 }, -// { 1, 1, 0, 1, 0 } -// }; + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1, 1, 1, 0, 0 }, + { 0, 1, 1, 1, 1 }, + { 1, 1, 0, 1, 0 } + }; -// CheckMatrices(output, target); -// } + CheckMatrices(output, target); +} -// /** -// * Test the one pass modification of the Bag of Words encoding algorithm -// * in case of individual character encoding. -// */ -// BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingIndividualCharactersTest) -// { -// std::vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test the one pass modification of the Bag of Words encoding algorithm + * in case of individual character encoding. + */ +BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingIndividualCharactersTest) +{ + std::vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// vector> output; -// BagOfWordsEncoding encoder; + vector> output; + BagOfWordsEncoding encoder; -// // Passing a empty string to encode characters -// encoder.Encode(input, output, CharExtract()); + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); -// vector> expected = { -// { 1, 1, 1, 0, 0 }, -// { 0, 1, 1, 1, 1 }, -// { 1, 1, 0, 1, 0 } -// }; + vector> expected = { + { 1, 1, 1, 0, 0 }, + { 0, 1, 1, 1, 1 }, + { 1, 1, 0, 1, 0 } + }; -// BOOST_REQUIRE(output == expected); -// } + BOOST_REQUIRE(output == expected); +} // /** // * Test the Tf-Idf Encoding using rawcount type and smoothidf as true, @@ -1008,10 +1003,10 @@ BOOST_AUTO_TEST_CASE(CharExtractDictionaryEncodingSerialization) // } /** - * Serialization test for the dictionary encoding algorithm with + * Serialization test for the TF-IDF encoding algorithm with * the CharExtract tokenizer. */ -BOOST_AUTO_TEST_CASE(CharExtractBagOfWordsEncodingSerialization) +BOOST_AUTO_TEST_CASE(CharExtractTfIdfEncodingSerialization) { using EncoderType = TfIdfEncoding; From f91a81e10731bd19c059fe4a93838490d7e4e374 Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Sat, 10 Aug 2019 12:23:07 +0530 Subject: [PATCH 017/265] uncommenting out test which is needed, next step to debug tfidf --- src/mlpack/tests/string_encoding_test.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 3bf3cbf68a..1e61d1f3f4 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -373,12 +373,12 @@ BOOST_AUTO_TEST_CASE(SplitByAnyOfDictionaryEncodingSerialization) } /** - * Serialization test for the Bag Of Words encoding algorithm with + * Serialization test for the dictionary encoding algorithm with * the CharExtract tokenizer. */ -BOOST_AUTO_TEST_CASE(CharExtractBagOfWordsEncodingSerialization) +BOOST_AUTO_TEST_CASE(CharExtractDictionaryEncodingSerialization) { - using EncoderType = BagOfWordsEncoding; + using EncoderType = DictionaryEncoding; EncoderType encoder; CharExtract tokenizer; @@ -431,9 +431,9 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) CheckMatrices(output, expected); } -* - * Test the one pass modification of the Bag of Words encoding algorithm. - +/** +* Test the one pass modification of the Bag of Words encoding algorithm. +*/ BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingTest) { using DictionaryType = StringEncodingDictionary; From bf72cc958125519ed4b503d3f089689cc5c29eaa Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Sat, 10 Aug 2019 12:44:38 +0530 Subject: [PATCH 018/265] Wrote serialization test, next to resolve comments --- .../tf_idf_encoding_policy.hpp | 11 +- src/mlpack/tests/string_encoding_test.cpp | 900 +++++++++--------- 2 files changed, 463 insertions(+), 448 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 42b16aa3b3..88bbdbff01 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -69,12 +69,11 @@ class TfIdfEncodingPolicy * @param dictionarySize The size of the dictionary (not used). */ template - static void InitMatrix(MatType& output, + void InitMatrix(MatType& output, size_t datasetSize, size_t /*maxNumTokens*/, size_t dictionarySize) { - std::cout<<"dataset "<= tokenCount.size()) { row_size.push_back(0); tokenCount.push_back(std::unordered_map()); } - std::cout<<"error"<; +/** + * Test the Tf-Idf Encoding using rawcount type and smoothidf as true, + * which is the deafult values used for algorithim. + */ +BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; -// arma::mat output; -// TfIdfEncoding encoder; -// SplitByAnyOf tokenizer(" "); + arma::mat output; + TfIdfEncoding encoder; + SplitByAnyOf tokenizer(" "); -// encoder.Encode(stringEncodingInput, output, tokenizer); + encoder.Encode(stringEncodingInput, output, tokenizer); -// const DictionaryType& dictionary = encoder.Dictionary(); + const DictionaryType& dictionary = encoder.Dictionary(); -// // Checking that everything is mapped to different numbers -// std::unordered_map keysCount; -// for (auto& keyValue : dictionary.Mapping()) -// { -// keysCount[keyValue.second]++; -// // Every token should be mapped only once -// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); -// } -// arma::mat expected = { -// { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, -// 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -// 0 }, -// { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, -// 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, -// { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, 0, 1, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1.69314718055995, 1.69314718055995 } -// }; -// CheckMatrices(output, expected, 1e-12); -// } + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } + arma::mat expected = { + { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, + 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, + 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995 } + }; + CheckMatrices(output, expected, 1e-12); +} -// /** -// * Test the one pass modification of the TfIdf encoding algorithm, using rawcount -// * as type of tf and smoothIdf as true. -// */ -// BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingTest) -// { -// using DictionaryType = StringEncodingDictionary; +/** + * Test the one pass modification of the TfIdf encoding algorithm, using rawcount + * as type of tf and smoothIdf as true. + */ +BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; -// vector> output; -// TfIdfEncoding encoder( -// (TfIdfEncodingPolicy())); -// SplitByAnyOf tokenizer(" "); + vector> output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy())); + SplitByAnyOf tokenizer(" "); -// encoder.Encode(stringEncodingInput, output, tokenizer); + encoder.Encode(stringEncodingInput, output, tokenizer); -// const DictionaryType& dictionary = encoder.Dictionary(); + const DictionaryType& dictionary = encoder.Dictionary(); -// // Checking that everything is mapped to different numbers -// std::unordered_map keysCount; -// for (auto& keyValue : dictionary.Mapping()) -// { -// keysCount[keyValue.second]++; -// // Every token should be mapped only once -// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); -// } + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } -// vector> expected = { -// { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, -// 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -// 0 }, -// { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, -// 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, -// { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, 0, 1, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, -// 1.69314718055995, 1.69314718055995, 1.69314718055995 } -// }; -// for (size_t i = 0; i < expected.size(); i++) -// for (size_t j = 0; j < expected[i].size(); j++) -// BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); -// } + vector> expected = { + { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, + 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, + 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, + 1.69314718055995, 1.69314718055995, 1.69314718055995 } + }; + for (size_t i = 0; i < expected.size(); i++) + for (size_t j = 0; j < expected[i].size(); j++) + BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); +} -// /** -// * Test TFIDF encoding for characters using lamda function, using rawcount as tf -// * type and smoothidf as true. -// */ -// BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) -// { -// vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test TFIDF encoding for characters using lamda function, using rawcount as tf + * type and smoothidf as true. + */ +BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// arma::mat output; -// TfIdfEncoding encoder; + arma::mat output; + TfIdfEncoding encoder; -// // Passing a empty string to encode charactersrawcountsmoothidftrue -// encoder.Encode(input, output, CharExtract()); -// arma::mat target = { -// { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, -// { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, -// { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } -// }; -// CheckMatrices(output, target, 1e-12); -// } + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, + { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, + { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + }; + CheckMatrices(output, target, 1e-12); +} -// /** -// * Test the one pass modification of the Tf-Idf encoding algorithm -// * in case of individual character encoding using default values. -// */ -// BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingIndividualCharactersTest) -// { -// std::vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test the one pass modification of the Tf-Idf encoding algorithm + * in case of individual character encoding using default values. + */ +BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingIndividualCharactersTest) +{ + std::vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// vector> output; -// TfIdfEncoding encoder; + vector> output; + TfIdfEncoding encoder; -// // Passing a empty string to encode characters -// encoder.Encode(input, output, CharExtract()); -// vector> expected = { -// { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, -// { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, -// { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } -// }; -// for (size_t i = 0; i < expected.size(); i++) -// for (size_t j = 0; j < expected[i].size(); j++) -// BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); -// } + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); + vector> expected = { + { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, + { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, + { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + }; + for (size_t i = 0; i < expected.size(); i++) + for (size_t j = 0; j < expected[i].size(); j++) + BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); +} -// /** -// * Test the Tf-Idf Encoding using rawcount type and smoothidf as false. -// */ -// BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) -// { -// using DictionaryType = StringEncodingDictionary; +/** + * Test the Tf-Idf Encoding using rawcount type and smoothidf as false. + */ +BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; -// arma::mat output; -// TfIdfEncoding encoder( -// (TfIdfEncodingPolicy(0, false))); -// SplitByAnyOf tokenizer(" "); + arma::mat output; + TfIdfEncoding encoder( + (TfIdfEncodingPolicy(0, false))); + SplitByAnyOf tokenizer(" "); -// encoder.Encode(stringEncodingInput, output, tokenizer); + encoder.Encode(stringEncodingInput, output, tokenizer); -// const DictionaryType& dictionary = encoder.Dictionary(); + const DictionaryType& dictionary = encoder.Dictionary(); -// // Checking that everything is mapped to different numbers -// std::unordered_map keysCount; -// for (auto& keyValue : dictionary.Mapping()) -// { -// keysCount[keyValue.second]++; -// // Every token should be mapped only once -// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); -// } + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } -// arma::mat expected = { -// { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, -// 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -// 0 }, -// { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, -// 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, -// { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, 0, 1, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 2.09861228866811, 2.09861228866811 } -// }; -// CheckMatrices(output, expected, 1e-12); -// } + arma::mat expected = { + { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, + 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, + 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811 } + }; + CheckMatrices(output, expected, 1e-12); +} -// * -// * Test the one pass modification of the TfIdf encoding algorithm, with rawcount -// * as type, but with smoothidf as false. - -// BOOST_AUTO_TEST_CASE(OnePassTfIdfRawCountEncodingTest) -// { -// using DictionaryType = StringEncodingDictionary; +/** + * Test the one pass modification of the TfIdf encoding algorithm, with rawcount + * as type, but with smoothidf as false. + */ +BOOST_AUTO_TEST_CASE(OnePassTfIdfRawCountEncodingTest) +{ + using DictionaryType = StringEncodingDictionary; -// vector> output; -// TfIdfEncoding encoder(0, false); -// SplitByAnyOf tokenizer(" "); + vector> output; + TfIdfEncoding encoder(0, false); + SplitByAnyOf tokenizer(" "); -// encoder.Encode(stringEncodingInput, output, tokenizer); + encoder.Encode(stringEncodingInput, output, tokenizer); -// const DictionaryType& dictionary = encoder.Dictionary(); + const DictionaryType& dictionary = encoder.Dictionary(); -// // Checking that everything is mapped to different numbers -// std::unordered_map keysCount; -// for (auto& keyValue : dictionary.Mapping()) -// { -// keysCount[keyValue.second]++; -// // Every token should be mapped only once -// BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); -// } + // Checking that everything is mapped to different numbers + std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) + { + keysCount[keyValue.second]++; + // Every token should be mapped only once + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); + } -// vector> expected = { -// { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, -// 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -// 0 }, -// { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, -// 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, -// { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, 0, 1, -// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, -// 2.09861228866811, 2.09861228866811, 2.09861228866811 } -// }; -// for (size_t i = 0; i < expected.size(); i++) -// for (size_t j = 0; j < expected[i].size(); j++) -// BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); -// } + vector> expected = { + { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, + 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, + 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, + 2.09861228866811, 2.09861228866811, 2.09861228866811 } + }; + for (size_t i = 0; i < expected.size(); i++) + for (size_t j = 0; j < expected[i].size(); j++) + BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); +} -// /** -// * Test TFIDF encoding for characters using lamda function, using rawcount as -// * tf type and smoothidf as false. -// */ -// BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) -// { -// vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test TFIDF encoding for characters using lamda function, using rawcount as + * tf type and smoothidf as false. + */ +BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// arma::mat output; -// TfIdfEncoding encoder(0, false); + arma::mat output; + TfIdfEncoding encoder(0, false); -// // Passing a empty string to encode charactersrawcountsmoothidftrue -// encoder.Encode(input, output, CharExtract()); -// arma::mat target = { -// { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, -// { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, -// { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } -// }; -// CheckMatrices(output, target, 1e-12); -// } + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, + { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, + { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } + }; + CheckMatrices(output, target, 1e-12); +} -// /** -// * Test the one pass modification of the Tf Idf encoding algorithm -// * in case of individual character encoding, using raw count as type, -// * and smoothidf as false. -// */ -// BOOST_AUTO_TEST_CASE(OnePassRawcountEncodingIndividualCharactersTest) -// { -// std::vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test the one pass modification of the Tf Idf encoding algorithm + * in case of individual character encoding, using raw count as type, + * and smoothidf as false. + */ +BOOST_AUTO_TEST_CASE(OnePassRawcountEncodingIndividualCharactersTest) +{ + std::vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// vector> output; -// TfIdfEncoding encoder(0, false); + vector> output; + TfIdfEncoding encoder(0, false); -// // Passing a empty string to encode characters -// encoder.Encode(input, output, CharExtract()); -// vector> expected = { -// { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, -// { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, -// { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } -// }; -// for (size_t i = 0; i < expected.size(); i++) -// for (size_t j = 0; j < expected[i].size(); j++) -// BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); -// } + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); + vector> expected = { + { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, + { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, + { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } + }; + for (size_t i = 0; i < expected.size(); i++) + for (size_t j = 0; j < expected[i].size(); j++) + BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); +} -// /** -// * Test TFIDF encoding for characters using lamda function, using binary -// * weighting scheme for tf and smoothidf as true. -// */ -// BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) -// { -// vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test TFIDF encoding for characters using lamda function, using binary + * weighting scheme for tf and smoothidf as true. + */ +BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// arma::mat output; -// TfIdfEncoding encoder(1, true); + arma::mat output; + TfIdfEncoding encoder(1, true); -// // Passing a empty string to encode charactersrawcountsmoothidftrue -// encoder.Encode(input, output, CharExtract()); -// arma::mat target = { -// { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, -// { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, -// { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } -// }; -// CheckMatrices(output, target, 1e-12); -// } + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, + { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, + { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + }; + CheckMatrices(output, target, 1e-12); +} -// /** -// * Test TFIDF encoding for characters using lamda function, using binary -// * weighting scheme for tf and smoothidf as true. -// */ -// BOOST_AUTO_TEST_CASE(OnePassBnarySmoothIdfEncodingIndividualCharactersTest) -// { -// std::vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test TFIDF encoding for characters using lamda function, using binary + * weighting scheme for tf and smoothidf as true. + */ +BOOST_AUTO_TEST_CASE(OnePassBnarySmoothIdfEncodingIndividualCharactersTest) +{ + std::vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// vector> output; -// TfIdfEncoding encoder(1, true); + vector> output; + TfIdfEncoding encoder(1, true); -// // Passing a empty string to encode characters -// encoder.Encode(input, output, CharExtract()); -// vector> expected = { -// { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, -// { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, -// { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } -// }; -// for (size_t i = 0; i < expected.size(); i++) -// for (size_t j = 0; j < expected[i].size(); j++) -// BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); -// } + // Passing a empty string to encode characters + encoder.Encode(input, output, CharExtract()); + vector> expected = { + { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, + { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, + { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + }; + for (size_t i = 0; i < expected.size(); i++) + for (size_t j = 0; j < expected[i].size(); j++) + BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); +} -// /** -// * Test TFIDF encoding for characters using lamda function, using binary -// * as weighting scheme and smoothidf as false. -// */ -// BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) -// { -// vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test TFIDF encoding for characters using lamda function, using binary + * as weighting scheme and smoothidf as false. + */ +BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// arma::mat output; -// TfIdfEncoding encoder(1, false); + arma::mat output; + TfIdfEncoding encoder(1, false); -// // Passing a empty string to encode charactersrawcountsmoothidftrue -// encoder.Encode(input, output, CharExtract()); -// arma::mat target = { -// { 1.4054651081081644, 1, 1.4054651081081644, 0, 0 }, -// { 0, 1, 1.4054651081081644, 1.4054651081081644, 2.0986122886681100 }, -// { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } -// }; -// CheckMatrices(output, target, 1e-12); -// } + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1.4054651081081644, 1, 1.4054651081081644, 0, 0 }, + { 0, 1, 1.4054651081081644, 1.4054651081081644, 2.0986122886681100 }, + { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } + }; + CheckMatrices(output, target, 1e-12); +} -// /** -// * Test TFIDF encoding for characters using lamda function, using sublinear -// * as weighting scheme and smoothidf as true. -// */ -// BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) -// { -// vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test TFIDF encoding for characters using lamda function, using sublinear + * as weighting scheme and smoothidf as true. + */ +BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// arma::mat output; -// TfIdfEncoding encoder(2, true); + arma::mat output; + TfIdfEncoding encoder(2, true); -// // Passing a empty string to encode charactersrawcountsmoothidftrue -// encoder.Encode(input, output, CharExtract()); -// arma::mat target = { -// { 1.2876820724517808, 1.6931471805599454, 2.1802352704293200, 0, 0 }, -// { 0, 1.6931471805599454, 2.1802352704293200, 2.1802352704293200, -// 1.6931471805599454 }, -// { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } -// }; -// CheckMatrices(output, target, 1e-12); -// } + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1.2876820724517808, 1.6931471805599454, 2.1802352704293200, 0, 0 }, + { 0, 1.6931471805599454, 2.1802352704293200, 2.1802352704293200, + 1.6931471805599454 }, + { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + }; + CheckMatrices(output, target, 1e-12); +} -// /** -// * Test TFIDF encoding for characters using lamda function, using sublinear -// * as weighting scheme and smoothidf as false. -// */ -// BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) -// { -// vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test TFIDF encoding for characters using lamda function, using sublinear + * as weighting scheme and smoothidf as false. + */ +BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// arma::mat output; -// TfIdfEncoding encoder(2, false); + arma::mat output; + TfIdfEncoding encoder(2, false); -// // Passing a empty string to encode charactersrawcountsmoothidftrue -// encoder.Encode(input, output, CharExtract()); -// arma::mat target = { -// { 1.4054651081081644, 1.6931471805599454, 2.3796592851687173, 0, 0 }, -// { 0, 1.6931471805599454, 2.3796592851687173, 2.3796592851687173, -// 2.0986122886681100 }, -// { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } -// }; -// CheckMatrices(output, target, 1e-12); -// } + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 1.4054651081081644, 1.6931471805599454, 2.3796592851687173, 0, 0 }, + { 0, 1.6931471805599454, 2.3796592851687173, 2.3796592851687173, + 2.0986122886681100 }, + { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } + }; + CheckMatrices(output, target, 1e-12); +} -// /** -// * Test TFIDF encoding for characters using lamda function, using term -// * Frequency as weighting scheme and smoothidf as true. -// */ -// BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) -// { -// vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test TFIDF encoding for characters using lamda function, using term + * Frequency as weighting scheme and smoothidf as true. + */ +BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// arma::mat output; -// TfIdfEncoding encoder(3, true); + arma::mat output; + TfIdfEncoding encoder(3, true); -// // Passing a empty string to encode charactersrawcountsmoothidftrue -// encoder.Encode(input, output, CharExtract()); -// arma::mat target = { -// { 0.2575364144903562, 0.4, 0.5150728289807124, 0, 0 }, -// { 0, 0.2857142857142857, 0.3679091635576516, 0.3679091635576516, -// 0.2418781686514208 }, -// { 0.4292273574839269, 0.3333333333333333, 0, 0.4292273574839269, 0 } -// }; -// CheckMatrices(output, target, 1e-12); -// } + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 0.2575364144903562, 0.4, 0.5150728289807124, 0, 0 }, + { 0, 0.2857142857142857, 0.3679091635576516, 0.3679091635576516, + 0.2418781686514208 }, + { 0.4292273574839269, 0.3333333333333333, 0, 0.4292273574839269, 0 } + }; + CheckMatrices(output, target, 1e-12); +} -// /** -// * Test TFIDF encoding for characters using lamda function, using Term -// * Frequency as weighting scheme and smoothidf as false. -// */ -// BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) -// { -// vector input = { -// "GACCA", -// "ABCABCD", -// "GAB" -// }; +/** + * Test TFIDF encoding for characters using lamda function, using Term + * Frequency as weighting scheme and smoothidf as false. + */ +BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) +{ + vector input = { + "GACCA", + "ABCABCD", + "GAB" + }; -// arma::mat output; -// TfIdfEncoding encoder(3, false); + arma::mat output; + TfIdfEncoding encoder(3, false); -// // Passing a empty string to encode charactersrawcountsmoothidftrue -// encoder.Encode(input, output, CharExtract()); -// arma::mat target = { -// { 0.2810930216216329, 0.4, 0.5621860432432658, 0, 0 }, -// { 0, 0.2857142857142857, 0.4015614594594755, 0.4015614594594755, -// 0.2998017555240157 }, -// { 0.4684883693693881, 0.3333333333333333, 0, 0.4684883693693881, 0 } -// }; -// CheckMatrices(output, target, 1e-12); -// } + // Passing a empty string to encode charactersrawcountsmoothidftrue + encoder.Encode(input, output, CharExtract()); + arma::mat target = { + { 0.2810930216216329, 0.4, 0.5621860432432658, 0, 0 }, + { 0, 0.2857142857142857, 0.4015614594594755, 0.4015614594594755, + 0.2998017555240157 }, + { 0.4684883693693881, 0.3333333333333333, 0, 0.4684883693693881, 0 } + }; + CheckMatrices(output, target, 1e-12); +} /** * Serialization test for the TF-IDF encoding algorithm with @@ -1013,27 +1013,51 @@ BOOST_AUTO_TEST_CASE(CharExtractTfIdfEncodingSerialization) EncoderType encoder; CharExtract tokenizer; arma::mat output; - vector input = { - "GACCA", - "ABCABCD", - "GAB" - }; - encoder.Encode(input, output, tokenizer); + + encoder.Encode(stringEncodingInput, output, tokenizer); EncoderType xmlEncoder, textEncoder, binaryEncoder; arma::mat xmlOutput, textOutput, binaryOutput; SerializeObjectAll(encoder, xmlEncoder, textEncoder, binaryEncoder); - std::cout<<"HHHHHHHHHHHHHH\n"; + CheckDictionaries(encoder.Dictionary(), xmlEncoder.Dictionary()); CheckDictionaries(encoder.Dictionary(), textEncoder.Dictionary()); CheckDictionaries(encoder.Dictionary(), binaryEncoder.Dictionary()); - std::cout<<"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFSDDDDDDDDDDDDDDDD\n"; - xmlEncoder.Encode(input, xmlOutput, tokenizer); - std::cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"; - textEncoder.Encode(input, textOutput, tokenizer); - binaryEncoder.Encode(input, binaryOutput, tokenizer); + xmlEncoder.Encode(stringEncodingInput, xmlOutput, tokenizer); + textEncoder.Encode(stringEncodingInput, textOutput, tokenizer); + binaryEncoder.Encode(stringEncodingInput, binaryOutput, tokenizer); + + CheckMatrices(output, xmlOutput, textOutput, binaryOutput); +} + +/** + * Serialization test for the TF-IDF encoding algorithm with + * the SplitByAnyOf tokenizer. + */ +BOOST_AUTO_TEST_CASE(SplitByAnyOfTfIdfEncodingSerialization) +{ + using EncoderType = TfIdfEncoding; + + EncoderType encoder; + SplitByAnyOf tokenizer(" "); + arma::mat output; + + encoder.Encode(stringEncodingInput, output, tokenizer); + + EncoderType xmlEncoder, textEncoder, binaryEncoder; + arma::mat xmlOutput, textOutput, binaryOutput; + + SerializeObjectAll(encoder, xmlEncoder, textEncoder, binaryEncoder); + + CheckDictionaries(encoder.Dictionary(), xmlEncoder.Dictionary()); + CheckDictionaries(encoder.Dictionary(), textEncoder.Dictionary()); + CheckDictionaries(encoder.Dictionary(), binaryEncoder.Dictionary()); + + xmlEncoder.Encode(stringEncodingInput, xmlOutput, tokenizer); + textEncoder.Encode(stringEncodingInput, textOutput, tokenizer); + binaryEncoder.Encode(stringEncodingInput, binaryOutput, tokenizer); CheckMatrices(output, xmlOutput, textOutput, binaryOutput); } From 494d7164f21831838dbdf7d5140dda9e835fe958 Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Sat, 10 Aug 2019 23:49:10 +0530 Subject: [PATCH 019/265] correction in test, update documentation, next target update minor documentation --- .../bag_of_words_encoding_policy.hpp | 34 ++-- .../dictionary_encoding_policy.hpp | 3 + .../tf_idf_encoding_policy.hpp | 67 ++++--- src/mlpack/tests/string_encoding_test.cpp | 177 +++++++++--------- 4 files changed, 141 insertions(+), 140 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index e5b253cbf4..01e113b64d 100644 --- a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -37,6 +37,7 @@ class BagOfWordsEncodingPolicy * @param maxNumTokens The maximum number of tokens in the strings of the input dataset (not used). * @param dictionarySize The size of the dictionary. + * @tparam MatType The type of output matrix. */ template static void InitMatrix(MatType& output, @@ -49,13 +50,14 @@ class BagOfWordsEncodingPolicy /** * The function initializes the output matrix. - * Overloaded function to store result in vector> + * Overloaded function to store result in vector> * * @param output Output matrix to store the encoded results. * @param datasetSize The number of strings in the input dataset. * @param maxNumTokens The maximum number of tokens in the strings of the input dataset. * @param dictionarySize The size of the dictionary (not used). + * @tparam OutputType The type of output vector. */ template static void InitMatrix(std::vector >& output, @@ -74,6 +76,7 @@ class BagOfWordsEncodingPolicy * @param value The encoded token. * @param row The row number at which the encoding is performed. * @param col The row token number at which the encoding is performed. + * @tparam MatType The type of output matrix. */ template static void Encode(MatType& output, @@ -81,19 +84,21 @@ class BagOfWordsEncodingPolicy size_t row, size_t /*col*/) { - // Important since Mapping starts from 1 whereas allowed column value is 0. - output(row, value-1) = 1; + // Important since Mapping of words,Dcitionary Encoding starts from 1, + // whereas allowed column value is 0. + output(row, value - 1) = 1; } /** - * The function performs the bag of words encoding algorithm i.e. it writess + * The function performs the bag of words encoding algorithm i.e. it writes * the encoded token to the ouput. - * Overload function to accepted vector> as output type. + * Overload function to accepted vector> as output type. * * @param output Output matrix to store the encoded results. * @param value The encoded token. * @param row The row number at which the encoding is performed. * @param col The row token number at which the encoding is performed. + * @tparam OutputType The type of output vector. */ template static void Encode(std::vector >& output, @@ -101,8 +106,9 @@ class BagOfWordsEncodingPolicy size_t row, size_t /*col*/) { - // Important since Mapping starts from 1 whereas allowed column value is 0. - output[row][value-1] = 1; + // Important since Mapping of words,Dcitionary Encoding starts from 1, + // whereas allowed column value is 0. + output[row][value - 1] = 1; } /** @@ -126,20 +132,6 @@ class BagOfWordsEncodingPolicy size_t /*value*/) { } }; -/** - * The specialization provides some information about the about the bag of - * words encoding policy. - */ -template<> -struct StringEncodingPolicyTraits -{ - /** - * Indicates if the policy is able to encode the token at once without - * any information about other tokens as well as the total tokens count. - */ - static const bool onePassEncoding = false; -}; - template using BagOfWordsEncoding = StringEncoding>; diff --git a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp index 9c972fd856..d3fd97efc7 100644 --- a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp @@ -38,6 +38,7 @@ class DictionaryEncodingPolicy * @param maxNumTokens The maximum number of tokens in the strings of the input dataset. * @param dictionarySize The size of the dictionary (not used). + * @tparam MatType The type of output matrix. */ template static void InitMatrix(MatType& output, @@ -56,6 +57,7 @@ class DictionaryEncodingPolicy * @param value The encoded token. * @param row The row number at which the encoding is performed. * @param col The row token number at which the encoding is performed. + * @tparam MatType The type of output matrix. */ template static void Encode(MatType& output, @@ -73,6 +75,7 @@ class DictionaryEncodingPolicy * * @param output Output vector to store the encoded results. * @param value The encoded token. + * @tparam OutputType The type of output vector. */ template static void Encode(std::vector& output, size_t value) diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 88bbdbff01..86719066e4 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -37,13 +37,13 @@ class TfIdfEncodingPolicy { public: /* - * Enum Class used to identify the type of tf encoding + * Enum class used to identify the type of tf encoding * * Follwing are the defination of the types - * binary : binary weighting scheme (0,1) - * rawCount : raw count weighting scheme (count of token for every row) - * termFrequency : term frequency weighting scheme (count / length(row)) - * subinerTf : logarthimic weighting scheme (log(tf) + 1) + * BINARY : binary weighting scheme (0,1) + * RAW_COUNT : raw count weighting scheme (count of token for every row) + * TERM_FREQUENCY : term frequency weighting scheme (count / length(row)) + * SUBLINEAR_TF : logarthimic weighting scheme (log(tf) + 1) * */ enum class TfTypes @@ -54,7 +54,8 @@ class TfIdfEncodingPolicy TERM_FREQUENCY, }; - TfIdfEncodingPolicy(size_t tfType = 0, bool smoothIdf = true) : + TfIdfEncodingPolicy(TfTypes tfType = TfTypes::RAW_COUNT, + bool smoothIdf = true) : tfType(tfType), smoothIdf(smoothIdf) { @@ -67,9 +68,10 @@ class TfIdfEncodingPolicy * @param maxNumTokens The maximum number of tokens in the strings of the input dataset. * @param dictionarySize The size of the dictionary (not used). + * @tparam MatType The type of output matrix. */ template - void InitMatrix(MatType& output, + static void InitMatrix(MatType& output, size_t datasetSize, size_t /*maxNumTokens*/, size_t dictionarySize) @@ -79,13 +81,14 @@ class TfIdfEncodingPolicy /** * The function initializes the output matrix. - * Overloaded function to store result in vector> + * Overloaded function to store result in vector> * * @param output Output matrix to store the encoded results. * @param datasetSize The number of strings in the input dataset. * @param maxNumTokens The maximum number of tokens in the strings of the input dataset. * @param dictionarySize The size of the dictionary (not used). + * @tparam OutputType The type of output vector. */ template static void InitMatrix(std::vector >& output, @@ -104,6 +107,7 @@ class TfIdfEncodingPolicy * @param value The encoded token. * @param row The row number at which the encoding is performed. * @param col The row token number at which the encoding is performed. + * @tparam MatType The type of output matrix. */ template void Encode(MatType& output, @@ -111,18 +115,19 @@ class TfIdfEncodingPolicy size_t row, size_t /*col*/) { - // Important since Mapping starts from 1 whereas allowed column value is 0. + // Important since Mapping of words,Dcitionary Encoding starts from 1, + // whereas allowed column value is 0. double idf, tf; if (smoothIdf) idf = std::log((output.n_rows + 1) / (1 + idfdict[value - 1])) + 1; else idf = std::log(output.n_rows / idfdict[value - 1]) + 1; - if (tfType == static_cast(TfTypes::TERM_FREQUENCY)) + if (tfType == TfTypes::TERM_FREQUENCY) tf = tokenCount[row][value - 1] / row_size[row]; - else if (tfType == static_cast(TfTypes::SUBLINEAR_TF)) + else if (tfType == TfTypes::SUBLINEAR_TF) tf = std::log(tokenCount[row][value - 1]) + 1; - else if (tfType == static_cast(TfTypes::BINARY)) + else if (tfType == TfTypes::BINARY) tf = tokenCount[row][value - 1] > 0 ? 1 : 0; else tf = tokenCount[row][value - 1]; @@ -133,12 +138,14 @@ class TfIdfEncodingPolicy /** * The function performs the TfIdf encoding algorithm i.e. it writes * the encoded token to the ouput. - * Overload function to accepted vector> as output type. + * Overload function to accepted vector> as output type. * - * @param output Output matrix to store the encoded results (sp_mat or mat). + * @param output Output matrix to store the encoded results. * @param value The encoded token. * @param row The row number at which the encoding is performed. * @param col The row token number at which the encoding is performed. + * @tparam OutputType The type of output vector. + * @tparam OutputType The type of output vector. */ template void Encode(std::vector >& output, @@ -146,18 +153,19 @@ class TfIdfEncodingPolicy size_t row, size_t /*col*/) { - // Important since Mapping starts from 1 whereas allowed column value is 0. + // Important since Mapping of words,Dcitionary Encoding starts from 1, + // whereas allowed column value is 0. double idf, tf; if (smoothIdf) idf = std::log((output.size() + 1) / (1 + idfdict[value - 1])) + 1; else idf = std::log(output.size() / idfdict[value - 1]) + 1; - if (tfType == static_cast(TfTypes::TERM_FREQUENCY)) + if (tfType == TfTypes::TERM_FREQUENCY) tf = tokenCount[row][value - 1] / row_size[row]; - else if (tfType == static_cast(TfTypes::SUBLINEAR_TF)) + else if (tfType == TfTypes::SUBLINEAR_TF) tf = std::log(tokenCount[row][value - 1]) + 1; - else if (tfType == static_cast(TfTypes::BINARY)) + else if (tfType == TfTypes::BINARY) tf = tokenCount[row][value - 1] > 0 ? 1 : 0; else tf = tokenCount[row][value - 1]; @@ -190,11 +198,14 @@ class TfIdfEncodingPolicy if (row >= tokenCount.size()) { row_size.push_back(0); - tokenCount.push_back(std::unordered_map()); + tokenCount.emplace_back(); + ///tokenCount.push_back(std::unordered_map()); } tokenCount.back()[value-1]++; + if (tokenCount.back()[value - 1] == 1) idfdict[value - 1]++; + row_size.back()++; } private: @@ -207,26 +218,12 @@ class TfIdfEncodingPolicy // smoothIdf variable to indicate smoothining. bool smoothIdf; // Type of Term Frequency to use. - size_t tfType; -}; - -/** - * The specialization provides some information about the dictionary encoding - * policy. - */ -template<> -struct StringEncodingPolicyTraits -{ - /** - * Indicates if the policy is able to encode the token at once without - * any information about other tokens as well as the total tokens count. - */ - static const bool onePassEncoding = false; + TfTypes tfType; }; template using TfIdfEncoding = StringEncoding>; + StringEncodingDictionary>; } // namespace data } // namespace mlpack diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 220eed1cc9..184abc67fb 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -407,7 +407,7 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) arma::mat output; BagOfWordsEncoding encoder; - SplitByAnyOf tokenizer(" "); + SplitByAnyOf tokenizer(" ,."); encoder.Encode(stringEncodingInput, output, tokenizer); const DictionaryType& dictionary = encoder.Dictionary(); @@ -422,26 +422,26 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) } arma::mat expected = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; CheckMatrices(output, expected); } /** -* Test the one pass modification of the Bag of Words encoding algorithm. +* Bag of Words encoding algorithm output saved in a vector. */ -BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingTest) +BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingTest) { using DictionaryType = StringEncodingDictionary; vector> output; BagOfWordsEncoding encoder( (BagOfWordsEncodingPolicy())); - SplitByAnyOf tokenizer(" "); + SplitByAnyOf tokenizer(" ,."); encoder.Encode(stringEncodingInput, output, tokenizer); @@ -458,18 +458,18 @@ BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingTest) vector> expected = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; BOOST_REQUIRE(output == expected); } /** -* Test Bag of Words encoding for characters using lamda function. +* Test Bag of Words encoding for characters. */ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) { @@ -494,8 +494,8 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) } /** - * Test the one pass modification of the Bag of Words encoding algorithm - * in case of individual character encoding. + * Test the Bag of Words encoding algorithm in case of individual + * character encoding, storing resulting in vector>. */ BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingIndividualCharactersTest) { @@ -530,10 +530,9 @@ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) arma::mat output; TfIdfEncoding encoder; - SplitByAnyOf tokenizer(" "); + SplitByAnyOf tokenizer(" ,."); encoder.Encode(stringEncodingInput, output, tokenizer); - const DictionaryType& dictionary = encoder.Dictionary(); // Checking that everything is mapped to different numbers @@ -548,37 +547,37 @@ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0 }, + 1.28768207245178, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, 0, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, + 1.28768207245178, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995 } + 1.69314718055995, 1.69314718055995, 1.69314718055995 } }; CheckMatrices(output, expected, 1e-12); } /** - * Test the one pass modification of the TfIdf encoding algorithm, using rawcount - * as type of tf and smoothIdf as true. + * Test the TfIdf encoding algorithm, using rawcount as type of tf and smoothIdf + * as true, storing the result in a vector>. */ -BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingTest) +BOOST_AUTO_TEST_CASE(VectorRawCountSmoothIdfEncodingTest) { using DictionaryType = StringEncodingDictionary; vector> output; TfIdfEncoding encoder( (TfIdfEncodingPolicy())); - SplitByAnyOf tokenizer(" "); + SplitByAnyOf tokenizer(" ,."); encoder.Encode(stringEncodingInput, output, tokenizer); @@ -597,21 +596,21 @@ BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingTest) { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0 }, + 1.28768207245178, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, 0, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, + 1.28768207245178, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995 } + 1.69314718055995, 1.69314718055995, 1.69314718055995 } }; for (size_t i = 0; i < expected.size(); i++) for (size_t j = 0; j < expected[i].size(); j++) @@ -619,8 +618,8 @@ BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingTest) } /** - * Test TFIDF encoding for characters using lamda function, using rawcount as tf - * type and smoothidf as true. + * Test TFIDF encoding for characters, using rawcount as tf type and + * smoothidf as true. */ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) { @@ -644,10 +643,10 @@ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) } /** - * Test the one pass modification of the Tf-Idf encoding algorithm + * Test the Tf-Idf encoding algorithm to store result in vector * in case of individual character encoding using default values. */ -BOOST_AUTO_TEST_CASE(OnePassRawCountSmoothIdfEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(VectorRawCountSmoothIdfEncodingIndividualCharactersTest) { std::vector input = { "GACCA", @@ -679,8 +678,8 @@ BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) arma::mat output; TfIdfEncoding encoder( - (TfIdfEncodingPolicy(0, false))); - SplitByAnyOf tokenizer(" "); + (TfIdfEncodingPolicy(TfIdfEncodingPolicy::TfTypes::RAW_COUNT, false))); + SplitByAnyOf tokenizer(" ,."); encoder.Encode(stringEncodingInput, output, tokenizer); @@ -699,36 +698,37 @@ BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0 }, + 1.40546510810816, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, 0, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, + 1.40546510810816, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811 } + 2.09861228866811, 2.09861228866811, 2.09861228866811 } }; CheckMatrices(output, expected, 1e-12); } /** - * Test the one pass modification of the TfIdf encoding algorithm, with rawcount + * Test the TfIdf encoding algorithm for output type as vector, with rawcount * as type, but with smoothidf as false. */ -BOOST_AUTO_TEST_CASE(OnePassTfIdfRawCountEncodingTest) +BOOST_AUTO_TEST_CASE(VectorTfIdfRawCountEncodingTest) { using DictionaryType = StringEncodingDictionary; vector> output; - TfIdfEncoding encoder(0, false); - SplitByAnyOf tokenizer(" "); + TfIdfEncoding + encoder(TfIdfEncodingPolicy::TfTypes::RAW_COUNT, false); + SplitByAnyOf tokenizer(" ,."); encoder.Encode(stringEncodingInput, output, tokenizer); @@ -747,21 +747,21 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfRawCountEncodingTest) { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0 }, + 1.40546510810816, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, 0, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, + 1.40546510810816, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811 } + 2.09861228866811, 2.09861228866811, 2.09861228866811 } }; for (size_t i = 0; i < expected.size(); i++) for (size_t j = 0; j < expected[i].size(); j++) @@ -769,7 +769,7 @@ BOOST_AUTO_TEST_CASE(OnePassTfIdfRawCountEncodingTest) } /** - * Test TFIDF encoding for characters using lamda function, using rawcount as + * Test TFIDF encoding for characters, using rawcount as * tf type and smoothidf as false. */ BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) @@ -781,7 +781,8 @@ BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder(0, false); + TfIdfEncoding + encoder(TfIdfEncodingPolicy::TfTypes::RAW_COUNT, false); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -794,11 +795,11 @@ BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) } /** - * Test the one pass modification of the Tf Idf encoding algorithm + * Test the Tf Idf encoding algorithm to store result in vector * in case of individual character encoding, using raw count as type, * and smoothidf as false. */ -BOOST_AUTO_TEST_CASE(OnePassRawcountEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(VectorRawcountEncodingIndividualCharactersTest) { std::vector input = { "GACCA", @@ -807,7 +808,8 @@ BOOST_AUTO_TEST_CASE(OnePassRawcountEncodingIndividualCharactersTest) }; vector> output; - TfIdfEncoding encoder(0, false); + TfIdfEncoding + encoder(TfIdfEncodingPolicy::TfTypes::RAW_COUNT, false); // Passing a empty string to encode characters encoder.Encode(input, output, CharExtract()); @@ -822,8 +824,8 @@ BOOST_AUTO_TEST_CASE(OnePassRawcountEncodingIndividualCharactersTest) } /** - * Test TFIDF encoding for characters using lamda function, using binary - * weighting scheme for tf and smoothidf as true. + * Test TFIDF encoding for characters, using binary weighting scheme + * for tf and smoothidf as true. */ BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) { @@ -834,7 +836,8 @@ BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder(1, true); + TfIdfEncoding + encoder(TfIdfEncodingPolicy::TfTypes::BINARY, true); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -847,10 +850,10 @@ BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) } /** - * Test TFIDF encoding for characters using lamda function, using binary + * Test TFIDF encoding for characters to store results in vector, using binary * weighting scheme for tf and smoothidf as true. */ -BOOST_AUTO_TEST_CASE(OnePassBnarySmoothIdfEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(VectorBnarySmoothIdfEncodingIndividualCharactersTest) { std::vector input = { "GACCA", @@ -859,7 +862,8 @@ BOOST_AUTO_TEST_CASE(OnePassBnarySmoothIdfEncodingIndividualCharactersTest) }; vector> output; - TfIdfEncoding encoder(1, true); + TfIdfEncoding + encoder(TfIdfEncodingPolicy::TfTypes::BINARY, true); // Passing a empty string to encode characters encoder.Encode(input, output, CharExtract()); @@ -874,7 +878,7 @@ BOOST_AUTO_TEST_CASE(OnePassBnarySmoothIdfEncodingIndividualCharactersTest) } /** - * Test TFIDF encoding for characters using lamda function, using binary + * Test TFIDF encoding for characters, using binary * as weighting scheme and smoothidf as false. */ BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) @@ -886,7 +890,8 @@ BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder(1, false); + TfIdfEncoding + encoder(TfIdfEncodingPolicy::TfTypes::BINARY, false); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -899,7 +904,7 @@ BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) } /** - * Test TFIDF encoding for characters using lamda function, using sublinear + * Test TFIDF encoding for characters, using sublinear * as weighting scheme and smoothidf as true. */ BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) @@ -911,7 +916,8 @@ BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder(2, true); + TfIdfEncoding + encoder(TfIdfEncodingPolicy::TfTypes::SUBLINEAR_TF, true); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -925,7 +931,7 @@ BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) } /** - * Test TFIDF encoding for characters using lamda function, using sublinear + * Test TFIDF encoding for characters, using sublinear * as weighting scheme and smoothidf as false. */ BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) @@ -937,7 +943,8 @@ BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder(2, false); + TfIdfEncoding + encoder(TfIdfEncodingPolicy::TfTypes::SUBLINEAR_TF, false); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -951,7 +958,7 @@ BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) } /** - * Test TFIDF encoding for characters using lamda function, using term + * Test TFIDF encoding for characters, using term * Frequency as weighting scheme and smoothidf as true. */ BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) @@ -963,7 +970,8 @@ BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder(3, true); + TfIdfEncoding + encoder(TfIdfEncodingPolicy::TfTypes::TERM_FREQUENCY, true); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); @@ -977,7 +985,7 @@ BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) } /** - * Test TFIDF encoding for characters using lamda function, using Term + * Test TFIDF encoding for characters, using Term * Frequency as weighting scheme and smoothidf as false. */ BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) @@ -989,7 +997,8 @@ BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding encoder(3, false); + TfIdfEncoding + encoder(TfIdfEncodingPolicy::TfTypes::TERM_FREQUENCY, false); // Passing a empty string to encode charactersrawcountsmoothidftrue encoder.Encode(input, output, CharExtract()); From cde8ad96e1c52f0ac3bccc907bc6e3e0703c08ee Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Sun, 11 Aug 2019 02:00:39 +0530 Subject: [PATCH 020/265] Resolve style comments and other minor comments --- .../tf_idf_encoding_policy.hpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 86719066e4..9e8a4a290a 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -23,11 +23,10 @@ namespace data { * Tf means term-frequency while tf-idf means term-frequency times inverse * document-frequency. This is a common term weighting scheme in information * retrieval, that has also found good use in document classification. - * The goal of using tf-idf instead of the raw frequencies of occurrence of a - * token in a given document is to scale down the impact of tokens that occur - * very frequently in a given corpus and that are hence empirically less - * informative than features that occur in a small fraction of the training - * corpus. + * The goal of using tf-idf is to scale down the impact of tokens that occur + * very frequently in a given corpus while using the raw frequencies of a token + * and that are hence empirically less informative than features that occur in + * a small fraction of the training corpus. * TfIdfEncodingPolicy is used as a helper class for StringEncoding. * The encoder assigns a tf-idf number to each unique token and treat * the dataset as categorical. The tokens are labeled in the order of their @@ -199,9 +198,8 @@ class TfIdfEncodingPolicy { row_size.push_back(0); tokenCount.emplace_back(); - ///tokenCount.push_back(std::unordered_map()); } - tokenCount.back()[value-1]++; + tokenCount.back()[value - 1]++; if (tokenCount.back()[value - 1] == 1) idfdict[value - 1]++; From 96cc5e26068041054f6fe6af37b94fedf6bc75f3 Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Sun, 11 Aug 2019 02:42:16 +0530 Subject: [PATCH 021/265] merge conflict removed --- src/mlpack/core/data/string_encoding_impl.hpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_impl.hpp b/src/mlpack/core/data/string_encoding_impl.hpp index 96c81641ca..86a14a107c 100644 --- a/src/mlpack/core/data/string_encoding_impl.hpp +++ b/src/mlpack/core/data/string_encoding_impl.hpp @@ -112,12 +112,8 @@ EncodeHelper(const std::vector& input, { size_t numColumns = 0; -<<<<<<< HEAD - for (size_t i = 0; i < input.size(); i++) -======= // The first pass adds the extracted tokens to the dictionary. - for (const std::string& line : input) ->>>>>>> lozhnikov/string-encoding-fixes + for (size_t i = 0; i < input.size(); i++) { boost::string_view strView(input[i]); auto token = tokenizer(strView); From 0c2323485fb6c1eb9d625232b8ce62a031176a1f Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Mon, 12 Aug 2019 13:38:36 +0530 Subject: [PATCH 022/265] update documentation --- .../string_encoding_policies/CMakeLists.txt | 4 +-- .../tf_idf_encoding_policy.hpp | 25 +++++++++++++------ src/mlpack/tests/string_encoding_test.cpp | 4 +-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/CMakeLists.txt b/src/mlpack/core/data/string_encoding_policies/CMakeLists.txt index b78b409453..9f570e08d7 100644 --- a/src/mlpack/core/data/string_encoding_policies/CMakeLists.txt +++ b/src/mlpack/core/data/string_encoding_policies/CMakeLists.txt @@ -1,10 +1,10 @@ # Define the files that we need to compile. # Anything not in this list will not be compiled into mlpack. set(SOURCES - dictionary_encoding_policy.hpp bag_of_words_encoding_policy.hpp - tf_idf_encoding_policy.hpp + dictionary_encoding_policy.hpp policy_traits.hpp + tf_idf_encoding_policy.hpp ) # add directory name to sources diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 9e8a4a290a..d1724ccd3d 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -20,13 +20,12 @@ namespace data { /** * Definition of the TfIdfEncodingPolicy class. * - * Tf means term-frequency while tf-idf means term-frequency times inverse - * document-frequency. This is a common term weighting scheme in information - * retrieval, that has also found good use in document classification. + * Tf-idf is weighing scheme that stands for term-frequency multiplied by + * inverse document-frequency. * The goal of using tf-idf is to scale down the impact of tokens that occur - * very frequently in a given corpus while using the raw frequencies of a token - * and that are hence empirically less informative than features that occur in - * a small fraction of the training corpus. + * very frequently in a given corpus while using the type of Term-Frequency + * of a token and that are hence empirically less informative than features + * that occur in a small fraction of the training corpus. * TfIdfEncodingPolicy is used as a helper class for StringEncoding. * The encoder assigns a tf-idf number to each unique token and treat * the dataset as categorical. The tokens are labeled in the order of their @@ -35,7 +34,7 @@ namespace data { class TfIdfEncodingPolicy { public: - /* + /** * Enum class used to identify the type of tf encoding * * Follwing are the defination of the types @@ -53,6 +52,18 @@ class TfIdfEncodingPolicy TERM_FREQUENCY, }; + /** + * A constructor for the class which is use to set the type of term frequency + * and also the value for somoothIdf. + * + * @param tfType The type of term frequency, The avialbale option are + * RAW_COUNT : The count of a specific token + * BINARY : 1 if token occurs in document and 0 otherwise; + * TERM_FREQUENCY : Raw_count ÷ (number of words in document) + * SUBLINEAR_TF : log(Raw_Count) + 1 + * + * @param smoothIdf Used to indicate whether to use smooth idf or not. + */ TfIdfEncodingPolicy(TfTypes tfType = TfTypes::RAW_COUNT, bool smoothIdf = true) : tfType(tfType), diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 5d8bf27803..d2fea48ef0 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -533,7 +533,7 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) * Test the Bag of Words encoding algorithm in case of individual * character encoding, storing resulting in vector>. */ -BOOST_AUTO_TEST_CASE(OnePassBagOfWordsEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingIndividualCharactersTest) { std::vector input = { "GACCA", @@ -1086,7 +1086,7 @@ BOOST_AUTO_TEST_CASE(SplitByAnyOfTfIdfEncodingSerialization) using EncoderType = TfIdfEncoding; EncoderType encoder; - SplitByAnyOf tokenizer(" "); + SplitByAnyOf tokenizer(" ,.\""); arma::mat output; encoder.Encode(stringEncodingInput, output, tokenizer); From fe950e933318d26f588ff18d61bf0c44e800658b Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Mon, 12 Aug 2019 20:45:42 +0530 Subject: [PATCH 023/265] remove a test --- src/mlpack/tests/string_encoding_test.cpp | 32 ++--------------------- 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index d2fea48ef0..926e8867fc 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -337,6 +337,8 @@ void CheckDictionaries(const StringEncodingDictionary& expected, const MapType& expectedMapping = expected.Mapping(); const MapType& mapping = obtained.Mapping(); + BOOST_REQUIRE_EQUAL(expected.Size(), obtained.Size()); + for (size_t i = 0; i < mapping.size(); i++) { BOOST_REQUIRE_EQUAL(mapping[i], expectedMapping[i]); @@ -1047,36 +1049,6 @@ BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) CheckMatrices(output, target, 1e-12); } -/** - * Serialization test for the TF-IDF encoding algorithm with - * the CharExtract tokenizer. - */ -BOOST_AUTO_TEST_CASE(CharExtractTfIdfEncodingSerialization) -{ - using EncoderType = TfIdfEncoding; - - EncoderType encoder; - CharExtract tokenizer; - arma::mat output; - - encoder.Encode(stringEncodingInput, output, tokenizer); - - EncoderType xmlEncoder, textEncoder, binaryEncoder; - arma::mat xmlOutput, textOutput, binaryOutput; - - SerializeObjectAll(encoder, xmlEncoder, textEncoder, binaryEncoder); - - CheckDictionaries(encoder.Dictionary(), xmlEncoder.Dictionary()); - CheckDictionaries(encoder.Dictionary(), textEncoder.Dictionary()); - CheckDictionaries(encoder.Dictionary(), binaryEncoder.Dictionary()); - - xmlEncoder.Encode(stringEncodingInput, xmlOutput, tokenizer); - textEncoder.Encode(stringEncodingInput, textOutput, tokenizer); - binaryEncoder.Encode(stringEncodingInput, binaryOutput, tokenizer); - - CheckMatrices(output, xmlOutput, textOutput, binaryOutput); -} - /** * Serialization test for the TF-IDF encoding algorithm with * the SplitByAnyOf tokenizer. From 497c3832f23dbf4ffaf5c505afec5978cad6fcbe Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Sun, 15 Sep 2019 23:02:40 +0530 Subject: [PATCH 024/265] Binding for Loading and Saving images --- src/mlpack/core/data/load_image_impl.hpp | 8 +- src/mlpack/methods/preprocess/CMakeLists.txt | 4 + .../preprocess/load_save_image_main.cpp | 94 +++++++++++++++++++ 3 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 src/mlpack/methods/preprocess/load_save_image_main.cpp diff --git a/src/mlpack/core/data/load_image_impl.hpp b/src/mlpack/core/data/load_image_impl.hpp index e4fa8230af..cc4a37595f 100644 --- a/src/mlpack/core/data/load_image_impl.hpp +++ b/src/mlpack/core/data/load_image_impl.hpp @@ -77,8 +77,8 @@ bool Load(const std::string& filename, info.Channels() = tempChannels; // Copy image into armadillo Mat. - matrix = arma::Mat(image, info.Width() * info.Height() * - info.Channels(), 1, true, true); + matrix = arma::conv_to >::from(arma::Mat(image, info.Width() * info.Height() * + info.Channels(), 1, true, true)); // Free the image pointer. free(image); @@ -108,11 +108,11 @@ bool Load(const std::vector& files, // Decide matrix dimension using the image height and width. matrix.set_size(info.Width() * info.Height() * info.Channels(), files.size()); - matrix.col(0) = img; + matrix.col(0) = arma::conv_to>::from(img); for (size_t i = 1; i < files.size() ; i++) { - arma::Mat colImg(matrix.colptr(i), matrix.n_rows, 1, + arma::Mat colImg(matrix.colptr(i), matrix.n_rows, 1, false, true); status &= Load(files[i], colImg, info, fatal, transpose); } diff --git a/src/mlpack/methods/preprocess/CMakeLists.txt b/src/mlpack/methods/preprocess/CMakeLists.txt index e1128afebc..36eb498bab 100644 --- a/src/mlpack/methods/preprocess/CMakeLists.txt +++ b/src/mlpack/methods/preprocess/CMakeLists.txt @@ -35,3 +35,7 @@ add_markdown_docs(preprocess_imputer "cli" "preprocessing") add_cli_executable(preprocess_scale) add_python_binding(preprocess_scale) add_markdown_docs(preprocess_scale "cli;python" "preprocessing") + +add_cli_executable(load_save_image) +add_python_binding(load_save_image) +add_markdown_docs(load_save_image "cli;python" "preprocessing") diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp new file mode 100644 index 0000000000..d39b8a11a3 --- /dev/null +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -0,0 +1,94 @@ +/** + * @file load_save_image_main.cpp + * @author Jeffin Sam + * + * A CLI executable to load and image dataset. + * + * 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. + */ +#ifdef HAS_STB // Compile this only if stb is present. + +#include +#include +#include +#include +#include +#include + +using namespace mlpack; +using namespace mlpack::util; +using namespace arma; +using namespace std; + +PROGRAM_INFO("Load Image", + // Short description. + "A utility to load and save image dataset. This utility will allow you to " + "load and save a single image or an array of images.", + // Long description. + "This utility takes a images or an array of images and loads them to arma matrix." + "You can specify the height " + PRINT_PARAM_STRING("height") + "width " + + PRINT_PARAM_STRING("width") + " and channel " + PRINT_PARAM_STRING("channel") + + "of the images that needs to be loaded. \n There are other options too, that" + "can be specified such as quality " + PRINT_PARAM_STRING("quality") + " and " + + PRINT_PARAM_STRING("transpose") + + "\n\n" + + "You can also provide a dataset and save them as image using " + + PRINT_PARAM_STRING("dataset") + "and " + PRINT_PARAM_STRING("save") + + "as an parameter. An example to load an " + "image" + "\n\n" + + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, + "channel", 3, "output", "Y") + "\n\n" + + " An example to save an image is :" + "\n\n" + + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, + "channel", 3, "dataset", "Y"), + SEE_ALSO("@preprocess_binarize", "#preprocess_binarize"), + SEE_ALSO("@preprocess_describe", "#preprocess_describe"), + SEE_ALSO("@preprocess_imputer", "#preprocess_imputer")); + +//DEFINE PATAM +PARAM_VECTOR_IN_REQ(string, "input", "Image filenames which has to " + "be loaded/saved", "i"); + +PARAM_INT_IN_REQ("width", "Width of the image", "W"); +PARAM_INT_IN_REQ("channel", "Number of channel", "C"); + + +PARAM_MATRIX_OUT("output", "Matrix to save images data to.", "o"); + +PARAM_INT_IN("quality", "Compression of the image if saved as jpg (0-100).", "q",90); + +PARAM_FLAG("transpose", "Loaded dataset to be transposed", "t"); + +PARAM_INT_IN_REQ("height", "Height of the images", "H"); +PARAM_FLAG("save", "Save a dataset as images", "s"); +PARAM_MATRIX_IN("dataset", "Input matrix to save as images.", "I"); + +static void mlpackMain() +{ + // Parse command line options. + const int height = CLI::GetParam("height"); + const int width = CLI::GetParam("width"); + const int channel = CLI::GetParam("channel"); + const int quality = CLI::GetParam("quality"); + data::ImageInfo info(height, width, channel, quality); + const vector fileNames = + CLI::GetParam >("input"); + arma::mat out; + if(!CLI::HasParam("save")) + data::Load(fileNames, out, info, false, !CLI::HasParam("transpose")); + if (CLI::HasParam("output")) + CLI::GetParam("output") = std::move(out); + if(CLI::HasParam("save")) + { + if(!CLI::HasParam("dataset")) + { + throw std::runtime_error("Please provide a input matrix to save images from"); + } + data::Save(fileNames, CLI::GetParam("dataset"), info, false, !CLI::HasParam("transpose")); + } +} + +#endif // HAS_STB. From 3671ce47bb6b5dd4f1ce3c6d988d986d29e17a9e Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Sun, 15 Sep 2019 23:03:43 +0530 Subject: [PATCH 025/265] correct saving of images implementation --- src/mlpack/core/data/save_impl.hpp | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/mlpack/core/data/save_impl.hpp b/src/mlpack/core/data/save_impl.hpp index 18a61530a5..e65d93f8ca 100644 --- a/src/mlpack/core/data/save_impl.hpp +++ b/src/mlpack/core/data/save_impl.hpp @@ -325,7 +325,8 @@ bool Save(const std::string& filename, bool status = false; try { - unsigned char* image = matrix.memptr(); + arma::Mat temp = arma::conv_to >::from(matrix); + unsigned char* image = temp.memptr(); if ("png" == Extension(filename)) { @@ -384,17 +385,10 @@ bool Save(const std::vector& files, // We transpose by default. So, un-transpose if necessary. if (!transpose) matrix = arma::trans(matrix); - - arma::Mat img; - bool status = Save(files[0], img, info, fatal, transpose); - - // Decide matrix dimension using the image height and width. - matrix.set_size(info.Width() * info.Height() * info.Channels(), files.size()); - matrix.col(0) = img; - - for (size_t i = 1; i < files.size() ; i++) + bool status = true; + for (size_t i = 0; i < files.size() ; i++) { - arma::Mat colImg(matrix.colptr(i), matrix.n_rows, 1, + arma::Mat colImg(matrix.colptr(i), matrix.n_rows, 1, false, true); status &= Save(files[i], colImg, info, fatal, transpose); } From e44eb663ca2e8dad7f72ebf84bb57496afb7d38b Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Mon, 16 Sep 2019 23:26:35 +0530 Subject: [PATCH 026/265] style fixing and rectify transpose option --- src/mlpack/core/data/save_impl.hpp | 7 +-- .../preprocess/load_save_image_main.cpp | 34 +++++------ src/mlpack/tests/image_load_test.cpp | 57 +++++++++++++++++++ 3 files changed, 76 insertions(+), 22 deletions(-) diff --git a/src/mlpack/core/data/save_impl.hpp b/src/mlpack/core/data/save_impl.hpp index e65d93f8ca..08ae1d3807 100644 --- a/src/mlpack/core/data/save_impl.hpp +++ b/src/mlpack/core/data/save_impl.hpp @@ -293,9 +293,6 @@ bool Save(const std::string& filename, const bool transpose) { Timer::Start("saving_image"); - // We transpose by default. So, un-transpose if necessary. - if (!transpose) - matrix = arma::trans(matrix); int tempWidth, tempHeight, tempChannels, tempQuality; @@ -382,9 +379,7 @@ bool Save(const std::vector& files, throw std::runtime_error(oss.str()); return false; } - // We transpose by default. So, un-transpose if necessary. - if (!transpose) - matrix = arma::trans(matrix); + bool status = true; for (size_t i = 0; i < files.size() ; i++) { diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index d39b8a11a3..3780674328 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -23,27 +23,26 @@ using namespace mlpack::util; using namespace arma; using namespace std; -PROGRAM_INFO("Load Image", +PROGRAM_INFO("Load Save Image", // Short description. "A utility to load and save image dataset. This utility will allow you to " "load and save a single image or an array of images.", // Long description. - "This utility takes a images or an array of images and loads them to arma matrix." - "You can specify the height " + PRINT_PARAM_STRING("height") + "width " + - PRINT_PARAM_STRING("width") + " and channel " + PRINT_PARAM_STRING("channel") + - "of the images that needs to be loaded. \n There are other options too, that" - "can be specified such as quality " + PRINT_PARAM_STRING("quality") + " and " + - PRINT_PARAM_STRING("transpose") + - "\n\n" + - "You can also provide a dataset and save them as image using " + - PRINT_PARAM_STRING("dataset") + "and " + PRINT_PARAM_STRING("save") + - "as an parameter. An example to load an " - "image" + "\n\n" + + "This utility takes a image or an array of images and loads them to a" + "matrix. You can specify the height " + PRINT_PARAM_STRING("height") + + " width " + PRINT_PARAM_STRING("width") + " and channel " + + PRINT_PARAM_STRING("channel") + "of the images that needs to be loaded. " + "\nThere are other options too, that can be specified such as " + + PRINT_PARAM_STRING("quality") + " and " + PRINT_PARAM_STRING("transpose") + + ".\n\n" + + "You can also provide a dataset and save them as images using " + + PRINT_PARAM_STRING("dataset") + " and " + PRINT_PARAM_STRING("save") + + "as an parameter. An example to load an image : " + "\n\n" + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, "channel", 3, "output", "Y") + "\n\n" + " An example to save an image is :" + "\n\n" + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, - "channel", 3, "dataset", "Y"), + "channel", 3, "dataset", "Y", "save", true), SEE_ALSO("@preprocess_binarize", "#preprocess_binarize"), SEE_ALSO("@preprocess_describe", "#preprocess_describe"), SEE_ALSO("@preprocess_imputer", "#preprocess_imputer")); @@ -58,7 +57,8 @@ PARAM_INT_IN_REQ("channel", "Number of channel", "C"); PARAM_MATRIX_OUT("output", "Matrix to save images data to.", "o"); -PARAM_INT_IN("quality", "Compression of the image if saved as jpg (0-100).", "q",90); +PARAM_INT_IN("quality", "Compression of the image if saved as jpg (0-100).", + "q", 90); PARAM_FLAG("transpose", "Loaded dataset to be transposed", "t"); @@ -85,9 +85,11 @@ static void mlpackMain() { if(!CLI::HasParam("dataset")) { - throw std::runtime_error("Please provide a input matrix to save images from"); + throw std::runtime_error("Please provide a input matrix to save " + "images from"); } - data::Save(fileNames, CLI::GetParam("dataset"), info, false, !CLI::HasParam("transpose")); + data::Save(fileNames, CLI::GetParam("dataset"), info, false, + !CLI::HasParam("transpose")); } } diff --git a/src/mlpack/tests/image_load_test.cpp b/src/mlpack/tests/image_load_test.cpp index 0e2bbb0459..65527af0e0 100644 --- a/src/mlpack/tests/image_load_test.cpp +++ b/src/mlpack/tests/image_load_test.cpp @@ -67,6 +67,63 @@ BOOST_AUTO_TEST_CASE(SaveImageAPITest) BOOST_REQUIRE_EQUAL(im1[i], im2[i]); } +/** + * Test if the image is saved correctly using API for transpose. + */ +BOOST_AUTO_TEST_CASE(SaveImageTransposeAPITest) +{ + data::ImageInfo info(5, 5, 3, 90); + + arma::Mat im1; + size_t dimension = info.Width() * info.Height() * info.Channels(); + im1 = arma::randi>(dimension, 1); + BOOST_REQUIRE(data::Save("APITest.bmp", im1, info, false, false) == true); + + arma::Mat im2; + BOOST_REQUIRE(data::Load("APITest.bmp", im2, info, false, false) == true); + + BOOST_REQUIRE_EQUAL(im1.n_cols, im2.n_cols); + BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows); + for (size_t i = 10; i < im1.n_elem; ++i) + BOOST_REQUIRE_EQUAL(im1[i], im2[i]); +} + +/** + * Test that the image is loaded correctly into the matrix using the API + * for vectors. + */ +BOOST_AUTO_TEST_CASE(LoadVectorImageAPITest) +{ + arma::Mat matrix; + data::ImageInfo info; + std::vector file = {"test_image.png", "test_image.png"}; + BOOST_REQUIRE(data::Load(file, matrix, info, false, + true) == true); + BOOST_REQUIRE_EQUAL(matrix.n_rows, 50 * 50 * 3); // width * height * channels. + BOOST_REQUIRE_EQUAL(matrix.n_cols, 2); +} + +/** + * Test if the image is saved correctly using vector saving API for transpose. + */ +BOOST_AUTO_TEST_CASE(SaveImageVectorAPITest) +{ + data::ImageInfo info(5, 5, 3, 90); + + arma::Mat im1; + size_t dimension = info.Width() * info.Height() * info.Channels(); + im1 = arma::randi>(dimension, 2); + std::vector file = {"APITest1.bmp", "APITest2.bmp"}; + BOOST_REQUIRE(data::Save(file, im1, info, false, false) == true); + + arma::Mat im2; + BOOST_REQUIRE(data::Load(file, im2, info, false, false) == true); + + BOOST_REQUIRE_EQUAL(im1.n_cols, im2.n_cols); + BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows); + for (size_t i = 10; i < im1.n_elem; ++i) + BOOST_REQUIRE_EQUAL(im1[i], im2[i]); +} BOOST_AUTO_TEST_SUITE_END(); #endif // HAS_STB. From a34307e81d78a277ad76896fa1bbdb0f4ca9b42b Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Mon, 16 Sep 2019 23:53:21 +0530 Subject: [PATCH 027/265] Add serialize to Image Info class, add save and load image info for the cli --- src/mlpack/core/data/image_info.hpp | 3 ++ src/mlpack/core/data/image_info_impl.hpp | 8 ++++ .../preprocess/load_save_image_main.cpp | 42 ++++++++++++++----- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/mlpack/core/data/image_info.hpp b/src/mlpack/core/data/image_info.hpp index cd8c86ee8e..64f2e8f2bf 100644 --- a/src/mlpack/core/data/image_info.hpp +++ b/src/mlpack/core/data/image_info.hpp @@ -81,6 +81,9 @@ class ImageInfo //! Modify the image quality. size_t& Quality() { return quality; } + template + void serialize(Archive& ar, const unsigned int /* version */); + private: // To store the image width. size_t width; diff --git a/src/mlpack/core/data/image_info_impl.hpp b/src/mlpack/core/data/image_info_impl.hpp index dac80d163e..64ffefe8c8 100644 --- a/src/mlpack/core/data/image_info_impl.hpp +++ b/src/mlpack/core/data/image_info_impl.hpp @@ -63,6 +63,14 @@ inline ImageInfo::ImageInfo(const size_t width, // Do nothing. } + template + void ImageInfo::serialize(Archive& ar, const unsigned int /* version */) + { + ar & BOOST_SERIALIZATION_NVP(width); + ar & BOOST_SERIALIZATION_NVP(channels); + ar & BOOST_SERIALIZATION_NVP(height); + ar & BOOST_SERIALIZATION_NVP(quality); + } } // namespace data } // namespace mlpack diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index 3780674328..ce2919032d 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -22,6 +22,7 @@ using namespace mlpack; using namespace mlpack::util; using namespace arma; using namespace std; +using namespace mlpack::data; PROGRAM_INFO("Load Save Image", // Short description. @@ -51,8 +52,8 @@ PROGRAM_INFO("Load Save Image", PARAM_VECTOR_IN_REQ(string, "input", "Image filenames which has to " "be loaded/saved", "i"); -PARAM_INT_IN_REQ("width", "Width of the image", "W"); -PARAM_INT_IN_REQ("channel", "Number of channel", "C"); +PARAM_INT_IN("width", "Width of the image", "W", 256); +PARAM_INT_IN("channel", "Number of channel", "C", 3); PARAM_MATRIX_OUT("output", "Matrix to save images data to.", "o"); @@ -62,23 +63,41 @@ PARAM_INT_IN("quality", "Compression of the image if saved as jpg (0-100).", PARAM_FLAG("transpose", "Loaded dataset to be transposed", "t"); -PARAM_INT_IN_REQ("height", "Height of the images", "H"); +PARAM_INT_IN("height", "Height of the images", "H", 256); PARAM_FLAG("save", "Save a dataset as images", "s"); PARAM_MATRIX_IN("dataset", "Input matrix to save as images.", "I"); +// Loading/saving of a Image Info model. +PARAM_MODEL_IN(ImageInfo, "input_model", "Input Image Info model.", "m"); +PARAM_MODEL_OUT(ImageInfo, "output_model", "Output Image Info model.", "M"); static void mlpackMain() { // Parse command line options. - const int height = CLI::GetParam("height"); - const int width = CLI::GetParam("width"); - const int channel = CLI::GetParam("channel"); - const int quality = CLI::GetParam("quality"); - data::ImageInfo info(height, width, channel, quality); + ImageInfo* info; + Timer::Start("Loading/Saving Image"); + if (CLI::HasParam("input_model")) + { + info = CLI::GetParam("input_model"); + } + else + { + if(!CLI::HasParam("width") || !CLI::HasParam("height") || + !CLI::HasParam("channel")) + { + throw std::runtime_error("Please provide height, width and " + "number of channels of the images."); + } + const int height = CLI::GetParam("height"); + const int width = CLI::GetParam("width"); + const int channel = CLI::GetParam("channel"); + const int quality = CLI::GetParam("quality"); + info = new ImageInfo(height, width, channel, quality); + } const vector fileNames = CLI::GetParam >("input"); arma::mat out; if(!CLI::HasParam("save")) - data::Load(fileNames, out, info, false, !CLI::HasParam("transpose")); + Load(fileNames, out, *info, false, !CLI::HasParam("transpose")); if (CLI::HasParam("output")) CLI::GetParam("output") = std::move(out); if(CLI::HasParam("save")) @@ -88,9 +107,12 @@ static void mlpackMain() throw std::runtime_error("Please provide a input matrix to save " "images from"); } - data::Save(fileNames, CLI::GetParam("dataset"), info, false, + Save(fileNames, CLI::GetParam("dataset"), *info, false, !CLI::HasParam("transpose")); } + if (CLI::HasParam("output_model")) + CLI::GetParam("output_model") = info; + } #endif // HAS_STB. From f2e1bc547e5a7e5750983b5381984b04b836fbb5 Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Tue, 17 Sep 2019 00:02:28 +0530 Subject: [PATCH 028/265] style fixes --- src/mlpack/core/data/load_image_impl.hpp | 4 +-- .../preprocess/load_save_image_main.cpp | 25 +++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/mlpack/core/data/load_image_impl.hpp b/src/mlpack/core/data/load_image_impl.hpp index cc4a37595f..9b69dcbd92 100644 --- a/src/mlpack/core/data/load_image_impl.hpp +++ b/src/mlpack/core/data/load_image_impl.hpp @@ -77,8 +77,8 @@ bool Load(const std::string& filename, info.Channels() = tempChannels; // Copy image into armadillo Mat. - matrix = arma::conv_to >::from(arma::Mat(image, info.Width() * info.Height() * - info.Channels(), 1, true, true)); + matrix = arma::conv_to >::from(arma::Mat(image, + info.Width() * info.Height() * info.Channels(), 1, true, true)); // Free the image pointer. free(image); diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index ce2919032d..673ff08744 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -29,19 +29,19 @@ PROGRAM_INFO("Load Save Image", "A utility to load and save image dataset. This utility will allow you to " "load and save a single image or an array of images.", // Long description. - "This utility takes a image or an array of images and loads them to a" + "This utility takes a image or an array of images and loads them to a" "matrix. You can specify the height " + PRINT_PARAM_STRING("height") + " width " + PRINT_PARAM_STRING("width") + " and channel " + PRINT_PARAM_STRING("channel") + "of the images that needs to be loaded. " - "\nThere are other options too, that can be specified such as " + + "\nThere are other options too, that can be specified such as " + PRINT_PARAM_STRING("quality") + " and " + PRINT_PARAM_STRING("transpose") + ".\n\n" + - "You can also provide a dataset and save them as images using " + + "You can also provide a dataset and save them as images using " + PRINT_PARAM_STRING("dataset") + " and " + PRINT_PARAM_STRING("save") + - "as an parameter. An example to load an image : " + "\n\n" + + "as an parameter. An example to load an image : " + "\n\n" + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, "channel", 3, "output", "Y") + "\n\n" + - " An example to save an image is :" + "\n\n" + + " An example to save an image is :" + "\n\n" + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, "channel", 3, "dataset", "Y", "save", true), SEE_ALSO("@preprocess_binarize", "#preprocess_binarize"), @@ -50,12 +50,11 @@ PROGRAM_INFO("Load Save Image", //DEFINE PATAM PARAM_VECTOR_IN_REQ(string, "input", "Image filenames which has to " - "be loaded/saved", "i"); + "be loaded/saved", "i"); PARAM_INT_IN("width", "Width of the image", "W", 256); PARAM_INT_IN("channel", "Number of channel", "C", 3); - PARAM_MATRIX_OUT("output", "Matrix to save images data to.", "o"); PARAM_INT_IN("quality", "Compression of the image if saved as jpg (0-100).", @@ -81,7 +80,7 @@ static void mlpackMain() } else { - if(!CLI::HasParam("width") || !CLI::HasParam("height") || + if (!CLI::HasParam("width") || !CLI::HasParam("height") || !CLI::HasParam("channel")) { throw std::runtime_error("Please provide height, width and " @@ -96,22 +95,22 @@ static void mlpackMain() const vector fileNames = CLI::GetParam >("input"); arma::mat out; - if(!CLI::HasParam("save")) + if (!CLI::HasParam("save")) Load(fileNames, out, *info, false, !CLI::HasParam("transpose")); if (CLI::HasParam("output")) CLI::GetParam("output") = std::move(out); - if(CLI::HasParam("save")) + if (CLI::HasParam("save")) { - if(!CLI::HasParam("dataset")) + if (!CLI::HasParam("dataset")) { throw std::runtime_error("Please provide a input matrix to save " "images from"); } - Save(fileNames, CLI::GetParam("dataset"), *info, false, + Save (fileNames, CLI::GetParam ("dataset"), *info, false, !CLI::HasParam("transpose")); } if (CLI::HasParam("output_model")) - CLI::GetParam("output_model") = info; + CLI::GetParam ("output_model") = info; } From 080e619fb2661c50448a9d0be89836ed1adb3aab Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Sun, 22 Sep 2019 05:05:41 +0530 Subject: [PATCH 029/265] Rectify build failures and add serialization for Image info class --- src/mlpack/core/data/image_info.hpp | 26 ++++++-- src/mlpack/core/data/image_info_impl.hpp | 16 ++--- .../preprocess/load_save_image_main.cpp | 28 +++++---- src/mlpack/tests/image_load_test.cpp | 60 +++++++++++++++++-- 4 files changed, 96 insertions(+), 34 deletions(-) diff --git a/src/mlpack/core/data/image_info.hpp b/src/mlpack/core/data/image_info.hpp index 64f2e8f2bf..199528e20d 100644 --- a/src/mlpack/core/data/image_info.hpp +++ b/src/mlpack/core/data/image_info.hpp @@ -56,10 +56,10 @@ class ImageInfo * @param channels Number of channels in the image. * @param quality Compression of the image if saved as jpg (0 - 100). */ - ImageInfo(const size_t width = 0, - const size_t height = 0, - const size_t channels = 3, - const size_t quality = 90); + ImageInfo(const size_t& width = 0, + const size_t& height = 0, + const size_t& channels = 3, + const size_t& quality = 90); //! Get the image width. const size_t& Width() const { return width; } @@ -82,7 +82,13 @@ class ImageInfo size_t& Quality() { return quality; } template - void serialize(Archive& ar, const unsigned int /* version */); + void serialize(Archive& ar, const unsigned int /* version */) + { + ar & BOOST_SERIALIZATION_NVP(width); + ar & BOOST_SERIALIZATION_NVP(channels); + ar & BOOST_SERIALIZATION_NVP(height); + ar & BOOST_SERIALIZATION_NVP(quality); + } private: // To store the image width. @@ -98,7 +104,15 @@ class ImageInfo size_t quality; }; #else -class ImageInfo { }; +class ImageInfo +{ + public: + template + void serialize(Archive& ar, const unsigned int /* version */) + { + // Nothing to do + } +}; #endif // HAS_STB. diff --git a/src/mlpack/core/data/image_info_impl.hpp b/src/mlpack/core/data/image_info_impl.hpp index 64ffefe8c8..b4e44a23bf 100644 --- a/src/mlpack/core/data/image_info_impl.hpp +++ b/src/mlpack/core/data/image_info_impl.hpp @@ -51,10 +51,10 @@ inline bool ImageFormatSupported(const std::string& fileName, const bool save) return false; } -inline ImageInfo::ImageInfo(const size_t width, - const size_t height, - const size_t channels, - const size_t quality) : +inline ImageInfo::ImageInfo(const size_t& width, + const size_t& height, + const size_t& channels, + const size_t& quality) : width(width), height(height), channels(channels), @@ -63,14 +63,6 @@ inline ImageInfo::ImageInfo(const size_t width, // Do nothing. } - template - void ImageInfo::serialize(Archive& ar, const unsigned int /* version */) - { - ar & BOOST_SERIALIZATION_NVP(width); - ar & BOOST_SERIALIZATION_NVP(channels); - ar & BOOST_SERIALIZATION_NVP(height); - ar & BOOST_SERIALIZATION_NVP(quality); - } } // namespace data } // namespace mlpack diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index 673ff08744..9eaf538377 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -9,14 +9,10 @@ * 3-clause BSD license along with mlpack. If not, see * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ -#ifdef HAS_STB // Compile this only if stb is present. - #include #include -#include #include -#include -#include +#include using namespace mlpack; using namespace mlpack::util; @@ -24,6 +20,8 @@ using namespace arma; using namespace std; using namespace mlpack::data; +#ifdef HAS_STB // Compile this only if stb is present. + PROGRAM_INFO("Load Save Image", // Short description. "A utility to load and save image dataset. This utility will allow you to " @@ -65,6 +63,7 @@ PARAM_FLAG("transpose", "Loaded dataset to be transposed", "t"); PARAM_INT_IN("height", "Height of the images", "H", 256); PARAM_FLAG("save", "Save a dataset as images", "s"); PARAM_MATRIX_IN("dataset", "Input matrix to save as images.", "I"); + // Loading/saving of a Image Info model. PARAM_MODEL_IN(ImageInfo, "input_model", "Input Image Info model.", "m"); PARAM_MODEL_OUT(ImageInfo, "output_model", "Output Image Info model.", "M"); @@ -72,7 +71,9 @@ PARAM_MODEL_OUT(ImageInfo, "output_model", "Output Image Info model.", "M"); static void mlpackMain() { // Parse command line options. - ImageInfo* info; + + data::ImageInfo* info; + Timer::Start("Loading/Saving Image"); if (CLI::HasParam("input_model")) { @@ -86,11 +87,11 @@ static void mlpackMain() throw std::runtime_error("Please provide height, width and " "number of channels of the images."); } - const int height = CLI::GetParam("height"); - const int width = CLI::GetParam("width"); - const int channel = CLI::GetParam("channel"); - const int quality = CLI::GetParam("quality"); - info = new ImageInfo(height, width, channel, quality); + const size_t& height = CLI::GetParam("height"); + const size_t& width = CLI::GetParam("width"); + const size_t& channel = CLI::GetParam("channel"); + const size_t& quality = CLI::GetParam("quality"); + info = new data::ImageInfo(width, height, channel, quality); } const vector fileNames = CLI::GetParam >("input"); @@ -113,5 +114,8 @@ static void mlpackMain() CLI::GetParam ("output_model") = info; } +#else -#endif // HAS_STB. +static void mlpackMain() {} + +#endif // HAS_STB. \ No newline at end of file diff --git a/src/mlpack/tests/image_load_test.cpp b/src/mlpack/tests/image_load_test.cpp index 65527af0e0..9a1f689436 100644 --- a/src/mlpack/tests/image_load_test.cpp +++ b/src/mlpack/tests/image_load_test.cpp @@ -12,6 +12,9 @@ #include #include +#include +#include "test_tools.hpp" +#include "serialization.hpp" using namespace mlpack; using namespace mlpack::data; @@ -72,15 +75,15 @@ BOOST_AUTO_TEST_CASE(SaveImageAPITest) */ BOOST_AUTO_TEST_CASE(SaveImageTransposeAPITest) { - data::ImageInfo info(5, 5, 3, 90); + data::ImageInfo* info = new ImageInfo(5, 5, 3, 90); arma::Mat im1; - size_t dimension = info.Width() * info.Height() * info.Channels(); + size_t dimension = info->Width() * info->Height() * info->Channels(); im1 = arma::randi>(dimension, 1); - BOOST_REQUIRE(data::Save("APITest.bmp", im1, info, false, false) == true); + BOOST_REQUIRE(data::Save("APITest.bmp", im1, *info, false, false) == true); arma::Mat im2; - BOOST_REQUIRE(data::Load("APITest.bmp", im2, info, false, false) == true); + BOOST_REQUIRE(data::Load("APITest.bmp", im2, *info, false, false) == true); BOOST_REQUIRE_EQUAL(im1.n_cols, im2.n_cols); BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows); @@ -124,6 +127,55 @@ BOOST_AUTO_TEST_CASE(SaveImageVectorAPITest) for (size_t i = 10; i < im1.n_elem; ++i) BOOST_REQUIRE_EQUAL(im1[i], im2[i]); } + +/** + * Test if the image is saved correctly using API for arm mat. + */ +BOOST_AUTO_TEST_CASE(SaveImageMatAPITest) +{ + data::ImageInfo* info = new ImageInfo(5, 5, 3, 90); + + arma::Mat im1; + size_t dimension = info->Width() * info->Height() * info->Channels(); + im1 = arma::randi>(dimension, 1); + arma::mat input = arma::conv_to::from(im1); + BOOST_REQUIRE(Save("APITest.bmp", input, *info, false, false) == true); + + arma::mat output; + BOOST_REQUIRE(Load("APITest.bmp", output, *info, false, false) == true); + + BOOST_REQUIRE_EQUAL(input.n_cols, output.n_cols); + BOOST_REQUIRE_EQUAL(input.n_rows, output.n_rows); + for (size_t i = 10; i < input.n_elem; ++i) + BOOST_REQUIRE_CLOSE(input[i], output[i], 1e-5); +} + +/** + * Serialization test for the ImageInfo class. + */ +BOOST_AUTO_TEST_CASE(ImageInfoSerialization) +{ + + data::ImageInfo info(5, 5, 3, 90); + data::ImageInfo xmlInfo, textInfo, binaryInfo; + + SerializeObjectAll(info, xmlInfo, textInfo, binaryInfo); + + BOOST_REQUIRE_EQUAL(info.Width(), xmlInfo.Width()); + BOOST_REQUIRE_EQUAL(info.Height(), xmlInfo.Height()); + BOOST_REQUIRE_EQUAL(info.Channels(), xmlInfo.Channels()); + BOOST_REQUIRE_EQUAL(info.Quality(), xmlInfo.Quality()); + BOOST_REQUIRE_EQUAL(info.Width(), textInfo.Width()); + BOOST_REQUIRE_EQUAL(info.Height(), textInfo.Height()); + BOOST_REQUIRE_EQUAL(info.Channels(), textInfo.Channels()); + BOOST_REQUIRE_EQUAL(info.Quality(), textInfo.Quality()); + BOOST_REQUIRE_EQUAL(info.Width(), binaryInfo.Width()); + BOOST_REQUIRE_EQUAL(info.Height(), binaryInfo.Height()); + BOOST_REQUIRE_EQUAL(info.Channels(), binaryInfo.Channels()); + BOOST_REQUIRE_EQUAL(info.Quality(), binaryInfo.Quality()); + +} + BOOST_AUTO_TEST_SUITE_END(); #endif // HAS_STB. From 178d243b0abe718600c0a4daac45947af1a22607 Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Sun, 22 Sep 2019 05:17:10 +0530 Subject: [PATCH 030/265] Remove extra lines and whitespace --- src/mlpack/core/data/image_info.hpp | 2 +- src/mlpack/core/data/save_impl.hpp | 3 ++- src/mlpack/methods/preprocess/load_save_image_main.cpp | 7 +++---- src/mlpack/tests/image_load_test.cpp | 2 -- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/mlpack/core/data/image_info.hpp b/src/mlpack/core/data/image_info.hpp index 199528e20d..ae0c54dd26 100644 --- a/src/mlpack/core/data/image_info.hpp +++ b/src/mlpack/core/data/image_info.hpp @@ -87,7 +87,7 @@ class ImageInfo ar & BOOST_SERIALIZATION_NVP(width); ar & BOOST_SERIALIZATION_NVP(channels); ar & BOOST_SERIALIZATION_NVP(height); - ar & BOOST_SERIALIZATION_NVP(quality); + ar & BOOST_SERIALIZATION_NVP(quality); } private: diff --git a/src/mlpack/core/data/save_impl.hpp b/src/mlpack/core/data/save_impl.hpp index 08ae1d3807..22b9449962 100644 --- a/src/mlpack/core/data/save_impl.hpp +++ b/src/mlpack/core/data/save_impl.hpp @@ -322,7 +322,8 @@ bool Save(const std::string& filename, bool status = false; try { - arma::Mat temp = arma::conv_to >::from(matrix); + arma::Mat temp = arma::conv_to > + ::from(matrix); unsigned char* image = temp.memptr(); if ("png" == Extension(filename)) diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index 9eaf538377..45a9c79ff7 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -46,7 +46,7 @@ PROGRAM_INFO("Load Save Image", SEE_ALSO("@preprocess_describe", "#preprocess_describe"), SEE_ALSO("@preprocess_imputer", "#preprocess_imputer")); -//DEFINE PATAM +// DEFINE PARAM PARAM_VECTOR_IN_REQ(string, "input", "Image filenames which has to " "be loaded/saved", "i"); @@ -107,15 +107,14 @@ static void mlpackMain() throw std::runtime_error("Please provide a input matrix to save " "images from"); } - Save (fileNames, CLI::GetParam ("dataset"), *info, false, + Save(fileNames, CLI::GetParam ("dataset"), *info, false, !CLI::HasParam("transpose")); } if (CLI::HasParam("output_model")) CLI::GetParam ("output_model") = info; - } #else static void mlpackMain() {} -#endif // HAS_STB. \ No newline at end of file +#endif // HAS_STB. diff --git a/src/mlpack/tests/image_load_test.cpp b/src/mlpack/tests/image_load_test.cpp index 9a1f689436..f585b25e68 100644 --- a/src/mlpack/tests/image_load_test.cpp +++ b/src/mlpack/tests/image_load_test.cpp @@ -155,7 +155,6 @@ BOOST_AUTO_TEST_CASE(SaveImageMatAPITest) */ BOOST_AUTO_TEST_CASE(ImageInfoSerialization) { - data::ImageInfo info(5, 5, 3, 90); data::ImageInfo xmlInfo, textInfo, binaryInfo; @@ -173,7 +172,6 @@ BOOST_AUTO_TEST_CASE(ImageInfoSerialization) BOOST_REQUIRE_EQUAL(info.Height(), binaryInfo.Height()); BOOST_REQUIRE_EQUAL(info.Channels(), binaryInfo.Channels()); BOOST_REQUIRE_EQUAL(info.Quality(), binaryInfo.Quality()); - } BOOST_AUTO_TEST_SUITE_END(); From d9de571a4797b165d08f9982a010d319d0a7fed0 Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Sun, 22 Sep 2019 14:32:34 +0530 Subject: [PATCH 031/265] add some CLI binding test --- .../preprocess/load_save_image_main.cpp | 7 +- src/mlpack/tests/CMakeLists.txt | 11 ++- .../tests/main_tests/load_save_image_test.cpp | 97 +++++++++++++++++++ 3 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 src/mlpack/tests/main_tests/load_save_image_test.cpp diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index 45a9c79ff7..8342a98086 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -71,7 +71,6 @@ PARAM_MODEL_OUT(ImageInfo, "output_model", "Output Image Info model.", "M"); static void mlpackMain() { // Parse command line options. - data::ImageInfo* info; Timer::Start("Loading/Saving Image"); @@ -97,9 +96,11 @@ static void mlpackMain() CLI::GetParam >("input"); arma::mat out; if (!CLI::HasParam("save")) + { Load(fileNames, out, *info, false, !CLI::HasParam("transpose")); - if (CLI::HasParam("output")) - CLI::GetParam("output") = std::move(out); + if (CLI::HasParam("output")) + CLI::GetParam("output") = std::move(out); + } if (CLI::HasParam("save")) { if (!CLI::HasParam("dataset")) diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index d443e3ec24..e74297a441 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -134,6 +134,7 @@ add_executable(mlpack_test main_tests/kfn_test.cpp main_tests/knn_test.cpp main_tests/linear_regression_test.cpp + main_tests/load_save_image_test.cpp main_tests/logistic_regression_test.cpp main_tests/local_coordinate_coding_test.cpp main_tests/lmnn_test.cpp @@ -182,11 +183,11 @@ add_custom_command(TARGET mlpack_test ) add_custom_command(TARGET mlpack_test POST_BUILD - COMMAND ${CMAKE_COMMAND} -E tar xjpf mnist_first250_training_4s_and_9s.tar.bz2 - COMMAND ${CMAKE_COMMAND} -E tar xjpf digits_train.tar.bz2 - COMMAND ${CMAKE_COMMAND} -E tar xjpf digits_test.tar.bz2 - COMMAND ${CMAKE_COMMAND} -E tar xjpf digits_train_label.tar.bz2 - COMMAND ${CMAKE_COMMAND} -E tar xjpf digits_test_label.tar.bz2 + COMMAND ${CMAKE_COMMAND} -E tar xjf mnist_first250_training_4s_and_9s.tar.bz2 + COMMAND ${CMAKE_COMMAND} -E tar xjf digits_train.tar.bz2 + COMMAND ${CMAKE_COMMAND} -E tar xjf digits_test.tar.bz2 + COMMAND ${CMAKE_COMMAND} -E tar xjf digits_train_label.tar.bz2 + COMMAND ${CMAKE_COMMAND} -E tar xjf digits_test_label.tar.bz2 WORKING_DIRECTORY ${PROJECT_BINARY_DIR} ) diff --git a/src/mlpack/tests/main_tests/load_save_image_test.cpp b/src/mlpack/tests/main_tests/load_save_image_test.cpp new file mode 100644 index 0000000000..84c56b1992 --- /dev/null +++ b/src/mlpack/tests/main_tests/load_save_image_test.cpp @@ -0,0 +1,97 @@ +/** + * @file load_save_image_test.cpp + * @author Jeffin Sam + * + * Test mlpackMain() of load_save_image_main.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. + */ +#define BINDING_TYPE BINDING_TYPE_TEST + +#include +static const std::string testName = "LoadSaveImage"; + +#include +#include + +#include "test_helper.hpp" +#include +#include "../test_tools.hpp" + +using namespace mlpack; + +struct LoadSaveImageTestFixture +{ + public: + LoadSaveImageTestFixture() + { + // Cache in the options for this program. + CLI::RestoreSettings(testName); + } + + ~LoadSaveImageTestFixture() + { + // Clear the settings. + bindings::tests::CleanMemory(); + CLI::ClearSettings(); + } +}; + +BOOST_FIXTURE_TEST_SUITE(LoadSaveImageMainTest, + LoadSaveImageTestFixture); + + +/** + * Check that two different scalers give two different output. + */ + +// arma::mat testimage = arma::conv_to::from( +// arma::randi>((50 * 50 * 3), 2)); + +// Global variable to avoid creating of multiple files +arma::mat input; +arma::mat output; + +BOOST_AUTO_TEST_CASE(LoadImageTest) +{ + SetInputParam>("input", {"test_image.png", "test_image.png"}); + SetInputParam("height", 50); + SetInputParam("width", 50); + SetInputParam("channel", 3); + + mlpackMain(); + output = CLI::GetParam("output"); + BOOST_REQUIRE_EQUAL(output.n_rows, 50 * 50 * 3); // width * height * channels. + BOOST_REQUIRE_EQUAL(output.n_cols, 2); +} + +BOOST_AUTO_TEST_CASE(SaveImageTest) +{ + // SetInputParam>("input", {"test_image777.png", "test_image999.png"}); + // SetInputParam("height", 50); + // SetInputParam("width", 50); + // SetInputParam("channel", 3); + // SetInputParam("save", true); + // SetInputParam("dataset", output); + // mlpackMain(); + // std::cout<<"output given is "<>("input", {"test_image777.png", "test_image999.png"}); + // SetInputParam("height", 50); + // SetInputParam("width", 50); + // SetInputParam("channel", 3); + // SetInputParam("save", false); + + // mlpackMain(); + // input = CLI::GetParam("output"); + // std::cout<<"outputnew is "< Date: Mon, 23 Sep 2019 13:52:11 +0530 Subject: [PATCH 032/265] Tried rectifyin test --- .../tests/main_tests/load_save_image_test.cpp | 66 +++++++++++++------ 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/src/mlpack/tests/main_tests/load_save_image_test.cpp b/src/mlpack/tests/main_tests/load_save_image_test.cpp index 84c56b1992..3a9c62e129 100644 --- a/src/mlpack/tests/main_tests/load_save_image_test.cpp +++ b/src/mlpack/tests/main_tests/load_save_image_test.cpp @@ -66,32 +66,56 @@ BOOST_AUTO_TEST_CASE(LoadImageTest) output = CLI::GetParam("output"); BOOST_REQUIRE_EQUAL(output.n_rows, 50 * 50 * 3); // width * height * channels. BOOST_REQUIRE_EQUAL(output.n_cols, 2); + + // SetInputParam>("input", {"test_image777.png", "test_image999.png"}); + // SetInputParam("height", 50); + // SetInputParam("width", 50); + // SetInputParam("channel", 3); + // SetInputParam("save", true); + // SetInputParam("dataset", output); + // mlpackMain(); + // std::cout<<"output given is "<>("input", {"test_image777.png", "test_image999.png"}); + // SetInputParam("height", 50); + // SetInputParam("width", 50); + // SetInputParam("channel", 3); + + // mlpackMain(); + // input = CLI::GetParam("output"); + // std::cout<<"outputnew is "<>("input", {"test_image777.png", "test_image999.png"}); - // SetInputParam("height", 50); - // SetInputParam("width", 50); - // SetInputParam("channel", 3); - // SetInputParam("save", true); - // SetInputParam("dataset", output); - // mlpackMain(); - // std::cout<<"output given is "<>("input", {"test_image777.png", "test_image999.png"}); - // SetInputParam("height", 50); - // SetInputParam("width", 50); - // SetInputParam("channel", 3); - // SetInputParam("save", false); + // std::cout<<"output entry is "<>("input", {"test_image777.png", "test_image999.png"}); + // SetInputParam("height", 50); + // SetInputParam("width", 50); + // SetInputParam("channel", 3); + // SetInputParam("save", true); + // SetInputParam("dataset", output); + // mlpackMain(); + // std::cout<<"output given is "<>("input", {"test_image777.png", "test_image999.png"}); + // SetInputParam("height", 50); + // SetInputParam("width", 50); + // SetInputParam("channel", 3); + + // mlpackMain(); + // input = CLI::GetParam("output"); + // std::cout<<"outputnew is "<("output"); - // std::cout<<"outputnew is "< Date: Mon, 23 Sep 2019 14:19:53 +0530 Subject: [PATCH 033/265] correctly build .so file --- src/mlpack/methods/preprocess/load_save_image_main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index 8342a98086..2c696e91d6 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -20,7 +20,6 @@ using namespace arma; using namespace std; using namespace mlpack::data; -#ifdef HAS_STB // Compile this only if stb is present. PROGRAM_INFO("Load Save Image", // Short description. @@ -68,6 +67,8 @@ PARAM_MATRIX_IN("dataset", "Input matrix to save as images.", "I"); PARAM_MODEL_IN(ImageInfo, "input_model", "Input Image Info model.", "m"); PARAM_MODEL_OUT(ImageInfo, "output_model", "Output Image Info model.", "M"); +#ifdef HAS_STB // Compile this only if stb is present. + static void mlpackMain() { // Parse command line options. From e15d479348d76d6961d883e6b5cab9b93dcaae9b Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Mon, 23 Sep 2019 14:36:36 +0530 Subject: [PATCH 034/265] add some more cli binding test --- .../tests/main_tests/load_save_image_test.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/mlpack/tests/main_tests/load_save_image_test.cpp b/src/mlpack/tests/main_tests/load_save_image_test.cpp index 3a9c62e129..d6d73614a9 100644 --- a/src/mlpack/tests/main_tests/load_save_image_test.cpp +++ b/src/mlpack/tests/main_tests/load_save_image_test.cpp @@ -118,4 +118,49 @@ BOOST_AUTO_TEST_CASE(SaveImageTest) } +/** + * Check Saved model is working. + */ +BOOST_AUTO_TEST_CASE(SavedModelTest) +{ + SetInputParam>("input", {"test_image.png", "test_image.png"}); + SetInputParam("height", 50); + SetInputParam("width", 50); + SetInputParam("channel", 3); + + mlpackMain(); + arma::mat randomOutput = CLI::GetParam("output"); + + SetInputParam>("input", {"test_image.png", "test_image.png"}); + SetInputParam("input_model", + CLI::GetParam("output_model")); + + mlpackMain(); + arma::mat savedOutput = CLI::GetParam("output"); + CheckMatrices(randomOutput, savedOutput); +} + +/** + * Check transpose option give two different output. + */ +BOOST_AUTO_TEST_CASE(TransposeTest) +{ + SetInputParam>("input", {"test_image.png", "test_image.png"}); + SetInputParam("height", 50); + SetInputParam("width", 50); + SetInputParam("channel", 3); + + mlpackMain(); + arma::mat normalOutput = CLI::GetParam("output"); + + SetInputParam>("input", {"test_image.png", "test_image.png"}); + SetInputParam("input_model", + CLI::GetParam("output_model")); + SetInputParam("transpose", true); + mlpackMain(); + arma::mat transposeOutput = CLI::GetParam("output"); + + CheckMatricesNotEqual(normalOutput, transposeOutput); +} + BOOST_AUTO_TEST_SUITE_END(); From 9b8b703f17235f7d0da117a50e0e810b27d4aebd Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Mon, 23 Sep 2019 19:04:25 +0530 Subject: [PATCH 035/265] Just for @rcurtin or @zoq to debug :) --- src/mlpack/methods/preprocess/load_save_image_main.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index 2c696e91d6..36240ed17f 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -117,6 +117,13 @@ static void mlpackMain() } #else -static void mlpackMain() {} +static void mlpackMain() +{ + if (!CLI::HasParam("input") || !CLI::HasParam("width") || + !CLI::HasParam("height") || !CLI::HasParam("channel")) + { + throw std::runtime_error("Are you sure We are on correct path"); + } +} #endif // HAS_STB. From a7d923d75549b3a28a3822f06a5a27785589550b Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Mon, 23 Sep 2019 23:39:47 +0530 Subject: [PATCH 036/265] Resolved python binding , add Variable in setup.py.in --- CMakeLists.txt | 2 ++ src/mlpack/bindings/python/setup.py.in | 7 ++++++- src/mlpack/methods/preprocess/load_save_image_main.cpp | 9 +-------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c60590cf8e..bfa79c5877 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -344,6 +344,7 @@ if (NOT STB_IMAGE_FOUND) install(FILES ${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(FILES ${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image_write.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) add_definitions(-DHAS_STB) + set(STB_AVAILABLE "1") else () list(GET STB_IMAGE_DOWNLOAD_STATUS_LIST 1 STB_DOWNLOAD_ERROR) message(WARNING @@ -357,6 +358,7 @@ else () # Already has STB installed. add_definitions(-DHAS_STB) set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} ${STB_IMAGE_INCLUDE_DIR}) + set(STB_AVAILABLE "1") endif () diff --git a/src/mlpack/bindings/python/setup.py.in b/src/mlpack/bindings/python/setup.py.in index 475afd85f1..793c3a4338 100644 --- a/src/mlpack/bindings/python/setup.py.in +++ b/src/mlpack/bindings/python/setup.py.in @@ -29,13 +29,18 @@ if not '${OpenMP_CXX_FLAGS}': else: extra_link_args=['${OpenMP_CXX_FLAGS}'] +if '${STB_AVAILABLE}': + extra_args = ['-DHAS_STB'] +else : + extra_args = [] + # Only build the extensions if we are asked to. if os.getenv('NO_BUILD') == '1': modules = [] else: cxx_flags = '${CMAKE_CXX_FLAGS}'.strip() cxx_flags = re.sub(' +', ' ', cxx_flags) - extra_args = ['-DBINDING_TYPE=BINDING_TYPE_PYX', + extra_args = extra_args + ['-DBINDING_TYPE=BINDING_TYPE_PYX', '-std=c++11'] + cxx_flags.split(' ') # This is used for parallel builds; CMake will set PYX_TO_BUILD accordingly. if module is not None: diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index 36240ed17f..2c696e91d6 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -117,13 +117,6 @@ static void mlpackMain() } #else -static void mlpackMain() -{ - if (!CLI::HasParam("input") || !CLI::HasParam("width") || - !CLI::HasParam("height") || !CLI::HasParam("channel")) - { - throw std::runtime_error("Are you sure We are on correct path"); - } -} +static void mlpackMain() {} #endif // HAS_STB. From f54b330b3d6b4e01a9b95e875f87e3e6d3e14ebb Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Tue, 24 Sep 2019 09:57:48 +0530 Subject: [PATCH 037/265] rectified test for load_save_image --- .../preprocess/load_save_image_main.cpp | 13 ++ .../tests/main_tests/load_save_image_test.cpp | 113 +++++++++--------- 2 files changed, 69 insertions(+), 57 deletions(-) diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index 2c696e91d6..b9f5867520 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -87,6 +87,19 @@ static void mlpackMain() throw std::runtime_error("Please provide height, width and " "number of channels of the images."); } + // Positive value for width. + RequireParamValue("width", [](int x) { return x >= 0;}, true, + "width must be Positive"); + // Positive value for height. + RequireParamValue("height", [](int x) { return x >= 0;}, true, + "height must be Positive"); + // Positive value for channel. + RequireParamValue("channel", [](int x) { return x >= 0;}, true, + "channel must be Positive"); + // Positive value for quality. + RequireParamValue("quality", [](int x) { return x >= 0;}, true, + "quality must be Positive"); + const size_t& height = CLI::GetParam("height"); const size_t& width = CLI::GetParam("width"); const size_t& channel = CLI::GetParam("channel"); diff --git a/src/mlpack/tests/main_tests/load_save_image_test.cpp b/src/mlpack/tests/main_tests/load_save_image_test.cpp index d6d73614a9..f25d6a3f66 100644 --- a/src/mlpack/tests/main_tests/load_save_image_test.cpp +++ b/src/mlpack/tests/main_tests/load_save_image_test.cpp @@ -43,18 +43,6 @@ struct LoadSaveImageTestFixture BOOST_FIXTURE_TEST_SUITE(LoadSaveImageMainTest, LoadSaveImageTestFixture); - -/** - * Check that two different scalers give two different output. - */ - -// arma::mat testimage = arma::conv_to::from( -// arma::randi>((50 * 50 * 3), 2)); - -// Global variable to avoid creating of multiple files -arma::mat input; -arma::mat output; - BOOST_AUTO_TEST_CASE(LoadImageTest) { SetInputParam>("input", {"test_image.png", "test_image.png"}); @@ -63,59 +51,40 @@ BOOST_AUTO_TEST_CASE(LoadImageTest) SetInputParam("channel", 3); mlpackMain(); - output = CLI::GetParam("output"); - BOOST_REQUIRE_EQUAL(output.n_rows, 50 * 50 * 3); // width * height * channels. + arma::mat output = CLI::GetParam("output"); + // width * height * channels. + BOOST_REQUIRE_EQUAL(output.n_rows, 50 * 50 * 3); BOOST_REQUIRE_EQUAL(output.n_cols, 2); - - // SetInputParam>("input", {"test_image777.png", "test_image999.png"}); - // SetInputParam("height", 50); - // SetInputParam("width", 50); - // SetInputParam("channel", 3); - // SetInputParam("save", true); - // SetInputParam("dataset", output); - // mlpackMain(); - // std::cout<<"output given is "<>("input", {"test_image777.png", "test_image999.png"}); - // SetInputParam("height", 50); - // SetInputParam("width", 50); - // SetInputParam("channel", 3); - - // mlpackMain(); - // input = CLI::GetParam("output"); - // std::cout<<"outputnew is "<::from( + arma::randi>((5 * 5 * 3), 2)); + SetInputParam>("input", {"test_image777.png", + "test_image999.png"}); + SetInputParam("height", 5); + SetInputParam("width", 5); + SetInputParam("channel", 3); + SetInputParam("save", true); + SetInputParam("dataset", testimage); + mlpackMain(); - // SetInputParam>("input", {"test_image777.png", "test_image999.png"}); - // SetInputParam("height", 50); - // SetInputParam("width", 50); - // SetInputParam("channel", 3); - // SetInputParam("save", true); - // SetInputParam("dataset", output); - // mlpackMain(); - // std::cout<<"output given is "<>("input", {"test_image777.png", "test_image999.png"}); - // SetInputParam("height", 50); - // SetInputParam("width", 50); - // SetInputParam("channel", 3); - - // mlpackMain(); - // input = CLI::GetParam("output"); - // std::cout<<"outputnew is "<>("input", {"test_image777.png", + "test_image999.png"}); + SetInputParam("height", 5); + SetInputParam("width", 5); + SetInputParam("channel", 3); + mlpackMain(); + arma::mat output = CLI::GetParam("output"); + BOOST_REQUIRE_EQUAL(output.n_rows, 5 * 5 * 3); + BOOST_REQUIRE_EQUAL(output.n_cols, 2); + for (size_t i = 0; i < output.n_elem; ++i) + BOOST_REQUIRE_CLOSE(testimage[i], output[i], 1e-5); } /** @@ -163,4 +132,34 @@ BOOST_AUTO_TEST_CASE(TransposeTest) CheckMatricesNotEqual(normalOutput, transposeOutput); } +/** + * Check whether binding throws error if height, width or channel are not + * specified. + */ +BOOST_AUTO_TEST_CASE(IncompleteTest) +{ + SetInputParam>("input", {"test_image.png", "test_image.png"}); + SetInputParam("height", 50); + SetInputParam("width", 50); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; +} + +/** + * Check for invalid height values. + */ +BOOST_AUTO_TEST_CASE(InvalidInputTest) +{ + SetInputParam>("input", {"test_image.png", "test_image.png"}); + SetInputParam("height", -50); + SetInputParam("width", 50); + SetInputParam("channel", 3); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; +} + BOOST_AUTO_TEST_SUITE_END(); From 27365b28e13b592c75ad5b9167fa9b1769947527 Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Wed, 25 Sep 2019 17:52:21 +0530 Subject: [PATCH 038/265] update documentation --- doc/tutorials/image/image.txt | 13 ++++++++----- src/mlpack/core/data/image_info.hpp | 8 ++++---- src/mlpack/core/data/image_info_impl.hpp | 8 ++++---- .../methods/preprocess/load_save_image_main.cpp | 8 ++++++-- src/mlpack/tests/image_load_test.cpp | 14 +++++++------- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/doc/tutorials/image/image.txt b/doc/tutorials/image/image.txt index 8a635bf260..1397d4f8dc 100644 --- a/doc/tutorials/image/image.txt +++ b/doc/tutorials/image/image.txt @@ -46,7 +46,6 @@ ImageInfo class contains the metadata of the images. const size_t channels); @endcode Other public memebers include: - - flipVertical Flip the image vertical upon loading. - quality Compression of the image if saved as jpg (0-100). @section load_api_imagetut Load @@ -61,7 +60,8 @@ Standalone loading of images. * @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). - * @param transpose If true, transpose the matrix after loading. + * @param transpose If true, Filps the image, same as transposing the + * matrix after loading. * @return Boolean value indicating success or failure of load. */ template @@ -97,7 +97,8 @@ Loading multiple images: * @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). - * @param transpose If true, transpose the matrix after loading. + * @param transpose If true, Filps the image, same as transposing the + * matrix after loading. * @return Boolean value indicating success or failure of load. */ template @@ -129,7 +130,8 @@ Saving one image: * @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). - * @param transpose If true, transpose the matrix after loading. + * @param transpose If true, Filps the image, same as transposing the + * matrix after loading. * @return Boolean value indicating success or failure of load. */ template @@ -160,7 +162,8 @@ Saving multiple images: * @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). - * @param transpose If true, transpose the matrix after loading. + * @param transpose If true, Filps the image, same as transposing the + * matrix after loading. * @return Boolean value indicating success or failure of load. */ template diff --git a/src/mlpack/core/data/image_info.hpp b/src/mlpack/core/data/image_info.hpp index ae0c54dd26..315d7c3afd 100644 --- a/src/mlpack/core/data/image_info.hpp +++ b/src/mlpack/core/data/image_info.hpp @@ -56,10 +56,10 @@ class ImageInfo * @param channels Number of channels in the image. * @param quality Compression of the image if saved as jpg (0 - 100). */ - ImageInfo(const size_t& width = 0, - const size_t& height = 0, - const size_t& channels = 3, - const size_t& quality = 90); + ImageInfo(const size_t width = 0, + const size_t height = 0, + const size_t channels = 3, + const size_t quality = 90); //! Get the image width. const size_t& Width() const { return width; } diff --git a/src/mlpack/core/data/image_info_impl.hpp b/src/mlpack/core/data/image_info_impl.hpp index b4e44a23bf..dac80d163e 100644 --- a/src/mlpack/core/data/image_info_impl.hpp +++ b/src/mlpack/core/data/image_info_impl.hpp @@ -51,10 +51,10 @@ inline bool ImageFormatSupported(const std::string& fileName, const bool save) return false; } -inline ImageInfo::ImageInfo(const size_t& width, - const size_t& height, - const size_t& channels, - const size_t& quality) : +inline ImageInfo::ImageInfo(const size_t width, + const size_t height, + const size_t channels, + const size_t quality) : width(width), height(height), channels(channels), diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index b9f5867520..c6b122312f 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -2,7 +2,7 @@ * @file load_save_image_main.cpp * @author Jeffin Sam * - * A CLI executable to load and image dataset. + * A CLI executable to load and save a image dataset. * * 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 @@ -40,7 +40,11 @@ PROGRAM_INFO("Load Save Image", "channel", 3, "output", "Y") + "\n\n" + " An example to save an image is :" + "\n\n" + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, - "channel", 3, "dataset", "Y", "save", true), + "channel", 3, "dataset", "Y", "save", true) + "\n\n" + + " An example to load an image and also flipping it while loading is :" + + "\n\n" + + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, + "channel", 3, "output", "Y", "transpose", true), SEE_ALSO("@preprocess_binarize", "#preprocess_binarize"), SEE_ALSO("@preprocess_describe", "#preprocess_describe"), SEE_ALSO("@preprocess_imputer", "#preprocess_imputer")); diff --git a/src/mlpack/tests/image_load_test.cpp b/src/mlpack/tests/image_load_test.cpp index f585b25e68..0acbee14bb 100644 --- a/src/mlpack/tests/image_load_test.cpp +++ b/src/mlpack/tests/image_load_test.cpp @@ -66,7 +66,7 @@ BOOST_AUTO_TEST_CASE(SaveImageAPITest) BOOST_REQUIRE_EQUAL(im1.n_cols, im2.n_cols); BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows); - for (size_t i = 10; i < im1.n_elem; ++i) + for (size_t i = 0; i < im1.n_elem; ++i) BOOST_REQUIRE_EQUAL(im1[i], im2[i]); } @@ -87,7 +87,7 @@ BOOST_AUTO_TEST_CASE(SaveImageTransposeAPITest) BOOST_REQUIRE_EQUAL(im1.n_cols, im2.n_cols); BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows); - for (size_t i = 10; i < im1.n_elem; ++i) + for (size_t i = 0; i < im1.n_elem; ++i) BOOST_REQUIRE_EQUAL(im1[i], im2[i]); } @@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(LoadVectorImageAPITest) */ BOOST_AUTO_TEST_CASE(SaveImageVectorAPITest) { - data::ImageInfo info(5, 5, 3, 90); + data::ImageInfo info(5, 5, 3); arma::Mat im1; size_t dimension = info.Width() * info.Height() * info.Channels(); @@ -124,16 +124,16 @@ BOOST_AUTO_TEST_CASE(SaveImageVectorAPITest) BOOST_REQUIRE_EQUAL(im1.n_cols, im2.n_cols); BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows); - for (size_t i = 10; i < im1.n_elem; ++i) + for (size_t i = 0; i < im1.n_elem; ++i) BOOST_REQUIRE_EQUAL(im1[i], im2[i]); } /** - * Test if the image is saved correctly using API for arm mat. + * Test if the image is saved correctly using API for arma mat. */ BOOST_AUTO_TEST_CASE(SaveImageMatAPITest) { - data::ImageInfo* info = new ImageInfo(5, 5, 3, 90); + data::ImageInfo* info = new ImageInfo(5, 5, 3); arma::Mat im1; size_t dimension = info->Width() * info->Height() * info->Channels(); @@ -146,7 +146,7 @@ BOOST_AUTO_TEST_CASE(SaveImageMatAPITest) BOOST_REQUIRE_EQUAL(input.n_cols, output.n_cols); BOOST_REQUIRE_EQUAL(input.n_rows, output.n_rows); - for (size_t i = 10; i < input.n_elem; ++i) + for (size_t i = 0; i < input.n_elem; ++i) BOOST_REQUIRE_CLOSE(input[i], output[i], 1e-5); } From 357e57cdff3e33c2b21453f01e635039986acb82 Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Wed, 25 Sep 2019 18:03:39 +0530 Subject: [PATCH 039/265] remove a white space --- src/mlpack/methods/preprocess/load_save_image_main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index c6b122312f..19f5d3b2c7 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -40,7 +40,7 @@ PROGRAM_INFO("Load Save Image", "channel", 3, "output", "Y") + "\n\n" + " An example to save an image is :" + "\n\n" + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, - "channel", 3, "dataset", "Y", "save", true) + "\n\n" + + "channel", 3, "dataset", "Y", "save", true) + "\n\n" + " An example to load an image and also flipping it while loading is :" + "\n\n" + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, From 8c412673aaa648ba509d0235e7e1245e6e91417b Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Thu, 3 Oct 2019 12:37:17 +0530 Subject: [PATCH 040/265] add serialization function which was deleted when resolving merge conflicts --- src/mlpack/core/data/image_info.hpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/mlpack/core/data/image_info.hpp b/src/mlpack/core/data/image_info.hpp index e7f444447c..16f69922ac 100644 --- a/src/mlpack/core/data/image_info.hpp +++ b/src/mlpack/core/data/image_info.hpp @@ -87,6 +87,15 @@ class ImageInfo //! Modify the image quality. size_t& Quality() { return quality; } + template + void serialize(Archive& ar, const unsigned int /* version */) + { + ar & BOOST_SERIALIZATION_NVP(width); + ar & BOOST_SERIALIZATION_NVP(channels); + ar & BOOST_SERIALIZATION_NVP(height); + ar & BOOST_SERIALIZATION_NVP(quality); + } + private: // To store the image width. size_t width; From 99cf51253c31843ae6615e685f2e5cb079c150ca Mon Sep 17 00:00:00 2001 From: jeffinsam Date: Mon, 4 Nov 2019 20:36:21 +0530 Subject: [PATCH 041/265] correctly appending the list --- src/mlpack/bindings/python/setup.py.in | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/mlpack/bindings/python/setup.py.in b/src/mlpack/bindings/python/setup.py.in index 06243f7ada..1e341844e1 100644 --- a/src/mlpack/bindings/python/setup.py.in +++ b/src/mlpack/bindings/python/setup.py.in @@ -56,14 +56,12 @@ if os.getenv('NO_BUILD') == '1': else: cxx_flags = '${CMAKE_CXX_FLAGS}'.strip() cxx_flags = re.sub(' +', ' ', cxx_flags) - extra_args = extra_args + ['-DBINDING_TYPE=BINDING_TYPE_PYX', - '-std=c++11'] + cxx_flags.split(' ') if cxx_flags: - extra_args = ['-DBINDING_TYPE=BINDING_TYPE_PYX', + extra_args = extra_args + ['-DBINDING_TYPE=BINDING_TYPE_PYX', '-std=c++11', '${OpenMP_CXX_FLAGS}'] + cxx_flags.split(' ') else: - extra_args = ['-DBINDING_TYPE=BINDING_TYPE_PYX', + extra_args = extra_args + ['-DBINDING_TYPE=BINDING_TYPE_PYX', '-std=c++11', '${OpenMP_CXX_FLAGS}'] From e0ef23c796580ae4604d4b1f5e9418e7144d564d Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Thu, 30 Jan 2020 15:52:26 +0530 Subject: [PATCH 042/265] Improvements and adding test --- src/mlpack/methods/preprocess/CMakeLists.txt | 8 ++-- .../preprocess/load_save_image_main.cpp | 32 ++++++++----- src/mlpack/tests/image_load_test.cpp | 26 +++++----- .../tests/main_tests/load_save_image_test.cpp | 48 +++++++++++++++++++ 4 files changed, 85 insertions(+), 29 deletions(-) diff --git a/src/mlpack/methods/preprocess/CMakeLists.txt b/src/mlpack/methods/preprocess/CMakeLists.txt index 56cdb1846c..39810a1c87 100644 --- a/src/mlpack/methods/preprocess/CMakeLists.txt +++ b/src/mlpack/methods/preprocess/CMakeLists.txt @@ -42,6 +42,8 @@ add_cli_executable(preprocess_scale) add_python_binding(preprocess_scale) add_markdown_docs(preprocess_scale "cli;python" "preprocessing") -add_cli_executable(load_save_image) -add_python_binding(load_save_image) -add_markdown_docs(load_save_image "cli;python" "preprocessing") +if (NOT HAS_STB) + add_cli_executable(load_save_image) + add_python_binding(load_save_image) + add_markdown_docs(load_save_image "cli;python" "preprocessing") +endif () \ No newline at end of file diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/load_save_image_main.cpp index 31454cefcb..8673861e42 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/load_save_image_main.cpp @@ -23,24 +23,30 @@ using namespace mlpack::data; PROGRAM_INFO("Load Save Image", // Short description. - "A utility to load and save image dataset. This utility will allow you to " - "load and save a single image or an list of images.", + "A utility to load an image or set of images into a single dataset that" + "can then be used by other mlpack methods and utilities. This can also" + "unpack an image dataset into individual files.", // Long description. "This utility takes a image or an array of images and loads them to a" " matrix. You can specify the height " + PRINT_PARAM_STRING("height") + " width " + PRINT_PARAM_STRING("width") + " and channel " + PRINT_PARAM_STRING("channel") + " of the images that needs to be loaded. " - "\nThere are other options too, that can be specified such as " + + "\n" + "There are other options too, that can be specified such as " + PRINT_PARAM_STRING("quality") + " and " + PRINT_PARAM_STRING("transpose") + ".\n\n" + "You can also provide a dataset and save them as images using " + PRINT_PARAM_STRING("dataset") + " and " + PRINT_PARAM_STRING("save") + - " as an parameter. An example to load an image : " + "\n\n" + + " as an parameter. An example to load an image : " + + "\n\n" + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, - "channel", 3, "output", "Y") + "\n\n" + - " An example to save an image is :" + "\n\n" + + "channel", 3, "output", "Y") + + "\n\n" + + " An example to save an image is :" + + "\n\n" + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, - "channel", 3, "dataset", "Y", "save", true) + "\n\n" + + "channel", 3, "dataset", "Y", "save", true) + + "\n\n" + " An example to load an image and also flipping it while loading is :" + "\n\n" + PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, @@ -50,8 +56,8 @@ PROGRAM_INFO("Load Save Image", SEE_ALSO("@preprocess_imputer", "#preprocess_imputer")); // DEFINE PARAM -PARAM_VECTOR_IN_REQ(string, "input", "Image filenames which has to " - "be loaded/saved", "i"); +PARAM_VECTOR_IN_REQ(string, "input", "Image filenames which have to " + "be loaded/saved.", "i"); PARAM_INT_IN("width", "Width of the image", "W", 256); PARAM_INT_IN("channel", "Number of channel", "C", 3); @@ -93,16 +99,16 @@ static void mlpackMain() } // Positive value for width. RequireParamValue("width", [](int x) { return x >= 0;}, true, - "width must be Positive"); + "width must be positive"); // Positive value for height. RequireParamValue("height", [](int x) { return x >= 0;}, true, - "height must be Positive"); + "height must be positive"); // Positive value for channel. RequireParamValue("channel", [](int x) { return x >= 0;}, true, - "channel must be Positive"); + "channel must be positive"); // Positive value for quality. RequireParamValue("quality", [](int x) { return x >= 0;}, true, - "quality must be Positive"); + "quality must be positive"); const size_t& height = CLI::GetParam("height"); const size_t& width = CLI::GetParam("width"); diff --git a/src/mlpack/tests/image_load_test.cpp b/src/mlpack/tests/image_load_test.cpp index 0acbee14bb..83a46b48ad 100644 --- a/src/mlpack/tests/image_load_test.cpp +++ b/src/mlpack/tests/image_load_test.cpp @@ -75,15 +75,15 @@ BOOST_AUTO_TEST_CASE(SaveImageAPITest) */ BOOST_AUTO_TEST_CASE(SaveImageTransposeAPITest) { - data::ImageInfo* info = new ImageInfo(5, 5, 3, 90); + data::ImageInfo info(5, 5, 3, 90); arma::Mat im1; - size_t dimension = info->Width() * info->Height() * info->Channels(); + size_t dimension = info.Width() * info.Height() * info.Channels(); im1 = arma::randi>(dimension, 1); - BOOST_REQUIRE(data::Save("APITest.bmp", im1, *info, false, false) == true); + BOOST_REQUIRE(data::Save("APITest.bmp", im1, info, false, false) == true); arma::Mat im2; - BOOST_REQUIRE(data::Load("APITest.bmp", im2, *info, false, false) == true); + BOOST_REQUIRE(data::Load("APITest.bmp", im2, info, false, false) == true); BOOST_REQUIRE_EQUAL(im1.n_cols, im2.n_cols); BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows); @@ -99,8 +99,8 @@ BOOST_AUTO_TEST_CASE(LoadVectorImageAPITest) { arma::Mat matrix; data::ImageInfo info; - std::vector file = {"test_image.png", "test_image.png"}; - BOOST_REQUIRE(data::Load(file, matrix, info, false, + std::vector files = {"test_image.png", "test_image.png"}; + BOOST_REQUIRE(data::Load(files, matrix, info, false, true) == true); BOOST_REQUIRE_EQUAL(matrix.n_rows, 50 * 50 * 3); // width * height * channels. BOOST_REQUIRE_EQUAL(matrix.n_cols, 2); @@ -116,11 +116,11 @@ BOOST_AUTO_TEST_CASE(SaveImageVectorAPITest) arma::Mat im1; size_t dimension = info.Width() * info.Height() * info.Channels(); im1 = arma::randi>(dimension, 2); - std::vector file = {"APITest1.bmp", "APITest2.bmp"}; - BOOST_REQUIRE(data::Save(file, im1, info, false, false) == true); + std::vector files = {"APITest1.bmp", "APITest2.bmp"}; + BOOST_REQUIRE(data::Save(files, im1, info, false, false) == true); arma::Mat im2; - BOOST_REQUIRE(data::Load(file, im2, info, false, false) == true); + BOOST_REQUIRE(data::Load(files, im2, info, false, false) == true); BOOST_REQUIRE_EQUAL(im1.n_cols, im2.n_cols); BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows); @@ -133,16 +133,16 @@ BOOST_AUTO_TEST_CASE(SaveImageVectorAPITest) */ BOOST_AUTO_TEST_CASE(SaveImageMatAPITest) { - data::ImageInfo* info = new ImageInfo(5, 5, 3); + data::ImageInfo info(5, 5, 3); arma::Mat im1; - size_t dimension = info->Width() * info->Height() * info->Channels(); + size_t dimension = info.Width() * info.Height() * info.Channels(); im1 = arma::randi>(dimension, 1); arma::mat input = arma::conv_to::from(im1); - BOOST_REQUIRE(Save("APITest.bmp", input, *info, false, false) == true); + BOOST_REQUIRE(Save("APITest.bmp", input, info, false, false) == true); arma::mat output; - BOOST_REQUIRE(Load("APITest.bmp", output, *info, false, false) == true); + BOOST_REQUIRE(Load("APITest.bmp", output, info, false, false) == true); BOOST_REQUIRE_EQUAL(input.n_cols, output.n_cols); BOOST_REQUIRE_EQUAL(input.n_rows, output.n_rows); diff --git a/src/mlpack/tests/main_tests/load_save_image_test.cpp b/src/mlpack/tests/main_tests/load_save_image_test.cpp index f25d6a3f66..016b3424e3 100644 --- a/src/mlpack/tests/main_tests/load_save_image_test.cpp +++ b/src/mlpack/tests/main_tests/load_save_image_test.cpp @@ -130,6 +130,8 @@ BOOST_AUTO_TEST_CASE(TransposeTest) arma::mat transposeOutput = CLI::GetParam("output"); CheckMatricesNotEqual(normalOutput, transposeOutput); + BOOST_REQUIRE_EQUAL(normalOutput.n_rows, transposeOutput.n_rows); + BOOST_REQUIRE_EQUAL(normalOutput.n_cols, transposeOutput.n_cols); } /** @@ -162,4 +164,50 @@ BOOST_AUTO_TEST_CASE(InvalidInputTest) Log::Fatal.ignoreInput = false; } +/** + * Check for invalid width values. + */ +BOOST_AUTO_TEST_CASE(InvalidWidthTest) +{ + SetInputParam>("input", {"test_image.png", "test_image.png"}); + SetInputParam("height", 50); + SetInputParam("width", -50); + SetInputParam("channel", 3); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; +} + +/** + * Check for invalid channel values. + */ +BOOST_AUTO_TEST_CASE(InvalidChannelTest) +{ + SetInputParam>("input", {"test_image.png", "test_image.png"}); + SetInputParam("height", 50); + SetInputParam("width", 50); + SetInputParam("channel", -1); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; +} + +/** + * Check for invalid input values. + */ +BOOST_AUTO_TEST_CASE(EmptyinputTest) +{ + SetInputParam>("input", {}); + SetInputParam("height", 50); + SetInputParam("width", 50); + SetInputParam("channel", 50); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; +} + + BOOST_AUTO_TEST_SUITE_END(); From e36fd142674d022983fc0dfb977f5104dc83771d Mon Sep 17 00:00:00 2001 From: Mrityunjay Tripathi Date: Tue, 11 Feb 2020 21:37:18 +0530 Subject: [PATCH 043/265] adding huber loss --- .../methods/ann/loss_functions/CMakeLists.txt | 2 + .../methods/ann/loss_functions/huber_loss.hpp | 106 ++++++++++++++++++ .../ann/loss_functions/huber_loss_impl.hpp | 85 ++++++++++++++ src/mlpack/tests/loss_functions_test.cpp | 28 +++++ 4 files changed, 221 insertions(+) create mode 100644 src/mlpack/methods/ann/loss_functions/huber_loss.hpp create mode 100644 src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp diff --git a/src/mlpack/methods/ann/loss_functions/CMakeLists.txt b/src/mlpack/methods/ann/loss_functions/CMakeLists.txt index afca2ea0fd..89fa459c4d 100644 --- a/src/mlpack/methods/ann/loss_functions/CMakeLists.txt +++ b/src/mlpack/methods/ann/loss_functions/CMakeLists.txt @@ -7,6 +7,8 @@ set(SOURCES dice_loss_impl.hpp earth_mover_distance.hpp earth_mover_distance_impl.hpp + huber_loss.hpp + huber_loss_impl.hpp kl_divergence.hpp kl_divergence_impl.hpp mean_squared_error.hpp diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp new file mode 100644 index 0000000000..448a2838ed --- /dev/null +++ b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp @@ -0,0 +1,106 @@ +/** + * @file huber_loss.hpp + * @author Mrityunjay Tripathi + * + * Definition of the Huber Loss function. + * + * 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_METHODS_ANN_LOSS_FUNCTION_HUBER_LOSS_HPP +#define MLPACK_METHODS_ANN_LOSS_FUNCTION_HUBER_LOSS_HPP + +#include + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +/** + * The Huber loss is a loss function used in robust regression, + * that is less sensitive to outliers in data than the squared error loss. + * This function is quadratic for small values of `y - f(x)`, + * and linear for large values, with equal values and slopes of the different + * sections at the two points where `|y - f(x)| = delta`. + * + * @tparam ActivationFunction Activation function used for the embedding layer. + * @tparam InputDataType Type of the input data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + * @tparam OutputDataType Type of the output data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + */ +template < + typename InputDataType = arma::mat, + typename OutputDataType = arma::mat +> +class HuberLoss +{ + public: + /** + * Create the HuberLoss object. + */ + HuberLoss( + const double delta = 1.0, + const bool mean = true + ); + + /** + * Computes the Huber Loss function. + * + * @param input Input data used for evaluating the specified function. + * @param target The target vector. + */ + template + double Forward(const InputType&& input, const TargetType&& target); + /** + * Ordinary feed backward pass of a neural network. + * + * @param input The propagated input activation. + * @param target The target vector. + * @param output The calculated error. + */ + template + void Backward(const InputType&& input, + const TargetType&& target, + OutputType&& output); + + //! Get the output parameter. + OutputDataType& OutputParameter() const { return outputParameter; } + //! Modify the output parameter. + OutputDataType& OutputParameter() { return outputParameter; } + + //! Get the value of delta. + double Delta() const { return delta; } + //! Set the value of delta. + double& Delta() { return delta; } + + //! Get the value of reduction type. + bool Mean() const { return mean; } + //! Set the value of reduction type. + bool& Mean() { return mean; } + + /** + * Serialize the layer + */ + template + void serialize(Archive& ar, const unsigned int /* version */); + + private: + //! Locally-stored output parameter object. + OutputDataType outputParameter; + + //! Hyperparameter `delta` defines the point upto which MSE is considered. + double delta; + + //! Reduction type. If true, performs mean of loss else sum. + bool mean; +}; // class HuberLoss + +} // namespace ann +} // namespace mlpack + +// Include implementation. +#include "huber_loss_impl.hpp" + +#endif diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp new file mode 100644 index 0000000000..a87c08c47a --- /dev/null +++ b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp @@ -0,0 +1,85 @@ +/** + * @file huber_loss_impl.hpp + * @author Mrityunjay Tripathi + * + * Implementation of the Huber Loss function. + * + * 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_METHODS_ANN_LOSS_FUNCTION_HUBER_LOSS_IMPL_HPP +#define MLPACK_METHODS_ANN_LOSS_FUNCTION_HUBER_LOSS_IMPL_HPP + +// In case it hasn't yet been included. +#include "huber_loss.hpp" + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +template +HuberLoss::HuberLoss( + const double delta, + const bool mean): + delta(delta), + mean(mean) +{ + // Nothing to do here. +} + +template +template +double HuberLoss::Forward( + const InputType&& input, const TargetType&& target) +{ + double lossThis; + double totalLoss = 0; + double absError; + + for (size_t i = 0; i < input.n_elem; ++i) + { + absError = std::abs(target[i] - input[i]); + lossThis = absError > delta + ? delta * (absError - 0.5 * delta) : 0.5 * std::pow(absError, 2); + totalLoss += lossThis; + } + if (mean) + { + return totalLoss / input.n_elem + } + return totalLoss; +} + +template +template +void HuberLoss::Backward( + const InputType&& input, + const TargetType&& target, + OutputType&& output) +{ + output.set_size(size(input)); + double absError; + + for (size_t i = 0; i < output.n_elem; ++i) + { + absError = std::abs(target[i] - input[i]); + output[i] = absError > delta + ? - delta * (target[i] - input[i]) / absError : input[i] - target[i]; + output[i] /= output.n_elem; + } +} + +template +template +void HuberLoss::serialize( + Archive& /* ar */, + const unsigned int /* version */) +{ + // Nothing to do here. +} + +} // namespace ann +} // namespace mlpack + +#endif diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index 643c975de5..afe2896978 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -33,6 +34,33 @@ using namespace mlpack::ann; BOOST_AUTO_TEST_SUITE(LossFunctionsTest); +/** + * Simple Huber Loss test. + */ +BOOST_AUTO_TEST_CASE(HuberLossTest) +{ + arma::mat input, target, output; + HuberLoss<> module; + + // Test the Forward function. + input = arma::mat("17.45 12.91 13.63 29.01 7.12 15.47 31.52 31.97"); + target = arma::mat("16.52 13.11 13.67 29.51 24.31 15.03 30.72 34.07"); + double loss = module.Forward(std::move(input), std::move(target)); + BOOST_REQUIRE_CLOSE_FRACTION(loss, 2.4106, 0.00001); + + // Test the backward function. + module.Backward(std::move(input), std::move(target), std::move(output)); + + // Expected Output: + // [0.1162 -0.0250 -0.0050 -0.0625 -0.1250 0.0550 0.1000 -0.1250] + double expectedOutputSum = arma::accu(output); + BOOST_REQUIRE_CLOSE_FRACTION(expectedOutputSum, -0.07125, 0.00001); + + BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); + BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); + +} + /** * Simple KL Divergence test. The loss should be zero if input = target. */ From c17e8ea7d58f2f783f915d59d9c33ee7e93d7cdc Mon Sep 17 00:00:00 2001 From: Mrityunjay Tripathi Date: Tue, 11 Feb 2020 23:58:56 +0530 Subject: [PATCH 044/265] style and error fix --- src/mlpack/methods/ann/loss_functions/huber_loss.hpp | 3 +-- src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp | 2 +- src/mlpack/tests/loss_functions_test.cpp | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp index 448a2838ed..3a9dff956e 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp @@ -42,8 +42,7 @@ class HuberLoss */ HuberLoss( const double delta = 1.0, - const bool mean = true - ); + const bool mean = true); /** * Computes the Huber Loss function. diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp index a87c08c47a..1615084cb8 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp @@ -46,7 +46,7 @@ double HuberLoss::Forward( } if (mean) { - return totalLoss / input.n_elem + return totalLoss / input.n_elem; } return totalLoss; } diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index afe2896978..70712c1d5d 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -58,7 +58,6 @@ BOOST_AUTO_TEST_CASE(HuberLossTest) BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); - } /** From 5ebf07de8b8a64b6815799726105c523b24d51ab Mon Sep 17 00:00:00 2001 From: Mrityunjay Tripathi Date: Wed, 12 Feb 2020 10:07:06 +0530 Subject: [PATCH 045/265] error in test solved --- src/mlpack/tests/loss_functions_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index 70712c1d5d..ff6cc7fe3a 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -46,13 +46,14 @@ BOOST_AUTO_TEST_CASE(HuberLossTest) input = arma::mat("17.45 12.91 13.63 29.01 7.12 15.47 31.52 31.97"); target = arma::mat("16.52 13.11 13.67 29.51 24.31 15.03 30.72 34.07"); double loss = module.Forward(std::move(input), std::move(target)); - BOOST_REQUIRE_CLOSE_FRACTION(loss, 2.4106, 0.00001); + BOOST_REQUIRE_CLOSE_FRACTION(loss, 2.4106375, 0.000001); // Test the backward function. module.Backward(std::move(input), std::move(target), std::move(output)); // Expected Output: // [0.1162 -0.0250 -0.0050 -0.0625 -0.1250 0.0550 0.1000 -0.1250] + // Sum of Expected Output = -0.07125. double expectedOutputSum = arma::accu(output); BOOST_REQUIRE_CLOSE_FRACTION(expectedOutputSum, -0.07125, 0.00001); From 3312343ff0b0bc23360f6b9a1e718339ab675dc9 Mon Sep 17 00:00:00 2001 From: Mrityunjay Tripathi Date: Wed, 12 Feb 2020 13:55:45 +0530 Subject: [PATCH 046/265] fixed test error --- src/mlpack/tests/loss_functions_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index ff6cc7fe3a..e9c0df1a18 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(HuberLossTest) input = arma::mat("17.45 12.91 13.63 29.01 7.12 15.47 31.52 31.97"); target = arma::mat("16.52 13.11 13.67 29.51 24.31 15.03 30.72 34.07"); double loss = module.Forward(std::move(input), std::move(target)); - BOOST_REQUIRE_CLOSE_FRACTION(loss, 2.4106375, 0.000001); + BOOST_REQUIRE_CLOSE_FRACTION(loss, 2.410631, 0.00001); // Test the backward function. module.Backward(std::move(input), std::move(target), std::move(output)); From 7174c0221e38dcdabd77098708c8bb21a7732719 Mon Sep 17 00:00:00 2001 From: Mrityunjay Tripathi Date: Fri, 14 Feb 2020 18:19:59 +0530 Subject: [PATCH 047/265] adding description about parameters. --- src/mlpack/methods/ann/loss_functions/huber_loss.hpp | 8 +++++++- src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp | 6 +----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp index 3a9dff956e..4e24419d5f 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp @@ -39,6 +39,11 @@ class HuberLoss public: /** * Create the HuberLoss object. + * + * @param delta The threshold value upto which squared error is followed and + * after which absolute error is considered. + * @param mean It takes either 1 or 0 i.e. true or false. If true then + * mean of the total loss is taken otherwise sum. */ HuberLoss( const double delta = 1.0, @@ -52,6 +57,7 @@ class HuberLoss */ template double Forward(const InputType&& input, const TargetType&& target); + /** * Ordinary feed backward pass of a neural network. * @@ -80,7 +86,7 @@ class HuberLoss bool& Mean() { return mean; } /** - * Serialize the layer + * Serialize the layer. */ template void serialize(Archive& ar, const unsigned int /* version */); diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp index 1615084cb8..654c25d09b 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp @@ -44,11 +44,7 @@ double HuberLoss::Forward( ? delta * (absError - 0.5 * delta) : 0.5 * std::pow(absError, 2); totalLoss += lossThis; } - if (mean) - { - return totalLoss / input.n_elem; - } - return totalLoss; + return mean ? totalLoss / input.n_elem : totalLoss; } template From b48bb73486bf53f4d8d9329a77f0b0cfcaba89f7 Mon Sep 17 00:00:00 2001 From: Mrityunjay Tripathi Date: Sun, 23 Feb 2020 13:44:38 +0530 Subject: [PATCH 048/265] slight changes in description and variable names --- src/mlpack/methods/ann/loss_functions/huber_loss.hpp | 9 ++++----- .../methods/ann/loss_functions/huber_loss_impl.hpp | 10 ++++------ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp index 4e24419d5f..4b021ca193 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp @@ -2,7 +2,7 @@ * @file huber_loss.hpp * @author Mrityunjay Tripathi * - * Definition of the Huber Loss function. + * Definition of the Huber loss function. * * 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 @@ -20,9 +20,9 @@ namespace ann /** Artificial Neural Network. */ { /** * The Huber loss is a loss function used in robust regression, * that is less sensitive to outliers in data than the squared error loss. - * This function is quadratic for small values of `y - f(x)`, + * This function is quadratic for small values of \f$ y - f(x) \f$, * and linear for large values, with equal values and slopes of the different - * sections at the two points where `|y - f(x)| = delta`. + * sections at the two points where \f$ |y - f(x)| = delta \f$. * * @tparam ActivationFunction Activation function used for the embedding layer. * @tparam InputDataType Type of the input data (arma::colvec, arma::mat, @@ -42,8 +42,7 @@ class HuberLoss * * @param delta The threshold value upto which squared error is followed and * after which absolute error is considered. - * @param mean It takes either 1 or 0 i.e. true or false. If true then - * mean of the total loss is taken otherwise sum. + * @param mean If true then mean loss is computed otherwise sum. */ HuberLoss( const double delta = 1.0, diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp index 654c25d09b..f3a0fb8c33 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp @@ -2,7 +2,7 @@ * @file huber_loss_impl.hpp * @author Mrityunjay Tripathi * - * Implementation of the Huber Loss function. + * Implementation of the Huber loss function. * * 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 @@ -33,18 +33,16 @@ template double HuberLoss::Forward( const InputType&& input, const TargetType&& target) { - double lossThis; - double totalLoss = 0; + double loss = 0; double absError; for (size_t i = 0; i < input.n_elem; ++i) { absError = std::abs(target[i] - input[i]); - lossThis = absError > delta + loss += absError > delta ? delta * (absError - 0.5 * delta) : 0.5 * std::pow(absError, 2); - totalLoss += lossThis; } - return mean ? totalLoss / input.n_elem : totalLoss; + return mean ? loss / input.n_elem : loss; } template From 4cff789eec9876cab2573f314103972a9116b736 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Tue, 3 Mar 2020 17:56:31 -0500 Subject: [PATCH 049/265] Use _Internal module, not util. --- src/mlpack/bindings/julia/CMakeLists.txt | 4 ++-- src/mlpack/bindings/julia/print_jl.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mlpack/bindings/julia/CMakeLists.txt b/src/mlpack/bindings/julia/CMakeLists.txt index 8bdc4f5b55..8b52ed9aa1 100644 --- a/src/mlpack/bindings/julia/CMakeLists.txt +++ b/src/mlpack/bindings/julia/CMakeLists.txt @@ -56,7 +56,7 @@ if (BUILD_JULIA_BINDINGS) file(WRITE "${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/mlpack.jl" "module mlpack\n\n" - "module util\n\n" + "module _Internal\n\n" "include(\"cli.jl\")\n") file(WRITE @@ -150,7 +150,7 @@ if (BUILD_JULIA_BINDINGS) # Append the code to define the function in the module. file(APPEND "${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/functions.jl" - "${name} = util.${name}\n") + "${name} = _Internal.${name}\n") endif () endmacro () diff --git a/src/mlpack/bindings/julia/print_jl.cpp b/src/mlpack/bindings/julia/print_jl.cpp index 939b606386..7f4fb86a02 100644 --- a/src/mlpack/bindings/julia/print_jl.cpp +++ b/src/mlpack/bindings/julia/print_jl.cpp @@ -62,7 +62,7 @@ void PrintJL(const util::ProgramDoc& programInfo, cout << endl; // We need to include utility functions. - cout << "using mlpack.util.cli" << endl; + cout << "using mlpack._Internal.cli" << endl; cout << endl; // Make sure the libraries we need are accessible. From c9856000dcef08f41b67befd5976f0855388de3e Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Tue, 3 Mar 2020 17:56:57 -0500 Subject: [PATCH 050/265] Print code snippets in ```julia blocks. --- .../bindings/julia/print_doc_functions_impl.hpp | 6 ++++++ .../bindings/markdown/print_doc_functions_impl.hpp | 13 ++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/mlpack/bindings/julia/print_doc_functions_impl.hpp b/src/mlpack/bindings/julia/print_doc_functions_impl.hpp index c428b5986a..c3c20ffffc 100644 --- a/src/mlpack/bindings/julia/print_doc_functions_impl.hpp +++ b/src/mlpack/bindings/julia/print_doc_functions_impl.hpp @@ -375,6 +375,9 @@ inline std::string ProgramCall(const std::string& programName, Args... args) { std::ostringstream oss; + // The code should appear in a Markdown code block. + oss << "```julia" << std::endl; + // Print any input argument definitions. The only input argument definitions // will be the definitions of matrices, which use the CSV.jl package, so we // should also include a `using CSV` in there too. @@ -401,6 +404,9 @@ inline std::string ProgramCall(const std::string& programName, Args... args) // Since `julia> ` is 8 characters, let's indent 12 otherwise it looks weird. oss << util::HyphenateString(ossCall.str(), 12); + // Close the Markdown code block. + oss << std::endl << "```"; + return oss.str(); } diff --git a/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp b/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp index 28e0fbea6d..8ed10c97e6 100644 --- a/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp +++ b/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp @@ -451,20 +451,20 @@ inline std::string PrintModel(const std::string& model) template std::string ProgramCall(const std::string& programName, Args... args) { - std::string s = "```"; if (BindingInfo::Language() == "cli") { - s += "bash\n"; + s += "```bash\n"; s += cli::ProgramCall(programName, args...); } else if (BindingInfo::Language() == "python") { - s += "python\n"; + s += "```python\n"; s += python::ProgramCall(programName, args...); } else if (BindingInfo::Language() == "julia") { - s += "julia\n"; + // Julia's ProgramCall() with a set of arguments will automatically enclose + // the text in Markdown code, so we don't need to. s += julia::ProgramCall(programName, args...); } else @@ -472,7 +472,10 @@ std::string ProgramCall(const std::string& programName, Args... args) throw std::invalid_argument("ProgramCall(): unknown " "BindingInfo::Language(): " + BindingInfo::Language() + "!"); } - s += "\n```"; + + // Close the Markdown code block, but only if we opened one. + if (BindingInfo::Language() != "julia") + s += "\n```"; return s; } From 6432555257c04851926c275bf70ed4df4601a370 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Tue, 3 Mar 2020 17:57:12 -0500 Subject: [PATCH 051/265] Always pass 64-bit integers back and forth. --- src/mlpack/bindings/julia/julia_util.cpp | 269 +++++++++++++-------- src/mlpack/bindings/julia/julia_util.h | 70 +++--- src/mlpack/bindings/julia/mlpack/cli.jl.in | 2 +- 3 files changed, 208 insertions(+), 133 deletions(-) diff --git a/src/mlpack/bindings/julia/julia_util.cpp b/src/mlpack/bindings/julia/julia_util.cpp index 8db14179a3..f696739163 100644 --- a/src/mlpack/bindings/julia/julia_util.cpp +++ b/src/mlpack/bindings/julia/julia_util.cpp @@ -7,6 +7,7 @@ #include #include #include +#include using namespace mlpack; @@ -21,11 +22,11 @@ void CLI_RestoreSettings(const char* programName) } /** - * Call CLI::SetParam(). + * Call CLI::SetParam(). Julia always gives us an int64. */ -void CLI_SetParamInt(const char* paramName, int paramValue) +void CLI_SetParamInt(const char* paramName, int64_t paramValue) { - CLI::GetParam(paramName) = paramValue; + CLI::GetParam(paramName) = int(paramValue); CLI::SetPassed(paramName); } @@ -60,10 +61,10 @@ void CLI_SetParamBool(const char* paramName, bool paramValue) * Call CLI::SetParam>() to set the length. */ void CLI_SetParamVectorStrLen(const char* paramName, - const size_t length) + const uint64_t length) { CLI::GetParam>(paramName).clear(); - CLI::GetParam>(paramName).resize(length); + CLI::GetParam>(paramName).resize(size_t(length)); CLI::SetPassed(paramName); } @@ -72,24 +73,25 @@ void CLI_SetParamVectorStrLen(const char* paramName, */ void CLI_SetParamVectorStrStr(const char* paramName, const char* str, - const size_t element) + const uint64_t element) { - CLI::GetParam>(paramName)[element] = + CLI::GetParam>(paramName)[size_t(element)] = std::string(str); } /** - * Call CLI::SetParam>(). + * Call CLI::SetParam>(). Julia always gives us int64s. */ void CLI_SetParamVectorInt(const char* paramName, - uint64_t* ints, - const size_t length) + int64_t* ints, + const uint64_t length) { // Create a std::vector object; unfortunately this requires copying the // vector elements. - std::vector vec(length); + std::vector vec; + vec.resize(size_t(length)); for (size_t i = 0; i < (size_t) length; ++i) - vec[i] = ints[i]; + vec[i] = int(ints[i]); CLI::GetParam>(paramName) = std::move(vec); CLI::SetPassed(paramName); @@ -100,12 +102,12 @@ void CLI_SetParamVectorInt(const char* paramName, */ void CLI_SetParamMat(const char* paramName, double* memptr, - const size_t rows, - const size_t cols, + const uint64_t rows, + const uint64_t cols, const bool pointsAsRows) { // Create the matrix as an alias. - arma::mat m(memptr, rows, cols, false, true); + arma::mat m(memptr, arma::uword(rows), arma::uword(cols), false, true); CLI::GetParam(paramName) = pointsAsRows ? m.t() : std::move(m); CLI::SetPassed(paramName); } @@ -114,16 +116,32 @@ void CLI_SetParamMat(const char* paramName, * Call CLI::SetParam>(). */ void CLI_SetParamUMat(const char* paramName, - size_t* memptr, - const size_t rows, - const size_t cols, + uint64_t* memptr, + const uint64_t rows, + const uint64_t cols, const bool pointsAsRows) { - // Create the matrix as an alias. - arma::Mat m(memptr, rows, cols, false, true); - CLI::GetParam>(paramName) = pointsAsRows ? m.t() : - std::move(m); - CLI::SetPassed(paramName); + // If we're on a 64-bit system, we can create the matrix as an alias. + if (sizeof(uint64_t) == sizeof(size_t)) + { + // Create the matrix as an alias. + arma::Mat m(memptr, arma::uword(rows), arma::uword(cols), false, + true); + CLI::GetParam>(paramName) = pointsAsRows ? m.t() : + std::move(m); + CLI::SetPassed(paramName); + } + else + { + // We have to perform conversion. Create an alias of the memory we got, and + // then convert it. + arma::Mat m(memptr, arma::uword(rows), arma::uword(cols), false, + true); + CLI::GetParam>(paramName) = pointsAsRows ? + arma::conv_to>::from(m.t()) : + arma::conv_to>::from(m); + CLI::SetPassed(paramName); + } } /** @@ -131,9 +149,9 @@ void CLI_SetParamUMat(const char* paramName, */ void CLI_SetParamRow(const char* paramName, double* memptr, - const size_t cols) + const uint64_t cols) { - arma::rowvec m(memptr, cols, false, true); + arma::rowvec m(memptr, arma::uword(cols), false, true); CLI::GetParam(paramName) = std::move(m); CLI::SetPassed(paramName); } @@ -143,9 +161,9 @@ void CLI_SetParamRow(const char* paramName, */ void CLI_SetParamURow(const char* paramName, size_t* memptr, - const size_t cols) + const uint64_t cols) { - arma::Row m(memptr, cols, false, true); + arma::Row m(memptr, arma::uword(cols), false, true); CLI::GetParam>(paramName) = std::move(m); CLI::SetPassed(paramName); } @@ -155,9 +173,9 @@ void CLI_SetParamURow(const char* paramName, */ void CLI_SetParamCol(const char* paramName, double* memptr, - const size_t rows) + const uint64_t rows) { - arma::vec m(memptr, rows, false, true); + arma::vec m(memptr, arma::uword(rows), false, true); CLI::GetParam(paramName) = std::move(m); CLI::SetPassed(paramName); } @@ -166,12 +184,24 @@ void CLI_SetParamCol(const char* paramName, * Call CLI::SetParam>(). */ void CLI_SetParamUCol(const char* paramName, - size_t* memptr, - const size_t rows) + uint64_t* memptr, + const uint64_t rows) { - arma::Col m(memptr, rows, false, true); - CLI::GetParam>(paramName) = std::move(m); - CLI::SetPassed(paramName); + // If Julia gave us the right size, we can use an alias; otherwise we have to + // copy. + if (sizeof(uint64_t) == sizeof(size_t)) + { + arma::Col m(memptr, arma::uword(rows), false, true); + CLI::GetParam>(paramName) = std::move(m); + CLI::SetPassed(paramName); + } + else + { + arma::Col m(memptr, arma::uword(rows), false, true); + CLI::GetParam>(paramName) = + arma::conv_to>::from(m); + CLI::SetPassed(paramName); + } } /** @@ -180,8 +210,8 @@ void CLI_SetParamUCol(const char* paramName, void CLI_SetParamMatWithInfo(const char* paramName, bool* dimensions, double* memptr, - const size_t rows, - const size_t cols, + const uint64_t rows, + const uint64_t cols, const bool pointsAreRows) { data::DatasetInfo d(pointsAreRows ? cols : rows); @@ -191,7 +221,7 @@ void CLI_SetParamMatWithInfo(const char* paramName, data::Datatype::numeric; } - arma::mat m(memptr, rows, cols, false, true); + arma::mat m(memptr, arma::uword(rows), arma::uword(cols), false, true); std::get<0>(CLI::GetParam>( paramName)) = std::move(d); std::get<1>(CLI::GetParam>( @@ -202,9 +232,9 @@ void CLI_SetParamMatWithInfo(const char* paramName, /** * Call CLI::GetParam(). */ -int CLI_GetParamInt(const char* paramName) +int64_t CLI_GetParamInt(const char* paramName) { - return CLI::GetParam(paramName); + return int64_t(CLI::GetParam(paramName)); } /** @@ -235,25 +265,25 @@ bool CLI_GetParamBool(const char* paramName) * Call CLI::GetParam>() and get the length of the * vector. */ -size_t CLI_GetParamVectorStrLen(const char* paramName) +uint64_t CLI_GetParamVectorStrLen(const char* paramName) { - return CLI::GetParam>(paramName).size(); + return uint64_t(CLI::GetParam>(paramName).size()); } /** * Call CLI::GetParam>() and get the i'th string. */ -const char* CLI_GetParamVectorStrStr(const char* paramName, const int i) +const char* CLI_GetParamVectorStrStr(const char* paramName, const int64_t i) { - return CLI::GetParam>(paramName)[i].c_str(); + return CLI::GetParam>(paramName)[int(i)].c_str(); } /** * Call CLI::GetParam>() and get the length of the vector. */ -size_t CLI_GetParamVectorIntLen(const char* paramName) +uint64_t CLI_GetParamVectorIntLen(const char* paramName) { - return CLI::GetParam>(paramName).size(); + return uint64_t(CLI::GetParam>(paramName).size()); } /** @@ -275,17 +305,17 @@ uint64_t* CLI_GetParamVectorIntPtr(const char* paramName) /** * Get the number of rows in a matrix parameter. */ -size_t CLI_GetParamMatRows(const char* paramName) +uint64_t CLI_GetParamMatRows(const char* paramName) { - return CLI::GetParam(paramName).n_rows; + return uint64_t(CLI::GetParam(paramName).n_rows); } /** * Get the number of columns in a matrix parameter. */ -size_t CLI_GetParamMatCols(const char* paramName) +uint64_t CLI_GetParamMatCols(const char* paramName) { - return CLI::GetParam(paramName).n_cols; + return uint64_t(CLI::GetParam(paramName).n_cols); } /** @@ -315,17 +345,17 @@ double* CLI_GetParamMat(const char* paramName) /** * Get the number of rows in an unsigned matrix parameter. */ -size_t CLI_GetParamUMatRows(const char* paramName) +uint64_t CLI_GetParamUMatRows(const char* paramName) { - return CLI::GetParam>(paramName).n_rows; + return uint64_t(CLI::GetParam>(paramName).n_rows); } /** * Get the number of columns in an unsigned matrix parameter. */ -size_t CLI_GetParamUMatCols(const char* paramName) +uint64_t CLI_GetParamUMatCols(const char* paramName) { - return CLI::GetParam>(paramName).n_cols; + return uint64_t(CLI::GetParam>(paramName).n_cols); } /** @@ -333,31 +363,45 @@ size_t CLI_GetParamUMatCols(const char* paramName) * Note that this will assume that whatever is calling will take ownership of * the memory! */ -size_t* CLI_GetParamUMat(const char* paramName) +uint64_t* CLI_GetParamUMat(const char* paramName) { - // Are we using preallocated memory? If so we have to handle this more - // carefully. arma::Mat& mat = CLI::GetParam>(paramName); - if (mat.n_elem <= arma::arma_config::mat_prealloc) + + // Unfortunately, if size_t is not uint64_t, we will incur a copy. + if (sizeof(uint64_t) == sizeof(size_t)) { - // Copy the memory to something that we can give back to Julia. - size_t* newMem = new size_t[mat.n_elem]; - arma::arrayops::copy(newMem, mat.mem, mat.n_elem); - return newMem; // We believe Julia will free it. Hopefully we are right. + // Are we using preallocated memory? If so we have to handle this more + // carefully. + if (mat.n_elem <= arma::arma_config::mat_prealloc) + { + // Copy the memory to something that we can give back to Julia. + size_t* newMem = new size_t[mat.n_elem]; + arma::arrayops::copy(newMem, mat.mem, mat.n_elem); + // We believe Julia will free it. Hopefully we are right. + return (uint64_t*) newMem; + } + else + { + arma::access::rw(mat.mem_state) = 1; + return (uint64_t*) mat.memptr(); + } } else { - arma::access::rw(mat.mem_state) = 1; - return mat.memptr(); + uint64_t* newMem = new uint64_t[mat.n_elem]; + for (size_t i = 0; i < mat.n_elem; ++i) + newMem[i] = uint64_t(mat[i]); + // We believe Julia will free it. Hopefully we are right. + return newMem; } } /** * Get the number of rows in a column vector parameter. */ -size_t CLI_GetParamColRows(const char* paramName) +uint64_t CLI_GetParamColRows(const char* paramName) { - return CLI::GetParam(paramName).n_rows; + return uint64_t(CLI::GetParam(paramName).n_rows); } /** @@ -387,9 +431,9 @@ double* CLI_GetParamCol(const char* paramName) /** * Get the number of columns in an unsigned column vector parameter. */ -size_t CLI_GetParamUColRows(const char* paramName) +uint64_t CLI_GetParamUColRows(const char* paramName) { - return CLI::GetParam>(paramName).n_rows; + return uint64_t(CLI::GetParam>(paramName).n_rows); } /** @@ -397,31 +441,46 @@ size_t CLI_GetParamUColRows(const char* paramName) * Note that this will assume that whatever is calling will take ownership of * the memory! */ -size_t* CLI_GetParamUCol(const char* paramName) +uint64_t* CLI_GetParamUCol(const char* paramName) { - // Are we using preallocated memory? If so we have to handle this more - // carefully. arma::Col& vec = CLI::GetParam>(paramName); - if (vec.n_elem <= arma::arma_config::mat_prealloc) + + // If size_t is not the uint64_t that Julia expects, then unfortunately we + // will have to make a copy. + if (sizeof(uint64_t) == sizeof(size_t)) { - // Copy the memory to something we can give back to Julia. - size_t* newMem = new size_t[vec.n_elem]; - arma::arrayops::copy(newMem, vec.mem, vec.n_elem); - return newMem; // We believe Julia will free it. Hopefully we are right. + // Are we using preallocated memory? If so we have to handle this more + // carefully. + if (vec.n_elem <= arma::arma_config::mat_prealloc) + { + // Copy the memory to something we can give back to Julia. + size_t* newMem = new size_t[vec.n_elem]; + arma::arrayops::copy(newMem, vec.mem, vec.n_elem); + // We believe Julia will free it. Hopefully we are right. + return (uint64_t*) newMem; + } + else + { + arma::access::rw(vec.mem_state) = 1; + return (uint64_t*) vec.memptr(); + } } else { - arma::access::rw(vec.mem_state) = 1; - return vec.memptr(); + uint64_t* newMem = new uint64_t[vec.n_elem]; + for (size_t i = 0; i < vec.n_elem; ++i) + newMem[i] = uint64_t(vec[i]); + // We believe Julia will free it. Hopefully we are right. + return newMem; } } /** * Get the number of columns in a row parameter. */ -size_t CLI_GetParamRowCols(const char* paramName) +uint64_t CLI_GetParamRowCols(const char* paramName) { - return CLI::GetParam(paramName).n_cols; + return uint64_t(CLI::GetParam(paramName).n_cols); } /** @@ -451,9 +510,9 @@ double* CLI_GetParamRow(const char* paramName) /** * Get the number of columns in a row parameter. */ -size_t CLI_GetParamURowCols(const char* paramName) +uint64_t CLI_GetParamURowCols(const char* paramName) { - return CLI::GetParam>(paramName).n_cols; + return uint64_t(CLI::GetParam>(paramName).n_cols); } /** @@ -461,41 +520,57 @@ size_t CLI_GetParamURowCols(const char* paramName) * Note that this will assume that whatever is calling will take ownership of * the memory! */ -size_t* CLI_GetParamURow(const char* paramName) +uint64_t* CLI_GetParamURow(const char* paramName) { - // Are we using preallocated memory? If so we have to handle this more - // carefully. arma::Row& vec = CLI::GetParam>(paramName); - if (vec.n_elem <= arma::arma_config::mat_prealloc) + + // If size_t is not the uint64_t that Julia expects, then unfortunately we + // will have to make a copy. + if (sizeof(size_t) == sizeof(uint64_t)) { - // Copy the memory to something we can give back to Julia. - size_t* newMem = new size_t[vec.n_elem]; - arma::arrayops::copy(newMem, vec.mem, vec.n_elem); - return newMem; + // Are we using preallocated memory? If so we have to handle this more + // carefully. + if (vec.n_elem <= arma::arma_config::mat_prealloc) + { + // Copy the memory to something we can give back to Julia. + size_t* newMem = new size_t[vec.n_elem]; + arma::arrayops::copy(newMem, vec.mem, vec.n_elem); + return (uint64_t*) newMem; + } + else + { + arma::access::rw(vec.mem_state) = 1; + return (uint64_t*) vec.memptr(); + } } else { - arma::access::rw(vec.mem_state) = 1; - return vec.memptr(); + uint64_t* newMem = new uint64_t[vec.n_elem]; + for (size_t i = 0; i < vec.n_elem; ++i) + newMem[i] = uint64_t(vec[i]); + // We believe that Julia will free the memory. Hopefully we are right. + return newMem; } } /** * Get the number of rows in a matrix with DatasetInfo parameter. */ -size_t CLI_GetParamMatWithInfoRows(const char* paramName) +uint64_t CLI_GetParamMatWithInfoRows(const char* paramName) { - return std::get<1>(CLI::GetParam>( - paramName)).n_rows; + return uint64_t(std::get<1>( + CLI::GetParam>( + paramName)).n_rows); } /** * Get the number of columns in a matrix with DatasetInfo parameter. */ -size_t CLI_GetParamMatWithInfoCols(const char* paramName) +uint64_t CLI_GetParamMatWithInfoCols(const char* paramName) { - return std::get<1>(CLI::GetParam>( - paramName)).n_cols; + return uint64_t(std::get<1>( + CLI::GetParam>( + paramName)).n_cols); } /** diff --git a/src/mlpack/bindings/julia/julia_util.h b/src/mlpack/bindings/julia/julia_util.h index 706d363919..6ffa514c3d 100644 --- a/src/mlpack/bindings/julia/julia_util.h +++ b/src/mlpack/bindings/julia/julia_util.h @@ -27,7 +27,7 @@ void CLI_RestoreSettings(const char* programName); /** * Call CLI::SetParam(). */ -void CLI_SetParamInt(const char* paramName, int paramValue); +void CLI_SetParamInt(const char* paramName, int64_t paramValue); /** * Call CLI::SetParam(). @@ -48,38 +48,38 @@ void CLI_SetParamBool(const char* paramName, bool paramValue); * Call CLI::SetParam>() to set the length. */ void CLI_SetParamVectorStrLen(const char* paramName, - const size_t length); + const uint64_t length); /** * Call CLI::SetParam>() to set an individual element. */ void CLI_SetParamVectorStrStr(const char* paramName, const char* str, - const size_t element); + const uint64_t element); /** * Call CLI::SetParam>(). */ void CLI_SetParamVectorInt(const char* paramName, - uint64_t* ints, - const size_t length); + int64_t* ints, + const uint64_t length); /** * Call CLI::SetParam(). */ void CLI_SetParamMat(const char* paramName, double* memptr, - const size_t rows, - const size_t cols, + const uint64_t rows, + const uint64_t cols, const bool pointsAsRows); /** * Call CLI::SetParam>(). */ void CLI_SetParamUMat(const char* paramName, - size_t* memptr, - const size_t rows, - const size_t cols, + uint64_t* memptr, + const uint64_t rows, + const uint64_t cols, const bool pointsAsRows); /** @@ -87,28 +87,28 @@ void CLI_SetParamUMat(const char* paramName, */ void CLI_SetParamRow(const char* paramName, double* memptr, - const size_t cols); + const uint64_t cols); /** * Call CLI::SetParam>(). */ void CLI_SetParamURow(const char* paramName, - size_t* memptr, - const size_t cols); + uint64_t* memptr, + const uint64_t cols); /** * Call CLI::SetParam(). */ void CLI_SetParamCol(const char* paramName, double* memptr, - const size_t rows); + const uint64_t rows); /** * Call CLI::SetParam>(). */ void CLI_SetParamUCol(const char* paramName, - size_t* memptr, - const size_t rows); + uint64_t* memptr, + const uint64_t rows); /** * Call CLI::SetParam>(). @@ -116,14 +116,14 @@ void CLI_SetParamUCol(const char* paramName, void CLI_SetParamMatWithInfo(const char* paramName, bool* dimensions, double* memptr, - const size_t rows, - const size_t cols, + const uint64_t rows, + const uint64_t cols, const bool pointsAreRows); /** * Call CLI::GetParam(). */ -int CLI_GetParamInt(const char* paramName); +int64_t CLI_GetParamInt(const char* paramName); /** * Call CLI::GetParam(). @@ -144,17 +144,17 @@ bool CLI_GetParamBool(const char* paramName); * Call CLI::GetParam>() and get the length of the * vector. */ -size_t CLI_GetParamVectorStrLen(const char* paramName); +uint64_t CLI_GetParamVectorStrLen(const char* paramName); /** * Call CLI::GetParam>() and get the i'th string. */ -const char* CLI_GetParamVectorStrStr(const char* paramName, const int i); +const char* CLI_GetParamVectorStrStr(const char* paramName, const int64_t i); /** * Call CLI::GetParam>() and get the length of the vector. */ -size_t CLI_GetParamVectorIntLen(const char* paramName); +uint64_t CLI_GetParamVectorIntLen(const char* paramName); /** * Call CLI::GetParam>() and return a pointer to the vector. @@ -166,12 +166,12 @@ uint64_t* CLI_GetParamVectorIntPtr(const char* paramName); /** * Get the number of rows in a matrix parameter. */ -size_t CLI_GetParamMatRows(const char* paramName); +uint64_t CLI_GetParamMatRows(const char* paramName); /** * Get the number of columns in a matrix parameter. */ -size_t CLI_GetParamMatCols(const char* paramName); +uint64_t CLI_GetParamMatCols(const char* paramName); /** * Get the memory pointer for a matrix parameter. @@ -183,24 +183,24 @@ double* CLI_GetParamMat(const char* paramName); /** * Get the number of rows in an unsigned matrix parameter. */ -size_t CLI_GetParamUMatRows(const char* paramName); +uint64_t CLI_GetParamUMatRows(const char* paramName); /** * Get the number of columns in an unsigned matrix parameter. */ -size_t CLI_GetParamUMatCols(const char* paramName); +uint64_t CLI_GetParamUMatCols(const char* paramName); /** * Get the memory pointer for an unsigned matrix parameter. * Note that this will assume that whatever is calling will take ownership of * the memory! */ -size_t* CLI_GetParamUMat(const char* paramName); +uint64_t* CLI_GetParamUMat(const char* paramName); /** * Get the number of rows in a column parameter. */ -size_t CLI_GetParamColRows(const char* paramName); +uint64_t CLI_GetParamColRows(const char* paramName); /** * Get the memory pointer for a column vector parameter. @@ -212,19 +212,19 @@ double* CLI_GetParamCol(const char* paramName); /** * Get the number of columns in an unsigned column vector parameter. */ -size_t CLI_GetParamUColRows(const char* paramName); +uint64_t CLI_GetParamUColRows(const char* paramName); /** * Get the memory pointer for an unsigned column vector parameter. * Note that this will assume that whatever is calling will take ownership of * the memory! */ -size_t* CLI_GetParamUCol(const char* paramName); +uint64_t* CLI_GetParamUCol(const char* paramName); /** * Get the number of columns in a row parameter. */ -size_t CLI_GetParamRowCols(const char* paramName); +uint64_t CLI_GetParamRowCols(const char* paramName); /** * Get the memory pointer for a row parameter. @@ -236,24 +236,24 @@ double* CLI_GetParamRow(const char* paramName); /** * Get the number of columns in a row parameter. */ -size_t CLI_GetParamURowCols(const char* paramName); +uint64_t CLI_GetParamURowCols(const char* paramName); /** * Get the memory pointer for a row parameter. * Note that this will assume that whatever is calling will take ownership of * the memory! */ -size_t* CLI_GetParamURow(const char* paramName); +uint64_t* CLI_GetParamURow(const char* paramName); /** * Get the number of rows in a matrix with DatasetInfo parameter. */ -size_t CLI_GetParamMatWithInfoRows(const char* paramName); +uint64_t CLI_GetParamMatWithInfoRows(const char* paramName); /** * Get the number of columns in a matrix with DatasetInfo parameter. */ -size_t CLI_GetParamMatWithInfoCols(const char* paramName); +uint64_t CLI_GetParamMatWithInfoCols(const char* paramName); /** * Get a pointer to an array of booleans representing whether or not dimensions diff --git a/src/mlpack/bindings/julia/mlpack/cli.jl.in b/src/mlpack/bindings/julia/mlpack/cli.jl.in index 687f532a2e..5e3b71e914 100644 --- a/src/mlpack/bindings/julia/mlpack/cli.jl.in +++ b/src/mlpack/bindings/julia/mlpack/cli.jl.in @@ -66,7 +66,7 @@ function CLIRestoreSettings(programName::String) end function CLISetParam(paramName::String, paramValue::Int) - ccall((:CLI_SetParamInt, library), Nothing, (Cstring, Int), paramName, + ccall((:CLI_SetParamInt, library), Nothing, (Cstring, Int64), paramName, paramValue); end From 4d45e0594455ef28f70838d863ac18e882bfd84a Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Wed, 4 Mar 2020 12:46:12 +0000 Subject: [PATCH 052/265] Additional i386 fixes. --- src/mlpack/bindings/julia/julia_util.cpp | 27 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/mlpack/bindings/julia/julia_util.cpp b/src/mlpack/bindings/julia/julia_util.cpp index f696739163..fc38e1377b 100644 --- a/src/mlpack/bindings/julia/julia_util.cpp +++ b/src/mlpack/bindings/julia/julia_util.cpp @@ -125,8 +125,8 @@ void CLI_SetParamUMat(const char* paramName, if (sizeof(uint64_t) == sizeof(size_t)) { // Create the matrix as an alias. - arma::Mat m(memptr, arma::uword(rows), arma::uword(cols), false, - true); + arma::Mat m((size_t*) memptr, arma::uword(rows), arma::uword(cols), + false, true); CLI::GetParam>(paramName) = pointsAsRows ? m.t() : std::move(m); CLI::SetPassed(paramName); @@ -160,12 +160,25 @@ void CLI_SetParamRow(const char* paramName, * Call CLI::SetParam>(). */ void CLI_SetParamURow(const char* paramName, - size_t* memptr, + uint64_t* memptr, const uint64_t cols) { - arma::Row m(memptr, arma::uword(cols), false, true); - CLI::GetParam>(paramName) = std::move(m); - CLI::SetPassed(paramName); + // If we're on a 64-bit system, we can create the matrix as an alias. + if (sizeof(uint64_t) == sizeof(size_t)) + { + arma::Row m((size_t*) memptr, arma::uword(cols), false, true); + CLI::GetParam>(paramName) = std::move(m); + CLI::SetPassed(paramName); + } + else + { + // We have to perform conversion. Create an alias of the memory we got, + // and then convert it. + arma::Row m(memptr, arma::uword(cols), false, true); + CLI::GetParam>(paramName) = + arma::conv_to>::from(m); + CLI::SetPassed(paramName); + } } /** @@ -191,7 +204,7 @@ void CLI_SetParamUCol(const char* paramName, // copy. if (sizeof(uint64_t) == sizeof(size_t)) { - arma::Col m(memptr, arma::uword(rows), false, true); + arma::Col m((size_t*) memptr, arma::uword(rows), false, true); CLI::GetParam>(paramName) = std::move(m); CLI::SetPassed(paramName); } From d5447718b4050ff3c63410ab12dc49c8c3ea526c Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Wed, 4 Mar 2020 08:25:26 -0500 Subject: [PATCH 053/265] Remove unneeded REQUIRE. --- src/mlpack/bindings/julia/mlpack/REQUIRE | 1 - 1 file changed, 1 deletion(-) delete mode 100644 src/mlpack/bindings/julia/mlpack/REQUIRE diff --git a/src/mlpack/bindings/julia/mlpack/REQUIRE b/src/mlpack/bindings/julia/mlpack/REQUIRE deleted file mode 100644 index aef1ca2bd9..0000000000 --- a/src/mlpack/bindings/julia/mlpack/REQUIRE +++ /dev/null @@ -1 +0,0 @@ -julia 0.7.0 From 0621165a8bb0aacd37b505265ef0bc9bf78138af Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Wed, 4 Mar 2020 08:50:55 -0500 Subject: [PATCH 054/265] Add docstrings to the mlpack module. --- src/mlpack/bindings/julia/CMakeLists.txt | 20 ++++++------- src/mlpack/bindings/julia/mlpack/mlpack.jl.in | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 src/mlpack/bindings/julia/mlpack/mlpack.jl.in diff --git a/src/mlpack/bindings/julia/CMakeLists.txt b/src/mlpack/bindings/julia/CMakeLists.txt index 8b52ed9aa1..1a31a9bead 100644 --- a/src/mlpack/bindings/julia/CMakeLists.txt +++ b/src/mlpack/bindings/julia/CMakeLists.txt @@ -18,10 +18,6 @@ if (BUILD_JULIA_BINDINGS) add_custom_command(TARGET julia PRE_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/) - add_custom_command(TARGET julia PRE_BUILD - COMMAND ${CMAKE_COMMAND} -E copy - ${CMAKE_CURRENT_SOURCE_DIR}/mlpack/REQUIRE - ${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/REQUIRE) add_library(mlpack_julia_util julia_util.h @@ -52,12 +48,16 @@ if (BUILD_JULIA_BINDINGS) configure_file(${CMAKE_SOURCE_DIR}/src/mlpack/bindings/julia/mlpack/cli.jl.in ${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/cli.jl) - # Create the empty mlpack.jl file that we will fill with includes. - file(WRITE - "${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/mlpack.jl" - "module mlpack\n\n" - "module _Internal\n\n" - "include(\"cli.jl\")\n") + # Create the empty mlpack.jl file that we will fill with includes using the + # exsiting template. Unfortunately COPY doesn't let us change the extension + # so we need a follow-up RENAME command. + file(COPY + "${CMAKE_CURRENT_SOURCE_DIR}/mlpack/mlpack.jl.in" + DESTINATION + "${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/") + file(RENAME + "${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/mlpack.jl.in" + "${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/mlpack.jl") file(WRITE "${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/functions.jl" diff --git a/src/mlpack/bindings/julia/mlpack/mlpack.jl.in b/src/mlpack/bindings/julia/mlpack/mlpack.jl.in new file mode 100644 index 0000000000..b2368ecb75 --- /dev/null +++ b/src/mlpack/bindings/julia/mlpack/mlpack.jl.in @@ -0,0 +1,30 @@ +""" + mlpack + +mlpack is a fast, flexible machine learning library, written in C++, that aims +to provide fast, extensible implementations of cutting-edge machine learning +algorithms. This module provides those implementations as Julia functions. + +Each function inside the module performs a specific machine learning task. + +For complete documentation of these functions, including example usage, see the +mlpack website's documentation for the Julia bindings: + +https://www.mlpack.org/doc/stable/julia_documentation.html + +Each function also contains an equivalent docstring; the Julia REPL's help +functionality can be used to access the documentation that way. +""" +module mlpack + +""" + mlpack._Internal + +This module contains internal implementations details of mlpack. There +shouldn't be any need to go digging around in here if you're just using mlpack. +(But don't let this comment discourage you if you're just curious and poking +around!) +""" +module _Internal + +include("cli.jl") From 466e00be0cae4f33f840d57eb2e2bd81bf9bd8fd Mon Sep 17 00:00:00 2001 From: Sriram Date: Wed, 4 Mar 2020 23:05:46 +0530 Subject: [PATCH 055/265] New NumFOCUS badge mockup --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 783012e600..5398a29d9e 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ src="https://cdn.rawgit.com/mlpack/mlpack.org/e7d36ed8/mlpack-black.svg" style=" Jenkins Coveralls License + NumFOCUS

From 573b350d3cd8207f3300fece1c12aaaf6e456582 Mon Sep 17 00:00:00 2001 From: Sriram Date: Wed, 4 Mar 2020 23:49:09 +0530 Subject: [PATCH 056/265] Match style of other badges --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5398a29d9e..180182e77e 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ src="https://cdn.rawgit.com/mlpack/mlpack.org/e7d36ed8/mlpack-black.svg" style=" Jenkins Coveralls License - NumFOCUS + NumFOCUS

From ffa6d8a56043b80f82754a6e0925032e5fecae76 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Thu, 5 Mar 2020 19:23:15 -0500 Subject: [PATCH 057/265] Refactor to avoid rvalue references. --- HISTORY.md | 9 +- doc/tutorials/ann/ann.txt | 24 +- src/mlpack/methods/ann/brnn_impl.hpp | 166 +++++------ .../ann/dists/bernoulli_distribution.hpp | 8 +- .../ann/dists/bernoulli_distribution_impl.hpp | 8 +- src/mlpack/methods/ann/ffn.hpp | 30 +- src/mlpack/methods/ann/ffn_impl.hpp | 139 ++++----- src/mlpack/methods/ann/gan/gan.hpp | 2 +- src/mlpack/methods/ann/gan/gan_impl.hpp | 35 ++- src/mlpack/methods/ann/gan/wgan_impl.hpp | 18 +- .../methods/ann/init_rules/network_init.hpp | 4 +- src/mlpack/methods/ann/layer/add.hpp | 14 +- src/mlpack/methods/ann/layer/add_impl.hpp | 14 +- src/mlpack/methods/ann/layer/add_merge.hpp | 26 +- .../methods/ann/layer/add_merge_impl.hpp | 44 +-- .../methods/ann/layer/alpha_dropout.hpp | 8 +- .../methods/ann/layer/alpha_dropout_impl.hpp | 4 +- .../methods/ann/layer/atrous_convolution.hpp | 14 +- .../ann/layer/atrous_convolution_impl.hpp | 23 +- src/mlpack/methods/ann/layer/base_layer.hpp | 8 +- src/mlpack/methods/ann/layer/batch_norm.hpp | 14 +- .../methods/ann/layer/batch_norm_impl.hpp | 10 +- .../ann/layer/bilinear_interpolation.hpp | 8 +- .../ann/layer/bilinear_interpolation_impl.hpp | 14 +- src/mlpack/methods/ann/layer/c_relu.hpp | 4 +- src/mlpack/methods/ann/layer/c_relu_impl.hpp | 4 +- src/mlpack/methods/ann/layer/concat.hpp | 26 +- src/mlpack/methods/ann/layer/concat_impl.hpp | 71 +++-- .../methods/ann/layer/concat_performance.hpp | 9 +- .../ann/layer/concat_performance_impl.hpp | 16 +- src/mlpack/methods/ann/layer/concatenate.hpp | 8 +- .../methods/ann/layer/concatenate_impl.hpp | 8 +- src/mlpack/methods/ann/layer/constant.hpp | 8 +- .../methods/ann/layer/constant_impl.hpp | 4 +- src/mlpack/methods/ann/layer/convolution.hpp | 14 +- .../methods/ann/layer/convolution_impl.hpp | 24 +- src/mlpack/methods/ann/layer/dropconnect.hpp | 14 +- .../methods/ann/layer/dropconnect_impl.hpp | 35 ++- src/mlpack/methods/ann/layer/dropout.hpp | 8 +- src/mlpack/methods/ann/layer/dropout_impl.hpp | 10 +- src/mlpack/methods/ann/layer/elu.hpp | 4 +- src/mlpack/methods/ann/layer/elu_impl.hpp | 4 +- src/mlpack/methods/ann/layer/fast_lstm.hpp | 16 +- .../methods/ann/layer/fast_lstm_impl.hpp | 28 +- .../methods/ann/layer/flexible_relu.hpp | 10 +- .../methods/ann/layer/flexible_relu_impl.hpp | 10 +- src/mlpack/methods/ann/layer/glimpse.hpp | 8 +- src/mlpack/methods/ann/layer/glimpse_impl.hpp | 4 +- src/mlpack/methods/ann/layer/gru.hpp | 14 +- src/mlpack/methods/ann/layer/gru_impl.hpp | 105 +++---- src/mlpack/methods/ann/layer/hard_tanh.hpp | 8 +- .../methods/ann/layer/hard_tanh_impl.hpp | 4 +- src/mlpack/methods/ann/layer/highway.hpp | 14 +- src/mlpack/methods/ann/layer/highway_impl.hpp | 60 ++-- src/mlpack/methods/ann/layer/join.hpp | 8 +- src/mlpack/methods/ann/layer/join_impl.hpp | 11 +- src/mlpack/methods/ann/layer/layer_norm.hpp | 14 +- .../methods/ann/layer/layer_norm_impl.hpp | 10 +- src/mlpack/methods/ann/layer/leaky_relu.hpp | 4 +- .../methods/ann/layer/leaky_relu_impl.hpp | 4 +- src/mlpack/methods/ann/layer/linear.hpp | 14 +- src/mlpack/methods/ann/layer/linear_impl.hpp | 10 +- .../methods/ann/layer/linear_no_bias.hpp | 14 +- .../methods/ann/layer/linear_no_bias_impl.hpp | 10 +- src/mlpack/methods/ann/layer/log_softmax.hpp | 8 +- .../methods/ann/layer/log_softmax_impl.hpp | 8 +- src/mlpack/methods/ann/layer/lookup.hpp | 14 +- src/mlpack/methods/ann/layer/lookup_impl.hpp | 14 +- src/mlpack/methods/ann/layer/lstm.hpp | 20 +- src/mlpack/methods/ann/layer/lstm_impl.hpp | 29 +- src/mlpack/methods/ann/layer/max_pooling.hpp | 8 +- .../methods/ann/layer/max_pooling_impl.hpp | 10 +- src/mlpack/methods/ann/layer/mean_pooling.hpp | 8 +- .../methods/ann/layer/mean_pooling_impl.hpp | 14 +- .../ann/layer/minibatch_discrimination.hpp | 14 +- .../layer/minibatch_discrimination_impl.hpp | 10 +- .../methods/ann/layer/multiply_constant.hpp | 4 +- .../ann/layer/multiply_constant_impl.hpp | 4 +- .../methods/ann/layer/multiply_merge.hpp | 14 +- .../methods/ann/layer/multiply_merge_impl.hpp | 23 +- src/mlpack/methods/ann/layer/padding.hpp | 8 +- src/mlpack/methods/ann/layer/padding_impl.hpp | 8 +- .../methods/ann/layer/parametric_relu.hpp | 10 +- .../ann/layer/parametric_relu_impl.hpp | 9 +- src/mlpack/methods/ann/layer/recurrent.hpp | 14 +- .../methods/ann/layer/recurrent_attention.hpp | 26 +- .../ann/layer/recurrent_attention_impl.hpp | 56 ++-- .../methods/ann/layer/recurrent_impl.hpp | 69 +++-- .../methods/ann/layer/reinforce_normal.hpp | 4 +- .../ann/layer/reinforce_normal_impl.hpp | 4 +- .../methods/ann/layer/reparametrization.hpp | 8 +- .../ann/layer/reparametrization_impl.hpp | 4 +- src/mlpack/methods/ann/layer/select.hpp | 8 +- src/mlpack/methods/ann/layer/select_impl.hpp | 8 +- src/mlpack/methods/ann/layer/sequential.hpp | 14 +- .../methods/ann/layer/sequential_impl.hpp | 60 ++-- src/mlpack/methods/ann/layer/subview.hpp | 8 +- .../ann/layer/transposed_convolution.hpp | 14 +- .../ann/layer/transposed_convolution_impl.hpp | 29 +- .../methods/ann/layer/virtual_batch_norm.hpp | 14 +- .../ann/layer/virtual_batch_norm_impl.hpp | 10 +- .../methods/ann/layer/vr_class_reward.hpp | 8 +- .../ann/layer/vr_class_reward_impl.hpp | 8 +- src/mlpack/methods/ann/layer/weight_norm.hpp | 14 +- .../methods/ann/layer/weight_norm_impl.hpp | 33 +-- .../loss_functions/cross_entropy_error.hpp | 8 +- .../cross_entropy_error_impl.hpp | 8 +- .../methods/ann/loss_functions/dice_loss.hpp | 8 +- .../ann/loss_functions/dice_loss_impl.hpp | 8 +- .../loss_functions/earth_mover_distance.hpp | 8 +- .../earth_mover_distance_impl.hpp | 8 +- .../ann/loss_functions/kl_divergence.hpp | 8 +- .../ann/loss_functions/kl_divergence_impl.hpp | 8 +- .../ann/loss_functions/mean_bias_error.hpp | 8 +- .../loss_functions/mean_bias_error_impl.hpp | 8 +- .../ann/loss_functions/mean_squared_error.hpp | 8 +- .../mean_squared_error_impl.hpp | 8 +- .../mean_squared_logarithmic_error.hpp | 8 +- .../mean_squared_logarithmic_error_impl.hpp | 8 +- .../negative_log_likelihood.hpp | 8 +- .../negative_log_likelihood_impl.hpp | 8 +- .../loss_functions/reconstruction_loss.hpp | 8 +- .../reconstruction_loss_impl.hpp | 14 +- .../sigmoid_cross_entropy_error.hpp | 10 +- .../sigmoid_cross_entropy_error_impl.hpp | 8 +- src/mlpack/methods/ann/rnn.hpp | 5 +- src/mlpack/methods/ann/rnn_impl.hpp | 99 ++++--- .../methods/ann/visitor/backward_visitor.hpp | 16 +- .../ann/visitor/backward_visitor_impl.hpp | 32 +-- .../methods/ann/visitor/bias_set_visitor.hpp | 4 +- .../ann/visitor/bias_set_visitor_impl.hpp | 8 +- .../methods/ann/visitor/forward_visitor.hpp | 6 +- .../ann/visitor/forward_visitor_impl.hpp | 8 +- .../ann/visitor/gradient_set_visitor.hpp | 4 +- .../ann/visitor/gradient_set_visitor_impl.hpp | 8 +- .../ann/visitor/gradient_update_visitor.hpp | 4 +- .../visitor/gradient_update_visitor_impl.hpp | 8 +- .../methods/ann/visitor/gradient_visitor.hpp | 10 +- .../ann/visitor/gradient_visitor_impl.hpp | 23 +- .../visitor/load_output_parameter_visitor.hpp | 4 +- .../load_output_parameter_visitor_impl.hpp | 4 +- .../ann/visitor/parameters_set_visitor.hpp | 4 +- .../visitor/parameters_set_visitor_impl.hpp | 4 +- .../ann/visitor/parameters_visitor.hpp | 4 +- .../ann/visitor/parameters_visitor_impl.hpp | 4 +- .../visitor/save_output_parameter_visitor.hpp | 4 +- .../save_output_parameter_visitor_impl.hpp | 4 +- .../ann/visitor/weight_set_visitor.hpp | 4 +- .../ann/visitor/weight_set_visitor_impl.hpp | 10 +- .../q_learning_impl.hpp | 3 +- .../worker/n_step_q_learning_worker.hpp | 5 +- .../worker/one_step_q_learning_worker.hpp | 5 +- .../worker/one_step_sarsa_worker.hpp | 5 +- .../tests/activation_functions_test.cpp | 40 ++- src/mlpack/tests/ann_dist_test.cpp | 18 +- src/mlpack/tests/ann_layer_test.cpp | 271 +++++++++--------- src/mlpack/tests/ann_test_tools.hpp | 16 +- src/mlpack/tests/ann_visitor_test.cpp | 8 +- src/mlpack/tests/callback_test.cpp | 1 + src/mlpack/tests/feedforward_network_test.cpp | 8 +- src/mlpack/tests/loss_functions_test.cpp | 70 ++--- 161 files changed, 1438 insertions(+), 1406 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index c34a395540..efa10d027c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,9 +2,9 @@ ###### ????-??-?? * Added `mean squared logarithmic error` loss function for neural networks (#2210). - + * Added `mean bias loss function` for neural networks (#2210). - + * The DecisionStump class has been marked deprecated; use the `DecisionTree` class with `NoRecursion=true` or use `ID3DecisionStump` instead (#2099). @@ -42,11 +42,14 @@ * Better error handling of eigendecompositions and Cholesky decompositions (#2088, #1840). - + * Add LiSHT activation function (#2182). * Add Valid and Same Padding for Transposed Convolution layer (#2163). + * Change neural network types to avoid unnecessary use of rvalue references + (#TODO). + ### mlpack 3.2.2 ###### 2019-11-26 * Add `valid` and `same` padding option in `Convolution` and `Atrous diff --git a/doc/tutorials/ann/ann.txt b/doc/tutorials/ann/ann.txt index c1e7da31c1..bcf79ac21d 100644 --- a/doc/tutorials/ann/ann.txt +++ b/doc/tutorials/ann/ann.txt @@ -328,7 +328,7 @@ implementation of a \c Forward() method. The interface looks like: @code template -void Forward(const arma::Mat&& input, arma::Mat&& output); +void Forward(const arma::Mat& input, arma::Mat& output); @endcode The method should calculate the output of the layer given the input matrix and @@ -339,9 +339,9 @@ through f: @code template -void Backward(const arma::Mat&& input, - arma::Mat&& gy, - arma::Mat&& g); +void Backward(const arma::Mat& input, + const arma::Mat& gy, + arma::Mat& g); @endcode Finally, if the layer is differentiable, the layer must also implement @@ -349,9 +349,9 @@ a Gradient() method: @code template -void Gradient(const arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient); +void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient); @endcode The Gradient function should calculate the gradient with respect to the input @@ -434,21 +434,21 @@ API, so we must implement some additional functions. @code template -void Forward(const InputType&& input, OutputType&& output) +void Forward(const InputType& input, OutputType& output) { output = arma::ones(input.n_rows, input.n_cols); } template -void Backward(const InputType&& input, ErrorType&& gy, GradientType&& g) +void Backward(const InputType& input, const ErrorType& gy, GradientType& g) { g = arma::zeros(gy.n_rows, gy.n_cols) + gy; } template -void Gradient(const InputType&& input, - ErrorType&& error, - GradientType&& gradient) +void Gradient(const InputType& input, + ErrorType& error, + GradientType& gradient) { gradient = arma::zeros(input.n_rows, input.n_cols) * error; } diff --git a/src/mlpack/methods/ann/brnn_impl.hpp b/src/mlpack/methods/ann/brnn_impl.hpp index 4778204cef..1a7bd09664 100644 --- a/src/mlpack/methods/ann/brnn_impl.hpp +++ b/src/mlpack/methods/ann/brnn_impl.hpp @@ -166,34 +166,34 @@ void BRNN results1, results2; for (size_t seqNum = 0; seqNum < rho; ++seqNum) { - forwardRNN.Forward(std::move(arma::mat( + forwardRNN.Forward(arma::mat( predictors.slice(seqNum).colptr(begin), - predictors.n_rows, batchSize, false, true))); - backwardRNN.Forward(std::move(arma::mat( + predictors.n_rows, batchSize, false, true)); + backwardRNN.Forward(arma::mat( predictors.slice(rho - seqNum - 1).colptr(begin), - predictors.n_rows, batchSize, false, true))); + predictors.n_rows, batchSize, false, true)); - boost::apply_visitor(SaveOutputParameterVisitor( - std::move(results1)), forwardRNN.network.back()); - boost::apply_visitor(SaveOutputParameterVisitor( - std::move(results2)), backwardRNN.network.back()); + boost::apply_visitor(SaveOutputParameterVisitor( results1), + forwardRNN.network.back()); + boost::apply_visitor(SaveOutputParameterVisitor( results2), + backwardRNN.network.back()); } if (outputSize == 0) { @@ -271,22 +271,22 @@ double BRNN results1, results2; for (size_t seqNum = 0; seqNum < rho; ++seqNum) { - forwardRNN.Forward(std::move(arma::mat( + forwardRNN.Forward(arma::mat( predictors.slice(seqNum).colptr(begin), - predictors.n_rows, batchSize, false, true))); - backwardRNN.Forward(std::move(arma::mat( + predictors.n_rows, batchSize, false, true)); + backwardRNN.Forward(arma::mat( predictors.slice(rho - seqNum - 1).colptr(begin), - predictors.n_rows, batchSize, false, true))); + predictors.n_rows, batchSize, false, true)); for (size_t l = 0; l < networkSize; ++l) { boost::apply_visitor(SaveOutputParameterVisitor( - std::move(forwardRNNOutputParameter)), forwardRNN.network[l]); + forwardRNNOutputParameter), forwardRNN.network[l]); boost::apply_visitor(SaveOutputParameterVisitor( - std::move(backwardRNNOutputParameter)), backwardRNN.network[l]); + backwardRNNOutputParameter), backwardRNN.network[l]); } - boost::apply_visitor(SaveOutputParameterVisitor( - std::move(results1)), forwardRNN.network.back()); - boost::apply_visitor(SaveOutputParameterVisitor( - std::move(results2)), backwardRNN.network.back()); + boost::apply_visitor(SaveOutputParameterVisitor( results1), + forwardRNN.network.back()); + boost::apply_visitor(SaveOutputParameterVisitor( results2), + backwardRNN.network.back()); } if (outputSize == 0) { @@ -410,18 +410,18 @@ EvaluateWithGradient(const arma::mat& /* parameters */, responseSeq = seqNum; } boost::apply_visitor(LoadOutputParameterVisitor( - std::move(results1)), forwardRNN.network.back()); + results1), forwardRNN.network.back()); boost::apply_visitor(LoadOutputParameterVisitor( - std::move(results2)), backwardRNN.network.back()); - boost::apply_visitor(ForwardVisitor(std::move(input), - std::move(boost::apply_visitor(outputParameterVisitor, mergeLayer))), + results2), backwardRNN.network.back()); + boost::apply_visitor(ForwardVisitor(input, + boost::apply_visitor(outputParameterVisitor, mergeLayer)), mergeLayer); boost::apply_visitor(ForwardVisitor( - std::move(boost::apply_visitor(outputParameterVisitor, mergeLayer)), - std::move(results.slice(seqNum))), mergeOutput); - performance += outputLayer.Forward(std::move(results.slice(seqNum)), - std::move(arma::mat(responses.slice(responseSeq).colptr(begin), - responses.n_rows, batchSize, false, true))); + boost::apply_visitor(outputParameterVisitor, mergeLayer), + results.slice(seqNum)), mergeOutput); + performance += outputLayer.Forward(results.slice(seqNum), + arma::mat(responses.slice(responseSeq).colptr(begin), + responses.n_rows, batchSize, false, true)); } // Calculate and storing delta parameters from output for t = 1 to T. @@ -436,25 +436,25 @@ EvaluateWithGradient(const arma::mat& /* parameters */, } else if (single && seqNum == 0) { - outputLayer.Backward(std::move(results.slice(seqNum)), - std::move(arma::mat(responses.slice(0).colptr(begin), - responses.n_rows, batchSize, false, true)), std::move(error)); + outputLayer.Backward(results.slice(seqNum), + arma::mat(responses.slice(0).colptr(begin), + responses.n_rows, batchSize, false, true), error); } else { - outputLayer.Backward(std::move(results.slice(seqNum)), - std::move(arma::mat(responses.slice(seqNum).colptr(begin), - responses.n_rows, batchSize, false, true)), std::move(error)); + outputLayer.Backward(results.slice(seqNum), + arma::mat(responses.slice(seqNum).colptr(begin), + responses.n_rows, batchSize, false, true), error); } - boost::apply_visitor(BackwardVisitor(std::move(results.slice(seqNum)), - std::move(error), std::move(delta)), mergeOutput); + boost::apply_visitor(BackwardVisitor(results.slice(seqNum), error, delta), + mergeOutput); allDelta.push_back(arma::mat(delta)); } // BPTT ForwardRNN from t = T to 1. totalGradient = arma::mat(gradient.memptr(), - parameter.n_elem/2, 1, false, false); + parameter.n_elem / 2, 1, false, false); forwardGradient.zeros(); forwardRNN.ResetGradients(forwardGradient); @@ -467,32 +467,32 @@ EvaluateWithGradient(const arma::mat& /* parameters */, for (size_t l = 0; l < networkSize; ++l) { boost::apply_visitor(LoadOutputParameterVisitor( - std::move(forwardRNNOutputParameter)), + forwardRNNOutputParameter), forwardRNN.network[networkSize - 1 - l]); } - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, forwardRNN.network.back())), - std::move(allDelta[rho - seqNum - 1]), std::move(delta), 0), + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, forwardRNN.network.back()), + allDelta[rho - seqNum - 1], delta, 0), mergeLayer); for (size_t i = 2; i < networkSize; ++i) { boost::apply_visitor(BackwardVisitor( - std::move(boost::apply_visitor(outputParameterVisitor, - forwardRNN.network[networkSize - i])), - std::move(boost::apply_visitor(deltaVisitor, - forwardRNN.network[networkSize - i + 1])), std::move( + boost::apply_visitor(outputParameterVisitor, + forwardRNN.network[networkSize - i]), boost::apply_visitor(deltaVisitor, - forwardRNN.network[networkSize - i]))), + forwardRNN.network[networkSize - i + 1]), + boost::apply_visitor(deltaVisitor, + forwardRNN.network[networkSize - i])), forwardRNN.network[networkSize - i]); } - forwardRNN.Gradient(std::move( + forwardRNN.Gradient( arma::mat(predictors.slice(rho - seqNum - 1).colptr(begin), - predictors.n_rows, batchSize, false, true))); + predictors.n_rows, batchSize, false, true)); boost::apply_visitor(GradientVisitor( - std::move(boost::apply_visitor(outputParameterVisitor, - forwardRNN.network[networkSize - 2])), - std::move(allDelta[rho - seqNum - 1]), 0), mergeLayer); + boost::apply_visitor(outputParameterVisitor, + forwardRNN.network[networkSize - 2]), + allDelta[rho - seqNum - 1], 0), mergeLayer); totalGradient += forwardGradient; } @@ -506,31 +506,31 @@ EvaluateWithGradient(const arma::mat& /* parameters */, for (size_t l = 0; l < networkSize; ++l) { boost::apply_visitor(LoadOutputParameterVisitor( - std::move(backwardRNNOutputParameter)), + backwardRNNOutputParameter), backwardRNN.network[networkSize - 1 - l]); } - boost::apply_visitor(BackwardVisitor(std::move( + boost::apply_visitor(BackwardVisitor( boost::apply_visitor(outputParameterVisitor, - backwardRNN.network.back())), - std::move(allDelta[seqNum]), std::move(delta), 1), mergeLayer); + backwardRNN.network.back()), + allDelta[seqNum], delta, 1), mergeLayer); for (size_t i = 2; i < networkSize; ++i) { boost::apply_visitor(BackwardVisitor( - std::move(boost::apply_visitor(outputParameterVisitor, - backwardRNN.network[networkSize - i])), std::move(boost::apply_visitor( - deltaVisitor, backwardRNN.network[networkSize - i + 1])), std::move( + boost::apply_visitor(outputParameterVisitor, + backwardRNN.network[networkSize - i]), boost::apply_visitor( + deltaVisitor, backwardRNN.network[networkSize - i + 1]), boost::apply_visitor(deltaVisitor, - backwardRNN.network[networkSize - i]))), + backwardRNN.network[networkSize - i])), backwardRNN.network[networkSize - i]); } - backwardRNN.Gradient(std::move( + backwardRNN.Gradient( arma::mat(predictors.slice(seqNum).colptr(begin), - predictors.n_rows, batchSize, false, true))); + predictors.n_rows, batchSize, false, true)); boost::apply_visitor(GradientVisitor( std::move(boost::apply_visitor(outputParameterVisitor, backwardRNN.network[networkSize - 2])), - std::move(allDelta[seqNum]), 1), mergeLayer); + allDelta[seqNum], 1), mergeLayer); totalGradient += backwardGradient; } return performance; diff --git a/src/mlpack/methods/ann/dists/bernoulli_distribution.hpp b/src/mlpack/methods/ann/dists/bernoulli_distribution.hpp index 7203db58e4..44ee24e29a 100644 --- a/src/mlpack/methods/ann/dists/bernoulli_distribution.hpp +++ b/src/mlpack/methods/ann/dists/bernoulli_distribution.hpp @@ -60,7 +60,7 @@ class BernoulliDistribution * @param eps The minimum value used for computing logarithms and * denominators. */ - BernoulliDistribution(const DataType&& param, + BernoulliDistribution(const DataType& param, const bool applyLogistic = true, const double eps = 1e-10); @@ -69,7 +69,7 @@ class BernoulliDistribution * * @param observation The observation matrix. */ - double Probability(const DataType&& observation) const + double Probability(const DataType& observation) const { return std::exp(LogProbability(observation)); } @@ -79,7 +79,7 @@ class BernoulliDistribution * * @param observation The observation matrix. */ - double LogProbability(const DataType&& observation) const; + double LogProbability(const DataType& observation) const; /** * Stores the gradient of the log probabilities of the observations in the @@ -88,7 +88,7 @@ class BernoulliDistribution * @param observation The observation matrix. * @param output The output matrix where the gradients are stored. */ - void LogProbBackward(const DataType&& observation, DataType&& output) const; + void LogProbBackward(const DataType& observation, DataType& output) const; /** * Return a matrix of randomly generated samples according to the diff --git a/src/mlpack/methods/ann/dists/bernoulli_distribution_impl.hpp b/src/mlpack/methods/ann/dists/bernoulli_distribution_impl.hpp index d6ba39248c..aa751f8f79 100644 --- a/src/mlpack/methods/ann/dists/bernoulli_distribution_impl.hpp +++ b/src/mlpack/methods/ann/dists/bernoulli_distribution_impl.hpp @@ -28,7 +28,7 @@ BernoulliDistribution::BernoulliDistribution() : template BernoulliDistribution::BernoulliDistribution( - const DataType&& param, + const DataType& param, const bool applyLogistic, const double eps) : logits(param), @@ -36,7 +36,9 @@ BernoulliDistribution::BernoulliDistribution( eps(eps) { if (applyLogistic) + { LogisticFunction::Fn(logits, probability); + } else { probability = arma::mat(logits.memptr(), logits.n_rows, @@ -58,7 +60,7 @@ DataType BernoulliDistribution::Sample() const template double BernoulliDistribution::LogProbability( - const DataType&& observation) const + const DataType& observation) const { return arma::accu(arma::log(probability + eps) % observation + arma::log(1 - probability + eps) % (1 - observation)) / @@ -67,7 +69,7 @@ double BernoulliDistribution::LogProbability( template void BernoulliDistribution::LogProbBackward( - const DataType&& observation, DataType&& output) const + const DataType& observation, DataType& output) const { if (!applyLogistic) { diff --git a/src/mlpack/methods/ann/ffn.hpp b/src/mlpack/methods/ann/ffn.hpp index a3b0442dcd..ada76165cf 100644 --- a/src/mlpack/methods/ann/ffn.hpp +++ b/src/mlpack/methods/ann/ffn.hpp @@ -153,7 +153,9 @@ class FFN * @param predictors Input variables. * @param responses Target outputs for input variables. */ - double Evaluate(arma::mat predictors, arma::mat responses); + template + double Evaluate(const PredictorsType& predictors, + const ResponsesType& responses); /** * Evaluate the feedforward network with the given parameters. This function @@ -313,7 +315,8 @@ class FFN * @param inputs The input data. * @param results The predicted results. */ - void Forward(arma::mat inputs, arma::mat& results); + template + void Forward(const PredictorsType& inputs, ResponsesType& results); /** * Perform a partial forward pass of the data. @@ -326,8 +329,9 @@ class FFN * @param begin The index of the first layer. * @param end The index of the last layer. */ - void Forward(arma::mat inputs, - arma::mat& results, + template + void Forward(const PredictorsType& inputs , + ResponsesType& results, const size_t begin, const size_t end); @@ -342,7 +346,12 @@ class FFN * @param gradients Computed gradients. * @return Training error of the current pass. */ - double Backward(arma::mat targets, arma::mat& gradients); + template + double Backward(const PredictorsType& inputs, + const TargetsType& targets, + GradientsType& gradients); private: // Helper functions. @@ -352,7 +361,8 @@ class FFN * * @param input Data sequence to compute probabilities for. */ - void Forward(arma::mat&& input); + template + void Forward(const InputType& input); /** * Prepare the network for the given data. @@ -373,7 +383,8 @@ class FFN * Iterate through all layer modules and update the the gradient using the * layer defined optimizer. */ - void Gradient(arma::mat&& input); + template + void Gradient(const InputType& input); /** * Reset the module status by setting the current deterministic parameter @@ -427,9 +438,6 @@ class FFN //! The current error for the backward pass. arma::mat error; - //! THe current input of the forward/backward pass. - arma::mat currentInput; - //! Locally-stored delta visitor. DeltaVisitor deltaVisitor; @@ -496,7 +504,7 @@ template> { - BOOST_STATIC_CONSTANT(int, value = 1); + BOOST_STATIC_CONSTANT(int, value = 2); }; } // namespace serialization diff --git a/src/mlpack/methods/ann/ffn_impl.hpp b/src/mlpack/methods/ann/ffn_impl.hpp index f39bbfdb13..df0a07f75d 100644 --- a/src/mlpack/methods/ann/ffn_impl.hpp +++ b/src/mlpack/methods/ann/ffn_impl.hpp @@ -41,7 +41,7 @@ FFN::FFN( numFunctions(0), deterministic(true) { - /* Nothing to do here */ + /* Nothing to do here. */ } template::Train( template +template void FFN::Forward( - arma::mat inputs, arma::mat& results) + const PredictorsType& inputs, ResponsesType& results) { if (parameter.is_empty()) ResetParameters(); @@ -124,25 +125,28 @@ void FFN::Forward( ResetDeterministic(); } - currentInput = std::move(inputs); - Forward(std::move(currentInput)); + Forward(inputs); results = boost::apply_visitor(outputParameterVisitor, network.back()); } template +template void FFN::Forward( - arma::mat inputs, arma::mat& results, const size_t begin, const size_t end) + const PredictorsType& inputs, + ResponsesType& results, + const size_t begin, + const size_t end) { - boost::apply_visitor(ForwardVisitor(std::move(inputs), std::move( - boost::apply_visitor(outputParameterVisitor, network[begin]))), + boost::apply_visitor(ForwardVisitor(inputs, + boost::apply_visitor(outputParameterVisitor, network[begin])), network[begin]); for (size_t i = 1; i < end - begin + 1; ++i) { - boost::apply_visitor(ForwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[begin + i - 1])), std::move( - boost::apply_visitor(outputParameterVisitor, network[begin + i]))), + boost::apply_visitor(ForwardVisitor(boost::apply_visitor( + outputParameterVisitor, network[begin + i - 1]), + boost::apply_visitor(outputParameterVisitor, network[begin + i])), network[begin + i]); } @@ -151,25 +155,28 @@ void FFN::Forward( template +template double FFN::Backward( - arma::mat targets, arma::mat& gradients) + const PredictorsType& inputs, + const TargetsType& targets, + GradientsType& gradients) { - double res = outputLayer.Forward(std::move(boost::apply_visitor( - outputParameterVisitor, network.back())), std::move(targets)); + double res = outputLayer.Forward(boost::apply_visitor( + outputParameterVisitor, network.back()), targets); for (size_t i = 0; i < network.size(); ++i) { res += boost::apply_visitor(lossVisitor, network[i]); } - outputLayer.Backward(std::move(boost::apply_visitor(outputParameterVisitor, - network.back())), std::move(targets), std::move(error)); + outputLayer.Backward(boost::apply_visitor(outputParameterVisitor, + network.back()), targets, error); gradients = arma::zeros(parameter.n_rows, parameter.n_cols); Backward(); ResetGradients(gradients); - Gradient(std::move(currentInput)); + Gradient(inputs); return res; } @@ -189,8 +196,7 @@ void FFN::Predict( } arma::mat resultsTemp; - Forward(std::move(arma::mat(predictors.colptr(0), - predictors.n_rows, 1, false, true))); + Forward(arma::mat(predictors.colptr(0), predictors.n_rows, 1, false, true)); resultsTemp = boost::apply_visitor(outputParameterVisitor, network.back()).col(0); @@ -199,8 +205,7 @@ void FFN::Predict( for (size_t i = 1; i < predictors.n_cols; i++) { - Forward(std::move(arma::mat(predictors.colptr(i), - predictors.n_rows, 1, false, true))); + Forward(arma::mat(predictors.colptr(i), predictors.n_rows, 1, false, true)); resultsTemp = boost::apply_visitor(outputParameterVisitor, network.back()); @@ -210,8 +215,9 @@ void FFN::Predict( template +template double FFN::Evaluate( - arma::mat predictors, arma::mat responses) + const PredictorsType& predictors, const ResponsesType& responses) { if (parameter.is_empty()) ResetParameters(); @@ -222,10 +228,10 @@ double FFN::Evaluate( ResetDeterministic(); } - Forward(std::move(predictors)); + Forward(predictors); - double res = outputLayer.Forward(std::move(boost::apply_visitor( - outputParameterVisitor, network.back())), std::move(responses)); + double res = outputLayer.Forward(boost::apply_visitor( + outputParameterVisitor, network.back()), responses); for (size_t i = 0; i < network.size(); ++i) { @@ -264,10 +270,10 @@ double FFN::Evaluate( ResetDeterministic(); } - Forward(std::move(predictors.cols(begin, begin + batchSize - 1))); + Forward(predictors.cols(begin, begin + batchSize - 1)); double res = outputLayer.Forward( - std::move(boost::apply_visitor(outputParameterVisitor, network.back())), - std::move(responses.cols(begin, begin + batchSize - 1))); + boost::apply_visitor(outputParameterVisitor, network.back()), + responses.cols(begin, begin + batchSize - 1)); for (size_t i = 0; i < network.size(); ++i) { @@ -325,10 +331,10 @@ EvaluateWithGradient(const arma::mat& /* parameters */, ResetDeterministic(); } - Forward(std::move(predictors.cols(begin, begin + batchSize - 1))); + Forward(predictors.cols(begin, begin + batchSize - 1)); double res = outputLayer.Forward( - std::move(boost::apply_visitor(outputParameterVisitor, network.back())), - std::move(responses.cols(begin, begin + batchSize - 1))); + boost::apply_visitor(outputParameterVisitor, network.back()), + responses.cols(begin, begin + batchSize - 1)); for (size_t i = 0; i < network.size(); ++i) { @@ -336,13 +342,13 @@ EvaluateWithGradient(const arma::mat& /* parameters */, } outputLayer.Backward( - std::move(boost::apply_visitor(outputParameterVisitor, network.back())), - std::move(responses.cols(begin, begin + batchSize - 1)), - std::move(error)); + boost::apply_visitor(outputParameterVisitor, network.back()), + responses.cols(begin, begin + batchSize - 1), + error); Backward(); ResetGradients(gradient); - Gradient(std::move(predictors.cols(begin, begin + batchSize - 1))); + Gradient(predictors.cols(begin, begin + batchSize - 1)); return res; } @@ -396,18 +402,19 @@ void FFN +template void FFN::Forward(arma::mat&& input) + CustomLayers...>::Forward(const InputType& input) { - boost::apply_visitor(ForwardVisitor(std::move(input), std::move( - boost::apply_visitor(outputParameterVisitor, network.front()))), + boost::apply_visitor(ForwardVisitor(input, + boost::apply_visitor(outputParameterVisitor, network.front())), network.front()); if (!reset) @@ -434,9 +441,9 @@ void FFN void FFN::Backward() { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network.back())), std::move(error), std::move( - boost::apply_visitor(deltaVisitor, network.back()))), network.back()); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, network.back()), error, + boost::apply_visitor(deltaVisitor, network.back())), network.back()); for (size_t i = 2; i < network.size(); ++i) { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[network.size() - i])), std::move( - boost::apply_visitor(deltaVisitor, network[network.size() - i + 1])), - std::move(boost::apply_visitor(deltaVisitor, - network[network.size() - i]))), network[network.size() - i]); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, network[network.size() - i]), + boost::apply_visitor(deltaVisitor, network[network.size() - i + 1]), + boost::apply_visitor(deltaVisitor, network[network.size() - i])), + network[network.size() - i]); } } template +template void FFN::Gradient(arma::mat&& input) + CustomLayers...>::Gradient(const InputType& input) { - boost::apply_visitor(GradientVisitor(std::move(input), std::move( - boost::apply_visitor(deltaVisitor, network[1]))), network.front()); + boost::apply_visitor(GradientVisitor(input, + boost::apply_visitor(deltaVisitor, network[1])), network.front()); for (size_t i = 1; i < network.size() - 1; ++i) { - boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[i - 1])), std::move( - boost::apply_visitor(deltaVisitor, network[i + 1]))), network[i]); + boost::apply_visitor(GradientVisitor(boost::apply_visitor( + outputParameterVisitor, network[i - 1]), + boost::apply_visitor(deltaVisitor, network[i + 1])), network[i]); } - boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[network.size() - 2])), std::move(error)), + boost::apply_visitor(GradientVisitor(boost::apply_visitor( + outputParameterVisitor, network[network.size() - 2]), error), network[network.size() - 1]); } @@ -505,7 +513,13 @@ void FFN::serialize( ar & BOOST_SERIALIZATION_NVP(parameter); ar & BOOST_SERIALIZATION_NVP(width); ar & BOOST_SERIALIZATION_NVP(height); - ar & BOOST_SERIALIZATION_NVP(currentInput); + + // Early versions used the currentInput member, which is now no longer needed. + if (version < 2) + { + arma::mat currentInput; // Temporary matrix to output. + ar & BOOST_SERIALIZATION_NVP(currentInput); + } // Earlier versions of the FFN code did not serialize whether or not the model // was reset. @@ -535,8 +549,8 @@ void FFN::serialize( size_t offset = 0; for (size_t i = 0; i < network.size(); ++i) { - offset += boost::apply_visitor(WeightSetVisitor(std::move(parameter), - offset), network[i]); + offset += boost::apply_visitor(WeightSetVisitor(parameter, offset), + network[i]); boost::apply_visitor(resetVisitor, network[i]); } @@ -562,7 +576,6 @@ void FFN::FFN( parameter(network.parameter), numFunctions(network.numFunctions), error(network.error), - currentInput(network.currentInput), deterministic(network.deterministic), delta(network.delta), inputParameter(network.inputParameter), @@ -613,7 +625,6 @@ FFN::FFN( parameter(std::move(network.parameter)), numFunctions(network.numFunctions), error(std::move(network.error)), - currentInput(std::move(network.currentInput)), deterministic(network.deterministic), delta(std::move(network.delta)), inputParameter(std::move(network.inputParameter)), diff --git a/src/mlpack/methods/ann/gan/gan.hpp b/src/mlpack/methods/ann/gan/gan.hpp index e90396c98b..b98c17f03c 100644 --- a/src/mlpack/methods/ann/gan/gan.hpp +++ b/src/mlpack/methods/ann/gan/gan.hpp @@ -289,7 +289,7 @@ class GAN * * @param input Sampled noise. */ - void Forward(arma::mat&& input); + void Forward(const arma::mat& input); /** * This function predicts the output of the network on the given input. diff --git a/src/mlpack/methods/ann/gan/gan_impl.hpp b/src/mlpack/methods/ann/gan/gan_impl.hpp index 9e45b63a30..6f622856b4 100644 --- a/src/mlpack/methods/ann/gan/gan_impl.hpp +++ b/src/mlpack/methods/ann/gan/gan_impl.hpp @@ -258,28 +258,27 @@ GAN::Evaluate( currentTarget = arma::mat(responses.memptr() + i, 1, batchSize, false, false); - discriminator.Forward(std::move(currentInput)); + discriminator.Forward(currentInput); double res = discriminator.outputLayer.Forward( - std::move(boost::apply_visitor( + boost::apply_visitor( outputParameterVisitor, - discriminator.network.back())), std::move(currentTarget)); + discriminator.network.back()), currentTarget); noise.imbue( [&]() { return noiseFunction();} ); - generator.Forward(std::move(noise)); + generator.Forward(noise); predictors.cols(numFunctions, numFunctions + batchSize - 1) = boost::apply_visitor(outputParameterVisitor, generator.network.back()); - discriminator.Forward(std::move(predictors.cols(numFunctions, - numFunctions + batchSize - 1))); + discriminator.Forward(predictors.cols(numFunctions, + numFunctions + batchSize - 1)); responses.cols(numFunctions, numFunctions + batchSize - 1) = arma::zeros(1, batchSize); currentTarget = arma::mat(responses.memptr() + numFunctions, 1, batchSize, false, false); res += discriminator.outputLayer.Forward( - std::move(boost::apply_visitor( - outputParameterVisitor, - discriminator.network.back())), std::move(currentTarget)); + boost::apply_visitor(outputParameterVisitor, + discriminator.network.back()), currentTarget); return res; } @@ -341,7 +340,7 @@ EvaluateWithGradient(const arma::mat& /* parameters */, i, gradientDiscriminator, batchSize); noise.imbue( [&]() { return noiseFunction();} ); - generator.Forward(std::move(noise)); + generator.Forward(noise); predictors.cols(numFunctions, numFunctions + batchSize - 1) = boost::apply_visitor(outputParameterVisitor, generator.network.back()); responses.cols(numFunctions, numFunctions + batchSize - 1) = @@ -419,18 +418,18 @@ template< typename PolicyType > void GAN::Forward( - arma::mat&& input) + const arma::mat& input) { if (parameter.is_empty()) { Reset(); } - generator.Forward(std::move(input)); + generator.Forward(input); arma::mat ganOutput = boost::apply_visitor(outputParameterVisitor, generator.network.back()); - discriminator.Forward(std::move(ganOutput)); + discriminator.Forward(ganOutput); } template< @@ -453,7 +452,7 @@ Predict(arma::mat input, arma::mat& output) ResetDeterministic(); } - Forward(std::move(input)); + Forward(input); output = boost::apply_visitor(outputParameterVisitor, discriminator.network.back()); @@ -502,8 +501,8 @@ serialize(Archive& ar, const unsigned int /* version */) size_t offset = 0; for (size_t i = 0; i < generator.network.size(); ++i) { - offset += boost::apply_visitor(WeightSetVisitor(std::move( - generator.parameter), offset), generator.network[i]); + offset += boost::apply_visitor(WeightSetVisitor( + generator.parameter, offset), generator.network[i]); boost::apply_visitor(resetVisitor, generator.network[i]); } @@ -511,8 +510,8 @@ serialize(Archive& ar, const unsigned int /* version */) offset = 0; for (size_t i = 0; i < discriminator.network.size(); ++i) { - offset += boost::apply_visitor(WeightSetVisitor(std::move( - discriminator.parameter), offset), discriminator.network[i]); + offset += boost::apply_visitor(WeightSetVisitor( + discriminator.parameter, offset), discriminator.network[i]); boost::apply_visitor(resetVisitor, discriminator.network[i]); } diff --git a/src/mlpack/methods/ann/gan/wgan_impl.hpp b/src/mlpack/methods/ann/gan/wgan_impl.hpp index 76073ef3b1..b21d685b68 100644 --- a/src/mlpack/methods/ann/gan/wgan_impl.hpp +++ b/src/mlpack/methods/ann/gan/wgan_impl.hpp @@ -50,28 +50,28 @@ GAN::Evaluate( currentTarget = arma::mat(responses.memptr() + i, 1, batchSize, false, false); - discriminator.Forward(std::move(currentInput)); + discriminator.Forward(currentInput); double res = discriminator.outputLayer.Forward( - std::move(boost::apply_visitor( + boost::apply_visitor( outputParameterVisitor, - discriminator.network.back())), std::move(currentTarget)); + discriminator.network.back()), currentTarget); noise.imbue( [&]() { return noiseFunction();} ); - generator.Forward(std::move(noise)); + generator.Forward(noise); predictors.cols(numFunctions, numFunctions + batchSize - 1) = boost::apply_visitor(outputParameterVisitor, generator.network.back()); - discriminator.Forward(std::move(predictors.cols(numFunctions, - numFunctions + batchSize - 1))); + discriminator.Forward(predictors.cols(numFunctions, + numFunctions + batchSize - 1)); responses.cols(numFunctions, numFunctions + batchSize - 1) = -arma::ones(1, batchSize); currentTarget = arma::mat(responses.memptr() + numFunctions, 1, batchSize, false, false); res += discriminator.outputLayer.Forward( - std::move(boost::apply_visitor( + boost::apply_visitor( outputParameterVisitor, - discriminator.network.back())), std::move(currentTarget)); + discriminator.network.back()), currentTarget); return res; } @@ -132,7 +132,7 @@ EvaluateWithGradient(const arma::mat& /* parameters */, i, gradientDiscriminator, batchSize); noise.imbue( [&]() { return noiseFunction();} ); - generator.Forward(std::move(noise)); + generator.Forward(noise); predictors.cols(numFunctions, numFunctions + batchSize - 1) = boost::apply_visitor(outputParameterVisitor, generator.network.back()); responses.cols(numFunctions, numFunctions + batchSize - 1) = diff --git a/src/mlpack/methods/ann/init_rules/network_init.hpp b/src/mlpack/methods/ann/init_rules/network_init.hpp index 45232e3a67..68e75789e5 100644 --- a/src/mlpack/methods/ann/init_rules/network_init.hpp +++ b/src/mlpack/methods/ann/init_rules/network_init.hpp @@ -92,8 +92,8 @@ class NetworkInitialization // hold various other modules. for (size_t i = 0, offset = parameterOffset; i < network.size(); ++i) { - offset += boost::apply_visitor(WeightSetVisitor(std::move(parameter), - offset), network[i]); + offset += boost::apply_visitor(WeightSetVisitor(parameter, offset), + network[i]); boost::apply_visitor(resetVisitor, network[i]); } diff --git a/src/mlpack/methods/ann/layer/add.hpp b/src/mlpack/methods/ann/layer/add.hpp index 47de360eed..12a121414b 100644 --- a/src/mlpack/methods/ann/layer/add.hpp +++ b/src/mlpack/methods/ann/layer/add.hpp @@ -49,7 +49,7 @@ class Add * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -61,9 +61,9 @@ class Add * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - const arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /** * Calculate the gradient using the output delta and the input activation. @@ -73,9 +73,9 @@ class Add * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& gradient); //! Get the parameters. OutputDataType const& Parameters() const { return weights; } diff --git a/src/mlpack/methods/ann/layer/add_impl.hpp b/src/mlpack/methods/ann/layer/add_impl.hpp index dda53d94dd..10e7ca3a1c 100644 --- a/src/mlpack/methods/ann/layer/add_impl.hpp +++ b/src/mlpack/methods/ann/layer/add_impl.hpp @@ -29,7 +29,7 @@ Add::Add(const size_t outSize) : template template void Add::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { output = input; output.each_col() += weights; @@ -38,9 +38,9 @@ void Add::Forward( template template void Add::Backward( - const arma::Mat&& /* input */, - const arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { g = gy; } @@ -48,9 +48,9 @@ void Add::Backward( template template void Add::Gradient( - const arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& gradient) { gradient = error; } diff --git a/src/mlpack/methods/ann/layer/add_merge.hpp b/src/mlpack/methods/ann/layer/add_merge.hpp index 28ffb2359f..cd92cdaec3 100644 --- a/src/mlpack/methods/ann/layer/add_merge.hpp +++ b/src/mlpack/methods/ann/layer/add_merge.hpp @@ -61,7 +61,7 @@ class AddMerge * @param output Resulting output activation. */ template - void Forward(InputType&& /* input */, OutputType&& output); + void Forward(const InputType& /* input */, OutputType& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -73,9 +73,9 @@ class AddMerge * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /** * This is the overload of Backward() that runs only a specific layer with @@ -87,9 +87,9 @@ class AddMerge * @param The index of the layer to run. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g, + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g, const size_t index); /* @@ -100,9 +100,9 @@ class AddMerge * @param gradient The calculated gradient. */ template - void Gradient(arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient); /* * This is the overload of Gradient() that runs a specific layer with the @@ -114,9 +114,9 @@ class AddMerge * @param The index of the layer to run. */ template - void Gradient(arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient, + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient, const size_t index); /* diff --git a/src/mlpack/methods/ann/layer/add_merge_impl.hpp b/src/mlpack/methods/ann/layer/add_merge_impl.hpp index 9fd717a2d1..a4985577f8 100644 --- a/src/mlpack/methods/ann/layer/add_merge_impl.hpp +++ b/src/mlpack/methods/ann/layer/add_merge_impl.hpp @@ -47,14 +47,14 @@ template template void AddMerge::Forward( - InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { if (run) { for (size_t i = 0; i < network.size(); ++i) { - boost::apply_visitor(ForwardVisitor(std::move(input), std::move( - boost::apply_visitor(outputParameterVisitor, network[i]))), + boost::apply_visitor(ForwardVisitor(input, + boost::apply_visitor(outputParameterVisitor, network[i])), network[i]); } } @@ -70,15 +70,17 @@ template template void AddMerge::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { if (run) { for (size_t i = 0; i < network.size(); ++i) { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[i])), std::move(gy), std::move( - boost::apply_visitor(deltaVisitor, network[i]))), network[i]); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, network[i]), gy, + boost::apply_visitor(deltaVisitor, network[i])), network[i]); } g = boost::apply_visitor(deltaVisitor, network[0]); @@ -95,12 +97,14 @@ template template void AddMerge::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g, + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g, const size_t index) { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[index])), std::move(gy), std::move( - boost::apply_visitor(deltaVisitor, network[index]))), network[index]); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, network[index]), gy, + boost::apply_visitor(deltaVisitor, network[index])), network[index]); g = boost::apply_visitor(deltaVisitor, network[index]); } @@ -108,16 +112,15 @@ template template void AddMerge::Gradient( - arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& /* gradient */ ) + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& /* gradient */ ) { if (run) { for (size_t i = 0; i < network.size(); ++i) { - boost::apply_visitor(GradientVisitor(std::move(input), std::move(error)), - network[i]); + boost::apply_visitor(GradientVisitor(input, error), network[i]); } } } @@ -126,13 +129,12 @@ template template void AddMerge::Gradient( - arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& /* gradient */, + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& /* gradient */, const size_t index) { - boost::apply_visitor(GradientVisitor(std::move(input), std::move(error)), - network[index]); + boost::apply_visitor(GradientVisitor(input, error), network[index]); } template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of the alpha_dropout layer. @@ -75,9 +75,9 @@ class AlphaDropout * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/alpha_dropout_impl.hpp b/src/mlpack/methods/ann/layer/alpha_dropout_impl.hpp index 78a7790536..fdeac03952 100644 --- a/src/mlpack/methods/ann/layer/alpha_dropout_impl.hpp +++ b/src/mlpack/methods/ann/layer/alpha_dropout_impl.hpp @@ -36,7 +36,7 @@ AlphaDropout::AlphaDropout( template template void AlphaDropout::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { // The dropout mask will not be multiplied in the deterministic mode // (during testing). @@ -58,7 +58,7 @@ void AlphaDropout::Forward( template template void AlphaDropout::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { g = gy % mask * a; } diff --git a/src/mlpack/methods/ann/layer/atrous_convolution.hpp b/src/mlpack/methods/ann/layer/atrous_convolution.hpp index 36d4f9618b..34d16cabb4 100644 --- a/src/mlpack/methods/ann/layer/atrous_convolution.hpp +++ b/src/mlpack/methods/ann/layer/atrous_convolution.hpp @@ -137,7 +137,7 @@ class AtrousConvolution * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -149,9 +149,9 @@ class AtrousConvolution * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /* * Calculate the gradient using the output delta and the input activation. @@ -161,9 +161,9 @@ class AtrousConvolution * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& gradient); //! Get the parameters. const OutputDataType& Parameters() const { return weights; } diff --git a/src/mlpack/methods/ann/layer/atrous_convolution_impl.hpp b/src/mlpack/methods/ann/layer/atrous_convolution_impl.hpp index 32362f71a2..33d854b5b3 100644 --- a/src/mlpack/methods/ann/layer/atrous_convolution_impl.hpp +++ b/src/mlpack/methods/ann/layer/atrous_convolution_impl.hpp @@ -184,10 +184,10 @@ void AtrousConvolution< GradientConvolutionRule, InputDataType, OutputDataType ->::Forward(const arma::Mat&& input, arma::Mat&& output) +>::Forward(const arma::Mat& input, arma::Mat& output) { batchSize = input.n_cols; - inputTemp = arma::cube(const_cast&&>(input).memptr(), + inputTemp = arma::cube(const_cast&>(input).memptr(), inputWidth, inputHeight, inSize * batchSize, false, false); if (padding.PadWLeft() != 0 || padding.PadWRight() != 0 || @@ -200,8 +200,7 @@ void AtrousConvolution< for (size_t i = 0; i < inputTemp.n_slices; ++i) { - padding.Forward(std::move(inputTemp.slice(i)), - std::move(inputPaddedTemp.slice(i))); + padding.Forward(inputTemp.slice(i), inputPaddedTemp.slice(i)); } } @@ -267,10 +266,10 @@ void AtrousConvolution< InputDataType, OutputDataType >::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { - arma::cube mappedError(gy.memptr(), outputWidth, outputHeight, - outSize * batchSize, false, false); + arma::cube mappedError(((arma::Mat&) gy).memptr(), outputWidth, + outputHeight, outSize * batchSize, false, false); g.set_size(inputTemp.n_rows * inputTemp.n_cols * inSize, batchSize); gTemp = arma::Cube(g.memptr(), inputTemp.n_rows, @@ -326,12 +325,12 @@ void AtrousConvolution< InputDataType, OutputDataType >::Gradient( - const arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& gradient) { - arma::cube mappedError(error.memptr(), outputWidth, outputHeight, - outSize * batchSize, false, false); + arma::cube mappedError(((arma::Mat&) error).memptr(), outputWidth, + outputHeight, outSize * batchSize, false, false); gradient.set_size(weights.n_elem, 1); gradientTemp = arma::Cube(gradient.memptr(), weight.n_rows, diff --git a/src/mlpack/methods/ann/layer/base_layer.hpp b/src/mlpack/methods/ann/layer/base_layer.hpp index c08185488b..6559845d43 100644 --- a/src/mlpack/methods/ann/layer/base_layer.hpp +++ b/src/mlpack/methods/ann/layer/base_layer.hpp @@ -69,7 +69,7 @@ class BaseLayer * @param output Resulting output activation. */ template - void Forward(const InputType&& input, OutputType&& output) + void Forward(const InputType& input, OutputType& output) { ActivationFunction::Fn(input, output); } @@ -84,9 +84,9 @@ class BaseLayer * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& input, - arma::Mat&& gy, - arma::Mat&& g) + void Backward(const arma::Mat& input, + const arma::Mat& gy, + arma::Mat& g) { arma::Mat derivative; ActivationFunction::Deriv(input, derivative); diff --git a/src/mlpack/methods/ann/layer/batch_norm.hpp b/src/mlpack/methods/ann/layer/batch_norm.hpp index e22facb9fe..80f220f15a 100644 --- a/src/mlpack/methods/ann/layer/batch_norm.hpp +++ b/src/mlpack/methods/ann/layer/batch_norm.hpp @@ -81,7 +81,7 @@ class BatchNorm * @param output Resulting output activations. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Backward pass through the layer. @@ -91,9 +91,9 @@ class BatchNorm * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& input, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& input, + const arma::Mat& gy, + arma::Mat& g); /** * Calculate the gradient using the output delta and the input activations. @@ -103,9 +103,9 @@ class BatchNorm * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient); //! Get the parameters. OutputDataType const& Parameters() const { return weights; } diff --git a/src/mlpack/methods/ann/layer/batch_norm_impl.hpp b/src/mlpack/methods/ann/layer/batch_norm_impl.hpp index 3c1922dce1..81c75d1997 100644 --- a/src/mlpack/methods/ann/layer/batch_norm_impl.hpp +++ b/src/mlpack/methods/ann/layer/batch_norm_impl.hpp @@ -63,7 +63,7 @@ void BatchNorm::Reset() template template void BatchNorm::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { // Mean and variance over the entire training set will be used to compute // the forward pass when deterministic is set to true. @@ -106,7 +106,7 @@ void BatchNorm::Forward( template template void BatchNorm::Backward( - const arma::Mat&& input, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& input, const arma::Mat& gy, arma::Mat& g) { const arma::mat stdInv = 1.0 / arma::sqrt(variance + eps); @@ -130,9 +130,9 @@ void BatchNorm::Backward( template template void BatchNorm::Gradient( - const arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& gradient) { gradient.set_size(size + size, 1); diff --git a/src/mlpack/methods/ann/layer/bilinear_interpolation.hpp b/src/mlpack/methods/ann/layer/bilinear_interpolation.hpp index 9ea1e20bbc..c80f0f4b57 100644 --- a/src/mlpack/methods/ann/layer/bilinear_interpolation.hpp +++ b/src/mlpack/methods/ann/layer/bilinear_interpolation.hpp @@ -65,7 +65,7 @@ class BilinearInterpolation * @param output The resulting interpolated output matrix. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -79,9 +79,9 @@ class BilinearInterpolation * @param output The resulting down-sampled output. */ template - void Backward(const arma::Mat&& /*input*/, - arma::Mat&& gradient, - arma::Mat&& output); + void Backward(const arma::Mat& /*input*/, + const arma::Mat& gradient, + arma::Mat& output); //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/bilinear_interpolation_impl.hpp b/src/mlpack/methods/ann/layer/bilinear_interpolation_impl.hpp index bd22e2684a..e2e1c005e7 100644 --- a/src/mlpack/methods/ann/layer/bilinear_interpolation_impl.hpp +++ b/src/mlpack/methods/ann/layer/bilinear_interpolation_impl.hpp @@ -54,7 +54,7 @@ BilinearInterpolation( template template void BilinearInterpolation::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { batchSize = input.n_cols; if (output.is_empty()) @@ -68,7 +68,7 @@ void BilinearInterpolation::Forward( assert(inRowSize >= 2); assert(inColSize >= 2); - arma::cube inputAsCube(const_cast&&>(input).memptr(), + arma::cube inputAsCube(const_cast&>(input).memptr(), inRowSize, inColSize, depth * batchSize, false, false); arma::cube outputAsCube(output.memptr(), outRowSize, outColSize, depth * batchSize, false, true); @@ -114,9 +114,9 @@ void BilinearInterpolation::Forward( template template void BilinearInterpolation::Backward( - const arma::Mat&& /*input*/, - arma::Mat&& gradient, - arma::Mat&& output) + const arma::Mat& /*input*/, + const arma::Mat& gradient, + arma::Mat& output) { if (output.is_empty()) output.set_size(inRowSize * inColSize * depth, batchSize); @@ -129,8 +129,8 @@ void BilinearInterpolation::Backward( assert(outRowSize >= 2); assert(outColSize >= 2); - arma::cube gradientAsCube(gradient.memptr(), outRowSize, outColSize, - depth * batchSize, false, false); + arma::cube gradientAsCube(((arma::Mat&) gradient).memptr(), outRowSize, + outColSize, depth * batchSize, false, false); arma::cube outputAsCube(output.memptr(), inRowSize, inColSize, depth * batchSize, false, true); diff --git a/src/mlpack/methods/ann/layer/c_relu.hpp b/src/mlpack/methods/ann/layer/c_relu.hpp index 5108646116..b9812bb4c0 100644 --- a/src/mlpack/methods/ann/layer/c_relu.hpp +++ b/src/mlpack/methods/ann/layer/c_relu.hpp @@ -63,7 +63,7 @@ class CReLU * @param output Resulting output activation. */ template - void Forward(const InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -75,7 +75,7 @@ class CReLU * @param g The calculated gradient. */ template - void Backward(const DataType&& input, DataType&& gy, DataType&& g); + void Backward(const DataType& input, const DataType& gy, DataType& g); //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/c_relu_impl.hpp b/src/mlpack/methods/ann/layer/c_relu_impl.hpp index e839526976..1bac0a179d 100644 --- a/src/mlpack/methods/ann/layer/c_relu_impl.hpp +++ b/src/mlpack/methods/ann/layer/c_relu_impl.hpp @@ -27,7 +27,7 @@ CReLU::CReLU() template template void CReLU::Forward( - const InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { output = arma::join_cols(arma::max(input, 0.0 * input), arma::max( (-1 * input), 0.0 * input)); @@ -36,7 +36,7 @@ void CReLU::Forward( template template void CReLU::Backward( - const DataType&& input, DataType&& gy, DataType&& g) + const DataType& input, const DataType& gy, DataType& g) { DataType temp; temp = gy % (input >= 0.0); diff --git a/src/mlpack/methods/ann/layer/concat.hpp b/src/mlpack/methods/ann/layer/concat.hpp index d180681f94..7716ee9207 100644 --- a/src/mlpack/methods/ann/layer/concat.hpp +++ b/src/mlpack/methods/ann/layer/concat.hpp @@ -80,7 +80,7 @@ class Concat * @param output Resulting output activation. */ template - void Forward(arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, using 3rd-order tensors as @@ -92,9 +92,9 @@ class Concat * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /** * This is the overload of Backward() that runs only a specific layer with @@ -106,9 +106,9 @@ class Concat * @param The index of the layer to run. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g, + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g, const size_t index); /* @@ -119,9 +119,9 @@ class Concat * @param gradient The calculated gradient. */ template - void Gradient(arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& /* gradient */); + void Gradient(const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& /* gradient */); /* * This is the overload of Gradient() that runs a specific layer with the @@ -133,9 +133,9 @@ class Concat * @param The index of the layer to run. */ template - void Gradient(arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient, + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient, const size_t index); /* diff --git a/src/mlpack/methods/ann/layer/concat_impl.hpp b/src/mlpack/methods/ann/layer/concat_impl.hpp index 7c2e9a82fb..ff69d7e0e8 100644 --- a/src/mlpack/methods/ann/layer/concat_impl.hpp +++ b/src/mlpack/methods/ann/layer/concat_impl.hpp @@ -99,14 +99,14 @@ template template void Concat::Forward( - arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { if (run) { for (size_t i = 0; i < network.size(); ++i) { - boost::apply_visitor(ForwardVisitor(std::move(input), std::move( - boost::apply_visitor(outputParameterVisitor, network[i]))), + boost::apply_visitor(ForwardVisitor(input, + boost::apply_visitor(outputParameterVisitor, network[i])), network[i]); } } @@ -134,13 +134,14 @@ template template void Concat::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { size_t rowCount = 0; if (run) { arma::Mat delta; - gy.reshape(gy.n_rows / channels, gy.n_cols * channels); + arma::Mat gyTmp(((arma::Mat&) gy).memptr(), gy.n_rows / channels, + gy.n_cols * channels, false, false); for (size_t i = 0; i < network.size(); ++i) { // Use rows from the error corresponding to the output from each layer. @@ -148,13 +149,13 @@ void Concat::Backward( outputParameterVisitor, network[i]).n_rows; // Extract from gy the parameters for the i-th network. - delta = gy.rows(rowCount / channels, (rowCount + rows) / channels - 1); + delta = gyTmp.rows(rowCount / channels, (rowCount + rows) / channels - 1); delta.reshape(delta.n_rows * channels, delta.n_cols / channels); - boost::apply_visitor(BackwardVisitor(std::move( + boost::apply_visitor(BackwardVisitor( boost::apply_visitor(outputParameterVisitor, - network[i])), std::move(delta), std::move( - boost::apply_visitor(deltaVisitor, network[i]))), network[i]); + network[i]), delta, + boost::apply_visitor(deltaVisitor, network[i])), network[i]); rowCount += rows; } @@ -163,7 +164,6 @@ void Concat::Backward( { g += boost::apply_visitor(deltaVisitor, network[i]); } - gy.reshape(gy.n_rows * channels, gy.n_cols / channels); } else { @@ -175,7 +175,9 @@ template template void Concat::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g, + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g, const size_t index) { size_t rowCount = 0, rows = 0; @@ -188,18 +190,16 @@ void Concat::Backward( rows = boost::apply_visitor(outputParameterVisitor, network[index]).n_rows; // Reshape gy to extract the i-th layer gy. - gy.reshape(gy.n_rows / channels, gy.n_cols * channels); + arma::Mat gyTmp(((arma::Mat&) gy).memptr(), gy.n_rows / channels, + gy.n_cols * channels, false, false); - arma::Mat delta = gy.rows(rowCount / channels, (rowCount + rows) / + arma::Mat delta = gyTmp.rows(rowCount / channels, (rowCount + rows) / channels - 1); delta.reshape(delta.n_rows * channels, delta.n_cols / channels); - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[index])), std::move(delta), std::move( - boost::apply_visitor(deltaVisitor, network[index]))), network[index]); - - // Reshape gy to its original shape. - gy.reshape(gy.n_rows * channels, gy.n_cols / channels); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, network[index]), delta, + boost::apply_visitor(deltaVisitor, network[index])), network[index]); g = boost::apply_visitor(deltaVisitor, network[index]); } @@ -208,32 +208,29 @@ template template void Concat::Gradient( - arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& /* gradient */) + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& /* gradient */) { if (run) { size_t rowCount = 0; // Reshape error to extract the i-th layer error. - error.reshape(error.n_rows / channels, error.n_cols * channels); + arma::Mat errorTmp(((arma::Mat&) error).memptr(), + error.n_rows / channels, error.n_cols * channels, false, false); for (size_t i = 0; i < network.size(); ++i) { size_t rows = boost::apply_visitor( outputParameterVisitor, network[i]).n_rows; // Extract from error the parameters for the i-th network. - arma::Mat err = error.rows(rowCount / channels, (rowCount + rows) / + arma::Mat err = errorTmp.rows(rowCount / channels, (rowCount + rows) / channels - 1); err.reshape(err.n_rows * channels, err.n_cols / channels); - boost::apply_visitor(GradientVisitor(std::move(input), - std::move(err)), network[i]); + boost::apply_visitor(GradientVisitor(input, err), network[i]); rowCount += rows; } - - // Reshape error to its original shape. - error.reshape(error.n_rows * channels, error.n_cols / channels); } } @@ -241,9 +238,9 @@ template template void Concat::Gradient( - arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& /* gradient */, + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& /* gradient */, const size_t index) { size_t rowCount = 0; @@ -255,15 +252,13 @@ void Concat::Gradient( size_t rows = boost::apply_visitor( outputParameterVisitor, network[index]).n_rows; - error.reshape(error.n_rows / channels, error.n_cols * channels); - arma::Mat err = error.rows(rowCount / channels, (rowCount + rows) / + arma::Mat errorTmp(((arma::Mat&) error).memptr(), + error.n_rows / channels, error.n_cols * channels, false, false); + arma::Mat err = errorTmp.rows(rowCount / channels, (rowCount + rows) / channels - 1); err.reshape(err.n_rows * channels, err.n_cols / channels); - boost::apply_visitor(GradientVisitor(std::move(input), - std::move(err)), network[index]); - - error.reshape(error.n_rows * channels, error.n_cols / channels); + boost::apply_visitor(GradientVisitor(input, err), network[index]); } template - double Forward(const arma::Mat&& input, arma::Mat&& target); + double Forward(const arma::Mat& input, arma::Mat& target); + /** * Ordinary feed backward pass of a neural network. The negative log * likelihood layer expectes that the input contains log-probabilities for @@ -68,9 +69,9 @@ class ConcatPerformance * @param output The calculated error. */ template - void Backward(const arma::Mat&& input, - const arma::Mat&& target, - arma::Mat&& output); + void Backward(const arma::Mat& input, + const arma::Mat& target, + arma::Mat& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/concat_performance_impl.hpp b/src/mlpack/methods/ann/layer/concat_performance_impl.hpp index b4c2d6b9e1..7cf55dc41b 100644 --- a/src/mlpack/methods/ann/layer/concat_performance_impl.hpp +++ b/src/mlpack/methods/ann/layer/concat_performance_impl.hpp @@ -44,7 +44,7 @@ double ConcatPerformance< OutputLayerType, InputDataType, OutputDataType ->::Forward(const arma::Mat&& input, arma::Mat&& target) +>::Forward(const arma::Mat& input, arma::Mat& target) { const size_t elements = input.n_elem / inSize; @@ -52,7 +52,7 @@ double ConcatPerformance< for (size_t i = 0; i < input.n_elem; i+= elements) { arma::mat subInput = input.submat(i, 0, i + elements - 1, 0); - output += outputLayer.Forward(std::move(subInput), std::move(target)); + output += outputLayer.Forward(subInput, target); } return output; @@ -69,17 +69,16 @@ void ConcatPerformance< InputDataType, OutputDataType >::Backward( - const arma::Mat&& input, - const arma::Mat&& target, - arma::Mat&& output) + const arma::Mat& input, + const arma::Mat& target, + arma::Mat& output) { const size_t elements = input.n_elem / inSize; arma::mat subInput = input.submat(0, 0, elements - 1, 0); arma::mat subOutput; - outputLayer.Backward(std::move(subInput), std::move(target), - std::move(subOutput)); + outputLayer.Backward(subInput, target, subOutput); output = arma::zeros(subOutput.n_elem, inSize); output.col(0) = subOutput; @@ -87,8 +86,7 @@ void ConcatPerformance< for (size_t i = elements, j = 0; i < input.n_elem; i+= elements, j++) { subInput = input.submat(i, 0, i + elements - 1, 0); - outputLayer.Backward(std::move(subInput), std::move(target), - std::move(subOutput)); + outputLayer.Backward(subInput, target, subOutput); output.col(j) = subOutput; } diff --git a/src/mlpack/methods/ann/layer/concatenate.hpp b/src/mlpack/methods/ann/layer/concatenate.hpp index 77fd985cf4..232da963d6 100644 --- a/src/mlpack/methods/ann/layer/concatenate.hpp +++ b/src/mlpack/methods/ann/layer/concatenate.hpp @@ -49,7 +49,7 @@ class Concatenate * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -61,9 +61,9 @@ class Concatenate * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - const arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); //! Get the parameters. OutputDataType const& Parameters() const { return weights; } diff --git a/src/mlpack/methods/ann/layer/concatenate_impl.hpp b/src/mlpack/methods/ann/layer/concatenate_impl.hpp index c1e3139f67..743c9b8f2f 100644 --- a/src/mlpack/methods/ann/layer/concatenate_impl.hpp +++ b/src/mlpack/methods/ann/layer/concatenate_impl.hpp @@ -28,7 +28,7 @@ Concatenate::Concatenate() template template void Concatenate::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { if (concat.is_empty()) Log::Warn << "The concat matrix has not been provided." << std::endl; @@ -46,9 +46,9 @@ void Concatenate::Forward( template template void Concatenate::Backward( - const arma::Mat&& /* input */, - const arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { g = gy.submat(0, 0, inRows - 1, concat.n_cols - 1); } diff --git a/src/mlpack/methods/ann/layer/constant.hpp b/src/mlpack/methods/ann/layer/constant.hpp index 62ed80faa9..c3f84f8ab0 100644 --- a/src/mlpack/methods/ann/layer/constant.hpp +++ b/src/mlpack/methods/ann/layer/constant.hpp @@ -51,7 +51,7 @@ class Constant * @param output Resulting output activation. */ template - void Forward(const InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed backward pass of a neural network. The backward pass of the @@ -62,9 +62,9 @@ class Constant * @param g The calculated gradient. */ template - void Backward(const DataType&& /* input */, - DataType&& /* gy */, - DataType&& g); + void Backward(const DataType& /* input */, + const DataType& /* gy */, + DataType& g); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/constant_impl.hpp b/src/mlpack/methods/ann/layer/constant_impl.hpp index 429cd44805..8a842c7ff4 100644 --- a/src/mlpack/methods/ann/layer/constant_impl.hpp +++ b/src/mlpack/methods/ann/layer/constant_impl.hpp @@ -33,7 +33,7 @@ Constant::Constant( template template void Constant::Forward( - const InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { if (inSize == 0) { @@ -46,7 +46,7 @@ void Constant::Forward( template template void Constant::Backward( - const DataType&& /* input */, DataType&& /* gy */, DataType&& g) + const DataType& /* input */, const DataType& /* gy */, DataType& g) { g = arma::zeros(inSize, 1); } diff --git a/src/mlpack/methods/ann/layer/convolution.hpp b/src/mlpack/methods/ann/layer/convolution.hpp index 11352ec371..9e16594657 100644 --- a/src/mlpack/methods/ann/layer/convolution.hpp +++ b/src/mlpack/methods/ann/layer/convolution.hpp @@ -123,7 +123,7 @@ class Convolution * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -135,9 +135,9 @@ class Convolution * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /* * Calculate the gradient using the output delta and the input activation. @@ -147,9 +147,9 @@ class Convolution * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& gradient); //! Get the parameters. const OutputDataType& Parameters() const { return weights; } diff --git a/src/mlpack/methods/ann/layer/convolution_impl.hpp b/src/mlpack/methods/ann/layer/convolution_impl.hpp index 5f44836a3c..ebabb31380 100644 --- a/src/mlpack/methods/ann/layer/convolution_impl.hpp +++ b/src/mlpack/methods/ann/layer/convolution_impl.hpp @@ -71,7 +71,8 @@ Convolution< std::tuple(padW, padW), std::tuple(padH, padH), inputWidth, - inputHeight) + inputHeight, + paddingType) { // Nothing to do here. } @@ -174,10 +175,10 @@ void Convolution< GradientConvolutionRule, InputDataType, OutputDataType ->::Forward(const arma::Mat&& input, arma::Mat&& output) +>::Forward(const arma::Mat& input, arma::Mat& output) { batchSize = input.n_cols; - inputTemp = arma::cube(const_cast&&>(input).memptr(), + inputTemp = arma::cube(const_cast&>(input).memptr(), inputWidth, inputHeight, inSize * batchSize, false, false); if (padWLeft != 0 || padWRight != 0 || padHTop != 0 || padHBottom != 0) @@ -187,8 +188,7 @@ void Convolution< for (size_t i = 0; i < inputTemp.n_slices; ++i) { - padding.Forward(std::move(inputTemp.slice(i)), - std::move(inputPaddedTemp.slice(i))); + padding.Forward(inputTemp.slice(i), inputPaddedTemp.slice(i)); } } @@ -253,10 +253,10 @@ void Convolution< InputDataType, OutputDataType >::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { - arma::cube mappedError(gy.memptr(), outputWidth, outputHeight, - outSize * batchSize, false, false); + arma::cube mappedError(((arma::Mat&) gy).memptr(), outputWidth, + outputHeight, outSize * batchSize, false, false); g.set_size(inputTemp.n_rows * inputTemp.n_cols * inSize, batchSize); gTemp = arma::Cube(g.memptr(), inputTemp.n_rows, @@ -308,11 +308,11 @@ void Convolution< InputDataType, OutputDataType >::Gradient( - const arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& gradient) { - arma::cube mappedError(error.memptr(), outputWidth, + arma::cube mappedError(((arma::Mat&) error).memptr(), outputWidth, outputHeight, outSize * batchSize, false, false); gradient.set_size(weights.n_elem, 1); diff --git a/src/mlpack/methods/ann/layer/dropconnect.hpp b/src/mlpack/methods/ann/layer/dropconnect.hpp index 1ed6f2ce0c..2e0b446435 100644 --- a/src/mlpack/methods/ann/layer/dropconnect.hpp +++ b/src/mlpack/methods/ann/layer/dropconnect.hpp @@ -86,7 +86,7 @@ class DropConnect * @param output Resulting output activation. */ template - void Forward(arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of the DropConnect layer. @@ -96,9 +96,9 @@ class DropConnect * @param g The calculated gradient. */ template - void Backward(arma::Mat&& input, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& input, + const arma::Mat& gy, + arma::Mat& g); /** * Calculate the gradient using the output delta and the input activation. @@ -108,9 +108,9 @@ class DropConnect * @param g The calculated gradient. */ template - void Gradient(arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& /* gradient */); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& /* gradient */); //! Get the model modules. std::vector >& Model() { return network; } diff --git a/src/mlpack/methods/ann/layer/dropconnect_impl.hpp b/src/mlpack/methods/ann/layer/dropconnect_impl.hpp index 7a3713ac38..85b56ff51d 100644 --- a/src/mlpack/methods/ann/layer/dropconnect_impl.hpp +++ b/src/mlpack/methods/ann/layer/dropconnect_impl.hpp @@ -57,31 +57,29 @@ DropConnect::~DropConnect() template template void DropConnect::Forward( - arma::Mat&& input, - arma::Mat&& output) + const arma::Mat& input, + arma::Mat& output) { // The DropConnect mask will not be multiplied in the deterministic mode // (during testing). if (deterministic) { - boost::apply_visitor(ForwardVisitor(std::move(input), std::move(output)), - baseLayer); + boost::apply_visitor(ForwardVisitor(input, output), baseLayer); } else { // Save weights for denoising. - boost::apply_visitor(ParametersVisitor(std::move(denoise)), baseLayer); + boost::apply_visitor(ParametersVisitor(denoise), baseLayer); // Scale with input / (1 - ratio) and set values to zero with // probability ratio. mask = arma::randu >(denoise.n_rows, denoise.n_cols); mask.transform([&](double val) { return (val > ratio); }); - boost::apply_visitor(ParametersSetVisitor(std::move(denoise % mask)), - baseLayer); + arma::mat tmp = denoise % mask; + boost::apply_visitor(ParametersSetVisitor(tmp), baseLayer); - boost::apply_visitor(ForwardVisitor(std::move(input), std::move(output)), - baseLayer); + boost::apply_visitor(ForwardVisitor(input, output), baseLayer); output = output * scale; } @@ -90,26 +88,25 @@ void DropConnect::Forward( template template void DropConnect::Backward( - arma::Mat&& input, - arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& input, + const arma::Mat& gy, + arma::Mat& g) { - boost::apply_visitor(BackwardVisitor(std::move(input), std::move(gy), - std::move(g)), baseLayer); + boost::apply_visitor(BackwardVisitor(input, gy, g), baseLayer); } template template void DropConnect::Gradient( - arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& /* gradient */) + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& /* gradient */) { - boost::apply_visitor(GradientVisitor(std::move(input), std::move(error)), + boost::apply_visitor(GradientVisitor(input, error), baseLayer); // Denoise the weights. - boost::apply_visitor(ParametersSetVisitor(std::move(denoise)), baseLayer); + boost::apply_visitor(ParametersSetVisitor(denoise), baseLayer); } template diff --git a/src/mlpack/methods/ann/layer/dropout.hpp b/src/mlpack/methods/ann/layer/dropout.hpp index 696d92869c..358239a7c2 100644 --- a/src/mlpack/methods/ann/layer/dropout.hpp +++ b/src/mlpack/methods/ann/layer/dropout.hpp @@ -66,7 +66,7 @@ class Dropout * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of the dropout layer. @@ -76,9 +76,9 @@ class Dropout * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/dropout_impl.hpp b/src/mlpack/methods/ann/layer/dropout_impl.hpp index ccac9588f8..750b11958c 100644 --- a/src/mlpack/methods/ann/layer/dropout_impl.hpp +++ b/src/mlpack/methods/ann/layer/dropout_impl.hpp @@ -32,8 +32,8 @@ Dropout::Dropout( template template void Dropout::Forward( - const arma::Mat&& input, - arma::Mat&& output) + const arma::Mat& input, + arma::Mat& output) { // The dropout mask will not be multiplied in the deterministic mode // (during testing). @@ -54,9 +54,9 @@ void Dropout::Forward( template template void Dropout::Backward( - const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { g = gy % mask * scale; } diff --git a/src/mlpack/methods/ann/layer/elu.hpp b/src/mlpack/methods/ann/layer/elu.hpp index 575b99e4d5..91375d59af 100644 --- a/src/mlpack/methods/ann/layer/elu.hpp +++ b/src/mlpack/methods/ann/layer/elu.hpp @@ -134,7 +134,7 @@ class ELU * @param output Resulting output activation. */ template - void Forward(const InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -146,7 +146,7 @@ class ELU * @param g The calculated gradient. */ template - void Backward(const DataType&& input, DataType&& gy, DataType&& g); + void Backward(const DataType& input, const DataType& gy, DataType& g); //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/elu_impl.hpp b/src/mlpack/methods/ann/layer/elu_impl.hpp index 3c94871e49..a184b1d76c 100644 --- a/src/mlpack/methods/ann/layer/elu_impl.hpp +++ b/src/mlpack/methods/ann/layer/elu_impl.hpp @@ -49,7 +49,7 @@ ELU::ELU(const double alpha) : template template void ELU::Forward( - const InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { output.set_size(arma::size(input)); for (size_t i = 0; i < input.n_elem; i++) @@ -77,7 +77,7 @@ void ELU::Forward( template template void ELU::Backward( - const DataType&& /* input */, DataType&& gy, DataType&& g) + const DataType& /* input */, const DataType& gy, DataType& g) { g = gy % derivative; } diff --git a/src/mlpack/methods/ann/layer/fast_lstm.hpp b/src/mlpack/methods/ann/layer/fast_lstm.hpp index e73906a5ba..ef20aaa145 100644 --- a/src/mlpack/methods/ann/layer/fast_lstm.hpp +++ b/src/mlpack/methods/ann/layer/fast_lstm.hpp @@ -91,7 +91,7 @@ class FastLSTM * @param output Resulting output activation. */ template - void Forward(InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -103,9 +103,9 @@ class FastLSTM * @param g The calculated gradient. */ template - void Backward(const InputType&& input, - ErrorType&& gy, - GradientType&& g); + void Backward(const InputType& input, + const ErrorType& gy, + GradientType& g); /* * Reset the layer parameter. @@ -128,9 +128,9 @@ class FastLSTM * @param gradient The calculated gradient. */ template - void Gradient(InputType&& input, - ErrorType&& error, - GradientType&& gradient); + void Gradient(const InputType& input, + const ErrorType& error, + GradientType& gradient); //! Get the maximum number of steps to backpropagate through time (BPTT). size_t Rho() const { return rho; } @@ -171,7 +171,7 @@ class FastLSTM * @param sigmoid The matrix to store the sigmoid approximation into. */ template - void FastSigmoid(InputType&& input, OutputType&& sigmoids) + void FastSigmoid(const InputType& input, OutputType& sigmoids) { for (size_t i = 0; i < input.n_elem; ++i) sigmoids(i) = FastSigmoid(input(i)); diff --git a/src/mlpack/methods/ann/layer/fast_lstm_impl.hpp b/src/mlpack/methods/ann/layer/fast_lstm_impl.hpp index 36d92c2a0d..47dfb4bf1e 100644 --- a/src/mlpack/methods/ann/layer/fast_lstm_impl.hpp +++ b/src/mlpack/methods/ann/layer/fast_lstm_impl.hpp @@ -112,7 +112,7 @@ void FastLSTM::ResetCell(const size_t size) template template void FastLSTM::Forward( - InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { // Check if the batch size changed, the number of cols is defines the input // batch size. @@ -128,9 +128,11 @@ void FastLSTM::Forward( forwardStep, forwardStep + batchStep); gate.cols(forwardStep, forwardStep + batchStep).each_col() += input2GateBias; - FastSigmoid(std::move( - gate.submat(0, forwardStep, 3 * outSize - 1, forwardStep + batchStep)), - std::move(gateActivation.cols(forwardStep, forwardStep + batchStep))); + arma::subview sigmoidOut = gateActivation.cols(forwardStep, + forwardStep + batchStep); + FastSigmoid( + gate.submat(0, forwardStep, 3 * outSize - 1, forwardStep + batchStep), + sigmoidOut); stateActivation.cols(forwardStep, forwardStep + batchStep) = arma::tanh( gate.submat(3 * outSize, forwardStep, 4 * outSize - 1, @@ -178,14 +180,20 @@ void FastLSTM::Forward( template template void FastLSTM::Backward( - const InputType&& /* input */, ErrorType&& gy, GradientType&& g) + const InputType& /* input */, const ErrorType& gy, GradientType& g) { + ErrorType gyLocal; if (gradientStepIdx > 0) { - gy += output2GateWeight.t() * prevError; + gyLocal = gy + output2GateWeight.t() * prevError; + } + else + { + gyLocal = ErrorType(((ErrorType&) gy).memptr(), gy.n_rows, gy.n_cols, false, + false); } - cellActivationError = gy % gateActivation.submat(outSize, + cellActivationError = gyLocal % gateActivation.submat(outSize, backwardStep - batchStep, 2 * outSize - 1, backwardStep) % (1 - arma::pow(cellActivation.cols(backwardStep - batchStep, backwardStep), 2)); @@ -225,7 +233,7 @@ void FastLSTM::Backward( prevError.submat(outSize, 0, 2 * outSize - 1, batchStep) = cellActivation.cols(backwardStep - batchStep, - backwardStep) % gy % gateActivation.submat( + backwardStep) % gyLocal % gateActivation.submat( outSize, backwardStep - batchStep, 2 * outSize - 1, backwardStep) % (1.0 - gateActivation.submat( outSize, backwardStep - batchStep, 2 * outSize - 1, backwardStep)); @@ -244,7 +252,9 @@ void FastLSTM::Backward( template template void FastLSTM::Gradient( - InputType&& input, ErrorType&& /* error */, GradientType&& gradient) + const InputType& input, + const ErrorType& /* error */, + GradientType& gradient) { // Gradient of the input to gate layer. gradient.submat(0, 0, input2GateWeight.n_elem - 1, 0) = diff --git a/src/mlpack/methods/ann/layer/flexible_relu.hpp b/src/mlpack/methods/ann/layer/flexible_relu.hpp index 89e013610f..184ecde71d 100644 --- a/src/mlpack/methods/ann/layer/flexible_relu.hpp +++ b/src/mlpack/methods/ann/layer/flexible_relu.hpp @@ -84,7 +84,7 @@ class FlexibleReLU * @param output Resulting output activation. */ template - void Forward(const InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -96,7 +96,7 @@ class FlexibleReLU * @param g The calculated gradient. */ template - void Backward(const DataType&& input, DataType&& gy, DataType&& g); + void Backward(const DataType& input, const DataType& gy, DataType& g); /** * Calculate the gradient using the output delta and the input activation. @@ -106,9 +106,9 @@ class FlexibleReLU * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient); //! Get the parameters. OutputDataType const& Parameters() const { return alpha; } diff --git a/src/mlpack/methods/ann/layer/flexible_relu_impl.hpp b/src/mlpack/methods/ann/layer/flexible_relu_impl.hpp index 52ab56f498..02af24bb38 100644 --- a/src/mlpack/methods/ann/layer/flexible_relu_impl.hpp +++ b/src/mlpack/methods/ann/layer/flexible_relu_impl.hpp @@ -41,7 +41,7 @@ void FlexibleReLU::Reset() template template void FlexibleReLU::Forward( - const InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { output = arma::clamp(input, 0.0, DBL_MAX) + alpha(0); } @@ -49,7 +49,7 @@ void FlexibleReLU::Forward( template template void FlexibleReLU::Backward( - const DataType&& input, DataType&& gy, DataType&& g) + const DataType& input, const DataType& gy, DataType& g) { //! Compute the first derivative of FlexibleReLU function. g = gy % arma::clamp(arma::sign(input), 0.0, 1.0); @@ -58,9 +58,9 @@ void FlexibleReLU::Backward( template template void FlexibleReLU::Gradient( - const arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient) { if (gradient.n_elem == 0) { diff --git a/src/mlpack/methods/ann/layer/glimpse.hpp b/src/mlpack/methods/ann/layer/glimpse.hpp index 357fbce726..1c25d25e63 100644 --- a/src/mlpack/methods/ann/layer/glimpse.hpp +++ b/src/mlpack/methods/ann/layer/glimpse.hpp @@ -113,7 +113,7 @@ class Glimpse * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of the glimpse layer. @@ -123,9 +123,9 @@ class Glimpse * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); //! Get the output parameter. OutputDataType& OutputParameter() const {return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/glimpse_impl.hpp b/src/mlpack/methods/ann/layer/glimpse_impl.hpp index a867656895..49f6854301 100644 --- a/src/mlpack/methods/ann/layer/glimpse_impl.hpp +++ b/src/mlpack/methods/ann/layer/glimpse_impl.hpp @@ -45,7 +45,7 @@ Glimpse::Glimpse( template template void Glimpse::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { inputTemp = arma::cube(input.colptr(0), inputWidth, inputHeight, inSize); outputTemp = arma::Cube(size, size, depth * inputTemp.n_slices); @@ -129,7 +129,7 @@ void Glimpse::Forward( template template void Glimpse::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { // Generate a cube using the backpropagated error matrix. arma::Cube mappedError = arma::zeros(outputWidth, diff --git a/src/mlpack/methods/ann/layer/gru.hpp b/src/mlpack/methods/ann/layer/gru.hpp index 63fee576be..6a863d4488 100644 --- a/src/mlpack/methods/ann/layer/gru.hpp +++ b/src/mlpack/methods/ann/layer/gru.hpp @@ -84,7 +84,7 @@ class GRU * @param output Resulting output activation. */ template - void Forward(arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -96,9 +96,9 @@ class GRU * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /* * Calculate the gradient using the output delta and the input activation. @@ -108,9 +108,9 @@ class GRU * @param gradient The calculated gradient. */ template - void Gradient(arma::Mat&& input, - arma::Mat&& /* error */, - arma::Mat&& /* gradient */); + void Gradient(const arma::Mat& input, + const arma::Mat& /* error */, + arma::Mat& /* gradient */); /* * Resets the cell to accept a new input. This breaks the BPTT chain starts a diff --git a/src/mlpack/methods/ann/layer/gru_impl.hpp b/src/mlpack/methods/ann/layer/gru_impl.hpp index 86c6b85414..b2e7cee39d 100644 --- a/src/mlpack/methods/ann/layer/gru_impl.hpp +++ b/src/mlpack/methods/ann/layer/gru_impl.hpp @@ -90,7 +90,7 @@ GRU::~GRU() template template void GRU::Forward( - arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { if (input.n_cols != batchSize) { @@ -114,13 +114,13 @@ void GRU::Forward( } // Process the input linearly(zt, rt, ot). - boost::apply_visitor(ForwardVisitor(std::move(input), std::move( - boost::apply_visitor(outputParameterVisitor, input2GateModule))), + boost::apply_visitor(ForwardVisitor(input, + boost::apply_visitor(outputParameterVisitor, input2GateModule)), input2GateModule); // Process the output(zt, rt) linearly. - boost::apply_visitor(ForwardVisitor(std::move(*prevOutput), std::move( - boost::apply_visitor(outputParameterVisitor, output2GateModule))), + boost::apply_visitor(ForwardVisitor(*prevOutput, + boost::apply_visitor(outputParameterVisitor, output2GateModule)), output2GateModule); // Merge the outputs(zt and rt). @@ -129,22 +129,22 @@ void GRU::Forward( boost::apply_visitor(outputParameterVisitor, output2GateModule)); // Pass the first outSize through inputGate(it). - boost::apply_visitor(ForwardVisitor(std::move(output.submat( - 0, 0, 1 * outSize - 1, batchSize - 1)), std::move(boost::apply_visitor( - outputParameterVisitor, inputGateModule))), inputGateModule); + boost::apply_visitor(ForwardVisitor(output.submat( + 0, 0, 1 * outSize - 1, batchSize - 1), boost::apply_visitor( + outputParameterVisitor, inputGateModule)), inputGateModule); // Pass the second through forgetGate. - boost::apply_visitor(ForwardVisitor(std::move(output.submat( - 1 * outSize, 0, 2 * outSize - 1, batchSize - 1)), std::move( - boost::apply_visitor(outputParameterVisitor, forgetGateModule))), + boost::apply_visitor(ForwardVisitor(output.submat( + 1 * outSize, 0, 2 * outSize - 1, batchSize - 1), + boost::apply_visitor(outputParameterVisitor, forgetGateModule)), forgetGateModule); arma::mat modInput = (boost::apply_visitor(outputParameterVisitor, forgetGateModule) % *prevOutput); // Pass that through the outputHidden2GateModule. - boost::apply_visitor(ForwardVisitor(std::move(modInput), std::move( - boost::apply_visitor(outputParameterVisitor, outputHidden2GateModule))), + boost::apply_visitor(ForwardVisitor(modInput, + boost::apply_visitor(outputParameterVisitor, outputHidden2GateModule)), outputHidden2GateModule); // Merge for ot. @@ -153,8 +153,8 @@ void GRU::Forward( boost::apply_visitor(outputParameterVisitor, outputHidden2GateModule); // Pass it through hiddenGate. - boost::apply_visitor(ForwardVisitor(std::move(outputH), std::move( - boost::apply_visitor(outputParameterVisitor, hiddenStateModule))), + boost::apply_visitor(ForwardVisitor(outputH, + boost::apply_visitor(outputParameterVisitor, hiddenStateModule)), hiddenStateModule); // Update the output (nextOutput): cmul1 + cmul2 @@ -205,7 +205,7 @@ void GRU::Forward( template template void GRU::Backward( - const arma::Mat&& input, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& input, const arma::Mat& gy, arma::Mat& g) { if (input.n_cols != batchSize) { @@ -228,9 +228,15 @@ void GRU::Backward( gradIterator = outParameter.end(); } + arma::Mat gyLocal; if ((outParameter.size() - backwardStep - 1) % rho != 0 && backwardStep != 0) { - gy += boost::apply_visitor(deltaVisitor, output2GateModule); + gyLocal = gy + boost::apply_visitor(deltaVisitor, output2GateModule); + } + else + { + gyLocal = arma::Mat(((arma::Mat&) gy).memptr(), gy.n_rows, + gy.n_cols, false, false); } if (backIterator == outParameter.end()) @@ -239,31 +245,31 @@ void GRU::Backward( } // Delta zt. - arma::mat dZt = gy % (*backIterator - + arma::mat dZt = gyLocal % (*backIterator - boost::apply_visitor(outputParameterVisitor, hiddenStateModule)); // Delta ot. - arma::mat dOt = gy % (arma::ones(outSize, batchSize) - + arma::mat dOt = gyLocal % (arma::ones(outSize, batchSize) - boost::apply_visitor(outputParameterVisitor, inputGateModule)); // Delta of input gate. - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, inputGateModule)), std::move(dZt), - std::move(boost::apply_visitor(deltaVisitor, inputGateModule))), + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, inputGateModule), dZt, + boost::apply_visitor(deltaVisitor, inputGateModule)), inputGateModule); // Delta of hidden gate. - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, hiddenStateModule)), std::move(dOt), - std::move(boost::apply_visitor(deltaVisitor, hiddenStateModule))), + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, hiddenStateModule), dOt, + boost::apply_visitor(deltaVisitor, hiddenStateModule)), hiddenStateModule); // Delta of outputHidden2GateModule. - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, outputHidden2GateModule)), - std::move(boost::apply_visitor(deltaVisitor, hiddenStateModule)), - std::move(boost::apply_visitor(deltaVisitor, outputHidden2GateModule))), + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, outputHidden2GateModule), + boost::apply_visitor(deltaVisitor, hiddenStateModule), + boost::apply_visitor(deltaVisitor, outputHidden2GateModule)), outputHidden2GateModule); // Delta rt. @@ -271,9 +277,9 @@ void GRU::Backward( *backIterator; // Delta of forget gate. - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, forgetGateModule)), std::move(dRt), - std::move(boost::apply_visitor(deltaVisitor, forgetGateModule))), + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, forgetGateModule), dRt, + boost::apply_visitor(deltaVisitor, forgetGateModule)), forgetGateModule); // Put delta zt. @@ -289,10 +295,12 @@ void GRU::Backward( boost::apply_visitor(deltaVisitor, hiddenStateModule); // Get delta ht - 1 for input gate and forget gate. - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, input2GateModule)), - std::move(prevError.submat(0, 0, 2 * outSize - 1, batchSize - 1)), - std::move(boost::apply_visitor(deltaVisitor, output2GateModule))), + arma::mat prevErrorSubview = prevError.submat(0, 0, 2 * outSize - 1, + batchSize - 1); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, input2GateModule), + prevErrorSubview, + boost::apply_visitor(deltaVisitor, output2GateModule)), output2GateModule); // Add delta ht - 1 from hidden state. @@ -301,13 +309,13 @@ void GRU::Backward( boost::apply_visitor(outputParameterVisitor, forgetGateModule); // Add delta ht - 1 from ht. - boost::apply_visitor(deltaVisitor, output2GateModule) += gy % + boost::apply_visitor(deltaVisitor, output2GateModule) += gyLocal % boost::apply_visitor(outputParameterVisitor, inputGateModule); // Get delta input. - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, input2GateModule)), std::move(prevError), - std::move(boost::apply_visitor(deltaVisitor, input2GateModule))), + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, input2GateModule), prevError, + boost::apply_visitor(deltaVisitor, input2GateModule)), input2GateModule); backwardStep++; @@ -319,9 +327,9 @@ void GRU::Backward( template template void GRU::Gradient( - arma::Mat&& input, - arma::Mat&& /* error */, - arma::Mat&& /* gradient */) + const arma::Mat& input, + const arma::Mat& /* error */, + arma::Mat& /* gradient */) { if (input.n_cols != batchSize) { @@ -349,19 +357,18 @@ void GRU::Gradient( gradIterator = --(--outParameter.end()); } - boost::apply_visitor(GradientVisitor(std::move(input), std::move(prevError)), - input2GateModule); + boost::apply_visitor(GradientVisitor(input, prevError), input2GateModule); boost::apply_visitor(GradientVisitor( - std::move(*gradIterator), - std::move(prevError.submat(0, 0, 2 * outSize - 1, batchSize - 1))), + *gradIterator, + prevError.submat(0, 0, 2 * outSize - 1, batchSize - 1)), output2GateModule); boost::apply_visitor(GradientVisitor( *gradIterator % boost::apply_visitor(outputParameterVisitor, forgetGateModule), - std::move(prevError.submat(2 * outSize, 0, 3 * outSize - 1, - batchSize - 1))), outputHidden2GateModule); + prevError.submat(2 * outSize, 0, 3 * outSize - 1, batchSize - 1)), + outputHidden2GateModule); gradIterator--; } diff --git a/src/mlpack/methods/ann/layer/hard_tanh.hpp b/src/mlpack/methods/ann/layer/hard_tanh.hpp index 8ff75b899f..fa5b974e33 100644 --- a/src/mlpack/methods/ann/layer/hard_tanh.hpp +++ b/src/mlpack/methods/ann/layer/hard_tanh.hpp @@ -67,7 +67,7 @@ class HardTanH * @param output Resulting output activation. */ template - void Forward(const InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -79,9 +79,9 @@ class HardTanH * @param g The calculated gradient. */ template - void Backward(const DataType&& input, - DataType&& gy, - DataType&& g); + void Backward(const DataType& input, + const DataType& gy, + DataType& g); //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/hard_tanh_impl.hpp b/src/mlpack/methods/ann/layer/hard_tanh_impl.hpp index 1332b89d8d..473330cb07 100644 --- a/src/mlpack/methods/ann/layer/hard_tanh_impl.hpp +++ b/src/mlpack/methods/ann/layer/hard_tanh_impl.hpp @@ -31,7 +31,7 @@ HardTanH::HardTanH( template template void HardTanH::Forward( - const InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { output = input; for (size_t i = 0; i < input.n_elem; i++) @@ -44,7 +44,7 @@ void HardTanH::Forward( template template void HardTanH::Backward( - const DataType&& input, DataType&& gy, DataType&& g) + const DataType& input, const DataType& gy, DataType& g) { g = gy; for (size_t i = 0; i < input.n_elem; i++) diff --git a/src/mlpack/methods/ann/layer/highway.hpp b/src/mlpack/methods/ann/layer/highway.hpp index f0216b8f0b..4c238cefc1 100644 --- a/src/mlpack/methods/ann/layer/highway.hpp +++ b/src/mlpack/methods/ann/layer/highway.hpp @@ -92,7 +92,7 @@ class Highway * @param output Resulting output activation. */ template - void Forward(arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed-backward pass of a neural network, calculating the function @@ -104,9 +104,9 @@ class Highway * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /** * Calculate the gradient using the output delta and the input activation. @@ -116,9 +116,9 @@ class Highway * @param gradient The calculated gradient. */ template - void Gradient(arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient); /** * Add a new module to the model. diff --git a/src/mlpack/methods/ann/layer/highway_impl.hpp b/src/mlpack/methods/ann/layer/highway_impl.hpp index 87acf9553d..80b4959a36 100644 --- a/src/mlpack/methods/ann/layer/highway_impl.hpp +++ b/src/mlpack/methods/ann/layer/highway_impl.hpp @@ -91,10 +91,10 @@ template template void Highway::Forward( - arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { - boost::apply_visitor(ForwardVisitor(std::move(input), std::move( - boost::apply_visitor(outputParameterVisitor, network.front()))), + boost::apply_visitor(ForwardVisitor(input, + boost::apply_visitor(outputParameterVisitor, network.front())), network.front()); if (!reset) @@ -121,9 +121,9 @@ void Highway::Forward( boost::apply_visitor(SetInputHeightVisitor(height), network[i]); } - boost::apply_visitor(ForwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[i - 1])), std::move( - boost::apply_visitor(outputParameterVisitor, network[i]))), + boost::apply_visitor(ForwardVisitor(boost::apply_visitor( + outputParameterVisitor, network[i - 1]), + boost::apply_visitor(outputParameterVisitor, network[i])), network[i]); if (!reset) @@ -167,23 +167,24 @@ template template void Highway::Backward( - const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network.back())), - std::move(gy % transformGateActivation), - std::move(boost::apply_visitor(deltaVisitor, network.back()))), + arma::Mat gyTransform = gy % transformGateActivation; + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, network.back()), + gyTransform, + boost::apply_visitor(deltaVisitor, network.back())), network.back()); for (size_t i = 2; i < network.size() + 1; ++i) { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[network.size() - i])), std::move( - boost::apply_visitor(deltaVisitor, network[network.size() - i + 1])), - std::move(boost::apply_visitor(deltaVisitor, - network[network.size() - i]))), network[network.size() - i]); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, network[network.size() - i]), + boost::apply_visitor(deltaVisitor, network[network.size() - i + 1]), + boost::apply_visitor(deltaVisitor, + network[network.size() - i])), network[network.size() - i]); } g = boost::apply_visitor(deltaVisitor, network.front()); @@ -198,24 +199,25 @@ template template void Highway::Gradient( - arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient) { - boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[network.size() - 2])), - std::move(error % transformGateActivation)), network.back()); + arma::Mat errorTransform = error % transformGateActivation; + boost::apply_visitor(GradientVisitor(boost::apply_visitor( + outputParameterVisitor, network[network.size() - 2]), + errorTransform), network.back()); for (size_t i = 2; i < network.size(); ++i) { - boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[network.size() - i - 1])), std::move( - boost::apply_visitor(deltaVisitor, network[network.size() - i + 1]))), + boost::apply_visitor(GradientVisitor(boost::apply_visitor( + outputParameterVisitor, network[network.size() - i - 1]), + boost::apply_visitor(deltaVisitor, network[network.size() - i + 1])), network[network.size() - i]); } - boost::apply_visitor(GradientVisitor(std::move(input), std::move( - boost::apply_visitor(deltaVisitor, network[1]))), network.front()); + boost::apply_visitor(GradientVisitor(input, + boost::apply_visitor(deltaVisitor, network[1])), network.front()); gradient.submat(0, 0, transformWeight.n_elem - 1, 0) = arma::vectorise( transformGateError * input.t()); diff --git a/src/mlpack/methods/ann/layer/join.hpp b/src/mlpack/methods/ann/layer/join.hpp index 2f6ecde25f..dcf0c06327 100644 --- a/src/mlpack/methods/ann/layer/join.hpp +++ b/src/mlpack/methods/ann/layer/join.hpp @@ -44,7 +44,7 @@ class Join * @param output Resulting output activation. */ template - void Forward(const InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -56,9 +56,9 @@ class Join * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/join_impl.hpp b/src/mlpack/methods/ann/layer/join_impl.hpp index 6ceee0c135..14d85bc88d 100644 --- a/src/mlpack/methods/ann/layer/join_impl.hpp +++ b/src/mlpack/methods/ann/layer/join_impl.hpp @@ -29,7 +29,7 @@ Join::Join() : template template void Join::Forward( - const InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { inSizeRows = input.n_rows; inSizeCols = input.n_cols; @@ -39,11 +39,12 @@ void Join::Forward( template template void Join::Backward( - const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { - g = arma::mat(gy.memptr(), inSizeRows, inSizeCols, false, false); + g = arma::mat(((arma::Mat&) gy).memptr(), inSizeRows, inSizeCols, false, + false); } template diff --git a/src/mlpack/methods/ann/layer/layer_norm.hpp b/src/mlpack/methods/ann/layer/layer_norm.hpp index f53d35a860..55642b21d2 100644 --- a/src/mlpack/methods/ann/layer/layer_norm.hpp +++ b/src/mlpack/methods/ann/layer/layer_norm.hpp @@ -90,7 +90,7 @@ class LayerNorm * @param output Resulting output activations. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Backward pass through the layer. @@ -100,9 +100,9 @@ class LayerNorm * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& input, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& input, + const arma::Mat& gy, + arma::Mat& g); /** * Calculate the gradient using the output delta and the input activations. @@ -112,9 +112,9 @@ class LayerNorm * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient); //! Get the parameters. OutputDataType const& Parameters() const { return weights; } diff --git a/src/mlpack/methods/ann/layer/layer_norm_impl.hpp b/src/mlpack/methods/ann/layer/layer_norm_impl.hpp index b9383d7dd4..eb49377972 100644 --- a/src/mlpack/methods/ann/layer/layer_norm_impl.hpp +++ b/src/mlpack/methods/ann/layer/layer_norm_impl.hpp @@ -57,7 +57,7 @@ void LayerNorm::Reset() template template void LayerNorm::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { mean = arma::mean(input, 0); variance = arma::var(input, 1, 0); @@ -78,7 +78,7 @@ void LayerNorm::Forward( template template void LayerNorm::Backward( - const arma::Mat&& input, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& input, const arma::Mat& gy, arma::Mat& g) { const arma::mat stdInv = 1.0 / arma::sqrt(variance + eps); @@ -102,9 +102,9 @@ void LayerNorm::Backward( template template void LayerNorm::Gradient( - const arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& gradient) { gradient.set_size(size + size, 1); diff --git a/src/mlpack/methods/ann/layer/leaky_relu.hpp b/src/mlpack/methods/ann/layer/leaky_relu.hpp index f9d7b230f9..1780c5a737 100644 --- a/src/mlpack/methods/ann/layer/leaky_relu.hpp +++ b/src/mlpack/methods/ann/layer/leaky_relu.hpp @@ -61,7 +61,7 @@ class LeakyReLU * @param output Resulting output activation. */ template - void Forward(const InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -73,7 +73,7 @@ class LeakyReLU * @param g The calculated gradient. */ template - void Backward(const DataType&& input, DataType&& gy, DataType&& g); + void Backward(const DataType& input, const DataType& gy, DataType& g); //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/leaky_relu_impl.hpp b/src/mlpack/methods/ann/layer/leaky_relu_impl.hpp index 5d024ad3dd..8053cd031a 100644 --- a/src/mlpack/methods/ann/layer/leaky_relu_impl.hpp +++ b/src/mlpack/methods/ann/layer/leaky_relu_impl.hpp @@ -30,7 +30,7 @@ LeakyReLU::LeakyReLU( template template void LeakyReLU::Forward( - const InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { output = arma::max(input, alpha * input); } @@ -38,7 +38,7 @@ void LeakyReLU::Forward( template template void LeakyReLU::Backward( - const DataType&& input, DataType&& gy, DataType&& g) + const DataType& input, const DataType& gy, DataType& g) { DataType derivative; derivative.set_size(arma::size(input)); diff --git a/src/mlpack/methods/ann/layer/linear.hpp b/src/mlpack/methods/ann/layer/linear.hpp index bf4074f61b..8d3c984683 100644 --- a/src/mlpack/methods/ann/layer/linear.hpp +++ b/src/mlpack/methods/ann/layer/linear.hpp @@ -64,7 +64,7 @@ class Linear * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -76,9 +76,9 @@ class Linear * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /* * Calculate the gradient using the output delta and the input activation. @@ -88,9 +88,9 @@ class Linear * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient); //! Get the parameters. OutputDataType const& Parameters() const { return weights; } diff --git a/src/mlpack/methods/ann/layer/linear_impl.hpp b/src/mlpack/methods/ann/layer/linear_impl.hpp index 127d789716..0ec31f1070 100644 --- a/src/mlpack/methods/ann/layer/linear_impl.hpp +++ b/src/mlpack/methods/ann/layer/linear_impl.hpp @@ -54,7 +54,7 @@ template template void Linear::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { output = weight * input; output.each_col() += bias; @@ -64,7 +64,7 @@ template template void Linear::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { g = weight.t() * gy; } @@ -73,9 +73,9 @@ template template void Linear::Gradient( - const arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient) { gradient.submat(0, 0, weight.n_elem - 1, 0) = arma::vectorise( error * input.t()); diff --git a/src/mlpack/methods/ann/layer/linear_no_bias.hpp b/src/mlpack/methods/ann/layer/linear_no_bias.hpp index 1b2c5ffc02..620bf896b9 100644 --- a/src/mlpack/methods/ann/layer/linear_no_bias.hpp +++ b/src/mlpack/methods/ann/layer/linear_no_bias.hpp @@ -63,7 +63,7 @@ class LinearNoBias * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -75,9 +75,9 @@ class LinearNoBias * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /* * Calculate the gradient using the output delta and the input activation. @@ -87,9 +87,9 @@ class LinearNoBias * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient); //! Get the parameters. OutputDataType const& Parameters() const { return weights; } diff --git a/src/mlpack/methods/ann/layer/linear_no_bias_impl.hpp b/src/mlpack/methods/ann/layer/linear_no_bias_impl.hpp index 41e432da17..fee30ec783 100644 --- a/src/mlpack/methods/ann/layer/linear_no_bias_impl.hpp +++ b/src/mlpack/methods/ann/layer/linear_no_bias_impl.hpp @@ -52,7 +52,7 @@ template template void LinearNoBias::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { output = weight * input; } @@ -61,7 +61,7 @@ template template void LinearNoBias::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { g = weight.t() * gy; } @@ -70,9 +70,9 @@ template template void LinearNoBias::Gradient( - const arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient) { gradient.submat(0, 0, weight.n_elem - 1, 0) = arma::vectorise( error * input.t()); diff --git a/src/mlpack/methods/ann/layer/log_softmax.hpp b/src/mlpack/methods/ann/layer/log_softmax.hpp index 613eac4aa3..25a9495d9d 100644 --- a/src/mlpack/methods/ann/layer/log_softmax.hpp +++ b/src/mlpack/methods/ann/layer/log_softmax.hpp @@ -49,7 +49,7 @@ class LogSoftMax * @param output Resulting output activation. */ template - void Forward(const InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -61,9 +61,9 @@ class LogSoftMax * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& input, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& input, + const arma::Mat& gy, + arma::Mat& g); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/log_softmax_impl.hpp b/src/mlpack/methods/ann/layer/log_softmax_impl.hpp index d4ca9c53ae..2c6ea6c635 100644 --- a/src/mlpack/methods/ann/layer/log_softmax_impl.hpp +++ b/src/mlpack/methods/ann/layer/log_softmax_impl.hpp @@ -27,7 +27,7 @@ LogSoftMax::LogSoftMax() template template void LogSoftMax::Forward( - const InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { arma::mat maxInput = arma::repmat(arma::max(input), input.n_rows, 1); output = (maxInput - input); @@ -64,9 +64,9 @@ void LogSoftMax::Forward( template template void LogSoftMax::Backward( - const arma::Mat&& input, - arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& input, + const arma::Mat& gy, + arma::Mat& g) { g = arma::exp(input) + gy; } diff --git a/src/mlpack/methods/ann/layer/lookup.hpp b/src/mlpack/methods/ann/layer/lookup.hpp index 59e5547cda..ac5ced3ca4 100644 --- a/src/mlpack/methods/ann/layer/lookup.hpp +++ b/src/mlpack/methods/ann/layer/lookup.hpp @@ -52,7 +52,7 @@ class Lookup * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -64,9 +64,9 @@ class Lookup * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - const arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /* * Calculate the gradient using the output delta and the input activation. @@ -76,9 +76,9 @@ class Lookup * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient); //! Get the parameters. OutputDataType const& Parameters() const { return weights; } diff --git a/src/mlpack/methods/ann/layer/lookup_impl.hpp b/src/mlpack/methods/ann/layer/lookup_impl.hpp index 3c4b8e96c3..2dde378e8a 100644 --- a/src/mlpack/methods/ann/layer/lookup_impl.hpp +++ b/src/mlpack/methods/ann/layer/lookup_impl.hpp @@ -32,7 +32,7 @@ Lookup::Lookup( template template void Lookup::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { output = weights.cols(arma::conv_to::from(input) - 1); } @@ -40,9 +40,9 @@ void Lookup::Forward( template template void Lookup::Backward( - const arma::Mat&& /* input */, - const arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { g = gy; } @@ -50,9 +50,9 @@ void Lookup::Backward( template template void Lookup::Gradient( - const arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient) { gradient = arma::zeros >(weights.n_rows, weights.n_cols); gradient.cols(arma::conv_to::from(input) - 1) = error; diff --git a/src/mlpack/methods/ann/layer/lstm.hpp b/src/mlpack/methods/ann/layer/lstm.hpp index 9c1f75c715..20a4b07248 100644 --- a/src/mlpack/methods/ann/layer/lstm.hpp +++ b/src/mlpack/methods/ann/layer/lstm.hpp @@ -84,7 +84,7 @@ class LSTM * @param output Resulting output activation. */ template - void Forward(InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed-forward pass of a neural network, evaluating the function @@ -96,9 +96,9 @@ class LSTM * @param useCellState Use the cellState passed in the LSTM cell. */ template - void Forward(InputType&& input, - OutputType&& output, - OutputType&& cellState, + void Forward(const InputType& input, + OutputType& output, + OutputType& cellState, bool useCellState = false); /** @@ -111,9 +111,9 @@ class LSTM * @param g The calculated gradient. */ template - void Backward(const InputType&& input, - ErrorType&& gy, - GradientType&& g); + void Backward(const InputType& input, + const ErrorType& gy, + GradientType& g); /* * Reset the layer parameter. @@ -136,9 +136,9 @@ class LSTM * @param gradient The calculated gradient. */ template - void Gradient(InputType&& input, - ErrorType&& error, - GradientType&& gradient); + void Gradient(const InputType& input, + const ErrorType& error, + GradientType& gradient); //! Get the maximum number of steps to backpropagate through time (BPTT). size_t Rho() const { return rho; } diff --git a/src/mlpack/methods/ann/layer/lstm_impl.hpp b/src/mlpack/methods/ann/layer/lstm_impl.hpp index d46ef43e00..4b4dd03e91 100644 --- a/src/mlpack/methods/ann/layer/lstm_impl.hpp +++ b/src/mlpack/methods/ann/layer/lstm_impl.hpp @@ -163,19 +163,19 @@ void LSTM::Reset() template template void LSTM::Forward( - InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { //! Locally-stored cellState. OutputType cellState; - Forward(std::move(input), std::move(output), std::move(cellState), false); + Forward(input, output, cellState, false); } // Forward when cellState is needed overloaded LSTM::Forward(). template template -void LSTM::Forward(InputType&& input, - OutputType&& output, - OutputType&& cellState, +void LSTM::Forward(const InputType& input, + OutputType& output, + OutputType& cellState, bool useCellState) { // Check if the batch size changed, the number of cols is defines the input @@ -288,20 +288,27 @@ void LSTM::Forward(InputType&& input, template template void LSTM::Backward( - const InputType&& /* input */, ErrorType&& gy, GradientType&& g) + const InputType& /* input */, const ErrorType& gy, GradientType& g) { + ErrorType gyLocal; if (gradientStepIdx > 0) { - gy += prevError; + gyLocal = gy + prevError; + } + else + { + // Make an alias. + gyLocal = ErrorType(((ErrorType&) gy).memptr(), gy.n_rows, gy.n_cols, false, + false); } outputGateError = - gy % cellActivation.cols(backwardStep - batchStep, backwardStep) % + gyLocal % cellActivation.cols(backwardStep - batchStep, backwardStep) % (outputGateActivation.cols(backwardStep - batchStep, backwardStep) % (1.0 - outputGateActivation.cols(backwardStep - batchStep, backwardStep))); - OutputDataType cellError = gy % + OutputDataType cellError = gyLocal % outputGateActivation.cols(backwardStep - batchStep, backwardStep) % (1 - arma::pow(cellActivation.cols(backwardStep - batchStep, backwardStep), 2)) + outputGateError.each_col() % @@ -359,7 +366,9 @@ void LSTM::Backward( template template void LSTM::Gradient( - InputType&& input, ErrorType&& /* error */, GradientType&& gradient) + const InputType& input, + const ErrorType& /* error */, + GradientType& gradient) { // Input2GateOutputWeight and input2GateOutputBias gradients. gradient.submat(0, 0, input2GateOutputWeight.n_elem - 1, 0) = diff --git a/src/mlpack/methods/ann/layer/max_pooling.hpp b/src/mlpack/methods/ann/layer/max_pooling.hpp index e0793c2ba2..8a38c7dadb 100644 --- a/src/mlpack/methods/ann/layer/max_pooling.hpp +++ b/src/mlpack/methods/ann/layer/max_pooling.hpp @@ -78,7 +78,7 @@ class MaxPooling * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, using 3rd-order tensors as @@ -90,9 +90,9 @@ class MaxPooling * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); //! Get the output parameter. const OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/max_pooling_impl.hpp b/src/mlpack/methods/ann/layer/max_pooling_impl.hpp index e89b295f5b..69722466c3 100644 --- a/src/mlpack/methods/ann/layer/max_pooling_impl.hpp +++ b/src/mlpack/methods/ann/layer/max_pooling_impl.hpp @@ -54,11 +54,11 @@ MaxPooling::MaxPooling( template template void MaxPooling::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { batchSize = input.n_cols; inSize = input.n_elem / (inputWidth * inputHeight * batchSize); - inputTemp = arma::cube(const_cast&&>(input).memptr(), + inputTemp = arma::cube(const_cast&>(input).memptr(), inputWidth, inputHeight, batchSize * inSize, false, false); if (floor) @@ -122,10 +122,10 @@ void MaxPooling::Forward( template template void MaxPooling::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { - arma::cube mappedError = arma::cube(gy.memptr(), outputWidth, - outputHeight, outSize, false, false); + arma::cube mappedError = arma::cube(((arma::Mat&) gy).memptr(), + outputWidth, outputHeight, outSize, false, false); gTemp = arma::zeros(inputTemp.n_rows, inputTemp.n_cols, inputTemp.n_slices); diff --git a/src/mlpack/methods/ann/layer/mean_pooling.hpp b/src/mlpack/methods/ann/layer/mean_pooling.hpp index 5894e0ab65..a3da66e441 100644 --- a/src/mlpack/methods/ann/layer/mean_pooling.hpp +++ b/src/mlpack/methods/ann/layer/mean_pooling.hpp @@ -58,7 +58,7 @@ class MeanPooling * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, using 3rd-order tensors as @@ -70,9 +70,9 @@ class MeanPooling * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/mean_pooling_impl.hpp b/src/mlpack/methods/ann/layer/mean_pooling_impl.hpp index 2c6d5d1da9..a9b6a4d1c9 100644 --- a/src/mlpack/methods/ann/layer/mean_pooling_impl.hpp +++ b/src/mlpack/methods/ann/layer/mean_pooling_impl.hpp @@ -54,11 +54,11 @@ MeanPooling::MeanPooling( template template void MeanPooling::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { batchSize = input.n_cols; inSize = input.n_elem / (inputWidth * inputHeight * batchSize); - inputTemp = arma::cube(const_cast&&>(input).memptr(), + inputTemp = arma::cube(const_cast&>(input).memptr(), inputWidth, inputHeight, batchSize * inSize, false, false); if (floor) @@ -97,12 +97,12 @@ void MeanPooling::Forward( template template void MeanPooling::Backward( - const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { - arma::cube mappedError = arma::cube(gy.memptr(), outputWidth, - outputHeight, outSize, false, false); + arma::cube mappedError = arma::cube(((arma::Mat&) gy).memptr(), + outputWidth, outputHeight, outSize, false, false); gTemp = arma::zeros(inputTemp.n_rows, inputTemp.n_cols, inputTemp.n_slices); diff --git a/src/mlpack/methods/ann/layer/minibatch_discrimination.hpp b/src/mlpack/methods/ann/layer/minibatch_discrimination.hpp index 4f995998bc..b9f258091e 100644 --- a/src/mlpack/methods/ann/layer/minibatch_discrimination.hpp +++ b/src/mlpack/methods/ann/layer/minibatch_discrimination.hpp @@ -81,7 +81,7 @@ class MiniBatchDiscrimination * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed-backward pass of a neural network, calculating the function @@ -93,9 +93,9 @@ class MiniBatchDiscrimination * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /** * Calculate the gradient using the output delta and the input activation. @@ -105,9 +105,9 @@ class MiniBatchDiscrimination * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& input, - arma::Mat&& /* error */, - arma::Mat&& gradient); + void Gradient(const arma::Mat& input, + const arma::Mat& /* error */, + arma::Mat& gradient); //! Get the parameters. OutputDataType const& Parameters() const { return weights; } diff --git a/src/mlpack/methods/ann/layer/minibatch_discrimination_impl.hpp b/src/mlpack/methods/ann/layer/minibatch_discrimination_impl.hpp index 7409a5c4d9..18f38d5da3 100644 --- a/src/mlpack/methods/ann/layer/minibatch_discrimination_impl.hpp +++ b/src/mlpack/methods/ann/layer/minibatch_discrimination_impl.hpp @@ -52,7 +52,7 @@ void MiniBatchDiscrimination::Reset() template template void MiniBatchDiscrimination::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { batchSize = input.n_cols; tempM = weight * input; @@ -88,7 +88,7 @@ void MiniBatchDiscrimination::Forward( template template void MiniBatchDiscrimination::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { g = gy.head_rows(A); arma::Mat gM = gy.tail_rows(B); @@ -117,9 +117,9 @@ void MiniBatchDiscrimination::Backward( template template void MiniBatchDiscrimination::Gradient( - const arma::Mat&& input, - arma::Mat&& /* error */, - arma::Mat&& gradient) + const arma::Mat& input, + const arma::Mat& /* error */, + arma::Mat& gradient) { gradient = arma::vectorise(deltaTemp * input.t()); } diff --git a/src/mlpack/methods/ann/layer/multiply_constant.hpp b/src/mlpack/methods/ann/layer/multiply_constant.hpp index 98cba880b9..9e020eb87f 100644 --- a/src/mlpack/methods/ann/layer/multiply_constant.hpp +++ b/src/mlpack/methods/ann/layer/multiply_constant.hpp @@ -47,7 +47,7 @@ class MultiplyConstant * @param output Resulting output activation. */ template - void Forward(const InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed backward pass of a neural network. The backward pass @@ -58,7 +58,7 @@ class MultiplyConstant * @param g The calculated gradient. */ template - void Backward(const DataType&& /* input */, DataType&& gy, DataType&& g); + void Backward(const DataType& /* input */, const DataType& gy, DataType& g); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/multiply_constant_impl.hpp b/src/mlpack/methods/ann/layer/multiply_constant_impl.hpp index 87d0007a93..648e296602 100644 --- a/src/mlpack/methods/ann/layer/multiply_constant_impl.hpp +++ b/src/mlpack/methods/ann/layer/multiply_constant_impl.hpp @@ -29,7 +29,7 @@ MultiplyConstant::MultiplyConstant( template template void MultiplyConstant::Forward( - const InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { output = input * scalar; } @@ -37,7 +37,7 @@ void MultiplyConstant::Forward( template template void MultiplyConstant::Backward( - const DataType&& /* input */, DataType&& gy, DataType&& g) + const DataType& /* input */, const DataType& gy, DataType& g) { g = gy * scalar; } diff --git a/src/mlpack/methods/ann/layer/multiply_merge.hpp b/src/mlpack/methods/ann/layer/multiply_merge.hpp index a247521c73..36430ea05d 100644 --- a/src/mlpack/methods/ann/layer/multiply_merge.hpp +++ b/src/mlpack/methods/ann/layer/multiply_merge.hpp @@ -61,7 +61,7 @@ class MultiplyMerge * @param output Resulting output activation. */ template - void Forward(InputType&& /* input */, OutputType&& output); + void Forward(const InputType& /* input */, OutputType& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -73,9 +73,9 @@ class MultiplyMerge * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /* * Calculate the gradient using the output delta and the input activation. @@ -85,9 +85,9 @@ class MultiplyMerge * @param gradient The calculated gradient. */ template - void Gradient(arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient); /* * Add a new module to the model. diff --git a/src/mlpack/methods/ann/layer/multiply_merge_impl.hpp b/src/mlpack/methods/ann/layer/multiply_merge_impl.hpp index 12ff210292..174bddfd2f 100644 --- a/src/mlpack/methods/ann/layer/multiply_merge_impl.hpp +++ b/src/mlpack/methods/ann/layer/multiply_merge_impl.hpp @@ -47,14 +47,14 @@ template template void MultiplyMerge::Forward( - InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { if (run) { for (size_t i = 0; i < network.size(); ++i) { - boost::apply_visitor(ForwardVisitor(std::move(input), std::move( - boost::apply_visitor(outputParameterVisitor, network[i]))), + boost::apply_visitor(ForwardVisitor(input, + boost::apply_visitor(outputParameterVisitor, network[i])), network[i]); } } @@ -70,15 +70,15 @@ template template void MultiplyMerge::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { if (run) { for (size_t i = 0; i < network.size(); ++i) { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[i])), std::move(gy), std::move( - boost::apply_visitor(deltaVisitor, network[i]))), network[i]); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, network[i]), gy, + boost::apply_visitor(deltaVisitor, network[i])), network[i]); } g = boost::apply_visitor(deltaVisitor, network[0]); @@ -95,16 +95,15 @@ template template void MultiplyMerge::Gradient( - arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& /* gradient */ ) + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& /* gradient */ ) { if (run) { for (size_t i = 0; i < network.size(); ++i) { - boost::apply_visitor(GradientVisitor(std::move(input), std::move(error)), - network[i]); + boost::apply_visitor(GradientVisitor(input, error), network[i]); } } } diff --git a/src/mlpack/methods/ann/layer/padding.hpp b/src/mlpack/methods/ann/layer/padding.hpp index 0469f2e931..7ebcab07a2 100644 --- a/src/mlpack/methods/ann/layer/padding.hpp +++ b/src/mlpack/methods/ann/layer/padding.hpp @@ -55,7 +55,7 @@ class Padding * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -67,9 +67,9 @@ class Padding * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - const arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/padding_impl.hpp b/src/mlpack/methods/ann/layer/padding_impl.hpp index 8578e7ca09..6daa98321b 100644 --- a/src/mlpack/methods/ann/layer/padding_impl.hpp +++ b/src/mlpack/methods/ann/layer/padding_impl.hpp @@ -38,7 +38,7 @@ Padding::Padding( template template void Padding::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { nRows = input.n_rows; nCols = input.n_cols; @@ -51,9 +51,9 @@ void Padding::Forward( template template void Padding::Backward( - const arma::Mat&& /* input */, - const arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { g = gy.submat(padWLeft, padHTop, padWLeft + nRows - 1, padHTop + nCols - 1); diff --git a/src/mlpack/methods/ann/layer/parametric_relu.hpp b/src/mlpack/methods/ann/layer/parametric_relu.hpp index 9edde60190..6e66f185c1 100644 --- a/src/mlpack/methods/ann/layer/parametric_relu.hpp +++ b/src/mlpack/methods/ann/layer/parametric_relu.hpp @@ -68,7 +68,7 @@ class PReLU * @param output Resulting output activation. */ template - void Forward(const InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -80,7 +80,7 @@ class PReLU * @param g The calculated gradient. */ template - void Backward(const DataType&& input, DataType&& gy, DataType&& g); + void Backward(const DataType& input, const DataType& gy, DataType& g); /** * Calculate the gradient using the output delta and the input activation. @@ -90,9 +90,9 @@ class PReLU * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient); //! Get the parameters. OutputDataType const& Parameters() const { return alpha; } diff --git a/src/mlpack/methods/ann/layer/parametric_relu_impl.hpp b/src/mlpack/methods/ann/layer/parametric_relu_impl.hpp index d7c8ac4ffb..a794d99eae 100644 --- a/src/mlpack/methods/ann/layer/parametric_relu_impl.hpp +++ b/src/mlpack/methods/ann/layer/parametric_relu_impl.hpp @@ -39,7 +39,7 @@ void PReLU::Reset() template template void PReLU::Forward( - const InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { output = input; arma::uvec negative = arma::find(input < 0); @@ -49,7 +49,7 @@ void PReLU::Forward( template template void PReLU::Backward( - const DataType&& input, DataType&& gy, DataType&& g) + const DataType& input, const DataType& gy, DataType& g) { DataType derivative; derivative.set_size(arma::size(input)); @@ -64,8 +64,9 @@ void PReLU::Backward( template template void PReLU::Gradient( - const arma::Mat&& input, arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient) { if (gradient.n_elem == 0) { diff --git a/src/mlpack/methods/ann/layer/recurrent.hpp b/src/mlpack/methods/ann/layer/recurrent.hpp index fc1914352a..97d6187e2e 100644 --- a/src/mlpack/methods/ann/layer/recurrent.hpp +++ b/src/mlpack/methods/ann/layer/recurrent.hpp @@ -83,7 +83,7 @@ class Recurrent * @param output Resulting output activation. */ template - void Forward(arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -95,9 +95,9 @@ class Recurrent * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /* * Calculate the gradient using the output delta and the input activation. @@ -107,9 +107,9 @@ class Recurrent * @param gradient The calculated gradient. */ template - void Gradient(arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& /* gradient */); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& /* gradient */); //! Get the model modules. std::vector >& Model() { return network; } diff --git a/src/mlpack/methods/ann/layer/recurrent_attention.hpp b/src/mlpack/methods/ann/layer/recurrent_attention.hpp index 087b6bdb6b..66993b31ab 100644 --- a/src/mlpack/methods/ann/layer/recurrent_attention.hpp +++ b/src/mlpack/methods/ann/layer/recurrent_attention.hpp @@ -83,7 +83,7 @@ class RecurrentAttention * @param output Resulting output activation. */ template - void Forward(arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -95,9 +95,9 @@ class RecurrentAttention * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /* * Calculate the gradient using the output delta and the input activation. @@ -107,9 +107,9 @@ class RecurrentAttention * @param gradient The calculated gradient. */ template - void Gradient(arma::Mat&& /* input */, - arma::Mat&& /* error */, - arma::Mat&& /* gradient */); + void Gradient(const arma::Mat& /* input */, + const arma::Mat& /* error */, + arma::Mat& /* gradient */); //! Get the model modules. std::vector>& Model() { return network; } @@ -154,19 +154,19 @@ class RecurrentAttention // Gradient of the action module. if (backwardStep == (rho - 1)) { - boost::apply_visitor(GradientVisitor(std::move(initialInput), - std::move(actionError)), actionModule); + boost::apply_visitor(GradientVisitor(initialInput, actionError), + actionModule); } else { - boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, actionModule)), std::move(actionError)), + boost::apply_visitor(GradientVisitor(boost::apply_visitor( + outputParameterVisitor, actionModule), actionError), actionModule); } // Gradient of the recurrent module. - boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, rnnModule)), std::move(recurrentError)), + boost::apply_visitor(GradientVisitor(boost::apply_visitor( + outputParameterVisitor, rnnModule), recurrentError), rnnModule); attentionGradient += intermediateGradient; diff --git a/src/mlpack/methods/ann/layer/recurrent_attention_impl.hpp b/src/mlpack/methods/ann/layer/recurrent_attention_impl.hpp index b29e369828..a276ed20c0 100644 --- a/src/mlpack/methods/ann/layer/recurrent_attention_impl.hpp +++ b/src/mlpack/methods/ann/layer/recurrent_attention_impl.hpp @@ -58,7 +58,7 @@ RecurrentAttention::RecurrentAttention( template template void RecurrentAttention::Forward( - arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { // Initialize the action input. if (initialInput.is_empty()) @@ -71,15 +71,15 @@ void RecurrentAttention::Forward( { if (forwardStep == 0) { - boost::apply_visitor(ForwardVisitor(std::move(initialInput), std::move( - boost::apply_visitor(outputParameterVisitor, actionModule))), + boost::apply_visitor(ForwardVisitor(initialInput, + boost::apply_visitor(outputParameterVisitor, actionModule)), actionModule); } else { - boost::apply_visitor(ForwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, rnnModule)), std::move(boost::apply_visitor( - outputParameterVisitor, actionModule))), actionModule); + boost::apply_visitor(ForwardVisitor(boost::apply_visitor( + outputParameterVisitor, rnnModule), boost::apply_visitor( + outputParameterVisitor, actionModule)), actionModule); } // Initialize the glimpse input. @@ -89,8 +89,8 @@ void RecurrentAttention::Forward( actionModule).n_elem - 1, 1) = boost::apply_visitor( outputParameterVisitor, actionModule); - boost::apply_visitor(ForwardVisitor(std::move(glimpseInput), - std::move(boost::apply_visitor(outputParameterVisitor, rnnModule))), + boost::apply_visitor(ForwardVisitor(glimpseInput, + boost::apply_visitor(outputParameterVisitor, rnnModule)), rnnModule); // Save the output parameter when training the module. @@ -99,7 +99,7 @@ void RecurrentAttention::Forward( for (size_t l = 0; l < network.size(); ++l) { boost::apply_visitor(SaveOutputParameterVisitor( - std::move(moduleOutputParameter)), network[l]); + moduleOutputParameter), network[l]); } } } @@ -113,9 +113,9 @@ void RecurrentAttention::Forward( template template void RecurrentAttention::Backward( - const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { if (intermediateGradient.is_empty() && backwardStep == 0) { @@ -137,9 +137,9 @@ void RecurrentAttention::Backward( { size_t offset = 0; offset += boost::apply_visitor(GradientSetVisitor( - std::move(intermediateGradient), offset), rnnModule); + intermediateGradient, offset), rnnModule); boost::apply_visitor(GradientSetVisitor( - std::move(intermediateGradient), offset), actionModule); + intermediateGradient, offset), actionModule); attentionGradient.zeros(); } @@ -159,24 +159,24 @@ void RecurrentAttention::Backward( for (size_t l = 0; l < network.size(); ++l) { boost::apply_visitor(LoadOutputParameterVisitor( - std::move(moduleOutputParameter)), network[network.size() - 1 - l]); + moduleOutputParameter), network[network.size() - 1 - l]); } if (backwardStep == (rho - 1)) { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, actionModule)), std::move(actionError), - std::move(actionDelta)), actionModule); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, actionModule), actionError, + actionDelta), actionModule); } else { - boost::apply_visitor(BackwardVisitor(std::move(initialInput), - std::move(actionError), std::move(actionDelta)), actionModule); + boost::apply_visitor(BackwardVisitor(initialInput, actionError, + actionDelta), actionModule); } - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, rnnModule)), std::move(recurrentError), - std::move(rnnDelta)), rnnModule); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, rnnModule), recurrentError, rnnDelta), + rnnModule); if (backwardStep == 0) { @@ -194,15 +194,15 @@ void RecurrentAttention::Backward( template template void RecurrentAttention::Gradient( - arma::Mat&& /* input */, - arma::Mat&& /* error */, - arma::Mat&& /* gradient */) + const arma::Mat& /* input */, + const arma::Mat& /* error */, + arma::Mat& /* gradient */) { size_t offset = 0; offset += boost::apply_visitor(GradientUpdateVisitor( - std::move(attentionGradient), offset), rnnModule); + attentionGradient, offset), rnnModule); boost::apply_visitor(GradientUpdateVisitor( - std::move(attentionGradient), offset), actionModule); + attentionGradient, offset), actionModule); } template diff --git a/src/mlpack/methods/ann/layer/recurrent_impl.hpp b/src/mlpack/methods/ann/layer/recurrent_impl.hpp index baa24f41b9..b79ba06fed 100644 --- a/src/mlpack/methods/ann/layer/recurrent_impl.hpp +++ b/src/mlpack/methods/ann/layer/recurrent_impl.hpp @@ -143,26 +143,24 @@ template template void Recurrent::Forward( - arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { if (forwardStep == 0) { - boost::apply_visitor(ForwardVisitor(std::move(input), std::move(output)), - initialModule); + boost::apply_visitor(ForwardVisitor(input, output), initialModule); } else { - boost::apply_visitor(ForwardVisitor(std::move(input), std::move( - boost::apply_visitor(outputParameterVisitor, inputModule))), + boost::apply_visitor(ForwardVisitor(input, + boost::apply_visitor(outputParameterVisitor, inputModule)), inputModule); - boost::apply_visitor(ForwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, transferModule)), std::move( - boost::apply_visitor(outputParameterVisitor, feedbackModule))), + boost::apply_visitor(ForwardVisitor(boost::apply_visitor( + outputParameterVisitor, transferModule), + boost::apply_visitor(outputParameterVisitor, feedbackModule)), feedbackModule); - boost::apply_visitor(ForwardVisitor(std::move(input), std::move(output)), - recurrentModule); + boost::apply_visitor(ForwardVisitor(input, output), recurrentModule); } output = boost::apply_visitor(outputParameterVisitor, transferModule); @@ -190,7 +188,7 @@ template template void Recurrent::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { if (!recurrentError.is_empty()) { @@ -203,26 +201,26 @@ void Recurrent::Backward( if (backwardStep < (rho - 1)) { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, recurrentModule)), std::move(recurrentError), - std::move(boost::apply_visitor(deltaVisitor, recurrentModule))), + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, recurrentModule), recurrentError, + boost::apply_visitor(deltaVisitor, recurrentModule)), recurrentModule); - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, inputModule)), std::move( - boost::apply_visitor(deltaVisitor, recurrentModule)), std::move(g)), + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, inputModule), + boost::apply_visitor(deltaVisitor, recurrentModule), g), inputModule); - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, feedbackModule)), std::move( - boost::apply_visitor(deltaVisitor, recurrentModule)), std::move( - boost::apply_visitor(deltaVisitor, feedbackModule))), feedbackModule); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, feedbackModule), + boost::apply_visitor(deltaVisitor, recurrentModule), + boost::apply_visitor(deltaVisitor, feedbackModule)), feedbackModule); } else { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, initialModule)), std::move(recurrentError), - std::move(g)), initialModule); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, initialModule), recurrentError, g), + initialModule); } recurrentError = boost::apply_visitor(deltaVisitor, feedbackModule); @@ -233,22 +231,21 @@ template template void Recurrent::Gradient( - arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& /* gradient */) + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& /* gradient */) { if (gradientStep < (rho - 1)) { - boost::apply_visitor(GradientVisitor(std::move(input), std::move(error)), - recurrentModule); + boost::apply_visitor(GradientVisitor(input, error), recurrentModule); - boost::apply_visitor(GradientVisitor(std::move(input), std::move( - boost::apply_visitor(deltaVisitor, mergeModule))), inputModule); + boost::apply_visitor(GradientVisitor(input, + boost::apply_visitor(deltaVisitor, mergeModule)), inputModule); - boost::apply_visitor(GradientVisitor(std::move( + boost::apply_visitor(GradientVisitor( feedbackOutputParameter[feedbackOutputParameter.size() - 2 - - gradientStep]), std::move(boost::apply_visitor(deltaVisitor, - mergeModule))), feedbackModule); + gradientStep], boost::apply_visitor(deltaVisitor, + mergeModule)), feedbackModule); } else { @@ -256,8 +253,8 @@ void Recurrent::Gradient( boost::apply_visitor(GradientZeroVisitor(), inputModule); boost::apply_visitor(GradientZeroVisitor(), feedbackModule); - boost::apply_visitor(GradientVisitor(std::move(input), std::move( - boost::apply_visitor(deltaVisitor, startModule))), initialModule); + boost::apply_visitor(GradientVisitor(input, + boost::apply_visitor(deltaVisitor, startModule)), initialModule); } gradientStep++; diff --git a/src/mlpack/methods/ann/layer/reinforce_normal.hpp b/src/mlpack/methods/ann/layer/reinforce_normal.hpp index bb0ea2e737..e9be3578a2 100644 --- a/src/mlpack/methods/ann/layer/reinforce_normal.hpp +++ b/src/mlpack/methods/ann/layer/reinforce_normal.hpp @@ -49,7 +49,7 @@ class ReinforceNormal * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -61,7 +61,7 @@ class ReinforceNormal * @param g The calculated gradient. */ template - void Backward(const DataType&& input, DataType&& /* gy */, DataType&& g); + void Backward(const DataType& input, const DataType& /* gy */, DataType& g); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/reinforce_normal_impl.hpp b/src/mlpack/methods/ann/layer/reinforce_normal_impl.hpp index dbe95e00bc..b526694dc9 100644 --- a/src/mlpack/methods/ann/layer/reinforce_normal_impl.hpp +++ b/src/mlpack/methods/ann/layer/reinforce_normal_impl.hpp @@ -29,7 +29,7 @@ ReinforceNormal::ReinforceNormal( template template void ReinforceNormal::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { if (!deterministic) { @@ -49,7 +49,7 @@ void ReinforceNormal::Forward( template template void ReinforceNormal::Backward( - const DataType&& input, DataType&& /* gy */, DataType&& g) + const DataType& input, const DataType& /* gy */, DataType& g) { g = (input - moduleInputParameter.back()) / std::pow(stdev, 2.0); diff --git a/src/mlpack/methods/ann/layer/reparametrization.hpp b/src/mlpack/methods/ann/layer/reparametrization.hpp index 1fc299e2ba..6de3f0862d 100644 --- a/src/mlpack/methods/ann/layer/reparametrization.hpp +++ b/src/mlpack/methods/ann/layer/reparametrization.hpp @@ -79,7 +79,7 @@ class Reparametrization * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -91,9 +91,9 @@ class Reparametrization * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& input, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& input, + const arma::Mat& gy, + arma::Mat& g); //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/reparametrization_impl.hpp b/src/mlpack/methods/ann/layer/reparametrization_impl.hpp index 21944725d3..6139ca1a86 100644 --- a/src/mlpack/methods/ann/layer/reparametrization_impl.hpp +++ b/src/mlpack/methods/ann/layer/reparametrization_impl.hpp @@ -50,7 +50,7 @@ Reparametrization::Reparametrization( template template void Reparametrization::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { if (input.n_rows != 2 * latentSize) { @@ -74,7 +74,7 @@ void Reparametrization::Forward( template template void Reparametrization::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { SoftplusFunction::Deriv(preStdDev, g); diff --git a/src/mlpack/methods/ann/layer/select.hpp b/src/mlpack/methods/ann/layer/select.hpp index 4ec71b9e16..e7c2b593ff 100644 --- a/src/mlpack/methods/ann/layer/select.hpp +++ b/src/mlpack/methods/ann/layer/select.hpp @@ -48,7 +48,7 @@ class Select * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -60,9 +60,9 @@ class Select * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/select_impl.hpp b/src/mlpack/methods/ann/layer/select_impl.hpp index f3a8b6797c..21b85c8fa5 100644 --- a/src/mlpack/methods/ann/layer/select_impl.hpp +++ b/src/mlpack/methods/ann/layer/select_impl.hpp @@ -31,7 +31,7 @@ Select::Select( template template void Select::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { if (elements == 0) { @@ -46,9 +46,9 @@ void Select::Forward( template template void Select::Backward( - const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { if (elements == 0) { diff --git a/src/mlpack/methods/ann/layer/sequential.hpp b/src/mlpack/methods/ann/layer/sequential.hpp index 57ec6742e0..be62af8d82 100644 --- a/src/mlpack/methods/ann/layer/sequential.hpp +++ b/src/mlpack/methods/ann/layer/sequential.hpp @@ -89,7 +89,7 @@ class Sequential * @param output Resulting output activation. */ template - void Forward(arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, using 3rd-order tensors as @@ -101,9 +101,9 @@ class Sequential * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /* * Calculate the gradient using the output delta and the input activation. @@ -113,9 +113,9 @@ class Sequential * @param gradient The calculated gradient. */ template - void Gradient(arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& /* gradient */); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& /* gradient */); /* * Add a new module to the model. diff --git a/src/mlpack/methods/ann/layer/sequential_impl.hpp b/src/mlpack/methods/ann/layer/sequential_impl.hpp index 268a21b974..89647277be 100644 --- a/src/mlpack/methods/ann/layer/sequential_impl.hpp +++ b/src/mlpack/methods/ann/layer/sequential_impl.hpp @@ -49,12 +49,11 @@ Sequential< template template -void Sequential< - InputDataType, OutputDataType, Residual, CustomLayers...>::Forward( - arma::Mat&& input, arma::Mat&& output) +void Sequential:: +Forward(const arma::Mat& input, arma::Mat& output) { - boost::apply_visitor(ForwardVisitor(std::move(input), std::move( - boost::apply_visitor(outputParameterVisitor, network.front()))), + boost::apply_visitor(ForwardVisitor(input, + boost::apply_visitor(outputParameterVisitor, network.front())), network.front()); if (!reset) @@ -81,9 +80,9 @@ void Sequential< boost::apply_visitor(SetInputHeightVisitor(height), network[i]); } - boost::apply_visitor(ForwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[i - 1])), std::move( - boost::apply_visitor(outputParameterVisitor, network[i]))), + boost::apply_visitor(ForwardVisitor(boost::apply_visitor( + outputParameterVisitor, network[i - 1]), + boost::apply_visitor(outputParameterVisitor, network[i])), network[i]); if (!reset) @@ -126,22 +125,22 @@ template void Sequential< InputDataType, OutputDataType, Residual, CustomLayers...>::Backward( - const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g) + const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network.back())), std::move(gy), - std::move(boost::apply_visitor(deltaVisitor, network.back()))), + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, network.back()), gy, + boost::apply_visitor(deltaVisitor, network.back())), network.back()); for (size_t i = 2; i < network.size() + 1; ++i) { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[network.size() - i])), std::move( - boost::apply_visitor(deltaVisitor, network[network.size() - i + 1])), - std::move(boost::apply_visitor(deltaVisitor, - network[network.size() - i]))), network[network.size() - i]); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, network[network.size() - i]), + boost::apply_visitor(deltaVisitor, network[network.size() - i + 1]), + boost::apply_visitor(deltaVisitor, network[network.size() - i])), + network[network.size() - i]); } g = boost::apply_visitor(deltaVisitor, network.front()); @@ -155,26 +154,25 @@ void Sequential< template template -void Sequential< - InputDataType, OutputDataType, Residual, CustomLayers...>::Gradient( - arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& /* gradient */) +void Sequential:: +Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& /* gradient */) { - boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[network.size() - 2])), std::move(error)), + boost::apply_visitor(GradientVisitor(boost::apply_visitor( + outputParameterVisitor, network[network.size() - 2]), error), network.back()); for (size_t i = 2; i < network.size(); ++i) { - boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, network[network.size() - i - 1])), std::move( - boost::apply_visitor(deltaVisitor, network[network.size() - i + 1]))), + boost::apply_visitor(GradientVisitor(boost::apply_visitor( + outputParameterVisitor, network[network.size() - i - 1]), + boost::apply_visitor(deltaVisitor, network[network.size() - i + 1])), network[network.size() - i]); } - boost::apply_visitor(GradientVisitor(std::move(input), std::move( - boost::apply_visitor(deltaVisitor, network[1]))), network.front()); + boost::apply_visitor(GradientVisitor(input, + boost::apply_visitor(deltaVisitor, network[1])), network.front()); } template - void Forward(InputType&& input, OutputType&& output) + void Forward(const InputType& input, OutputType& output) { size_t batchSize = input.n_cols / inSize; @@ -112,9 +112,9 @@ class Subview * @param g The calculated gradient. */ template - void Backward(arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g) + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g) { g = gy; } diff --git a/src/mlpack/methods/ann/layer/transposed_convolution.hpp b/src/mlpack/methods/ann/layer/transposed_convolution.hpp index 2ce2fb3b72..eda7364e1d 100644 --- a/src/mlpack/methods/ann/layer/transposed_convolution.hpp +++ b/src/mlpack/methods/ann/layer/transposed_convolution.hpp @@ -144,7 +144,7 @@ class TransposedConvolution * @param output Resulting output activation. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -156,9 +156,9 @@ class TransposedConvolution * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /* * Calculate the gradient using the output delta and the input activation. @@ -168,9 +168,9 @@ class TransposedConvolution * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& gradient); //! Get the parameters. OutputDataType const& Parameters() const { return weights; } diff --git a/src/mlpack/methods/ann/layer/transposed_convolution_impl.hpp b/src/mlpack/methods/ann/layer/transposed_convolution_impl.hpp index c0ec126337..4f9e84d3a5 100644 --- a/src/mlpack/methods/ann/layer/transposed_convolution_impl.hpp +++ b/src/mlpack/methods/ann/layer/transposed_convolution_impl.hpp @@ -207,10 +207,10 @@ void TransposedConvolution< GradientConvolutionRule, InputDataType, OutputDataType ->::Forward(const arma::Mat&& input, arma::Mat&& output) +>::Forward(const arma::Mat& input, arma::Mat& output) { batchSize = input.n_cols; - inputTemp = arma::cube(const_cast&&>(input).memptr(), + inputTemp = arma::cube(const_cast&>(input).memptr(), inputWidth, inputHeight, inSize * batchSize, false, false); if (strideWidth > 1 || strideHeight > 1) @@ -227,8 +227,8 @@ void TransposedConvolution< for (size_t i = 0; i < inputExpandedTemp.n_slices; ++i) { - paddingForward.Forward(std::move(inputExpandedTemp.slice(i)), - std::move(inputPaddedTemp.slice(i))); + paddingForward.Forward(inputExpandedTemp.slice(i), + inputPaddedTemp.slice(i)); } } else @@ -250,8 +250,7 @@ void TransposedConvolution< for (size_t i = 0; i < inputTemp.n_slices; ++i) { - paddingForward.Forward(std::move(inputTemp.slice(i)), - std::move(inputPaddedTemp.slice(i))); + paddingForward.Forward(inputTemp.slice(i), inputPaddedTemp.slice(i)); } } @@ -312,10 +311,10 @@ void TransposedConvolution< InputDataType, OutputDataType >::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { - arma::Cube mappedError(gy.memptr(), outputWidth, outputHeight, - outSize * batchSize, false, false); + arma::Cube mappedError(((arma::Mat&) gy).memptr(), outputWidth, + outputHeight, outSize * batchSize, false, false); arma::Cube mappedErrorPadded; if (paddingBackward.PadWLeft() != 0 || paddingBackward.PadWRight() != 0 || paddingBackward.PadHTop() != 0 || paddingBackward.PadHBottom() != 0) @@ -327,8 +326,8 @@ void TransposedConvolution< for (size_t i = 0; i < mappedError.n_slices; ++i) { - paddingBackward.Forward(std::move(mappedError.slice(i)), - std::move(mappedErrorPadded.slice(i))); + paddingBackward.Forward(mappedError.slice(i), + mappedErrorPadded.slice(i)); } } g.set_size(inputTemp.n_rows * inputTemp.n_cols * inSize, batchSize); @@ -382,11 +381,11 @@ void TransposedConvolution< InputDataType, OutputDataType >::Gradient( - const arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& gradient) { - arma::Cube mappedError(error.memptr(), outputWidth, + arma::Cube mappedError(((arma::Mat&) error).memptr(), outputWidth, outputHeight, outSize * batchSize, false, false); gradient.set_size(weights.n_elem, 1); diff --git a/src/mlpack/methods/ann/layer/virtual_batch_norm.hpp b/src/mlpack/methods/ann/layer/virtual_batch_norm.hpp index 01ce3c5903..72b06e16b4 100644 --- a/src/mlpack/methods/ann/layer/virtual_batch_norm.hpp +++ b/src/mlpack/methods/ann/layer/virtual_batch_norm.hpp @@ -76,7 +76,7 @@ class VirtualBatchNorm * @param output Resulting output activations. */ template - void Forward(const arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Backward pass through the layer. @@ -86,9 +86,9 @@ class VirtualBatchNorm * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& /* input */, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& /* input */, + const arma::Mat& gy, + arma::Mat& g); /** * Calculate the gradient using the output delta and the input activations. @@ -98,9 +98,9 @@ class VirtualBatchNorm * @param gradient The calculated gradient. */ template - void Gradient(const arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& gradient); //! Get the parameters. OutputDataType const& Parameters() const { return weights; } diff --git a/src/mlpack/methods/ann/layer/virtual_batch_norm_impl.hpp b/src/mlpack/methods/ann/layer/virtual_batch_norm_impl.hpp index b528f6ad98..250ef9392d 100644 --- a/src/mlpack/methods/ann/layer/virtual_batch_norm_impl.hpp +++ b/src/mlpack/methods/ann/layer/virtual_batch_norm_impl.hpp @@ -65,7 +65,7 @@ void VirtualBatchNorm::Reset() template template void VirtualBatchNorm::Forward( - const arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { inputParameter = input; arma::mat inputMean = arma::mean(input, 1); @@ -90,7 +90,7 @@ void VirtualBatchNorm::Forward( template template void VirtualBatchNorm::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { const arma::mat stdInv = 1.0 / arma::sqrt(variance + eps); @@ -115,9 +115,9 @@ void VirtualBatchNorm::Backward( template template void VirtualBatchNorm::Gradient( - const arma::Mat&& /* input */, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& /* input */, + const arma::Mat& error, + arma::Mat& gradient) { gradient.set_size(size + size, 1); diff --git a/src/mlpack/methods/ann/layer/vr_class_reward.hpp b/src/mlpack/methods/ann/layer/vr_class_reward.hpp index 7a6880f054..6c68673bd2 100644 --- a/src/mlpack/methods/ann/layer/vr_class_reward.hpp +++ b/src/mlpack/methods/ann/layer/vr_class_reward.hpp @@ -55,7 +55,7 @@ class VRClassReward * between 1 and the number of classes. */ template - double Forward(const InputType&& input, const TargetType&& target); + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. The negative log @@ -69,9 +69,9 @@ class VRClassReward * @param output The calculated error. */ template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const {return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/vr_class_reward_impl.hpp b/src/mlpack/methods/ann/layer/vr_class_reward_impl.hpp index 679c314677..43a17d83e6 100644 --- a/src/mlpack/methods/ann/layer/vr_class_reward_impl.hpp +++ b/src/mlpack/methods/ann/layer/vr_class_reward_impl.hpp @@ -34,7 +34,7 @@ VRClassReward::VRClassReward( template template double VRClassReward::Forward( - const InputType&& input, const TargetType&& target) + const InputType& input, const TargetType& target) { double output = 0; for (size_t i = 0; i < input.n_cols - 1; ++i) @@ -66,9 +66,9 @@ double VRClassReward::Forward( template template void VRClassReward::Backward( - const InputType&& input, - const TargetType&& target, - OutputType&& output) + const InputType& input, + const TargetType& target, + OutputType& output) { output = arma::zeros(input.n_rows, input.n_cols); for (size_t i = 0; i < (input.n_cols - 1); ++i) diff --git a/src/mlpack/methods/ann/layer/weight_norm.hpp b/src/mlpack/methods/ann/layer/weight_norm.hpp index c4762c22d4..ed4814943f 100644 --- a/src/mlpack/methods/ann/layer/weight_norm.hpp +++ b/src/mlpack/methods/ann/layer/weight_norm.hpp @@ -85,7 +85,7 @@ class WeightNorm * @param output Resulting output activations. */ template - void Forward(arma::Mat&& input, arma::Mat&& output); + void Forward(const arma::Mat& input, arma::Mat& output); /** * Backward pass through the layer. This function calls the Backward() @@ -96,9 +96,9 @@ class WeightNorm * @param g The calculated gradient. */ template - void Backward(const arma::Mat&& input, - arma::Mat&& gy, - arma::Mat&& g); + void Backward(const arma::Mat& input, + const arma::Mat& gy, + arma::Mat& g); /** * Calculate the gradient using the output delta, input activations and the @@ -109,9 +109,9 @@ class WeightNorm * @param gradient The calculated gradient. */ template - void Gradient(arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient); + void Gradient(const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient); //! Get the delta. OutputDataType const& Delta() const { return delta; } diff --git a/src/mlpack/methods/ann/layer/weight_norm_impl.hpp b/src/mlpack/methods/ann/layer/weight_norm_impl.hpp index d56b6156dc..1140106dfc 100644 --- a/src/mlpack/methods/ann/layer/weight_norm_impl.hpp +++ b/src/mlpack/methods/ann/layer/weight_norm_impl.hpp @@ -50,13 +50,12 @@ void WeightNorm::Reset() { // Set the weights of the inside layer to layerWeights. // This is done to set the non-bias terms correctly. - boost::apply_visitor(WeightSetVisitor(std::move(layerWeights), 0), - wrappedLayer); + boost::apply_visitor(WeightSetVisitor(layerWeights, 0), wrappedLayer); boost::apply_visitor(resetVisitor, wrappedLayer); - biasWeightSize = boost::apply_visitor(BiasSetVisitor(std::move(weights), - 0), wrappedLayer); + biasWeightSize = boost::apply_visitor(BiasSetVisitor(weights, 0), + wrappedLayer); vectorParameter = arma::mat(weights.memptr() + biasWeightSize, layerWeightSize - biasWeightSize, 1, false, false); @@ -69,15 +68,15 @@ template template void WeightNorm::Forward( - arma::Mat&& input, arma::Mat&& output) + const arma::Mat& input, arma::Mat& output) { // Initialize the non-bias weights of wrapped layer. const double normVectorParameter = arma::norm(vectorParameter, 2); layerWeights.rows(0, layerWeightSize - biasWeightSize - 1) = scalarParameter(0) * vectorParameter / normVectorParameter; - boost::apply_visitor(ForwardVisitor(std::move(input), std::move( - boost::apply_visitor(outputParameterVisitor, wrappedLayer))), + boost::apply_visitor(ForwardVisitor(input, + boost::apply_visitor(outputParameterVisitor, wrappedLayer)), wrappedLayer); output = boost::apply_visitor(outputParameterVisitor, wrappedLayer); @@ -87,11 +86,11 @@ template template void WeightNorm::Backward( - const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat& /* input */, const arma::Mat& gy, arma::Mat& g) { - boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor( - outputParameterVisitor, wrappedLayer)), std::move(gy), std::move( - boost::apply_visitor(deltaVisitor, wrappedLayer))), wrappedLayer); + boost::apply_visitor(BackwardVisitor(boost::apply_visitor( + outputParameterVisitor, wrappedLayer), gy, + boost::apply_visitor(deltaVisitor, wrappedLayer)), wrappedLayer); g = boost::apply_visitor(deltaVisitor, wrappedLayer); } @@ -100,15 +99,14 @@ template template void WeightNorm::Gradient( - arma::Mat&& input, - arma::Mat&& error, - arma::Mat&& gradient) + const arma::Mat& input, + const arma::Mat& error, + arma::Mat& gradient) { ResetGradients(layerGradients); // Calculate the gradients of the wrapped layer. - boost::apply_visitor(GradientVisitor(std::move(input), - std::move(error)), wrappedLayer); + boost::apply_visitor(GradientVisitor(input, error), wrappedLayer); // Store the norm of vector parameter temporarily. const double normVectorParameter = arma::norm(vectorParameter, 2); @@ -137,8 +135,7 @@ template::ResetGradients( arma::mat& gradient) { - boost::apply_visitor(GradientSetVisitor(std::move(gradient), 0), - wrappedLayer); + boost::apply_visitor(GradientSetVisitor(gradient, 0), wrappedLayer); } template - double Forward(const InputType&& input, const TargetType&& target); + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. @@ -59,9 +59,9 @@ class CrossEntropyError * @param output The calculated error. */ template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/cross_entropy_error_impl.hpp b/src/mlpack/methods/ann/loss_functions/cross_entropy_error_impl.hpp index 1edc6df5ee..9c6d73e481 100644 --- a/src/mlpack/methods/ann/loss_functions/cross_entropy_error_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/cross_entropy_error_impl.hpp @@ -28,7 +28,7 @@ CrossEntropyError::CrossEntropyError( template template double CrossEntropyError::Forward( - const InputType&& input, const TargetType&& target) + const InputType& input, const TargetType& target) { return -arma::accu(target % arma::log(input + eps) + (1. - target) % arma::log(1. - input + eps)); @@ -37,9 +37,9 @@ double CrossEntropyError::Forward( template template void CrossEntropyError::Backward( - const InputType&& input, - const TargetType&& target, - OutputType&& output) + const InputType& input, + const TargetType& target, + OutputType& output) { output = (1. - target) / (1. - input + eps) - target / (input + eps); } diff --git a/src/mlpack/methods/ann/loss_functions/dice_loss.hpp b/src/mlpack/methods/ann/loss_functions/dice_loss.hpp index 4b10db6c5b..a30618e424 100644 --- a/src/mlpack/methods/ann/loss_functions/dice_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/dice_loss.hpp @@ -62,7 +62,7 @@ class DiceLoss * @param target The target vector. */ template - double Forward(const InputType&& input, const TargetType&& target); + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. @@ -72,9 +72,9 @@ class DiceLoss * @param output The calculated error. */ template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/dice_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/dice_loss_impl.hpp index 77413de5bd..708c1102f7 100644 --- a/src/mlpack/methods/ann/loss_functions/dice_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/dice_loss_impl.hpp @@ -28,7 +28,7 @@ DiceLoss::DiceLoss( template template double DiceLoss::Forward( - const InputType&& input, const TargetType&& target) + const InputType& input, const TargetType& target) { return 1 - ((2 * arma::accu(target % input) + smooth) / (arma::accu(target % target) + arma::accu( @@ -38,9 +38,9 @@ double DiceLoss::Forward( template template void DiceLoss::Backward( - const InputType&& input, - const TargetType&& target, - OutputType&& output) + const InputType& input, + const TargetType& target, + OutputType& output) { output = -2 * (target * (arma::accu(input % input) + arma::accu(target % target) + smooth) - input * diff --git a/src/mlpack/methods/ann/loss_functions/earth_mover_distance.hpp b/src/mlpack/methods/ann/loss_functions/earth_mover_distance.hpp index 14e5daa8d5..0683715bd4 100644 --- a/src/mlpack/methods/ann/loss_functions/earth_mover_distance.hpp +++ b/src/mlpack/methods/ann/loss_functions/earth_mover_distance.hpp @@ -45,7 +45,7 @@ class EarthMoverDistance * @param target The target vector. */ template - double Forward(const InputType&& input, const TargetType&& target); + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. @@ -55,9 +55,9 @@ class EarthMoverDistance * @param output The calculated error. */ template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/earth_mover_distance_impl.hpp b/src/mlpack/methods/ann/loss_functions/earth_mover_distance_impl.hpp index b7db1786a4..9ba7ac8f47 100644 --- a/src/mlpack/methods/ann/loss_functions/earth_mover_distance_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/earth_mover_distance_impl.hpp @@ -27,7 +27,7 @@ EarthMoverDistance::EarthMoverDistance() template template double EarthMoverDistance::Forward( - const InputType&& input, const TargetType&& target) + const InputType& input, const TargetType& target) { return -arma::accu(target % input); } @@ -35,9 +35,9 @@ double EarthMoverDistance::Forward( template template void EarthMoverDistance::Backward( - const InputType&& /* input */, - const TargetType&& target, - OutputType&& output) + const InputType& /* input */, + const TargetType& target, + OutputType& output) { output = -target; } diff --git a/src/mlpack/methods/ann/loss_functions/kl_divergence.hpp b/src/mlpack/methods/ann/loss_functions/kl_divergence.hpp index 87f32de6f9..ad39d16c62 100644 --- a/src/mlpack/methods/ann/loss_functions/kl_divergence.hpp +++ b/src/mlpack/methods/ann/loss_functions/kl_divergence.hpp @@ -60,7 +60,7 @@ class KLDivergence * @param target Target data to compare with. */ template - double Forward(const InputType&& input, const TargetType&& target); + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. @@ -70,9 +70,9 @@ class KLDivergence * @param output The calculated error. */ template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp b/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp index 590878ed3b..4bd6a143dc 100644 --- a/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp @@ -29,7 +29,7 @@ KLDivergence::KLDivergence(const bool takeMean) : template template double KLDivergence::Forward( - const InputType&& input, const TargetType&& target) + const InputType& input, const TargetType& target) { if (takeMean) { @@ -45,9 +45,9 @@ double KLDivergence::Forward( template template void KLDivergence::Backward( - const InputType&& input, - const TargetType&& target, - OutputType&& output) + const InputType& input, + const TargetType& target, + OutputType& output) { if (takeMean) { diff --git a/src/mlpack/methods/ann/loss_functions/mean_bias_error.hpp b/src/mlpack/methods/ann/loss_functions/mean_bias_error.hpp index 1ec50e67b5..40418980d3 100644 --- a/src/mlpack/methods/ann/loss_functions/mean_bias_error.hpp +++ b/src/mlpack/methods/ann/loss_functions/mean_bias_error.hpp @@ -45,7 +45,7 @@ class MeanBiasError * @param target The target vector. */ template - double Forward(const InputType&& input, const TargetType&& target); + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. @@ -55,9 +55,9 @@ class MeanBiasError * @param output The calculated error. */ template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/mean_bias_error_impl.hpp b/src/mlpack/methods/ann/loss_functions/mean_bias_error_impl.hpp index 4a7a2114d2..8488ae487a 100644 --- a/src/mlpack/methods/ann/loss_functions/mean_bias_error_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/mean_bias_error_impl.hpp @@ -28,7 +28,7 @@ MeanBiasError::MeanBiasError() template template double MeanBiasError::Forward( - const InputType&& input, const TargetType&& target) + const InputType& input, const TargetType& target) { return arma::accu(target - input) / target.n_cols; } @@ -36,9 +36,9 @@ double MeanBiasError::Forward( template template void MeanBiasError::Backward( - const InputType&& input, - const TargetType&& target, - OutputType&& output) + const InputType& input, + const TargetType& /* target */, + OutputType& output) { output.set_size(arma::size(input)); output.fill(-1.0); diff --git a/src/mlpack/methods/ann/loss_functions/mean_squared_error.hpp b/src/mlpack/methods/ann/loss_functions/mean_squared_error.hpp index 6ab98d1791..6dc6642a0d 100644 --- a/src/mlpack/methods/ann/loss_functions/mean_squared_error.hpp +++ b/src/mlpack/methods/ann/loss_functions/mean_squared_error.hpp @@ -46,7 +46,7 @@ class MeanSquaredError * @param target The target vector. */ template - double Forward(const InputType&& input, const TargetType&& target); + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. @@ -56,9 +56,9 @@ class MeanSquaredError * @param output The calculated error. */ template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/mean_squared_error_impl.hpp b/src/mlpack/methods/ann/loss_functions/mean_squared_error_impl.hpp index 82c9cc3bd9..d7203b2499 100644 --- a/src/mlpack/methods/ann/loss_functions/mean_squared_error_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/mean_squared_error_impl.hpp @@ -27,7 +27,7 @@ MeanSquaredError::MeanSquaredError() template template double MeanSquaredError::Forward( - const InputType&& input, const TargetType&& target) + const InputType& input, const TargetType& target) { return arma::accu(arma::square(input - target)) / target.n_cols; } @@ -35,9 +35,9 @@ double MeanSquaredError::Forward( template template void MeanSquaredError::Backward( - const InputType&& input, - const TargetType&& target, - OutputType&& output) + const InputType& input, + const TargetType& target, + OutputType& output) { output = 2 * (input - target) / target.n_cols; } diff --git a/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error.hpp b/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error.hpp index d6cf0b30f9..54b74d17d5 100644 --- a/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error.hpp +++ b/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error.hpp @@ -45,7 +45,7 @@ class MeanSquaredLogarithmicError * @param target The target vector. */ template - double Forward(const InputType&& input, const TargetType&& target); + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. @@ -55,9 +55,9 @@ class MeanSquaredLogarithmicError * @param output The calculated error. */ template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error_impl.hpp b/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error_impl.hpp index 61e21c159e..4a2aa75d8a 100644 --- a/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error_impl.hpp @@ -28,7 +28,7 @@ MeanSquaredLogarithmicError template template double MeanSquaredLogarithmicError::Forward( - const InputType&& input, const TargetType&& target) + const InputType& input, const TargetType& target) { return arma::accu(arma::square(arma::log(1. + target) - arma::log(1. + input))) / target.n_cols; @@ -37,9 +37,9 @@ double MeanSquaredLogarithmicError::Forward( template template void MeanSquaredLogarithmicError::Backward( - const InputType&& input, - const TargetType&& target, - OutputType&& output) + const InputType& input, + const TargetType& target, + OutputType& output) { output = 2 * (arma::log(1. + input) - arma::log(1. + target)) / ((1. + input) * target.n_cols); diff --git a/src/mlpack/methods/ann/loss_functions/negative_log_likelihood.hpp b/src/mlpack/methods/ann/loss_functions/negative_log_likelihood.hpp index fdc5433eaf..200fa1c5f3 100644 --- a/src/mlpack/methods/ann/loss_functions/negative_log_likelihood.hpp +++ b/src/mlpack/methods/ann/loss_functions/negative_log_likelihood.hpp @@ -48,7 +48,7 @@ class NegativeLogLikelihood * between 1 and the number of classes. */ template - double Forward(const InputType&& input, TargetType&& target); + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. The negative log @@ -62,9 +62,9 @@ class NegativeLogLikelihood * @param output The calculated error. */ template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the input parameter. InputDataType& InputParameter() const { return inputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/negative_log_likelihood_impl.hpp b/src/mlpack/methods/ann/loss_functions/negative_log_likelihood_impl.hpp index 922546753b..d006b17912 100644 --- a/src/mlpack/methods/ann/loss_functions/negative_log_likelihood_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/negative_log_likelihood_impl.hpp @@ -27,7 +27,7 @@ NegativeLogLikelihood::NegativeLogLikelihood() template template double NegativeLogLikelihood::Forward( - const InputType&& input, TargetType&& target) + const InputType& input, const TargetType& target) { double output = 0; for (size_t i = 0; i < input.n_cols; ++i) @@ -45,9 +45,9 @@ double NegativeLogLikelihood::Forward( template template void NegativeLogLikelihood::Backward( - const InputType&& input, - const TargetType&& target, - OutputType&& output) + const InputType& input, + const TargetType& target, + OutputType& output) { output = arma::zeros(input.n_rows, input.n_cols); for (size_t i = 0; i < input.n_cols; ++i) diff --git a/src/mlpack/methods/ann/loss_functions/reconstruction_loss.hpp b/src/mlpack/methods/ann/loss_functions/reconstruction_loss.hpp index 71cf33cd3c..d8c775efc0 100644 --- a/src/mlpack/methods/ann/loss_functions/reconstruction_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/reconstruction_loss.hpp @@ -49,7 +49,7 @@ class ReconstructionLoss * @param target The target matrix. */ template - double Forward(const InputType&& input, const TargetType&& target); + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. @@ -59,9 +59,9 @@ class ReconstructionLoss * @param output The calculated error. */ template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/reconstruction_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/reconstruction_loss_impl.hpp index 73fd57f64d..02ea50f4a7 100644 --- a/src/mlpack/methods/ann/loss_functions/reconstruction_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/reconstruction_loss_impl.hpp @@ -31,20 +31,20 @@ ReconstructionLoss< template template double ReconstructionLoss::Forward( - const InputType&& input, const TargetType&& target) + const InputType& input, const TargetType& target) { - dist = DistType(std::move(input)); - return -dist.LogProbability(std::move(target)); + dist = DistType(input); + return -dist.LogProbability(target); } template template void ReconstructionLoss::Backward( - const InputType&& /* input */, - const TargetType&& target, - OutputType&& output) + const InputType& /* input */, + const TargetType& target, + OutputType& output) { - dist.LogProbBackward(std::move(target), std::move(output)); + dist.LogProbBackward(target, output); output *= -1; } diff --git a/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp b/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp index 6021d36e2d..fec584de8c 100644 --- a/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp +++ b/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp @@ -64,8 +64,8 @@ class SigmoidCrossEntropyError * @param target The target vector. */ template - inline double Forward(const InputType&& input, - const TargetType&& target); + inline double Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. * @@ -74,9 +74,9 @@ class SigmoidCrossEntropyError * @param output The calculated error. */ template - inline void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + inline void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error_impl.hpp b/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error_impl.hpp index 853729a0b5..1ab976874a 100644 --- a/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error_impl.hpp @@ -30,7 +30,7 @@ SigmoidCrossEntropyError template template inline double SigmoidCrossEntropyError::Forward( - const InputType&& input, const TargetType&& target) + const InputType& input, const TargetType& target) { double maximum = 0; for (size_t i = 0; i < input.n_elem; ++i) @@ -45,9 +45,9 @@ inline double SigmoidCrossEntropyError::Forward( template template inline void SigmoidCrossEntropyError::Backward( - const InputType&& input, - const TargetType&& target, - OutputType&& output) + const InputType& input, + const TargetType& target, + OutputType& output) { output = 1.0 / (1.0 + arma::exp(-input)) - target; } diff --git a/src/mlpack/methods/ann/rnn.hpp b/src/mlpack/methods/ann/rnn.hpp index c94d647518..5c6bdf2b74 100644 --- a/src/mlpack/methods/ann/rnn.hpp +++ b/src/mlpack/methods/ann/rnn.hpp @@ -295,7 +295,8 @@ class RNN * * @param input Data sequence to compute probabilities for. */ - void Forward(arma::mat&& input); + template + void Forward(const InputType& input); /** * Reset the state of RNN cells in the network for new input sequence. @@ -313,7 +314,7 @@ class RNN * layer defined optimizer. */ template - void Gradient(InputType&& input); + void Gradient(const InputType& input); /** * Reset the module status by setting the current deterministic parameter diff --git a/src/mlpack/methods/ann/rnn_impl.hpp b/src/mlpack/methods/ann/rnn_impl.hpp index 054d38923e..806efac8f7 100644 --- a/src/mlpack/methods/ann/rnn_impl.hpp +++ b/src/mlpack/methods/ann/rnn_impl.hpp @@ -158,8 +158,8 @@ void RNN::Predict( const size_t effectiveBatchSize = std::min(batchSize, size_t(predictors.n_cols)); - Forward(std::move(arma::mat(predictors.slice(0).colptr(0), - predictors.n_rows, effectiveBatchSize, false, true))); + Forward(arma::mat(predictors.slice(0).colptr(0), predictors.n_rows, + effectiveBatchSize, false, true)); arma::mat resultsTemp = boost::apply_visitor(outputParameterVisitor, network.back()); @@ -175,8 +175,8 @@ void RNN::Predict( size_t(predictors.n_cols - begin)); for (size_t seqNum = !begin; seqNum < rho; ++seqNum) { - Forward(std::move(arma::mat(predictors.slice(seqNum).colptr(begin), - predictors.n_rows, effectiveBatchSize, false, true))); + Forward(arma::mat(predictors.slice(seqNum).colptr(begin), + predictors.n_rows, effectiveBatchSize, false, true)); results.slice(seqNum).submat(0, begin, results.n_rows - 1, begin + effectiveBatchSize - 1) = boost::apply_visitor(outputParameterVisitor, @@ -224,16 +224,16 @@ double RNN::Evaluate( // Wrap a matrix around our data to avoid a copy. arma::mat stepData(predictors.slice(seqNum).colptr(begin), predictors.n_rows, batchSize, false, true); - Forward(std::move(stepData)); + Forward(stepData); if (!single) { responseSeq = seqNum; } - performance += outputLayer.Forward(std::move(boost::apply_visitor( - outputParameterVisitor, network.back())), - std::move(arma::mat(responses.slice(responseSeq).colptr(begin), - responses.n_rows, batchSize, false, true))); + performance += outputLayer.Forward(boost::apply_visitor( + outputParameterVisitor, network.back()), + arma::mat(responses.slice(responseSeq).colptr(begin), + responses.n_rows, batchSize, false, true)); } if (outputSize == 0) @@ -306,7 +306,7 @@ EvaluateWithGradient(const arma::mat& /* parameters */, // Wrap a matrix around our data to avoid a copy. arma::mat stepData(predictors.slice(seqNum).colptr(begin), predictors.n_rows, batchSize, false, true); - Forward(std::move(stepData)); + Forward(stepData); if (!single) { responseSeq = seqNum; @@ -314,14 +314,14 @@ EvaluateWithGradient(const arma::mat& /* parameters */, for (size_t l = 0; l < network.size(); ++l) { - boost::apply_visitor(SaveOutputParameterVisitor( - std::move(moduleOutputParameter)), network[l]); + boost::apply_visitor(SaveOutputParameterVisitor( moduleOutputParameter), + network[l]); } - performance += outputLayer.Forward(std::move(boost::apply_visitor( - outputParameterVisitor, network.back())), - std::move(arma::mat(responses.slice(responseSeq).colptr(begin), - responses.n_rows, batchSize, false, true))); + performance += outputLayer.Forward(boost::apply_visitor( + outputParameterVisitor, network.back()), + arma::mat(responses.slice(responseSeq).colptr(begin), + responses.n_rows, batchSize, false, true)); } if (outputSize == 0) @@ -344,8 +344,8 @@ EvaluateWithGradient(const arma::mat& /* parameters */, currentGradient.zeros(); for (size_t l = 0; l < network.size(); ++l) { - boost::apply_visitor(LoadOutputParameterVisitor( - std::move(moduleOutputParameter)), network[network.size() - 1 - l]); + boost::apply_visitor(LoadOutputParameterVisitor(moduleOutputParameter), + network[network.size() - 1 - l]); } if (single && seqNum > 0) @@ -354,24 +354,23 @@ EvaluateWithGradient(const arma::mat& /* parameters */, } else if (single && seqNum == 0) { - outputLayer.Backward(std::move(boost::apply_visitor( - outputParameterVisitor, network.back())), - std::move(arma::mat(responses.slice(0).colptr(begin), - responses.n_rows, batchSize, false, true)), std::move(error)); + outputLayer.Backward(boost::apply_visitor( + outputParameterVisitor, network.back()), + arma::mat(responses.slice(0).colptr(begin), + responses.n_rows, batchSize, false, true), error); } else { - outputLayer.Backward(std::move(boost::apply_visitor( - outputParameterVisitor, network.back())), - std::move(arma::mat( - responses.slice(effectiveRho - seqNum - 1).colptr(begin), - responses.n_rows, batchSize, false, true)), std::move(error)); + outputLayer.Backward(boost::apply_visitor( + outputParameterVisitor, network.back()), + arma::mat(responses.slice(effectiveRho - seqNum - 1).colptr(begin), + responses.n_rows, batchSize, false, true), error); } Backward(); - Gradient(std::move( + Gradient( arma::mat(predictors.slice(effectiveRho - seqNum - 1).colptr(begin), - predictors.n_rows, batchSize, false, true))); + predictors.n_rows, batchSize, false, true)); gradient += currentGradient; } @@ -444,25 +443,25 @@ void RNN& layer : network) { - offset += boost::apply_visitor(GradientSetVisitor(std::move(gradient), - offset), layer); + offset += boost::apply_visitor(GradientSetVisitor(gradient, offset), layer); } } template +template void RNN::Forward(arma::mat&& input) + CustomLayers...>::Forward(const InputType& input) { - boost::apply_visitor(ForwardVisitor(std::move(input), std::move( - boost::apply_visitor(outputParameterVisitor, network.front()))), + boost::apply_visitor(ForwardVisitor(input, + boost::apply_visitor(outputParameterVisitor, network.front())), network.front()); for (size_t i = 1; i < network.size(); ++i) { boost::apply_visitor(ForwardVisitor( - std::move(boost::apply_visitor(outputParameterVisitor, network[i - 1])), - std::move(boost::apply_visitor(outputParameterVisitor, network[i]))), + boost::apply_visitor(outputParameterVisitor, network[i - 1]), + boost::apply_visitor(outputParameterVisitor, network[i])), network[i]); } } @@ -472,17 +471,17 @@ template::Backward() { boost::apply_visitor(BackwardVisitor( - std::move(boost::apply_visitor(outputParameterVisitor, network.back())), - std::move(error), std::move(boost::apply_visitor(deltaVisitor, - network.back()))), network.back()); + boost::apply_visitor(outputParameterVisitor, network.back()), + error, boost::apply_visitor(deltaVisitor, + network.back())), network.back()); for (size_t i = 2; i < network.size(); ++i) { boost::apply_visitor(BackwardVisitor( - std::move(boost::apply_visitor(outputParameterVisitor, - network[network.size() - i])), std::move(boost::apply_visitor( - deltaVisitor, network[network.size() - i + 1])), std::move( - boost::apply_visitor(deltaVisitor, network[network.size() - i]))), + boost::apply_visitor(outputParameterVisitor, + network[network.size() - i]), boost::apply_visitor( + deltaVisitor, network[network.size() - i + 1]), + boost::apply_visitor(deltaVisitor, network[network.size() - i])), network[network.size() - i]); } } @@ -491,16 +490,16 @@ template template void RNN::Gradient(InputType&& input) + CustomLayers...>::Gradient(const InputType& input) { - boost::apply_visitor(GradientVisitor(std::move(input), std::move( - boost::apply_visitor(deltaVisitor, network[1]))), network.front()); + boost::apply_visitor(GradientVisitor(input, + boost::apply_visitor(deltaVisitor, network[1])), network.front()); for (size_t i = 1; i < network.size() - 1; ++i) { boost::apply_visitor(GradientVisitor( - std::move(boost::apply_visitor(outputParameterVisitor, network[i - 1])), - std::move(boost::apply_visitor(deltaVisitor, network[i + 1]))), + boost::apply_visitor(outputParameterVisitor, network[i - 1]), + boost::apply_visitor(deltaVisitor, network[i + 1])), network[i]); } } @@ -544,8 +543,8 @@ void RNN::serialize( size_t offset = 0; for (LayerTypes& layer : network) { - offset += boost::apply_visitor(WeightSetVisitor(std::move(parameter), - offset), layer); + offset += boost::apply_visitor(WeightSetVisitor(parameter, offset), + layer); boost::apply_visitor(resetVisitor, layer); } diff --git a/src/mlpack/methods/ann/visitor/backward_visitor.hpp b/src/mlpack/methods/ann/visitor/backward_visitor.hpp index a893f35d16..b7755e42cb 100644 --- a/src/mlpack/methods/ann/visitor/backward_visitor.hpp +++ b/src/mlpack/methods/ann/visitor/backward_visitor.hpp @@ -30,11 +30,15 @@ class BackwardVisitor : public boost::static_visitor public: //! Execute the Backward() function given the input, error and delta //! parameter. - BackwardVisitor(arma::mat&& input, arma::mat&& error, arma::mat&& delta); + BackwardVisitor(const arma::mat& input, + const arma::mat& error, + arma::mat& delta); //! Execute the Backward() function for the layer with the specified index. - BackwardVisitor(arma::mat&& input, arma::mat&& error, arma::mat&& delta, - const size_t index); + BackwardVisitor(const arma::mat& input, + const arma::mat& error, + arma::mat& delta, + const size_t index); //! Execute the Backward() function. template @@ -44,13 +48,13 @@ class BackwardVisitor : public boost::static_visitor private: //! The input parameter set. - arma::mat&& input; + const arma::mat& input; //! The error parameter. - arma::mat&& error; + const arma::mat& error; //! The delta parameter. - arma::mat&& delta; + arma::mat& delta; //! The index of the layer to run. size_t index; diff --git a/src/mlpack/methods/ann/visitor/backward_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/backward_visitor_impl.hpp index ef449e45bd..e49f3c2e41 100644 --- a/src/mlpack/methods/ann/visitor/backward_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/backward_visitor_impl.hpp @@ -19,25 +19,25 @@ namespace mlpack { namespace ann { //! BackwardVisitor visitor class. -inline BackwardVisitor::BackwardVisitor(arma::mat&& input, - arma::mat&& error, - arma::mat&& delta) : - input(std::move(input)), - error(std::move(error)), - delta(std::move(delta)), +inline BackwardVisitor::BackwardVisitor(const arma::mat& input, + const arma::mat& error, + arma::mat& delta) : + input(input), + error(error), + delta(delta), index(0), hasIndex(false) { /* Nothing to do here. */ } -inline BackwardVisitor::BackwardVisitor(arma::mat&& input, - arma::mat&& error, - arma::mat&& delta, +inline BackwardVisitor::BackwardVisitor(const arma::mat& input, + const arma::mat& error, + arma::mat& delta, const size_t index) : - input(std::move(input)), - error(std::move(error)), - delta(std::move(delta)), + input(input), + error(error), + delta(delta), index(index), hasIndex(true) { @@ -60,7 +60,7 @@ inline typename std::enable_if< !HasRunCheck::value, void>::type BackwardVisitor::LayerBackward(T* layer, arma::mat& /* input */) const { - layer->Backward(std::move(input), std::move(error), std::move(delta)); + layer->Backward(input, error, delta); } template @@ -70,13 +70,11 @@ BackwardVisitor::LayerBackward(T* layer, arma::mat& /* input */) const { if (!hasIndex) { - layer->Backward(std::move(input), std::move(error), - std::move(delta)); + layer->Backward(input, error, delta); } else { - layer->Backward(std::move(input), std::move(error), - std::move(delta), index); + layer->Backward(input, error, delta, index); } } diff --git a/src/mlpack/methods/ann/visitor/bias_set_visitor.hpp b/src/mlpack/methods/ann/visitor/bias_set_visitor.hpp index d0c4c75706..e67f4a1e02 100644 --- a/src/mlpack/methods/ann/visitor/bias_set_visitor.hpp +++ b/src/mlpack/methods/ann/visitor/bias_set_visitor.hpp @@ -27,7 +27,7 @@ class BiasSetVisitor : public boost::static_visitor { public: //! Update the bias parameters given the parameters' set and offset. - BiasSetVisitor(arma::mat&& weight, const size_t offset = 0); + BiasSetVisitor(arma::mat& weight, const size_t offset = 0); //! Update the parameters' set. template @@ -37,7 +37,7 @@ class BiasSetVisitor : public boost::static_visitor private: //! The parameters' set. - arma::mat&& weight; + arma::mat& weight; //! The parameters' offset. const size_t offset; diff --git a/src/mlpack/methods/ann/visitor/bias_set_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/bias_set_visitor_impl.hpp index 46e0913ad1..73051c80d8 100644 --- a/src/mlpack/methods/ann/visitor/bias_set_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/bias_set_visitor_impl.hpp @@ -19,8 +19,8 @@ namespace mlpack { namespace ann { //! BiasSetVisitor visitor class. -inline BiasSetVisitor::BiasSetVisitor(arma::mat&& weight, const size_t offset) : - weight(std::move(weight)), +inline BiasSetVisitor::BiasSetVisitor(arma::mat& weight, const size_t offset) : + weight(weight), offset(offset) { /* Nothing to do here. */ @@ -57,7 +57,7 @@ BiasSetVisitor::LayerSize(T* layer) const for (size_t i = 0; i < layer->Model().size(); ++i) { modelOffset += boost::apply_visitor(BiasSetVisitor( - std::move(weight), modelOffset + offset), layer->Model()[i]); + weight, modelOffset + offset), layer->Model()[i]); } return modelOffset; @@ -89,7 +89,7 @@ BiasSetVisitor::LayerSize(T* layer) const for (size_t i = 0; i < layer->Model().size(); ++i) { modelOffset += boost::apply_visitor(BiasSetVisitor( - std::move(weight), modelOffset + offset), layer->Model()[i]); + weight, modelOffset + offset), layer->Model()[i]); } return modelOffset; diff --git a/src/mlpack/methods/ann/visitor/forward_visitor.hpp b/src/mlpack/methods/ann/visitor/forward_visitor.hpp index c28ab3ad34..e2c181a5d6 100644 --- a/src/mlpack/methods/ann/visitor/forward_visitor.hpp +++ b/src/mlpack/methods/ann/visitor/forward_visitor.hpp @@ -29,7 +29,7 @@ class ForwardVisitor : public boost::static_visitor { public: //! Execute the Forward() function given the input and output parameter. - ForwardVisitor(arma::mat&& input, arma::mat&& output); + ForwardVisitor(const arma::mat& input, arma::mat& output); //! Execute the Forward() function. template @@ -39,10 +39,10 @@ class ForwardVisitor : public boost::static_visitor private: //! The input parameter set. - arma::mat&& input; + const arma::mat& input; //! The output parameter set. - arma::mat&& output; + arma::mat& output; }; } // namespace ann diff --git a/src/mlpack/methods/ann/visitor/forward_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/forward_visitor_impl.hpp index ffddac9bb7..44baacdc75 100644 --- a/src/mlpack/methods/ann/visitor/forward_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/forward_visitor_impl.hpp @@ -19,9 +19,9 @@ namespace mlpack { namespace ann { //! ForwardVisitor visitor class. -inline ForwardVisitor::ForwardVisitor(arma::mat&& input, arma::mat&& output) : - input(std::move(input)), - output(std::move(output)) +inline ForwardVisitor::ForwardVisitor(const arma::mat& input, arma::mat& output) : + input(input), + output(output) { /* Nothing to do here. */ } @@ -29,7 +29,7 @@ inline ForwardVisitor::ForwardVisitor(arma::mat&& input, arma::mat&& output) : template inline void ForwardVisitor::operator()(LayerType* layer) const { - layer->Forward(std::move(input), std::move(output)); + layer->Forward(input, output); } inline void ForwardVisitor::operator()(MoreTypes layer) const diff --git a/src/mlpack/methods/ann/visitor/gradient_set_visitor.hpp b/src/mlpack/methods/ann/visitor/gradient_set_visitor.hpp index 13992cd89d..62a19c8ae0 100644 --- a/src/mlpack/methods/ann/visitor/gradient_set_visitor.hpp +++ b/src/mlpack/methods/ann/visitor/gradient_set_visitor.hpp @@ -27,7 +27,7 @@ class GradientSetVisitor : public boost::static_visitor { public: //! Update the gradient parameter given the gradient set. - GradientSetVisitor(arma::mat&& gradient, size_t offset = 0); + GradientSetVisitor(arma::mat& gradient, size_t offset = 0); //! Update the gradient parameter. template @@ -37,7 +37,7 @@ class GradientSetVisitor : public boost::static_visitor private: //! The gradient set. - arma::mat&& gradient; + arma::mat& gradient; //! The gradient offset. size_t offset; diff --git a/src/mlpack/methods/ann/visitor/gradient_set_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/gradient_set_visitor_impl.hpp index 863c68df14..677578182c 100644 --- a/src/mlpack/methods/ann/visitor/gradient_set_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/gradient_set_visitor_impl.hpp @@ -19,9 +19,9 @@ namespace mlpack { namespace ann { //! GradientSetVisitor visitor class. -inline GradientSetVisitor::GradientSetVisitor(arma::mat&& gradient, +inline GradientSetVisitor::GradientSetVisitor(arma::mat& gradient, size_t offset) : - gradient(std::move(gradient)), + gradient(gradient), offset(offset) { /* Nothing to do here. */ @@ -60,7 +60,7 @@ GradientSetVisitor::LayerGradients(T* layer, arma::mat& /* input */) const for (size_t i = 0; i < layer->Model().size(); ++i) { modelOffset += boost::apply_visitor(GradientSetVisitor( - std::move(gradient), modelOffset + offset), layer->Model()[i]); + gradient, modelOffset + offset), layer->Model()[i]); } return modelOffset; @@ -79,7 +79,7 @@ GradientSetVisitor::LayerGradients(T* layer, arma::mat& /* input */) const for (size_t i = 0; i < layer->Model().size(); ++i) { modelOffset += boost::apply_visitor(GradientSetVisitor( - std::move(gradient), modelOffset + offset), layer->Model()[i]); + gradient, modelOffset + offset), layer->Model()[i]); } return modelOffset; diff --git a/src/mlpack/methods/ann/visitor/gradient_update_visitor.hpp b/src/mlpack/methods/ann/visitor/gradient_update_visitor.hpp index 2913aaf977..6eec2a777b 100644 --- a/src/mlpack/methods/ann/visitor/gradient_update_visitor.hpp +++ b/src/mlpack/methods/ann/visitor/gradient_update_visitor.hpp @@ -27,7 +27,7 @@ class GradientUpdateVisitor : public boost::static_visitor { public: //! Update the gradient parameter given the gradient set. - GradientUpdateVisitor(arma::mat&& gradient, size_t offset = 0); + GradientUpdateVisitor(arma::mat& gradient, size_t offset = 0); //! Update the gradient parameter. template @@ -37,7 +37,7 @@ class GradientUpdateVisitor : public boost::static_visitor private: //! The gradient set. - arma::mat&& gradient; + arma::mat& gradient; //! The gradient offset. size_t offset; diff --git a/src/mlpack/methods/ann/visitor/gradient_update_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/gradient_update_visitor_impl.hpp index 6c77c9ae27..1c31b5a6f8 100644 --- a/src/mlpack/methods/ann/visitor/gradient_update_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/gradient_update_visitor_impl.hpp @@ -19,9 +19,9 @@ namespace mlpack { namespace ann { //! GradientUpdateVisitor visitor class. -inline GradientUpdateVisitor::GradientUpdateVisitor(arma::mat&& gradient, +inline GradientUpdateVisitor::GradientUpdateVisitor(arma::mat& gradient, size_t offset) : - gradient(std::move(gradient)), + gradient(gradient), offset(offset) { /* Nothing to do here. */ @@ -63,7 +63,7 @@ GradientUpdateVisitor::LayerGradients(T* layer, arma::mat& /* input */) const for (size_t i = 0; i < layer->Model().size(); ++i) { modelOffset += boost::apply_visitor(GradientUpdateVisitor( - std::move(gradient), modelOffset + offset), layer->Model()[i]); + gradient, modelOffset + offset), layer->Model()[i]); } return modelOffset; @@ -85,7 +85,7 @@ GradientUpdateVisitor::LayerGradients(T* layer, arma::mat& /* input */) const for (size_t i = 0; i < layer->Model().size(); ++i) { modelOffset += boost::apply_visitor(GradientUpdateVisitor( - std::move(gradient), modelOffset + offset), layer->Model()[i]); + gradient, modelOffset + offset), layer->Model()[i]); } return modelOffset; diff --git a/src/mlpack/methods/ann/visitor/gradient_visitor.hpp b/src/mlpack/methods/ann/visitor/gradient_visitor.hpp index e95099cb68..64ab4d7fd1 100644 --- a/src/mlpack/methods/ann/visitor/gradient_visitor.hpp +++ b/src/mlpack/methods/ann/visitor/gradient_visitor.hpp @@ -30,10 +30,12 @@ class GradientVisitor : public boost::static_visitor public: //! Executes the Gradient() method of the given module using the input and //! delta parameter. - GradientVisitor(arma::mat&& input, arma::mat&& delta); + GradientVisitor(const arma::mat& input, const arma::mat& delta); //! Executes the Gradient() method for the layer with the specified index. - GradientVisitor(arma::mat&& input, arma::mat&& delta, const size_t index); + GradientVisitor(const arma::mat& input, + const arma::mat& delta, + const size_t index); //! Executes the Gradient() method. template @@ -43,10 +45,10 @@ class GradientVisitor : public boost::static_visitor private: //! The input set. - arma::mat&& input; + const arma::mat& input; //! The delta parameter. - arma::mat&& delta; + const arma::mat& delta; //! Index of the layer to run. size_t index; diff --git a/src/mlpack/methods/ann/visitor/gradient_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/gradient_visitor_impl.hpp index 77852e80f1..02b40d6db9 100644 --- a/src/mlpack/methods/ann/visitor/gradient_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/gradient_visitor_impl.hpp @@ -19,19 +19,21 @@ namespace mlpack { namespace ann { //! GradientVisitor visitor class. -inline GradientVisitor::GradientVisitor(arma::mat&& input, arma::mat&& delta) : - input(std::move(input)), - delta(std::move(delta)), +inline GradientVisitor::GradientVisitor(const arma::mat& input, + const arma::mat& delta) : + input(input), + delta(delta), index(0), hasIndex(false) { /* Nothing to do here. */ } -inline GradientVisitor::GradientVisitor(arma::mat&& input, arma::mat&& delta, +inline GradientVisitor::GradientVisitor(const arma::mat& input, + const arma::mat& delta, const size_t index) : - input(std::move(input)), - delta(std::move(delta)), + input(input), + delta(delta), index(index), hasIndex(true) { @@ -55,8 +57,7 @@ inline typename std::enable_if< !HasRunCheck::value, void>::type GradientVisitor::LayerGradients(T* layer, arma::mat& /* input */) const { - layer->Gradient(std::move(input), std::move(delta), - std::move(layer->Gradient())); + layer->Gradient(input, delta, layer->Gradient()); } template @@ -67,13 +68,11 @@ GradientVisitor::LayerGradients(T* layer, arma::mat& /* input */) const { if (!hasIndex) { - layer->Gradient(std::move(input), std::move(delta), - std::move(layer->Gradient())); + layer->Gradient(input, delta, layer->Gradient()); } else { - layer->Gradient(std::move(input), std::move(delta), - std::move(layer->Gradient()), index); + layer->Gradient(input, delta, layer->Gradient(), index); } } diff --git a/src/mlpack/methods/ann/visitor/load_output_parameter_visitor.hpp b/src/mlpack/methods/ann/visitor/load_output_parameter_visitor.hpp index e8cd371fe2..2ba506f78c 100644 --- a/src/mlpack/methods/ann/visitor/load_output_parameter_visitor.hpp +++ b/src/mlpack/methods/ann/visitor/load_output_parameter_visitor.hpp @@ -29,7 +29,7 @@ class LoadOutputParameterVisitor : public boost::static_visitor { public: //! Restore the output parameter given a parameter set. - LoadOutputParameterVisitor(std::vector&& parameter); + LoadOutputParameterVisitor(std::vector& parameter); //! Restore the output parameter. template @@ -39,7 +39,7 @@ class LoadOutputParameterVisitor : public boost::static_visitor private: //! The parameter set. - std::vector&& parameter; + std::vector& parameter; //! Restore the output parameter for a module which doesn't implement the //! Model() function. diff --git a/src/mlpack/methods/ann/visitor/load_output_parameter_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/load_output_parameter_visitor_impl.hpp index 427fda1d45..a395ad12dc 100644 --- a/src/mlpack/methods/ann/visitor/load_output_parameter_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/load_output_parameter_visitor_impl.hpp @@ -20,7 +20,7 @@ namespace ann { //! LoadOutputParameterVisitor visitor class. inline LoadOutputParameterVisitor::LoadOutputParameterVisitor( - std::vector&& parameter) : parameter(std::move(parameter)) + std::vector& parameter) : parameter(parameter) { /* Nothing to do here. */ } @@ -52,7 +52,7 @@ LoadOutputParameterVisitor::OutputParameter(T* layer) const { for (size_t i = 0; i < layer->Model().size(); ++i) { - boost::apply_visitor(LoadOutputParameterVisitor(std::move(parameter)), + boost::apply_visitor(LoadOutputParameterVisitor(parameter), layer->Model()[layer->Model().size() - i - 1]); } diff --git a/src/mlpack/methods/ann/visitor/parameters_set_visitor.hpp b/src/mlpack/methods/ann/visitor/parameters_set_visitor.hpp index 87f0fac275..bedad4b9aa 100644 --- a/src/mlpack/methods/ann/visitor/parameters_set_visitor.hpp +++ b/src/mlpack/methods/ann/visitor/parameters_set_visitor.hpp @@ -28,7 +28,7 @@ class ParametersSetVisitor : public boost::static_visitor { public: //! Update the parameters set given the parameters matrix. - ParametersSetVisitor(arma::mat&& parameters); + ParametersSetVisitor(arma::mat& parameters); //! Update the parameters set. template @@ -38,7 +38,7 @@ class ParametersSetVisitor : public boost::static_visitor private: //! The parameters set. - arma::mat&& parameters; + arma::mat& parameters; //! Do not update the parameters set if the module doesn't implement the //! Parameters() function. diff --git a/src/mlpack/methods/ann/visitor/parameters_set_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/parameters_set_visitor_impl.hpp index d7fb7a09ea..a1bbe87dbd 100644 --- a/src/mlpack/methods/ann/visitor/parameters_set_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/parameters_set_visitor_impl.hpp @@ -19,8 +19,8 @@ namespace mlpack { namespace ann { //! ParametersSetVisitor visitor class. -inline ParametersSetVisitor::ParametersSetVisitor(arma::mat&& parameters) : - parameters(std::move(parameters)) +inline ParametersSetVisitor::ParametersSetVisitor(arma::mat& parameters) : + parameters(parameters) { /* Nothing to do here. */ } diff --git a/src/mlpack/methods/ann/visitor/parameters_visitor.hpp b/src/mlpack/methods/ann/visitor/parameters_visitor.hpp index 58c207ebe5..2a63586dc8 100644 --- a/src/mlpack/methods/ann/visitor/parameters_visitor.hpp +++ b/src/mlpack/methods/ann/visitor/parameters_visitor.hpp @@ -29,7 +29,7 @@ class ParametersVisitor : public boost::static_visitor { public: //! Store the parameters set into the given parameters matrix. - ParametersVisitor(arma::mat&& parameters); + ParametersVisitor(arma::mat& parameters); //! Set the parameters set. template @@ -39,7 +39,7 @@ class ParametersVisitor : public boost::static_visitor private: //! The parameters set. - arma::mat&& parameters; + arma::mat& parameters; //! Do not set the parameters set if the module doesn't implement the //! Parameters() function. diff --git a/src/mlpack/methods/ann/visitor/parameters_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/parameters_visitor_impl.hpp index 0b9a680337..957dcfc7a9 100644 --- a/src/mlpack/methods/ann/visitor/parameters_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/parameters_visitor_impl.hpp @@ -19,8 +19,8 @@ namespace mlpack { namespace ann { //! ParametersVisitor visitor class. -inline ParametersVisitor::ParametersVisitor(arma::mat&& parameters) : - parameters(std::move(parameters)) +inline ParametersVisitor::ParametersVisitor(arma::mat& parameters) : + parameters(parameters) { /* Nothing to do here. */ } diff --git a/src/mlpack/methods/ann/visitor/save_output_parameter_visitor.hpp b/src/mlpack/methods/ann/visitor/save_output_parameter_visitor.hpp index ed285e560e..dbfd908b08 100644 --- a/src/mlpack/methods/ann/visitor/save_output_parameter_visitor.hpp +++ b/src/mlpack/methods/ann/visitor/save_output_parameter_visitor.hpp @@ -28,7 +28,7 @@ class SaveOutputParameterVisitor : public boost::static_visitor { public: //! Save the output parameter into the given parameter set. - SaveOutputParameterVisitor(std::vector&& parameter); + SaveOutputParameterVisitor(std::vector& parameter); //! Save the output parameter. template @@ -38,7 +38,7 @@ class SaveOutputParameterVisitor : public boost::static_visitor private: //! The parameter set. - std::vector&& parameter; + std::vector& parameter; //! Save the output parameter for a module which doesn't implement the //! Model() function. diff --git a/src/mlpack/methods/ann/visitor/save_output_parameter_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/save_output_parameter_visitor_impl.hpp index 725dcc49b8..a73719abd6 100644 --- a/src/mlpack/methods/ann/visitor/save_output_parameter_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/save_output_parameter_visitor_impl.hpp @@ -20,7 +20,7 @@ namespace ann { //! SaveOutputParameterVisitor visitor class. inline SaveOutputParameterVisitor::SaveOutputParameterVisitor( - std::vector&& parameter) : parameter(std::move(parameter)) + std::vector& parameter) : parameter(parameter) { /* Nothing to do here. */ } @@ -53,7 +53,7 @@ SaveOutputParameterVisitor::OutputParameter(T* layer) const for (size_t i = 0; i < layer->Model().size(); ++i) { - boost::apply_visitor(SaveOutputParameterVisitor(std::move(parameter)), + boost::apply_visitor(SaveOutputParameterVisitor(parameter), layer->Model()[i]); } } diff --git a/src/mlpack/methods/ann/visitor/weight_set_visitor.hpp b/src/mlpack/methods/ann/visitor/weight_set_visitor.hpp index 62d93c3290..4bbad8cc4c 100644 --- a/src/mlpack/methods/ann/visitor/weight_set_visitor.hpp +++ b/src/mlpack/methods/ann/visitor/weight_set_visitor.hpp @@ -27,7 +27,7 @@ class WeightSetVisitor : public boost::static_visitor { public: //! Update the parameters given the parameters set and offset. - WeightSetVisitor(arma::mat&& weight, const size_t offset = 0); + WeightSetVisitor(arma::mat& weight, const size_t offset = 0); //! Update the parameters set. template @@ -37,7 +37,7 @@ class WeightSetVisitor : public boost::static_visitor private: //! The parameters set. - arma::mat&& weight; + arma::mat& weight; //! The parameters offset. const size_t offset; diff --git a/src/mlpack/methods/ann/visitor/weight_set_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/weight_set_visitor_impl.hpp index 9be9593647..14edbc54b3 100644 --- a/src/mlpack/methods/ann/visitor/weight_set_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/weight_set_visitor_impl.hpp @@ -19,9 +19,9 @@ namespace mlpack { namespace ann { //! WeightSetVisitor visitor class. -inline WeightSetVisitor::WeightSetVisitor(arma::mat&& weight, +inline WeightSetVisitor::WeightSetVisitor(arma::mat& weight, const size_t offset) : - weight(std::move(weight)), + weight(weight), offset(offset) { /* Nothing to do here. */ @@ -30,7 +30,7 @@ inline WeightSetVisitor::WeightSetVisitor(arma::mat&& weight, template inline size_t WeightSetVisitor::operator()(LayerType* layer) const { - return LayerSize(layer, std::move(layer->OutputParameter())); + return LayerSize(layer, layer->OutputParameter()); } inline size_t WeightSetVisitor::operator()(MoreTypes layer) const @@ -57,7 +57,7 @@ WeightSetVisitor::LayerSize(T* layer, P&& /*output */) const for (size_t i = 0; i < layer->Model().size(); ++i) { modelOffset += boost::apply_visitor(WeightSetVisitor( - std::move(weight), modelOffset + offset), layer->Model()[i]); + weight, modelOffset + offset), layer->Model()[i]); } return modelOffset; @@ -88,7 +88,7 @@ WeightSetVisitor::LayerSize(T* layer, P&& /* output */) const for (size_t i = 0; i < layer->Model().size(); ++i) { modelOffset += boost::apply_visitor(WeightSetVisitor( - std::move(weight), modelOffset + offset), layer->Model()[i]); + weight, modelOffset + offset), layer->Model()[i]); } return modelOffset; diff --git a/src/mlpack/methods/reinforcement_learning/q_learning_impl.hpp b/src/mlpack/methods/reinforcement_learning/q_learning_impl.hpp index d90b738fcf..38234ed673 100644 --- a/src/mlpack/methods/reinforcement_learning/q_learning_impl.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_learning_impl.hpp @@ -179,6 +179,7 @@ double QLearning< // Compute the update target. arma::mat target; learningNetwork.Forward(sampledStates, target); + /** * If the agent is at a terminal state, then we don't need to add the * discounted reward. At terminal state, the agent wont perform any @@ -199,7 +200,7 @@ double QLearning< // Learn from experience. arma::mat gradients; - learningNetwork.Backward(target, gradients); + learningNetwork.Backward(sampledStates, target, gradients); replayMethod.Update(target, sampledActions, nextActionValues, gradients); diff --git a/src/mlpack/methods/reinforcement_learning/worker/n_step_q_learning_worker.hpp b/src/mlpack/methods/reinforcement_learning/worker/n_step_q_learning_worker.hpp index 1ca53315bb..b532eab03b 100644 --- a/src/mlpack/methods/reinforcement_learning/worker/n_step_q_learning_worker.hpp +++ b/src/mlpack/methods/reinforcement_learning/worker/n_step_q_learning_worker.hpp @@ -301,12 +301,13 @@ class NStepQLearningWorker target = config.Discount() * target + std::get<2>(transition); // Compute the training target for current state. - network.Forward(std::get<0>(transition).Encode(), actionValue); + arma::mat input = std::get<0>(transition).Encode(); + network.Forward(input, actionValue); actionValue[std::get<1>(transition)] = target; // Compute gradient. arma::mat gradients; - network.Backward(actionValue, gradients); + network.Backward(input, actionValue, gradients); // Accumulate gradients. totalGradients += gradients; diff --git a/src/mlpack/methods/reinforcement_learning/worker/one_step_q_learning_worker.hpp b/src/mlpack/methods/reinforcement_learning/worker/one_step_q_learning_worker.hpp index 02a1c738f8..d7132bcc95 100644 --- a/src/mlpack/methods/reinforcement_learning/worker/one_step_q_learning_worker.hpp +++ b/src/mlpack/methods/reinforcement_learning/worker/one_step_q_learning_worker.hpp @@ -301,12 +301,13 @@ class OneStepQLearningWorker config.Discount() * targetActionValue; // Compute the training target for current state. - network.Forward(std::get<0>(transition).Encode(), actionValue); + arma::mat input = std::get<0>(transition).Encode(); + network.Forward(input, actionValue); actionValue[std::get<1>(transition)] = targetActionValue; // Compute gradient. arma::mat gradients; - network.Backward(actionValue, gradients); + network.Backward(input, actionValue, gradients); // Accumulate gradients. totalGradients += gradients; diff --git a/src/mlpack/methods/reinforcement_learning/worker/one_step_sarsa_worker.hpp b/src/mlpack/methods/reinforcement_learning/worker/one_step_sarsa_worker.hpp index ef974f0525..6dca26c194 100644 --- a/src/mlpack/methods/reinforcement_learning/worker/one_step_sarsa_worker.hpp +++ b/src/mlpack/methods/reinforcement_learning/worker/one_step_sarsa_worker.hpp @@ -314,12 +314,13 @@ class OneStepSarsaWorker config.Discount() * targetActionValue; // Compute the training target for current state. - network.Forward(std::get<0>(transition).Encode(), actionValue); + arma::mat input = std::get<0>(transition).Encode(); + network.Forward(input, actionValue); actionValue[std::get<1>(transition)] = targetActionValue; // Compute gradient. arma::mat gradients; - network.Backward(actionValue, gradients); + network.Backward(input, actionValue, gradients); // Accumulate gradients. totalGradients += gradients; diff --git a/src/mlpack/tests/activation_functions_test.cpp b/src/mlpack/tests/activation_functions_test.cpp index 43a00b85f3..469b411259 100644 --- a/src/mlpack/tests/activation_functions_test.cpp +++ b/src/mlpack/tests/activation_functions_test.cpp @@ -133,7 +133,7 @@ void CheckHardTanHActivationCorrect(const arma::colvec input, // Test the activation function using the entire vector as input. arma::colvec activations; - htf.Forward(std::move(input), std::move(activations)); + htf.Forward(input, activations); for (size_t i = 0; i < activations.n_elem; i++) { BOOST_REQUIRE_CLOSE(activations.at(i), target.at(i), 1e-3); @@ -157,7 +157,7 @@ void CheckHardTanHDerivativeCorrect(const arma::colvec input, // This error vector will be set to 1 to get the derivatives. arma::colvec error = arma::ones(input.n_elem); - htf.Backward(std::move(input), std::move(error), std::move(derivatives)); + htf.Backward(input, error, derivatives); for (size_t i = 0; i < derivatives.n_elem; i++) { @@ -179,7 +179,7 @@ void CheckLeakyReLUActivationCorrect(const arma::colvec input, // Test the activation function using the entire vector as input. arma::colvec activations; - lrf.Forward(std::move(input), std::move(activations)); + lrf.Forward(input, activations); for (size_t i = 0; i < activations.n_elem; i++) { BOOST_REQUIRE_CLOSE(activations.at(i), target.at(i), 1e-3); @@ -204,7 +204,7 @@ void CheckLeakyReLUDerivativeCorrect(const arma::colvec input, // This error vector will be set to 1 to get the derivatives. arma::colvec error = arma::ones(input.n_elem); - lrf.Backward(std::move(input), std::move(error), std::move(derivatives)); + lrf.Backward(input, error, derivatives); for (size_t i = 0; i < derivatives.n_elem; i++) { BOOST_REQUIRE_CLOSE(derivatives.at(i), target.at(i), 1e-3); @@ -226,7 +226,7 @@ void CheckELUActivationCorrect(const arma::colvec input, // Test the activation function using the entire vector as input. arma::colvec activations; - lrf.Forward(std::move(input), std::move(activations)); + lrf.Forward(input, activations); for (size_t i = 0; i < activations.n_elem; i++) { BOOST_REQUIRE_CLOSE(activations.at(i), target.at(i), 1e-3); @@ -251,9 +251,8 @@ void CheckELUDerivativeCorrect(const arma::colvec input, // This error vector will be set to 1 to get the derivatives. arma::colvec error = arma::ones(input.n_elem); - lrf.Forward(std::move(input), std::move(activations)); - lrf.Backward(std::move(activations), std::move(error), - std::move(derivatives)); + lrf.Forward(input, activations); + lrf.Backward(activations, error, derivatives); for (size_t i = 0; i < derivatives.n_elem; i++) { BOOST_REQUIRE_CLOSE(derivatives.at(i), target.at(i), 1e-3); @@ -275,7 +274,7 @@ void CheckPReLUActivationCorrect(const arma::colvec input, // Test the activation function using the entire vector as input. arma::colvec activations; - prelu.Forward(std::move(input), std::move(activations)); + prelu.Forward(input, activations); for (size_t i = 0; i < activations.n_elem; i++) { BOOST_REQUIRE_CLOSE(activations.at(i), target.at(i), 1e-3); @@ -301,7 +300,7 @@ void CheckPReLUDerivativeCorrect(const arma::colvec input, // This error vector will be set to 1 to get the derivatives. arma::colvec error = arma::ones(input.n_elem); - prelu.Backward(std::move(input), std::move(error), std::move(derivatives)); + prelu.Backward(input, error, derivatives); for (size_t i = 0; i < derivatives.n_elem; i++) { BOOST_REQUIRE_CLOSE(derivatives.at(i), target.at(i), 1e-3); @@ -327,7 +326,7 @@ void CheckPReLUGradientCorrect(const arma::colvec input, // This error vector will be set to 1 to get the gradient. arma::colvec error = arma::ones(input.n_elem); - prelu.Gradient(std::move(input), std::move(error), std::move(gradient)); + prelu.Gradient(input, error, gradient); BOOST_REQUIRE_EQUAL(gradient.n_rows, 1); BOOST_REQUIRE_EQUAL(gradient.n_cols, 1); BOOST_REQUIRE_CLOSE(gradient(0), target(0), 1e-3); @@ -345,7 +344,7 @@ BOOST_AUTO_TEST_CASE(SELUFunctionNormalizedTest) SELU selu; - selu.Forward(std::move(input), output); + selu.Forward(input, output); BOOST_REQUIRE_LE(arma::as_scalar(arma::abs(arma::mean(input) - arma::mean(output))), 0.1); @@ -367,7 +366,7 @@ BOOST_AUTO_TEST_CASE(SELUFunctionUnnormalizedTest) SELU selu; - selu.Forward(std::move(input), output); + selu.Forward(input, output); BOOST_REQUIRE_GE(arma::as_scalar(arma::abs(arma::mean(input) - arma::mean(output))), 0.1); @@ -391,18 +390,16 @@ BOOST_AUTO_TEST_CASE(SELUFunctionDerivativeTest) SELU selu; - selu.Forward(std::move(input), activations); - selu.Backward(std::move(activations), std::move(error), - std::move(derivatives)); + selu.Forward(input, activations); + selu.Backward(activations, error, derivatives); BOOST_REQUIRE_LE(arma::as_scalar(arma::abs(arma::mean(derivatives) - selu.Lambda())), 10e-4); input.fill(-1); - selu.Forward(std::move(input), activations); - selu.Backward(std::move(activations), std::move(error), - std::move(derivatives)); + selu.Forward(input, activations); + selu.Backward(activations, error, derivatives); BOOST_REQUIRE_LE(arma::as_scalar(arma::abs(arma::mean(derivatives) - selu.Lambda() * selu.Alpha() - arma::mean(activations))), 10e-4); @@ -582,12 +579,11 @@ BOOST_AUTO_TEST_CASE(CReLUFunctionTest) CReLU<> crelu; // Test the activation function using the entire vector as input. arma::colvec activations; - crelu.Forward(std::move(activationData), std::move(activations)); + crelu.Forward(activationData, activations); arma::colvec derivatives; // This error vector will be set to 1 to get the derivatives. arma::colvec error = arma::ones(desiredActivations.n_elem); - crelu.Backward(std::move(desiredActivations), std::move(error), - std::move(derivatives)); + crelu.Backward(desiredActivations, error, derivatives); for (size_t i = 0; i < activations.n_elem; i++) { BOOST_REQUIRE_CLOSE(activations.at(i), desiredActivations.at(i), 1e-3); diff --git a/src/mlpack/tests/ann_dist_test.cpp b/src/mlpack/tests/ann_dist_test.cpp index cf9baca90d..6c6dad1371 100644 --- a/src/mlpack/tests/ann_dist_test.cpp +++ b/src/mlpack/tests/ann_dist_test.cpp @@ -30,7 +30,7 @@ BOOST_AUTO_TEST_SUITE(ANNDistTest); BOOST_AUTO_TEST_CASE(SimpleBernoulliDistributionTest) { arma::mat param = arma::mat("1 1 0"); - BernoulliDistribution<> module(std::move(param), false); + BernoulliDistribution<> module(param, false); arma::mat sample = module.Sample(); // As the probabilities are [1, 1, 0], the bernoulli samples should be @@ -53,7 +53,7 @@ BOOST_AUTO_TEST_CASE(JacobianBernoulliDistributionTest) arma::mat target; target.randn(targetElements, 1); - BernoulliDistribution<> module(std::move(param), false); + BernoulliDistribution<> module(param, false); const double perturbation = 1e-6; double outputA, outputB, original; @@ -66,16 +66,16 @@ BOOST_AUTO_TEST_CASE(JacobianBernoulliDistributionTest) { original = module.Probability()(j); module.Probability()(j) = original - perturbation; - outputA = module.LogProbability(std::move(target)); + outputA = module.LogProbability(target); module.Probability()(j) = original + perturbation; - outputB = module.LogProbability(std::move(target)); + outputB = module.LogProbability(target); module.Probability()(j) = original; outputB -= outputA; outputB /= 2 * perturbation; jacobianA(j) = outputB; } - module.LogProbBackward(std::move(target), std::move(jacobianB)); + module.LogProbBackward(target, jacobianB); BOOST_REQUIRE_LE(arma::max(arma::max(arma::abs(jacobianA - jacobianB))), 1e-5); } @@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(JacobianBernoulliDistributionLogisticTest) arma::mat target; target.randn(targetElements, 1); - BernoulliDistribution<> module(std::move(param)); + BernoulliDistribution<> module(param); const double perturbation = 1e-6; double outputA, outputB, original; @@ -110,10 +110,10 @@ BOOST_AUTO_TEST_CASE(JacobianBernoulliDistributionLogisticTest) original = module.Logits()(j); module.Logits()(j) = original - perturbation; LogisticFunction::Fn(module.Logits(), module.Probability()); - outputA = module.LogProbability(std::move(target)); + outputA = module.LogProbability(target); module.Logits()(j) = original + perturbation; LogisticFunction::Fn(module.Logits(), module.Probability()); - outputB = module.LogProbability(std::move(target)); + outputB = module.LogProbability(target); module.Logits()(j) = original; LogisticFunction::Fn(module.Logits(), module.Probability()); outputB -= outputA; @@ -121,7 +121,7 @@ BOOST_AUTO_TEST_CASE(JacobianBernoulliDistributionLogisticTest) jacobianA(j) = outputB; } - module.LogProbBackward(std::move(target), std::move(jacobianB)); + module.LogProbBackward(target, jacobianB); BOOST_REQUIRE_LE(arma::max(arma::max(arma::abs(jacobianA - jacobianB))), 3e-5); } diff --git a/src/mlpack/tests/ann_layer_test.cpp b/src/mlpack/tests/ann_layer_test.cpp index 136460c2f1..0f7a4bbc91 100644 --- a/src/mlpack/tests/ann_layer_test.cpp +++ b/src/mlpack/tests/ann_layer_test.cpp @@ -41,21 +41,21 @@ BOOST_AUTO_TEST_CASE(SimpleAddLayerTest) // Test the Forward function. input = arma::zeros(10, 1); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(module.Parameters()), arma::accu(output)); // Test the Backward function. - module.Backward(std::move(input), std::move(output), std::move(delta)); + module.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(output), arma::accu(delta)); // Test the forward function. input = arma::ones(10, 1); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_CLOSE(10 + arma::accu(module.Parameters()), arma::accu(output), 1e-3); // Test the backward function. - module.Backward(std::move(input), std::move(output), std::move(delta)); + module.Backward(input, output, delta); BOOST_REQUIRE_CLOSE(arma::accu(output), arma::accu(delta), 1e-3); } @@ -131,20 +131,20 @@ BOOST_AUTO_TEST_CASE(SimpleConstantLayerTest) // Test the Forward function. input = arma::zeros(10, 1); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), 30.0); // Test the Backward function. - module.Backward(std::move(input), std::move(output), std::move(delta)); + module.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 0); // Test the forward function. input = arma::ones(10, 1); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), 30.0); // Test the backward function. - module.Backward(std::move(input), std::move(output), std::move(delta)); + module.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 0); } @@ -183,19 +183,19 @@ BOOST_AUTO_TEST_CASE(SimpleDropoutLayerTest) // Test the Forward function. arma::mat output; - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_LE( arma::as_scalar(arma::abs(arma::mean(output) - (1 - p))), 0.05); // Test the Backward function. arma::mat delta; - module.Backward(std::move(input), std::move(input), std::move(delta)); + module.Backward(input, input, delta); BOOST_REQUIRE_LE( arma::as_scalar(arma::abs(arma::mean(delta) - (1 - p))), 0.05); // Test the Forward function. module.Deterministic() = true; - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(input), arma::accu(output)); } @@ -219,7 +219,7 @@ BOOST_AUTO_TEST_CASE(DropoutProbabilityTest) module.Deterministic() = false; arma::mat output; - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); // Return a column vector containing the indices of elements of X that // are non-zero, we just need the number of non-zero values. @@ -244,7 +244,7 @@ BOOST_AUTO_TEST_CASE(NoDropoutTest) module.Deterministic() = false; arma::mat output; - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), arma::accu(input)); } @@ -267,7 +267,7 @@ BOOST_AUTO_TEST_CASE(SimpleAlphaDropoutLayerTest) // Test the Forward function when training phase. arma::mat output; - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); // Check whether mean remains nearly same. BOOST_REQUIRE_LE( arma::as_scalar(arma::abs(arma::mean(input) - arma::mean(output))), 0.1); @@ -278,13 +278,13 @@ BOOST_AUTO_TEST_CASE(SimpleAlphaDropoutLayerTest) // Test the Backward function when training phase. arma::mat delta; - module.Backward(std::move(input), std::move(input), std::move(delta)); + module.Backward(input, input, delta); BOOST_REQUIRE_LE( arma::as_scalar(arma::abs(arma::mean(delta) - 0)), 0.05); // Test the Forward function when testing phase. module.Deterministic() = true; - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(input), arma::accu(output)); } @@ -308,7 +308,7 @@ BOOST_AUTO_TEST_CASE(AlphaDropoutProbabilityTest) module.Deterministic() = false; arma::mat output; - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); // Return a column vector containing the indices of elements of X // that are not alphaDash, we just need the number of @@ -336,7 +336,7 @@ BOOST_AUTO_TEST_CASE(NoAlphaDropoutTest) module.Deterministic() = false; arma::mat output; - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), arma::accu(input)); } @@ -353,13 +353,13 @@ BOOST_AUTO_TEST_CASE(SimpleLinearLayerTest) // Test the Forward function. input = arma::zeros(10, 1); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_CLOSE(arma::accu( module.Parameters().submat(100, 0, module.Parameters().n_elem - 1, 0)), arma::accu(output), 1e-3); // Test the Backward function. - module.Backward(std::move(input), std::move(input), std::move(delta)); + module.Backward(input, input, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 0); } @@ -439,11 +439,11 @@ BOOST_AUTO_TEST_CASE(SimpleLinearNoBiasLayerTest) // Test the Forward function. input = arma::zeros(10, 1); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(0, arma::accu(output)); // Test the Backward function. - module.Backward(std::move(input), std::move(input), std::move(delta)); + module.Backward(input, input, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 0); } @@ -457,13 +457,13 @@ BOOST_AUTO_TEST_CASE(SimplePaddingLayerTest) // Test the Forward function. input = arma::randu(10, 1); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(input), arma::accu(output)); BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows + 3); BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols + 7); // Test the Backward function. - module.Backward(std::move(input), std::move(output), std::move(delta)); + module.Backward(input, output, delta); CheckMatrices(delta, input); } @@ -688,20 +688,20 @@ BOOST_AUTO_TEST_CASE(SimpleSelectLayerTest) // Test the Forward function. Select<> moduleA(3); - moduleA.Forward(std::move(input), std::move(outputA)); + moduleA.Forward(input, outputA); BOOST_REQUIRE_EQUAL(30, arma::accu(outputA)); // Test the Forward function. Select<> moduleB(3, 5); - moduleB.Forward(std::move(input), std::move(outputB)); + moduleB.Forward(input, outputB); BOOST_REQUIRE_EQUAL(15, arma::accu(outputB)); // Test the Backward function. - moduleA.Backward(std::move(input), std::move(outputA), std::move(delta)); + moduleA.Backward(input, outputA, delta); BOOST_REQUIRE_EQUAL(30, arma::accu(delta)); // Test the Backward function. - moduleB.Backward(std::move(input), std::move(outputA), std::move(delta)); + moduleB.Backward(input, outputA, delta); BOOST_REQUIRE_EQUAL(15, arma::accu(delta)); } @@ -715,14 +715,14 @@ BOOST_AUTO_TEST_CASE(SimpleJoinLayerTest) // Test the Forward function. Join<> module; - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(50, arma::accu(output)); bool b = output.n_rows == 1 || output.n_cols == 1; BOOST_REQUIRE_EQUAL(b, true); // Test the Backward function. - module.Backward(std::move(input), std::move(output), std::move(delta)); + module.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(50, arma::accu(delta)); b = delta.n_rows == input.n_rows && input.n_cols; @@ -744,18 +744,17 @@ BOOST_AUTO_TEST_CASE(SimpleAddMergeLayerTest) for (size_t m = 0; m < numMergeModules; ++m) { IdentityLayer<> identityLayer; - identityLayer.Forward(std::move(input), - std::move(identityLayer.OutputParameter())); + identityLayer.Forward(input, identityLayer.OutputParameter()); module.Add >(identityLayer); } // Test the Forward function. - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(10 * numMergeModules, arma::accu(output)); // Test the Backward function. - module.Backward(std::move(input), std::move(output), std::move(delta)); + module.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(output), arma::accu(delta)); } } @@ -961,9 +960,9 @@ BOOST_AUTO_TEST_CASE(ReadCellStateParamLSTMLayerTest) input.n_rows, input.n_cols, false, true); // Apply Forward() on LSTM layer. - lstm.Forward(std::move(stepData), // Input. - std::move(outLstm), // Output. - std::move(cellLstm), // Cell state. + lstm.Forward(stepData, // Input. + outLstm, // Output. + cellLstm, // Cell state. false); // Don't write into the cell state. // Compute the value of cell state and output. @@ -1043,9 +1042,9 @@ BOOST_AUTO_TEST_CASE(WriteCellStateParamLSTMLayerTest) } // Apply Forward() on the LSTM layer. - lstm.Forward(std::move(stepData), // Input. - std::move(outLstm), // Output. - std::move(cellLstm), // Cell state. + lstm.Forward(stepData, // Input. + outLstm, // Output. + cellLstm, // Cell state. true); // Write into cell state. // Compute the value of cell state and output. @@ -1081,18 +1080,18 @@ BOOST_AUTO_TEST_CASE(WriteCellStateParamLSTMLayerTest) arma::mat stepData(input.slice(0).memptr(), input.n_rows, input.n_cols, false, true); - lstm.Forward(std::move(stepData), // Input. - std::move(outLstm), // Output. - std::move(cellLstm), // Cell state. - true); // Write into cell state. + lstm.Forward(stepData, // Input. + outLstm, // Output. + cellLstm, // Cell state. + true); // Write into cell state. for (size_t seqNum = 1; seqNum < rho; ++seqNum) { arma::mat empty; // Should throw error. - BOOST_REQUIRE_THROW(lstm.Forward(std::move(stepData), // Input. - std::move(outLstm), // Output. - std::move(empty), // Cell state. + BOOST_REQUIRE_THROW(lstm.Forward(stepData, // Input. + outLstm, // Output. + empty, // Cell state. true), // Write into cell state. std::runtime_error); } @@ -1160,7 +1159,7 @@ BOOST_AUTO_TEST_CASE(ForwardGRULayerTest) arma::mat input = arma::ones(3, 1); arma::mat output; - gru.Forward(std::move(input), std::move(output)); + gru.Forward(input, output); // Compute the z_t gate output. arma::mat expectedOutput = arma::ones(3, 1); @@ -1175,7 +1174,7 @@ BOOST_AUTO_TEST_CASE(ForwardGRULayerTest) expectedOutput = output; - gru.Forward(std::move(input), std::move(output)); + gru.Forward(input, output); double s = arma::as_scalar(arma::sum(expectedOutput)); @@ -1218,7 +1217,7 @@ BOOST_AUTO_TEST_CASE(SimpleConcatLayerTest) // Test the Forward function. input = arma::zeros(10, 1); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_CLOSE(arma::accu( moduleA.Parameters().submat(100, 0, moduleA.Parameters().n_elem - 1, 0)) + arma::accu(moduleB.Parameters().submat(100, 0, @@ -1227,7 +1226,7 @@ BOOST_AUTO_TEST_CASE(SimpleConcatLayerTest) // Test the Backward function. error = arma::zeros(20, 1); - module.Backward(std::move(input), std::move(error), std::move(delta)); + module.Backward(input, error, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 0); } @@ -1260,8 +1259,8 @@ BOOST_AUTO_TEST_CASE(ConcatAlongAxisTest) moduleB.Parameters().randu(); // Compute output of each layer. - moduleA.Forward(std::move(input), std::move(outputA)); - moduleB.Forward(std::move(input), std::move(outputB)); + moduleA.Forward(input, outputA); + moduleB.Forward(input, outputB); arma::cube A(outputA.memptr(), outputWidth, outputHeight, outputChannel); arma::cube B(outputB.memptr(), outputWidth, outputHeight, outputChannel); @@ -1305,7 +1304,7 @@ BOOST_AUTO_TEST_CASE(ConcatAlongAxisTest) Concat<> module(inputSize, axis); module.Add(moduleA); module.Add(moduleB); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); arma::cube concatOut(output.memptr(), x * outputWidth, y * outputHeight, z * outputChannel); @@ -1374,12 +1373,12 @@ BOOST_AUTO_TEST_CASE(SimpleConcatenateLayerTest) module.Concat() = arma::ones(5, 1) * 0.5; // Test the Forward function. - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), 7.5); // Test the Backward function. - module.Backward(std::move(input), std::move(output), std::move(delta)); + module.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 5); } @@ -1447,7 +1446,7 @@ BOOST_AUTO_TEST_CASE(SimpleLookupLayerTest) input(0) = 1; input(1) = 3; - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); // The Lookup module uses index - 1 for the cols. const double outputSum = arma::accu(module.Parameters().col(0)) + @@ -1456,7 +1455,7 @@ BOOST_AUTO_TEST_CASE(SimpleLookupLayerTest) BOOST_REQUIRE_CLOSE(outputSum, arma::accu(output), 1e-3); // Test the Backward function. - module.Backward(std::move(input), std::move(input), std::move(delta)); + module.Backward(input, input, delta); BOOST_REQUIRE_EQUAL(arma::accu(input), arma::accu(input)); // Test the Gradient function. @@ -1464,7 +1463,7 @@ BOOST_AUTO_TEST_CASE(SimpleLookupLayerTest) error = error.t(); error.col(1) *= 0.5; - module.Gradient(std::move(input), std::move(error), std::move(gradient)); + module.Gradient(input, error, gradient); // The Lookup module uses index - 1 for the cols. const double gradientSum = arma::accu(gradient.col(0)) + @@ -1484,7 +1483,7 @@ BOOST_AUTO_TEST_CASE(SimpleLogSoftmaxLayerTest) // Test the Forward function. input = arma::mat("0.5; 0.5"); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_SMALL(arma::accu(arma::abs( arma::mat("-0.6931; -0.6931") - output)), 1e-3); @@ -1492,7 +1491,7 @@ BOOST_AUTO_TEST_CASE(SimpleLogSoftmaxLayerTest) error = arma::zeros(input.n_rows, input.n_cols); // Assume LogSoftmax layer is always associated with NLL output layer. error(1, 0) = -1; - module.Backward(std::move(input), std::move(error), std::move(delta)); + module.Backward(input, error, delta); BOOST_REQUIRE_SMALL(arma::accu(arma::abs( arma::mat("1.6487; 0.6487") - delta)), 1e-3); } @@ -1521,13 +1520,12 @@ BOOST_AUTO_TEST_CASE(SimpleBilinearInterpolationLayerTest) 2.0000 2.4000 2.8000 3.0000 3.0000 \ 2.0000 2.4000 2.8000 3.0000 3.0000"); expectedOutput.reshape(25, 1); - layer.Forward(std::move(input), std::move(output)); + layer.Forward(input, output); CheckMatrices(output - expectedOutput, arma::zeros(output.n_rows), 1e-12); expectedOutput = arma::mat("1.0000 1.9000 1.9000 2.8000"); expectedOutput.reshape(4, 1); - layer.Backward(std::move(output), std::move(output), - std::move(unzoomedOutput)); + layer.Backward(output, output, unzoomedOutput); CheckMatrices(unzoomedOutput - expectedOutput, arma::zeros(input.n_rows), 1e-12); } @@ -1549,7 +1547,7 @@ BOOST_AUTO_TEST_CASE(BatchNormTest) // Non-Deteministic Forward Pass Test. model.Deterministic() = false; - model.Forward(std::move(input), std::move(output)); + model.Forward(input, output); arma::mat result; result << 1.1658 << 0.1100 << -1.2758 << arma::endr << 1.2579 << -0.0699 << -1.1880 << arma::endr @@ -1576,7 +1574,7 @@ BOOST_AUTO_TEST_CASE(BatchNormTest) result.clear(); model.Deterministic() = true; - model.Forward(std::move(input), std::move(output)); + model.Forward(input, output); result << 1.1658 << 0.1100 << -1.2757 << arma::endr << 1.2579 << -0.0699 << -1.1880 << arma::endr @@ -1733,12 +1731,12 @@ BOOST_AUTO_TEST_CASE(SimpleTransposedConvolutionLayerTest) module1.Parameters()(0) = 1.0; module1.Parameters()(8) = 2.0; module1.Reset(); - module1.Forward(std::move(input), std::move(output)); + module1.Forward(input, output); // Value calculated using tensorflow.nn.conv2d_transpose() BOOST_REQUIRE_EQUAL(arma::accu(output), 360.0); // Test the backward function. - module1.Backward(std::move(input), std::move(output), std::move(delta)); + module1.Backward(input, output, delta); // Value calculated using tensorflow.nn.conv2d() BOOST_REQUIRE_EQUAL(arma::accu(delta), 720.0); @@ -1753,12 +1751,12 @@ BOOST_AUTO_TEST_CASE(SimpleTransposedConvolutionLayerTest) module2.Parameters()(12) = 1.0; module2.Parameters()(15) = 2.0; module2.Reset(); - module2.Forward(std::move(input), std::move(output)); + module2.Forward(input, output); // Value calculated using torch.nn.functional.conv_transpose2d() BOOST_REQUIRE_EQUAL(arma::accu(output), 1512.0); // Test the backward function. - module2.Backward(std::move(input), std::move(output), std::move(delta)); + module2.Backward(input, output, delta); // Value calculated using torch.nn.functional.conv2d() BOOST_REQUIRE_EQUAL(arma::accu(delta), 6504.0); @@ -1771,12 +1769,12 @@ BOOST_AUTO_TEST_CASE(SimpleTransposedConvolutionLayerTest) module3.Parameters()(3) = 3.0; module3.Parameters()(8) = 1.0; module3.Reset(); - module3.Forward(std::move(input), std::move(output)); + module3.Forward(input, output); // Value calculated using torch.nn.functional.conv_transpose2d() BOOST_REQUIRE_EQUAL(arma::accu(output), 2370.0); // Test the backward function. - module3.Backward(std::move(input), std::move(output), std::move(delta)); + module3.Backward(input, output, delta); // Value calculated using torch.nn.functional.conv2d() BOOST_REQUIRE_EQUAL(arma::accu(delta), 19154.0); @@ -1789,12 +1787,12 @@ BOOST_AUTO_TEST_CASE(SimpleTransposedConvolutionLayerTest) module4.Parameters()(6) = 6.0; module4.Parameters()(8) = 8.0; module4.Reset(); - module4.Forward(std::move(input), std::move(output)); + module4.Forward(input, output); // Value calculated using torch.nn.functional.conv_transpose2d() BOOST_REQUIRE_EQUAL(arma::accu(output), 6000.0); // Test the backward function. - module4.Backward(std::move(input), std::move(output), std::move(delta)); + module4.Backward(input, output, delta); // Value calculated using torch.nn.functional.conv2d() BOOST_REQUIRE_EQUAL(arma::accu(delta), 86208.0); @@ -1807,12 +1805,12 @@ BOOST_AUTO_TEST_CASE(SimpleTransposedConvolutionLayerTest) module5.Parameters()(6) = 4.0; module5.Parameters()(8) = 2.0; module5.Reset(); - module5.Forward(std::move(input), std::move(output)); + module5.Forward(input, output); // Value calculated using torch.nn.functional.conv_transpose2d() BOOST_REQUIRE_EQUAL(arma::accu(output), 120.0); // Test the backward function. - module5.Backward(std::move(input), std::move(output), std::move(delta)); + module5.Backward(input, output, delta); // Value calculated using torch.nn.functional.conv2d() BOOST_REQUIRE_EQUAL(arma::accu(delta), 960.0); @@ -1825,12 +1823,12 @@ BOOST_AUTO_TEST_CASE(SimpleTransposedConvolutionLayerTest) module6.Parameters()(6) = 2.0; module6.Parameters()(8) = 4.0; module6.Reset(); - module6.Forward(std::move(input), std::move(output)); + module6.Forward(input, output); // Value calculated using torch.nn.functional.conv_transpose2d() BOOST_REQUIRE_EQUAL(arma::accu(output), 410.0); // Test the backward function. - module6.Backward(std::move(input), std::move(output), std::move(delta)); + module6.Backward(input, output, delta); // Value calculated using torch.nn.functional.conv2d() BOOST_REQUIRE_EQUAL(arma::accu(delta), 4444.0); @@ -1843,11 +1841,11 @@ BOOST_AUTO_TEST_CASE(SimpleTransposedConvolutionLayerTest) module7.Parameters()(4) = 2.0; module7.Parameters()(8) = 4.0; module7.Reset(); - module7.Forward(std::move(input), std::move(output)); + module7.Forward(input, output); // Value calculated using torch.nn.functional.conv_transpose2d() BOOST_REQUIRE_EQUAL(arma::accu(output), 606.0); - module7.Backward(std::move(input), std::move(output), std::move(delta)); + module7.Backward(input, output, delta); // Value calculated using torch.nn.functional.conv2d() BOOST_REQUIRE_EQUAL(arma::accu(delta), 7732.0); } @@ -1919,18 +1917,17 @@ BOOST_AUTO_TEST_CASE(SimpleMultiplyMergeLayerTest) for (size_t m = 0; m < numMergeModules; ++m) { IdentityLayer<> identityLayer; - identityLayer.Forward(std::move(input), - std::move(identityLayer.OutputParameter())); + identityLayer.Forward(input, identityLayer.OutputParameter()); module.Add >(identityLayer); } // Test the Forward function. - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_EQUAL(10, arma::accu(output)); // Test the Backward function. - module.Backward(std::move(input), std::move(output), std::move(delta)); + module.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(output), arma::accu(delta)); } } @@ -1949,12 +1946,12 @@ BOOST_AUTO_TEST_CASE(SimpleAtrousConvolutionLayerTest) module1.Parameters()(0) = 1.0; module1.Parameters()(8) = 2.0; module1.Reset(); - module1.Forward(std::move(input), std::move(output)); + module1.Forward(input, output); // Value calculated using tensorflow.nn.atrous_conv2d() BOOST_REQUIRE_EQUAL(arma::accu(output), 792.0); // Test the Backward function. - module1.Backward(std::move(input), std::move(output), std::move(delta)); + module1.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 2376); AtrousConvolution<> module2(1, 1, 3, 3, 2, 2, 0, 0, 7, 7, 2, 2); @@ -1965,12 +1962,12 @@ BOOST_AUTO_TEST_CASE(SimpleAtrousConvolutionLayerTest) module2.Parameters()(3) = 1.0; module2.Parameters()(6) = 1.0; module2.Reset(); - module2.Forward(std::move(input), std::move(output)); + module2.Forward(input, output); // Value calculated using tensorflow.nn.conv2d() BOOST_REQUIRE_EQUAL(arma::accu(output), 264.0); // Test the backward function. - module2.Backward(std::move(input), std::move(output), std::move(delta)); + module2.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 792.0); } @@ -2094,14 +2091,14 @@ BOOST_AUTO_TEST_CASE(AtrousConvolutionLayerPaddingTest) input = arma::linspace(0, 48, 49); module1.Parameters() = arma::mat(9 + 1, 1, arma::fill::zeros); module1.Reset(); - module1.Forward(std::move(input), std::move(output)); + module1.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), 0); BOOST_REQUIRE_EQUAL(output.n_rows, 9); BOOST_REQUIRE_EQUAL(output.n_cols, 1); // Test the Backward function. - module1.Backward(std::move(input), std::move(output), std::move(delta)); + module1.Backward(input, output, delta); // Check same padding option. AtrousConvolution<> module2(1, 1, 3, 3, 1, 1, @@ -2112,14 +2109,14 @@ BOOST_AUTO_TEST_CASE(AtrousConvolutionLayerPaddingTest) input = arma::linspace(0, 48, 49); module2.Parameters() = arma::mat(9 + 1, 1, arma::fill::zeros); module2.Reset(); - module2.Forward(std::move(input), std::move(output)); + module2.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), 0); BOOST_REQUIRE_EQUAL(output.n_rows, 49); BOOST_REQUIRE_EQUAL(output.n_cols, 1); // Test the backward function. - module2.Backward(std::move(input), std::move(output), std::move(delta)); + module2.Backward(input, output, delta); } /** @@ -2135,7 +2132,7 @@ BOOST_AUTO_TEST_CASE(LayerNormTest) LayerNorm<> model(input.n_rows); model.Reset(); - model.Forward(std::move(input), std::move(output)); + model.Forward(input, output); arma::mat result; result << 1.2247 << 1.2978 << arma::endr << 0 << -1.1355 << arma::endr @@ -2218,13 +2215,13 @@ BOOST_AUTO_TEST_CASE(AddMergeRunTest) linear->Reset(); input = arma::zeros(10, 1); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); double parameterSum = arma::accu(linear->Parameters().submat( 100, 0, linear->Parameters().n_elem - 1, 0)); // Test the Backward function. - module.Backward(std::move(input), std::move(input), std::move(delta)); + module.Backward(input, input, delta); // Clean up before we break, delete linear; @@ -2250,13 +2247,13 @@ BOOST_AUTO_TEST_CASE(MultiplyMergeRunTest) linear->Reset(); input = arma::zeros(10, 1); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); double parameterSum = arma::accu(linear->Parameters().submat( 100, 0, linear->Parameters().n_elem - 1, 0)); // Test the Backward function. - module.Backward(std::move(input), std::move(input), std::move(delta)); + module.Backward(input, input, delta); // Clean up before we break, delete linear; @@ -2275,19 +2272,19 @@ BOOST_AUTO_TEST_CASE(SimpleSubviewLayerTest) // Test the Forward function for a vector. input = arma::ones(20, 1); - moduleRow.Forward(std::move(input), std::move(output)); + moduleRow.Forward(input, output); BOOST_REQUIRE_EQUAL(output.n_rows, 10); Subview<> moduleMat(4, 3, 6, 0, 2); // Test the Forward function for a matrix. input = arma::ones(20, 8); - moduleMat.Forward(std::move(input), std::move(outputMat)); + moduleMat.Forward(input, outputMat); BOOST_REQUIRE_EQUAL(outputMat.n_rows, 12); BOOST_REQUIRE_EQUAL(outputMat.n_cols, 2); // Test the Backward function. - moduleMat.Backward(std::move(input), std::move(input), std::move(delta)); + moduleMat.Backward(input, input, delta); BOOST_REQUIRE_EQUAL(accu(delta), 160); BOOST_REQUIRE_EQUAL(delta.n_rows, 20); } @@ -2304,21 +2301,21 @@ BOOST_AUTO_TEST_CASE(SubviewIndexTest) Subview<> moduleStart(1, 0, 9); arma::mat subStart = arma::linspace(1, 10, 10); - moduleStart.Forward(std::move(input), std::move(outputStart)); + moduleStart.Forward(input, outputStart); CheckMatrices(outputStart, subStart); // Slicing from the mid indices. Subview<> moduleMid(1, 6, 15); arma::mat subMid = arma::linspace(7, 16, 10); - moduleMid.Forward(std::move(input), std::move(outputMid)); + moduleMid.Forward(input, outputMid); CheckMatrices(outputMid, subMid); // Slicing from the end indices. Subview<> moduleEnd(1, 10, 19); arma::mat subEnd = arma::linspace(11, 20, 10); - moduleEnd.Forward(std::move(input), std::move(outputEnd)); + moduleEnd.Forward(input, outputEnd); CheckMatrices(outputEnd, subEnd); } @@ -2334,14 +2331,14 @@ BOOST_AUTO_TEST_CASE(SubviewBatchTest) // Test with inSize 1. input = arma::ones(20, 8); - moduleCol.Forward(std::move(input), std::move(outputCol)); + moduleCol.Forward(input, outputCol); CheckMatrices(outputCol, input); // Few rows and columns selected. Subview<> moduleMat(4, 3, 6, 0, 2); // Test with inSize greater than 1. - moduleMat.Forward(std::move(input), std::move(outputMat)); + moduleMat.Forward(input, outputMat); output = arma::ones(12, 2); CheckMatrices(outputMat, output); @@ -2349,7 +2346,7 @@ BOOST_AUTO_TEST_CASE(SubviewBatchTest) Subview<> moduleDef(4, 1, 6, 0, 4); // Test with inSize greater than 1 and endCol >= inSize. - moduleDef.Forward(std::move(input), std::move(outputDef)); + moduleDef.Forward(input, outputDef); output = arma::ones(24, 2); CheckMatrices(outputDef, output); } @@ -2367,12 +2364,12 @@ BOOST_AUTO_TEST_CASE(SimpleReparametrizationLayerTest) // output should be small enough. input = join_cols(arma::ones(5, 1) * -15, arma::zeros(5, 1)); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); BOOST_REQUIRE_LE(arma::accu(output), 1e-5); // Test the Backward function. arma::mat gy = arma::zeros(5, 1); - module.Backward(std::move(input), std::move(gy), std::move(delta)); + module.Backward(input, gy, delta); BOOST_REQUIRE(arma::accu(delta) != 0); // klBackward will be added. } @@ -2388,8 +2385,8 @@ BOOST_AUTO_TEST_CASE(ReparametrizationLayerStochasticTest) arma::zeros(5, 1)); // Test if two forward passes generate same output. - module.Forward(std::move(input), std::move(outputA)); - module.Forward(std::move(input), std::move(outputB)); + module.Forward(input, outputA); + module.Forward(input, outputB); CheckMatrices(outputA, outputB); } @@ -2404,14 +2401,14 @@ BOOST_AUTO_TEST_CASE(ReparametrizationLayerIncludeKlTest) input = join_cols(arma::ones(5, 1), arma::zeros(5, 1)); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); // As KL divergence is not included, with the above inputs, the delta // matrix should be all zeros. gy = arma::zeros(output.n_rows, output.n_cols); - module.Backward(std::move(output), std::move(gy), std::move(delta)); + module.Backward(output, gy, delta); - BOOST_REQUIRE_EQUAL(arma::accu(std::move(delta)), 0); + BOOST_REQUIRE_EQUAL(arma::accu(delta), 0); } /** @@ -2549,14 +2546,14 @@ BOOST_AUTO_TEST_CASE(SimpleResidualLayerTest) // Test the Forward function (pass the same input to both). input = arma::randu(10, 1); - sequential->Forward(std::move(input), std::move(outputA)); - residual->Forward(std::move(input), std::move(outputB)); + sequential->Forward(input, outputA); + residual->Forward(input, outputB); CheckMatrices(outputA, outputB - input); // Test the Backward function (pass the same error to both). - sequential->Backward(std::move(input), std::move(input), std::move(deltaA)); - residual->Backward(std::move(input), std::move(input), std::move(deltaB)); + sequential->Backward(input, input, deltaA); + residual->Backward(input, input, deltaB); CheckMatrices(deltaA, deltaB - input); @@ -2593,8 +2590,8 @@ BOOST_AUTO_TEST_CASE(SimpleHighwayLayerTest) // Test the Forward function (pass the same input to both). input = arma::randu(10, 1); - sequential->Forward(std::move(input), std::move(outputA)); - highway->Forward(std::move(input), std::move(outputB)); + sequential->Forward(input, outputA); + highway->Forward(input, outputB); CheckMatrices(outputB, input * 0.5 + outputA * 0.5); @@ -2774,10 +2771,10 @@ BOOST_AUTO_TEST_CASE(WeightNormRunTest) linear->Bias().zeros(); input = arma::zeros(10, 1); - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); // Test the Backward function. - module.Backward(std::move(input), std::move(input), std::move(delta)); + module.Backward(input, input, delta); BOOST_REQUIRE_EQUAL(0, arma::accu(output)); BOOST_REQUIRE_EQUAL(arma::accu(delta), 0); @@ -2904,14 +2901,14 @@ BOOST_AUTO_TEST_CASE(ConvolutionLayerPaddingTest) input = arma::linspace(0, 48, 49); module1.Parameters() = arma::mat(9 + 1, 1, arma::fill::zeros); module1.Reset(); - module1.Forward(std::move(input), std::move(output)); + module1.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), 0); BOOST_REQUIRE_EQUAL(output.n_rows, 25); BOOST_REQUIRE_EQUAL(output.n_cols, 1); // Test the Backward function. - module1.Backward(std::move(input), std::move(output), std::move(delta)); + module1.Backward(input, output, delta); // Check same padding option. Convolution<> module2(1, 1, 3, 3, 1, 1, std::tuple(0, 0), @@ -2921,14 +2918,14 @@ BOOST_AUTO_TEST_CASE(ConvolutionLayerPaddingTest) input = arma::linspace(0, 48, 49); module2.Parameters() = arma::mat(9 + 1, 1, arma::fill::zeros); module2.Reset(); - module2.Forward(std::move(input), std::move(output)); + module2.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), 0); BOOST_REQUIRE_EQUAL(output.n_rows, 49); BOOST_REQUIRE_EQUAL(output.n_cols, 1); // Test the backward function. - module2.Backward(std::move(input), std::move(output), std::move(delta)); + module2.Backward(input, output, delta); } /** @@ -2944,12 +2941,12 @@ BOOST_AUTO_TEST_CASE(TransposedConvolutionLayerPaddingTest) input = arma::linspace(0, 15, 16); module1.Parameters() = arma::mat(9 + 1, 1, arma::fill::zeros); module1.Reset(); - module1.Forward(std::move(input), std::move(output)); + module1.Forward(input, output); // Value calculated using tensorflow.nn.conv2d_transpose(). BOOST_REQUIRE_EQUAL(arma::accu(output), 0.0); // Test the Backward Function. - module1.Backward(std::move(input), std::move(output), std::move(delta)); + module1.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 0.0); // Test Valid for non zero padding. @@ -2964,12 +2961,12 @@ BOOST_AUTO_TEST_CASE(TransposedConvolutionLayerPaddingTest) module2.Parameters()(6) = 4.0; module2.Parameters()(8) = 2.0; module2.Reset(); - module2.Forward(std::move(input), std::move(output)); + module2.Forward(input, output); // Value calculated using torch.nn.functional.conv_transpose2d(). BOOST_REQUIRE_EQUAL(arma::accu(output), 120.0); // Test the Backward Function. - module2.Backward(std::move(input), std::move(output), std::move(delta)); + module2.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 960.0); // Test for same padding type. @@ -2978,13 +2975,13 @@ BOOST_AUTO_TEST_CASE(TransposedConvolutionLayerPaddingTest) input = arma::linspace(0, 8, 9); module3.Parameters() = arma::mat(9 + 1, 1, arma::fill::zeros); module3.Reset(); - module3.Forward(std::move(input), std::move(output)); + module3.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), 0); BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); // Test the Backward Function. - module3.Backward(std::move(input), std::move(output), std::move(delta)); + module3.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 0.0); // Output shape should equal input. @@ -2995,13 +2992,13 @@ BOOST_AUTO_TEST_CASE(TransposedConvolutionLayerPaddingTest) input = arma::linspace(0, 24, 25); module4.Parameters() = arma::mat(9 + 1, 1, arma::fill::zeros); module4.Reset(); - module4.Forward(std::move(input), std::move(output)); + module4.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), 0); BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); // Test the Backward Function. - module4.Backward(std::move(input), std::move(output), std::move(delta)); + module4.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 0.0); TransposedConvolution<> module5(1, 1, 3, 3, 2, 2, 0, 0, 2, 2, 2, 2, "SAME"); @@ -3009,13 +3006,13 @@ BOOST_AUTO_TEST_CASE(TransposedConvolutionLayerPaddingTest) input = arma::linspace(0, 3, 4); module5.Parameters() = arma::mat(25 + 1, 1, arma::fill::zeros); module5.Reset(); - module5.Forward(std::move(input), std::move(output)); + module5.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), 0); BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); // Test the Backward Function. - module5.Backward(std::move(input), std::move(output), std::move(delta)); + module5.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 0.0); TransposedConvolution<> module6(1, 1, 4, 4, 1, 1, 1, 1, 5, 5, 5, 5, "SAME"); @@ -3023,13 +3020,13 @@ BOOST_AUTO_TEST_CASE(TransposedConvolutionLayerPaddingTest) input = arma::linspace(0, 24, 25); module6.Parameters() = arma::mat(16 + 1, 1, arma::fill::zeros); module6.Reset(); - module6.Forward(std::move(input), std::move(output)); + module6.Forward(input, output); BOOST_REQUIRE_EQUAL(arma::accu(output), 0); BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); // Test the Backward Function. - module6.Backward(std::move(input), std::move(output), std::move(delta)); + module6.Backward(input, output, delta); BOOST_REQUIRE_EQUAL(arma::accu(delta), 0.0); } BOOST_AUTO_TEST_SUITE_END(); diff --git a/src/mlpack/tests/ann_test_tools.hpp b/src/mlpack/tests/ann_test_tools.hpp index be789befd5..00a4c80810 100644 --- a/src/mlpack/tests/ann_test_tools.hpp +++ b/src/mlpack/tests/ann_test_tools.hpp @@ -53,7 +53,7 @@ double JacobianTest(ModuleType& module, ResetFunction(module); // Initialize the jacobian matrix. - module.Forward(std::move(input), std::move(output)); + module.Forward(input, output); jacobianA = arma::zeros(input.n_elem, output.n_elem); // Share the input paramter matrix. @@ -64,9 +64,9 @@ double JacobianTest(ModuleType& module, { double original = sin(i); sin(i) = original - perturbation; - module.Forward(std::move(input), std::move(outputA)); + module.Forward(input, outputA); sin(i) = original + perturbation; - module.Forward(std::move(input), std::move(outputB)); + module.Forward(input, outputB); sin(i) = original; outputB -= outputA; @@ -90,7 +90,7 @@ double JacobianTest(ModuleType& module, derivTemp(i) = 1; arma::mat delta; - module.Backward(std::move(input), std::move(deriv), std::move(delta)); + module.Backward(input, deriv, delta); jacobianB.col(i) = delta; } @@ -106,10 +106,10 @@ double JacobianPerformanceTest(ModuleType& module, arma::mat& target, const double eps = 1e-6) { - module.Forward(std::move(input), std::move(target)); + module.Forward(input, target); arma::mat delta; - module.Backward(std::move(input), std::move(target), std::move(delta)); + module.Backward(input, target, delta); arma::mat centralDifference = arma::zeros(delta.n_rows, delta.n_cols); arma::mat inputTemp = arma::mat(input.memptr(), input.n_rows, input.n_cols, @@ -121,9 +121,9 @@ double JacobianPerformanceTest(ModuleType& module, for (size_t i = 0; i < input.n_elem; ++i) { inputTemp(i) = inputTemp(i) + eps; - double outputA = module.Forward(std::move(input), std::move(target)); + double outputA = module.Forward(input, target); inputTemp(i) = inputTemp(i) - (2 * eps); - double outputB = module.Forward(std::move(input), std::move(target)); + double outputB = module.Forward(input, target); centralDifferenceTemp(i) = (outputA - outputB) / (2 * eps); inputTemp(i) = inputTemp(i) + eps; diff --git a/src/mlpack/tests/ann_visitor_test.cpp b/src/mlpack/tests/ann_visitor_test.cpp index e7db9708b2..ef45919bfb 100644 --- a/src/mlpack/tests/ann_visitor_test.cpp +++ b/src/mlpack/tests/ann_visitor_test.cpp @@ -35,22 +35,20 @@ BOOST_AUTO_TEST_CASE(BiasSetVisitorTest) ResetVisitor resetVisitor; - boost::apply_visitor(WeightSetVisitor(std::move(layerWeights), 0), linear); + boost::apply_visitor(WeightSetVisitor(layerWeights, 0), linear); boost::apply_visitor(resetVisitor, linear); arma::mat weight = {"1 2 3 4 5 6 7 8 9 10"}; - size_t biasSize = boost::apply_visitor(BiasSetVisitor(std::move(weight), - 0), linear); + size_t biasSize = boost::apply_visitor(BiasSetVisitor(weight, 0), linear); BOOST_REQUIRE_EQUAL(biasSize, 10); arma::mat input(10, 1), output; input.randu(); - boost::apply_visitor(ForwardVisitor(std::move(input), std::move(output)), - linear); + boost::apply_visitor(ForwardVisitor(input, output), linear); BOOST_REQUIRE_EQUAL(arma::accu(output), 55); } diff --git a/src/mlpack/tests/callback_test.cpp b/src/mlpack/tests/callback_test.cpp index 6bd3db2147..a75b647b40 100644 --- a/src/mlpack/tests/callback_test.cpp +++ b/src/mlpack/tests/callback_test.cpp @@ -253,6 +253,7 @@ BOOST_AUTO_TEST_CASE(RBMCallbackTest) // Call the train function with printloss callback. double objVal = model.Train(msgd, ens::ProgressBar(70, stream)); + BOOST_REQUIRE(!std::isnan(objVal)); BOOST_REQUIRE_GT(stream.str().length(), 0); } diff --git a/src/mlpack/tests/feedforward_network_test.cpp b/src/mlpack/tests/feedforward_network_test.cpp index b84f2a557b..952d1779b5 100644 --- a/src/mlpack/tests/feedforward_network_test.cpp +++ b/src/mlpack/tests/feedforward_network_test.cpp @@ -183,7 +183,7 @@ BOOST_AUTO_TEST_CASE(ForwardBackwardTest) arma::mat currentResuls; model.Forward(currentData, currentResuls); arma::mat gradients; - model.Backward(currentLabels, gradients); + model.Backward(currentData, currentLabels, gradients); #if ENS_VERSION_MAJOR == 1 opt.Update(model.Parameters(), stepSize, gradients); #else @@ -600,10 +600,8 @@ BOOST_AUTO_TEST_CASE(FFNReturnModel) // Get the layer parameter from layer A and layer B and store them in // parameterA and parameterB. arma::mat parameterA, parameterB; - boost::apply_visitor(ParametersVisitor(std::move(parameterA)), - model.Model()[0]); - boost::apply_visitor(ParametersVisitor(std::move(parameterB)), - model.Model()[1]); + boost::apply_visitor(ParametersVisitor(parameterA), model.Model()[0]); + boost::apply_visitor(ParametersVisitor(parameterB), model.Model()[1]); CheckMatrices(parameterA, arma::ones(3 * 3 + 3, 1)); CheckMatrices(parameterB, arma::zeros(3 * 4 + 4, 1)); diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index 7988a961d0..4787223062 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -48,7 +48,7 @@ BOOST_AUTO_TEST_CASE(SimpleKLDivergenceTest) // Test the Forward function. Loss should be 0 if input = target. input = arma::ones(10, 1); target = arma::ones(10, 1); - loss = module.Forward(std::move(input), std::move(target)); + loss = module.Forward(input, target); BOOST_REQUIRE_SMALL(loss, 0.00001); } @@ -64,11 +64,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanSquaredLogarithmicErrorTest) // the manually calculated result. input = arma::zeros(1, 8); target = arma::zeros(1, 8); - double error = module.Forward(std::move(input), std::move(target)); + double error = module.Forward(input, target); BOOST_REQUIRE_SMALL(error, 0.00001); // Test the Backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input, target, output); // The output should be equal to 0. CheckMatrices(input, output); BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); @@ -77,11 +77,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanSquaredLogarithmicErrorTest) // Test the error function on a single input. input = arma::mat("2"); target = arma::mat("3"); - error = module.Forward(std::move(input), std::move(target)); + error = module.Forward(input, target); BOOST_REQUIRE_CLOSE(error, 0.082760974810151655, 0.001); // Test the Backward function on a single input. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input, target, output); BOOST_REQUIRE_CLOSE(arma::accu(output), -0.1917880483011872, 0.001); BOOST_REQUIRE_EQUAL(output.n_elem, 1); } @@ -99,11 +99,11 @@ BOOST_AUTO_TEST_CASE(KLDivergenceMeanTest) input = arma::mat("1 1 1 1 1 1 1 1 1 1"); target = arma::exp(arma::mat("2 1 1 1 1 1 1 1 1 1")); - loss = module.Forward(std::move(input), std::move(target)); + loss = module.Forward(input, target); BOOST_REQUIRE_CLOSE_FRACTION(loss, -1.1 , 0.00001); // Test the Backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input, target, output); BOOST_REQUIRE_CLOSE_FRACTION(arma::as_scalar(output), -0.1, 0.00001); } @@ -120,11 +120,11 @@ BOOST_AUTO_TEST_CASE(KLDivergenceNoMeanTest) input = arma::mat("1 1 1 1 1 1 1 1 1 1"); target = arma::exp(arma::mat("2 1 1 1 1 1 1 1 1 1")); - loss = module.Forward(std::move(input), std::move(target)); + loss = module.Forward(input, target); BOOST_REQUIRE_CLOSE_FRACTION(loss, -11, 0.00001); // Test the Backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input, target, output); BOOST_REQUIRE_CLOSE_FRACTION(arma::as_scalar(output), -1, 0.00001); } @@ -140,11 +140,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanSquaredErrorTest) // the manually calculated result. input = arma::mat("1.0 0.0 1.0 0.0 -1.0 0.0 -1.0 0.0"); target = arma::zeros(1, 8); - double error = module.Forward(std::move(input), std::move(target)); + double error = module.Forward(input, target); BOOST_REQUIRE_EQUAL(error, 0.5); // Test the Backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input, target, output); // We subtract a zero vector, so according to the used backward formula: // output = 2 * (input - target) / target.n_cols, // output * nofColumns / 2 should be equal to input. @@ -155,11 +155,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanSquaredErrorTest) // Test the error function on a single input. input = arma::mat("2"); target = arma::mat("3"); - error = module.Forward(std::move(input), std::move(target)); + error = module.Forward(input, target); BOOST_REQUIRE_EQUAL(error, 1.0); // Test the Backward function on a single input. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input, target, output); // Test whether the output is negative. BOOST_REQUIRE_EQUAL(arma::accu(output), -2); BOOST_REQUIRE_EQUAL(output.n_elem, 1); @@ -177,16 +177,16 @@ BOOST_AUTO_TEST_CASE(SimpleCrossEntropyErrorTest) // the manually calculated result. input1 = arma::mat("0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5"); target1 = arma::zeros(1, 8); - double error1 = module.Forward(std::move(input1), std::move(target1)); + double error1 = module.Forward(input1, target1); BOOST_REQUIRE_SMALL(error1 - 8 * std::log(2), 2e-5); input2 = arma::mat("0 1 1 0 1 0 0 1"); target2 = arma::mat("0 1 1 0 1 0 0 1"); - double error2 = module.Forward(std::move(input2), std::move(target2)); + double error2 = module.Forward(input2, target2); BOOST_REQUIRE_SMALL(error2, 1e-5); // Test the Backward function. - module.Backward(std::move(input1), std::move(target1), std::move(output)); + module.Backward(input1, target1, output); for (double el : output) { // For the 0.5 constant vector we should get 1 / (1 - 0.5) = 2 everywhere. @@ -195,7 +195,7 @@ BOOST_AUTO_TEST_CASE(SimpleCrossEntropyErrorTest) BOOST_REQUIRE_EQUAL(output.n_rows, input1.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input1.n_cols); - module.Backward(std::move(input2), std::move(target2), std::move(output)); + module.Backward(input2, target2, output); for (size_t i = 0; i < 8; ++i) { double el = output.at(0, i); @@ -221,25 +221,25 @@ BOOST_AUTO_TEST_CASE(SimpleSigmoidCrossEntropyErrorTest) // the calculated result. input1 = arma::mat("0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5"); target1 = arma::zeros(1, 8); - double error1 = module.Forward(std::move(input1), std::move(target1)); + double error1 = module.Forward(input1, target1); double expected = 0.97407699; // Value computed using tensorflow. BOOST_REQUIRE_SMALL(error1 / input1.n_elem - expected, 1e-7); input2 = arma::mat("1 2 3 4 5"); target2 = arma::mat("0 0 1 0 1"); - double error2 = module.Forward(std::move(input2), std::move(target2)); + double error2 = module.Forward(input2, target2); expected = 1.5027283; BOOST_REQUIRE_SMALL(error2 / input2.n_elem - expected, 1e-6); input3 = arma::mat("0 -1 -1 0 -1 0 0 -1"); target3 = arma::mat("0 -1 -1 0 -1 0 0 -1"); - double error3 = module.Forward(std::move(input3), std::move(target3)); + double error3 = module.Forward(input3, target3); expected = 0.00320443; BOOST_REQUIRE_SMALL(error3 / input3.n_elem - expected, 1e-6); // Test the Backward function. - module.Backward(std::move(input1), std::move(target1), std::move(output)); + module.Backward(input1, target1, output); expected = 0.62245929; for (size_t i = 0; i < output.n_elem; i++) BOOST_REQUIRE_SMALL(output(i) - expected, 1e-5); @@ -248,13 +248,13 @@ BOOST_AUTO_TEST_CASE(SimpleSigmoidCrossEntropyErrorTest) expectedOutput = arma::mat( "0.7310586 0.88079709 -0.04742587 0.98201376 -0.00669285"); - module.Backward(std::move(input2), std::move(target2), std::move(output)); + module.Backward(input2, target2, output); for (size_t i = 0; i < output.n_elem; i++) BOOST_REQUIRE_SMALL(output(i) - expectedOutput(i), 1e-5); BOOST_REQUIRE_EQUAL(output.n_rows, input2.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input2.n_cols); - module.Backward(std::move(input3), std::move(target3), std::move(output)); + module.Backward(input3, target3, output); expectedOutput = arma::mat("0.5 1.2689414"); for (size_t i = 0; i < 8; ++i) { @@ -280,18 +280,18 @@ BOOST_AUTO_TEST_CASE(SimpleEarthMoverDistanceLayerTest) // the manually calculated result. input1 = arma::mat("0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5"); target1 = arma::zeros(1, 8); - double error1 = module.Forward(std::move(input1), std::move(target1)); + double error1 = module.Forward(input1, target1); double expected = 0.0; BOOST_REQUIRE_SMALL(error1 / input1.n_elem - expected, 1e-7); input2 = arma::mat("1 2 3 4 5"); target2 = arma::mat("1 0 1 0 1"); - double error2 = module.Forward(std::move(input2), std::move(target2)); + double error2 = module.Forward(input2, target2); expected = -1.8; BOOST_REQUIRE_SMALL(error2 / input2.n_elem - expected, 1e-6); // Test the Backward function. - module.Backward(std::move(input1), std::move(target1), std::move(output)); + module.Backward(input1, target1, output); expected = 0.0; for (size_t i = 0; i < output.n_elem; i++) BOOST_REQUIRE_SMALL(output(i) - expected, 1e-5); @@ -299,7 +299,7 @@ BOOST_AUTO_TEST_CASE(SimpleEarthMoverDistanceLayerTest) BOOST_REQUIRE_EQUAL(output.n_cols, input1.n_cols); expectedOutput = arma::mat("-1 0 -1 0 -1"); - module.Backward(std::move(input2), std::move(target2), std::move(output)); + module.Backward(input2, target2, output); for (size_t i = 0; i < output.n_elem; i++) BOOST_REQUIRE_SMALL(output(i) - expectedOutput(i), 1e-5); BOOST_REQUIRE_EQUAL(output.n_rows, input2.n_rows); @@ -404,16 +404,16 @@ BOOST_AUTO_TEST_CASE(DiceLossTest) // Test the Forward function. Loss should be 0 if input = target. input1 = arma::ones(10, 1); target = arma::ones(10, 1); - loss = module.Forward(std::move(input1), std::move(target)); + loss = module.Forward(input1, target); BOOST_REQUIRE_SMALL(loss, 0.00001); // Test the Forward function. Loss should be 0.185185185. input2 = arma::ones(10, 1) * 0.5; - loss = module.Forward(std::move(input2), std::move(target)); + loss = module.Forward(input2, target); BOOST_REQUIRE_CLOSE(loss, 0.185185185, 0.00001); // Test the Backward function for input = target. - module.Backward(std::move(input1), std::move(target), std::move(output)); + module.Backward(input1, target, output); for (double el : output) { // For input = target we should get 0.0 everywhere. @@ -423,7 +423,7 @@ BOOST_AUTO_TEST_CASE(DiceLossTest) BOOST_REQUIRE_EQUAL(output.n_cols, input1.n_cols); // Test the Backward function. - module.Backward(std::move(input2), std::move(target), std::move(output)); + module.Backward(input2, target, output); for (double el : output) { // For the 0.5 constant vector we should get -0.0877914951989026 everywhere. @@ -445,11 +445,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanBiasErrorTest) // the manually calculated result. input = arma::mat("1.0 0.0 1.0 -1.0 -1.0 0.0 -1.0 0.0"); target = arma::zeros(1, 8); - double error = module.Forward(std::move(input), std::move(target)); + double error = module.Forward(input, target); BOOST_REQUIRE_EQUAL(error, 0.125); // Test the Backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input, target, output); // We should get a vector with -1 everywhere. for (double el : output) { @@ -461,11 +461,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanBiasErrorTest) // Test the error function on a single input. input = arma::mat("2"); target = arma::mat("3"); - error = module.Forward(std::move(input), std::move(target)); + error = module.Forward(input, target); BOOST_REQUIRE_EQUAL(error, 1.0); // Test the Backward function on a single input. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input, target, output); // Test whether the output is negative. BOOST_REQUIRE_EQUAL(arma::accu(output), -1); BOOST_REQUIRE_EQUAL(output.n_elem, 1); From b5d00eda93db78c2cda96ad9ea9883c539fe74a5 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Thu, 5 Mar 2020 19:32:04 -0500 Subject: [PATCH 058/265] Fix style issues. --- src/mlpack/methods/ann/brnn_impl.hpp | 20 ++++++++++---------- src/mlpack/methods/ann/rnn_impl.hpp | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/mlpack/methods/ann/brnn_impl.hpp b/src/mlpack/methods/ann/brnn_impl.hpp index 1a7bd09664..be97702932 100644 --- a/src/mlpack/methods/ann/brnn_impl.hpp +++ b/src/mlpack/methods/ann/brnn_impl.hpp @@ -173,9 +173,9 @@ void BRNN Date: Fri, 6 Mar 2020 09:40:59 +0530 Subject: [PATCH 059/265] Minor changes reflecting discusssion --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 180182e77e..17249b8d82 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ src="https://cdn.rawgit.com/mlpack/mlpack.org/e7d36ed8/mlpack-black.svg" style=" Jenkins Coveralls License - NumFOCUS + NumFOCUS

From 02fa559bc6b56f0cbdea44511c5a7a530ab5ed31 Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Fri, 6 Mar 2020 13:02:57 +0530 Subject: [PATCH 060/265] Add note for waiting time and review policy --- CONTRIBUTING.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b377a24e69..0a6c693227 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -28,3 +28,32 @@ Members of the Contributors team are encouraged to review pull requests that have already been reviewed, and pull request contributors are encouraged to seek multiple reviews. Reviews from anyone not on the Contributors team are always appreciated and encouraged! + +## Reviewing Pull Requests + +All mlpack contributors who choose to review and provide feedback on Pull Requests have a responsibility to both the project and the individual making the contribution. + +Reviews and feedback must be helpful, insightful, and geared towards improving the contribution. If there are reasons why you feel the PR should not be merged, explain what those are. Do not expect to be able to block a Pull Request from advancing simply because you say "No" without giving an explanation. Be open to having your mind changed. Be open to working with the contributor to make the Pull Request better. + +Reviews that are dismissive or disrespectful of the contributor or any other reviewers are strictly prohibited. + +When reviewing a Pull Request, the primary goals are : + +- For the codebase/project to improve +- For the person submitting the request to succeed + +Even if a Pull Request does not gets merged, the submitters should come away from the experience feeling like their effort was not wasted or unappreciated. Every Pull Request from a new contributor is an opportunity to grow the community. + +When changes are necessary, request them, do not demand them, and do not assume that the contributor already knows how to do that. Be there to lend a helping hand in case of need. + +Since there is clearly a high difference between pull request being raised and those being reviewed we highly encourage everyone to review each others pull request keeping in mind all the above mentioned points. + +Let's welcome new contributors with <3, and not overwhelm them. + +## Pull Request Waiting Time + +Since all those associated with the project are working during their free time to improve the project, It sometimes becomes difficult to spare time for a particular pull request and hence please wait patiently until one of the contributors or maintainers reviews your pull request or patches. + +There is a minimum waiting time which we try to respect, so that people who may have important input in such a huge project are able to respond + +Constant pinging or getting in touch to get the pull request wouldn't be of much help and may overload the maintainer which could potentialy reduce the throughput. From d7308791041a612fde354171432d926d5b7ef359 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Fri, 6 Mar 2020 08:23:32 -0500 Subject: [PATCH 061/265] Use Csize_t instead of uint64_t, and UInt/Int. --- .../bindings/julia/default_param_impl.hpp | 4 +- src/mlpack/bindings/julia/get_julia_type.hpp | 4 +- .../julia/get_printable_type_impl.hpp | 8 +- src/mlpack/bindings/julia/julia_util.cpp | 266 ++++++------------ src/mlpack/bindings/julia/julia_util.h | 64 ++--- src/mlpack/bindings/julia/mlpack/cli.jl.in | 124 ++++---- .../julia/print_doc_functions_impl.hpp | 2 +- .../bindings/julia/print_type_doc_impl.hpp | 10 +- src/mlpack/bindings/julia/tests/runtests.jl | 16 +- 9 files changed, 209 insertions(+), 289 deletions(-) diff --git a/src/mlpack/bindings/julia/default_param_impl.hpp b/src/mlpack/bindings/julia/default_param_impl.hpp index 6a25fb494f..47f1bd7ab1 100644 --- a/src/mlpack/bindings/julia/default_param_impl.hpp +++ b/src/mlpack/bindings/julia/default_param_impl.hpp @@ -116,11 +116,11 @@ std::string DefaultParamImpl( else if (std::is_same>::value || std::is_same>::value) { - return "Int64[]"; + return "Int[]"; } else if (std::is_same>::value) { - return "zeros(Int64, 0, 0)"; + return "zeros(Int, 0, 0)"; } else { diff --git a/src/mlpack/bindings/julia/get_julia_type.hpp b/src/mlpack/bindings/julia/get_julia_type.hpp index 9ff62e4cb9..096c420ab9 100644 --- a/src/mlpack/bindings/julia/get_julia_type.hpp +++ b/src/mlpack/bindings/julia/get_julia_type.hpp @@ -101,9 +101,9 @@ inline std::string GetJuliaType( const typename std::enable_if::value>::type* = 0) { // size_t matrices are special: we want to represent them in Julia as - // Array{Int64, X} not UInt64 because Julia displays UInt64s strangely. + // Array{Int, X} not UInt because Julia displays UInts strangely. if (std::is_same::value) - return std::string("Array{Int64, ") + (T::is_col || T::is_row ? "1" : "2") + return std::string("Array{Int, ") + (T::is_col || T::is_row ? "1" : "2") + "}"; else return "Array{" + GetJuliaType() + ", " diff --git a/src/mlpack/bindings/julia/get_printable_type_impl.hpp b/src/mlpack/bindings/julia/get_printable_type_impl.hpp index c46e75b365..524fe62a0b 100644 --- a/src/mlpack/bindings/julia/get_printable_type_impl.hpp +++ b/src/mlpack/bindings/julia/get_printable_type_impl.hpp @@ -47,7 +47,7 @@ std::string GetPrintableType( const typename std::enable_if::value>::type*) { if (std::is_same>::value) - return "Array{Int64, 1}"; + return "Array{Int, 1}"; else if (std::is_same>::value) return "Array{String, 1}"; else @@ -65,15 +65,15 @@ std::string GetPrintableType( if (std::is_same::value) return "Float64 matrix-like"; else if (std::is_same>::value) - return "Int64 matrix-like"; + return "Int matrix-like"; else if (std::is_same::value) return "Float64 vector-like"; else if (std::is_same>::value) - return "Int64 vector-like"; + return "Int vector-like"; else if (std::is_same::value) return "Float64 vector-like"; else if (std::is_same>::value) - return "Int64 vector-like"; + return "Int vector-like"; else throw std::invalid_argument("unknown Armadillo type " + data.cppType); } diff --git a/src/mlpack/bindings/julia/julia_util.cpp b/src/mlpack/bindings/julia/julia_util.cpp index fc38e1377b..81b17155cd 100644 --- a/src/mlpack/bindings/julia/julia_util.cpp +++ b/src/mlpack/bindings/julia/julia_util.cpp @@ -61,10 +61,10 @@ void CLI_SetParamBool(const char* paramName, bool paramValue) * Call CLI::SetParam>() to set the length. */ void CLI_SetParamVectorStrLen(const char* paramName, - const uint64_t length) + const size_t length) { CLI::GetParam>(paramName).clear(); - CLI::GetParam>(paramName).resize(size_t(length)); + CLI::GetParam>(paramName).resize(length); CLI::SetPassed(paramName); } @@ -73,9 +73,9 @@ void CLI_SetParamVectorStrLen(const char* paramName, */ void CLI_SetParamVectorStrStr(const char* paramName, const char* str, - const uint64_t element) + const size_t element) { - CLI::GetParam>(paramName)[size_t(element)] = + CLI::GetParam>(paramName)[element] = std::string(str); } @@ -84,13 +84,13 @@ void CLI_SetParamVectorStrStr(const char* paramName, */ void CLI_SetParamVectorInt(const char* paramName, int64_t* ints, - const uint64_t length) + const size_t length) { // Create a std::vector object; unfortunately this requires copying the // vector elements. std::vector vec; - vec.resize(size_t(length)); - for (size_t i = 0; i < (size_t) length; ++i) + vec.resize(length); + for (size_t i = 0; i < length; ++i) vec[i] = int(ints[i]); CLI::GetParam>(paramName) = std::move(vec); @@ -102,8 +102,8 @@ void CLI_SetParamVectorInt(const char* paramName, */ void CLI_SetParamMat(const char* paramName, double* memptr, - const uint64_t rows, - const uint64_t cols, + const size_t rows, + const size_t cols, const bool pointsAsRows) { // Create the matrix as an alias. @@ -116,32 +116,17 @@ void CLI_SetParamMat(const char* paramName, * Call CLI::SetParam>(). */ void CLI_SetParamUMat(const char* paramName, - uint64_t* memptr, - const uint64_t rows, - const uint64_t cols, + size_t* memptr, + const size_t rows, + const size_t cols, const bool pointsAsRows) { - // If we're on a 64-bit system, we can create the matrix as an alias. - if (sizeof(uint64_t) == sizeof(size_t)) - { - // Create the matrix as an alias. - arma::Mat m((size_t*) memptr, arma::uword(rows), arma::uword(cols), - false, true); - CLI::GetParam>(paramName) = pointsAsRows ? m.t() : - std::move(m); - CLI::SetPassed(paramName); - } - else - { - // We have to perform conversion. Create an alias of the memory we got, and - // then convert it. - arma::Mat m(memptr, arma::uword(rows), arma::uword(cols), false, - true); - CLI::GetParam>(paramName) = pointsAsRows ? - arma::conv_to>::from(m.t()) : - arma::conv_to>::from(m); - CLI::SetPassed(paramName); - } + // Create the matrix as an alias. + arma::Mat m(memptr, arma::uword(rows), arma::uword(cols), false, + true); + CLI::GetParam>(paramName) = pointsAsRows ? m.t() : + std::move(m); + CLI::SetPassed(paramName); } /** @@ -149,7 +134,7 @@ void CLI_SetParamUMat(const char* paramName, */ void CLI_SetParamRow(const char* paramName, double* memptr, - const uint64_t cols) + const size_t cols) { arma::rowvec m(memptr, arma::uword(cols), false, true); CLI::GetParam(paramName) = std::move(m); @@ -160,25 +145,12 @@ void CLI_SetParamRow(const char* paramName, * Call CLI::SetParam>(). */ void CLI_SetParamURow(const char* paramName, - uint64_t* memptr, - const uint64_t cols) + size_t* memptr, + const size_t cols) { - // If we're on a 64-bit system, we can create the matrix as an alias. - if (sizeof(uint64_t) == sizeof(size_t)) - { - arma::Row m((size_t*) memptr, arma::uword(cols), false, true); - CLI::GetParam>(paramName) = std::move(m); - CLI::SetPassed(paramName); - } - else - { - // We have to perform conversion. Create an alias of the memory we got, - // and then convert it. - arma::Row m(memptr, arma::uword(cols), false, true); - CLI::GetParam>(paramName) = - arma::conv_to>::from(m); - CLI::SetPassed(paramName); - } + arma::Row m(memptr, arma::uword(cols), false, true); + CLI::GetParam>(paramName) = std::move(m); + CLI::SetPassed(paramName); } /** @@ -186,7 +158,7 @@ void CLI_SetParamURow(const char* paramName, */ void CLI_SetParamCol(const char* paramName, double* memptr, - const uint64_t rows) + const size_t rows) { arma::vec m(memptr, arma::uword(rows), false, true); CLI::GetParam(paramName) = std::move(m); @@ -197,24 +169,12 @@ void CLI_SetParamCol(const char* paramName, * Call CLI::SetParam>(). */ void CLI_SetParamUCol(const char* paramName, - uint64_t* memptr, - const uint64_t rows) + size_t* memptr, + const size_t rows) { - // If Julia gave us the right size, we can use an alias; otherwise we have to - // copy. - if (sizeof(uint64_t) == sizeof(size_t)) - { - arma::Col m((size_t*) memptr, arma::uword(rows), false, true); - CLI::GetParam>(paramName) = std::move(m); - CLI::SetPassed(paramName); - } - else - { - arma::Col m(memptr, arma::uword(rows), false, true); - CLI::GetParam>(paramName) = - arma::conv_to>::from(m); - CLI::SetPassed(paramName); - } + arma::Col m(memptr, arma::uword(rows), false, true); + CLI::GetParam>(paramName) = std::move(m); + CLI::SetPassed(paramName); } /** @@ -223,8 +183,8 @@ void CLI_SetParamUCol(const char* paramName, void CLI_SetParamMatWithInfo(const char* paramName, bool* dimensions, double* memptr, - const uint64_t rows, - const uint64_t cols, + const size_t rows, + const size_t cols, const bool pointsAreRows) { data::DatasetInfo d(pointsAreRows ? cols : rows); @@ -278,9 +238,9 @@ bool CLI_GetParamBool(const char* paramName) * Call CLI::GetParam>() and get the length of the * vector. */ -uint64_t CLI_GetParamVectorStrLen(const char* paramName) +size_t CLI_GetParamVectorStrLen(const char* paramName) { - return uint64_t(CLI::GetParam>(paramName).size()); + return CLI::GetParam>(paramName).size(); } /** @@ -294,9 +254,9 @@ const char* CLI_GetParamVectorStrStr(const char* paramName, const int64_t i) /** * Call CLI::GetParam>() and get the length of the vector. */ -uint64_t CLI_GetParamVectorIntLen(const char* paramName) +size_t CLI_GetParamVectorIntLen(const char* paramName) { - return uint64_t(CLI::GetParam>(paramName).size()); + return CLI::GetParam>(paramName).size(); } /** @@ -304,13 +264,13 @@ uint64_t CLI_GetParamVectorIntLen(const char* paramName) * The vector will be created in-place and it is expected that the calling * function will take ownership. */ -uint64_t* CLI_GetParamVectorIntPtr(const char* paramName) +size_t* CLI_GetParamVectorIntPtr(const char* paramName) { const size_t size = CLI::GetParam>(paramName).size(); - uint64_t* ints = new uint64_t[size]; + size_t* ints = new size_t[size]; for (size_t i = 0; i < size; ++i) - ints[i] = CLI::GetParam>(paramName)[i]; + ints[i] = size_t(CLI::GetParam>(paramName)[i]); return ints; } @@ -318,17 +278,17 @@ uint64_t* CLI_GetParamVectorIntPtr(const char* paramName) /** * Get the number of rows in a matrix parameter. */ -uint64_t CLI_GetParamMatRows(const char* paramName) +size_t CLI_GetParamMatRows(const char* paramName) { - return uint64_t(CLI::GetParam(paramName).n_rows); + return CLI::GetParam(paramName).n_rows; } /** * Get the number of columns in a matrix parameter. */ -uint64_t CLI_GetParamMatCols(const char* paramName) +size_t CLI_GetParamMatCols(const char* paramName) { - return uint64_t(CLI::GetParam(paramName).n_cols); + return CLI::GetParam(paramName).n_cols; } /** @@ -358,17 +318,17 @@ double* CLI_GetParamMat(const char* paramName) /** * Get the number of rows in an unsigned matrix parameter. */ -uint64_t CLI_GetParamUMatRows(const char* paramName) +size_t CLI_GetParamUMatRows(const char* paramName) { - return uint64_t(CLI::GetParam>(paramName).n_rows); + return CLI::GetParam>(paramName).n_rows; } /** * Get the number of columns in an unsigned matrix parameter. */ -uint64_t CLI_GetParamUMatCols(const char* paramName) +size_t CLI_GetParamUMatCols(const char* paramName) { - return uint64_t(CLI::GetParam>(paramName).n_cols); + return CLI::GetParam>(paramName).n_cols; } /** @@ -376,45 +336,33 @@ uint64_t CLI_GetParamUMatCols(const char* paramName) * Note that this will assume that whatever is calling will take ownership of * the memory! */ -uint64_t* CLI_GetParamUMat(const char* paramName) +size_t* CLI_GetParamUMat(const char* paramName) { arma::Mat& mat = CLI::GetParam>(paramName); - // Unfortunately, if size_t is not uint64_t, we will incur a copy. - if (sizeof(uint64_t) == sizeof(size_t)) + // Are we using preallocated memory? If so we have to handle this more + // carefully. + if (mat.n_elem <= arma::arma_config::mat_prealloc) { - // Are we using preallocated memory? If so we have to handle this more - // carefully. - if (mat.n_elem <= arma::arma_config::mat_prealloc) - { - // Copy the memory to something that we can give back to Julia. - size_t* newMem = new size_t[mat.n_elem]; - arma::arrayops::copy(newMem, mat.mem, mat.n_elem); - // We believe Julia will free it. Hopefully we are right. - return (uint64_t*) newMem; - } - else - { - arma::access::rw(mat.mem_state) = 1; - return (uint64_t*) mat.memptr(); - } + // Copy the memory to something that we can give back to Julia. + size_t* newMem = new size_t[mat.n_elem]; + arma::arrayops::copy(newMem, mat.mem, mat.n_elem); + // We believe Julia will free it. Hopefully we are right. + return newMem; } else { - uint64_t* newMem = new uint64_t[mat.n_elem]; - for (size_t i = 0; i < mat.n_elem; ++i) - newMem[i] = uint64_t(mat[i]); - // We believe Julia will free it. Hopefully we are right. - return newMem; + arma::access::rw(mat.mem_state) = 1; + return mat.memptr(); } } /** * Get the number of rows in a column vector parameter. */ -uint64_t CLI_GetParamColRows(const char* paramName) +size_t CLI_GetParamColRows(const char* paramName) { - return uint64_t(CLI::GetParam(paramName).n_rows); + return CLI::GetParam(paramName).n_rows; } /** @@ -444,9 +392,9 @@ double* CLI_GetParamCol(const char* paramName) /** * Get the number of columns in an unsigned column vector parameter. */ -uint64_t CLI_GetParamUColRows(const char* paramName) +size_t CLI_GetParamUColRows(const char* paramName) { - return uint64_t(CLI::GetParam>(paramName).n_rows); + return CLI::GetParam>(paramName).n_rows; } /** @@ -454,46 +402,33 @@ uint64_t CLI_GetParamUColRows(const char* paramName) * Note that this will assume that whatever is calling will take ownership of * the memory! */ -uint64_t* CLI_GetParamUCol(const char* paramName) +size_t* CLI_GetParamUCol(const char* paramName) { arma::Col& vec = CLI::GetParam>(paramName); - // If size_t is not the uint64_t that Julia expects, then unfortunately we - // will have to make a copy. - if (sizeof(uint64_t) == sizeof(size_t)) + // Are we using preallocated memory? If so we have to handle this more + // carefully. + if (vec.n_elem <= arma::arma_config::mat_prealloc) { - // Are we using preallocated memory? If so we have to handle this more - // carefully. - if (vec.n_elem <= arma::arma_config::mat_prealloc) - { - // Copy the memory to something we can give back to Julia. - size_t* newMem = new size_t[vec.n_elem]; - arma::arrayops::copy(newMem, vec.mem, vec.n_elem); - // We believe Julia will free it. Hopefully we are right. - return (uint64_t*) newMem; - } - else - { - arma::access::rw(vec.mem_state) = 1; - return (uint64_t*) vec.memptr(); - } + // Copy the memory to something we can give back to Julia. + size_t* newMem = new size_t[vec.n_elem]; + arma::arrayops::copy(newMem, vec.mem, vec.n_elem); + // We believe Julia will free it. Hopefully we are right. + return newMem; } else { - uint64_t* newMem = new uint64_t[vec.n_elem]; - for (size_t i = 0; i < vec.n_elem; ++i) - newMem[i] = uint64_t(vec[i]); - // We believe Julia will free it. Hopefully we are right. - return newMem; + arma::access::rw(vec.mem_state) = 1; + return vec.memptr(); } } /** * Get the number of columns in a row parameter. */ -uint64_t CLI_GetParamRowCols(const char* paramName) +size_t CLI_GetParamRowCols(const char* paramName) { - return uint64_t(CLI::GetParam(paramName).n_cols); + return CLI::GetParam(paramName).n_cols; } /** @@ -523,9 +458,9 @@ double* CLI_GetParamRow(const char* paramName) /** * Get the number of columns in a row parameter. */ -uint64_t CLI_GetParamURowCols(const char* paramName) +size_t CLI_GetParamURowCols(const char* paramName) { - return uint64_t(CLI::GetParam>(paramName).n_cols); + return CLI::GetParam>(paramName).n_cols; } /** @@ -533,57 +468,42 @@ uint64_t CLI_GetParamURowCols(const char* paramName) * Note that this will assume that whatever is calling will take ownership of * the memory! */ -uint64_t* CLI_GetParamURow(const char* paramName) +size_t* CLI_GetParamURow(const char* paramName) { arma::Row& vec = CLI::GetParam>(paramName); - // If size_t is not the uint64_t that Julia expects, then unfortunately we - // will have to make a copy. - if (sizeof(size_t) == sizeof(uint64_t)) + // Are we using preallocated memory? If so we have to handle this more + // carefully. + if (vec.n_elem <= arma::arma_config::mat_prealloc) { - // Are we using preallocated memory? If so we have to handle this more - // carefully. - if (vec.n_elem <= arma::arma_config::mat_prealloc) - { - // Copy the memory to something we can give back to Julia. - size_t* newMem = new size_t[vec.n_elem]; - arma::arrayops::copy(newMem, vec.mem, vec.n_elem); - return (uint64_t*) newMem; - } - else - { - arma::access::rw(vec.mem_state) = 1; - return (uint64_t*) vec.memptr(); - } + // Copy the memory to something we can give back to Julia. + size_t* newMem = new size_t[vec.n_elem]; + arma::arrayops::copy(newMem, vec.mem, vec.n_elem); + return newMem; } else { - uint64_t* newMem = new uint64_t[vec.n_elem]; - for (size_t i = 0; i < vec.n_elem; ++i) - newMem[i] = uint64_t(vec[i]); - // We believe that Julia will free the memory. Hopefully we are right. - return newMem; + arma::access::rw(vec.mem_state) = 1; + return vec.memptr(); } } /** * Get the number of rows in a matrix with DatasetInfo parameter. */ -uint64_t CLI_GetParamMatWithInfoRows(const char* paramName) +size_t CLI_GetParamMatWithInfoRows(const char* paramName) { - return uint64_t(std::get<1>( - CLI::GetParam>( - paramName)).n_rows); + return std::get<1>( CLI::GetParam>( + paramName)).n_rows; } /** * Get the number of columns in a matrix with DatasetInfo parameter. */ -uint64_t CLI_GetParamMatWithInfoCols(const char* paramName) +size_t CLI_GetParamMatWithInfoCols(const char* paramName) { - return uint64_t(std::get<1>( - CLI::GetParam>( - paramName)).n_cols); + return std::get<1>( CLI::GetParam>( + paramName)).n_cols; } /** diff --git a/src/mlpack/bindings/julia/julia_util.h b/src/mlpack/bindings/julia/julia_util.h index 6ffa514c3d..9e15601640 100644 --- a/src/mlpack/bindings/julia/julia_util.h +++ b/src/mlpack/bindings/julia/julia_util.h @@ -48,38 +48,38 @@ void CLI_SetParamBool(const char* paramName, bool paramValue); * Call CLI::SetParam>() to set the length. */ void CLI_SetParamVectorStrLen(const char* paramName, - const uint64_t length); + const size_t length); /** * Call CLI::SetParam>() to set an individual element. */ void CLI_SetParamVectorStrStr(const char* paramName, const char* str, - const uint64_t element); + const size_t element); /** * Call CLI::SetParam>(). */ void CLI_SetParamVectorInt(const char* paramName, int64_t* ints, - const uint64_t length); + const size_t length); /** * Call CLI::SetParam(). */ void CLI_SetParamMat(const char* paramName, double* memptr, - const uint64_t rows, - const uint64_t cols, + const size_t rows, + const size_t cols, const bool pointsAsRows); /** * Call CLI::SetParam>(). */ void CLI_SetParamUMat(const char* paramName, - uint64_t* memptr, - const uint64_t rows, - const uint64_t cols, + size_t* memptr, + const size_t rows, + const size_t cols, const bool pointsAsRows); /** @@ -87,28 +87,28 @@ void CLI_SetParamUMat(const char* paramName, */ void CLI_SetParamRow(const char* paramName, double* memptr, - const uint64_t cols); + const size_t cols); /** * Call CLI::SetParam>(). */ void CLI_SetParamURow(const char* paramName, - uint64_t* memptr, - const uint64_t cols); + size_t* memptr, + const size_t cols); /** * Call CLI::SetParam(). */ void CLI_SetParamCol(const char* paramName, double* memptr, - const uint64_t rows); + const size_t rows); /** * Call CLI::SetParam>(). */ void CLI_SetParamUCol(const char* paramName, - uint64_t* memptr, - const uint64_t rows); + size_t* memptr, + const size_t rows); /** * Call CLI::SetParam>(). @@ -116,8 +116,8 @@ void CLI_SetParamUCol(const char* paramName, void CLI_SetParamMatWithInfo(const char* paramName, bool* dimensions, double* memptr, - const uint64_t rows, - const uint64_t cols, + const size_t rows, + const size_t cols, const bool pointsAreRows); /** @@ -144,7 +144,7 @@ bool CLI_GetParamBool(const char* paramName); * Call CLI::GetParam>() and get the length of the * vector. */ -uint64_t CLI_GetParamVectorStrLen(const char* paramName); +size_t CLI_GetParamVectorStrLen(const char* paramName); /** * Call CLI::GetParam>() and get the i'th string. @@ -154,24 +154,24 @@ const char* CLI_GetParamVectorStrStr(const char* paramName, const int64_t i); /** * Call CLI::GetParam>() and get the length of the vector. */ -uint64_t CLI_GetParamVectorIntLen(const char* paramName); +size_t CLI_GetParamVectorIntLen(const char* paramName); /** * Call CLI::GetParam>() and return a pointer to the vector. * The vector will be created in-place and it is expected that the calling * function will take ownership. */ -uint64_t* CLI_GetParamVectorIntPtr(const char* paramName); +size_t* CLI_GetParamVectorIntPtr(const char* paramName); /** * Get the number of rows in a matrix parameter. */ -uint64_t CLI_GetParamMatRows(const char* paramName); +size_t CLI_GetParamMatRows(const char* paramName); /** * Get the number of columns in a matrix parameter. */ -uint64_t CLI_GetParamMatCols(const char* paramName); +size_t CLI_GetParamMatCols(const char* paramName); /** * Get the memory pointer for a matrix parameter. @@ -183,24 +183,24 @@ double* CLI_GetParamMat(const char* paramName); /** * Get the number of rows in an unsigned matrix parameter. */ -uint64_t CLI_GetParamUMatRows(const char* paramName); +size_t CLI_GetParamUMatRows(const char* paramName); /** * Get the number of columns in an unsigned matrix parameter. */ -uint64_t CLI_GetParamUMatCols(const char* paramName); +size_t CLI_GetParamUMatCols(const char* paramName); /** * Get the memory pointer for an unsigned matrix parameter. * Note that this will assume that whatever is calling will take ownership of * the memory! */ -uint64_t* CLI_GetParamUMat(const char* paramName); +size_t* CLI_GetParamUMat(const char* paramName); /** * Get the number of rows in a column parameter. */ -uint64_t CLI_GetParamColRows(const char* paramName); +size_t CLI_GetParamColRows(const char* paramName); /** * Get the memory pointer for a column vector parameter. @@ -212,19 +212,19 @@ double* CLI_GetParamCol(const char* paramName); /** * Get the number of columns in an unsigned column vector parameter. */ -uint64_t CLI_GetParamUColRows(const char* paramName); +size_t CLI_GetParamUColRows(const char* paramName); /** * Get the memory pointer for an unsigned column vector parameter. * Note that this will assume that whatever is calling will take ownership of * the memory! */ -uint64_t* CLI_GetParamUCol(const char* paramName); +size_t* CLI_GetParamUCol(const char* paramName); /** * Get the number of columns in a row parameter. */ -uint64_t CLI_GetParamRowCols(const char* paramName); +size_t CLI_GetParamRowCols(const char* paramName); /** * Get the memory pointer for a row parameter. @@ -236,24 +236,24 @@ double* CLI_GetParamRow(const char* paramName); /** * Get the number of columns in a row parameter. */ -uint64_t CLI_GetParamURowCols(const char* paramName); +size_t CLI_GetParamURowCols(const char* paramName); /** * Get the memory pointer for a row parameter. * Note that this will assume that whatever is calling will take ownership of * the memory! */ -uint64_t* CLI_GetParamURow(const char* paramName); +size_t* CLI_GetParamURow(const char* paramName); /** * Get the number of rows in a matrix with DatasetInfo parameter. */ -uint64_t CLI_GetParamMatWithInfoRows(const char* paramName); +size_t CLI_GetParamMatWithInfoRows(const char* paramName); /** * Get the number of columns in a matrix with DatasetInfo parameter. */ -uint64_t CLI_GetParamMatWithInfoCols(const char* paramName); +size_t CLI_GetParamMatWithInfoCols(const char* paramName); /** * Get a pointer to an array of booleans representing whether or not dimensions diff --git a/src/mlpack/bindings/julia/mlpack/cli.jl.in b/src/mlpack/bindings/julia/mlpack/cli.jl.in index 5e3b71e914..d4e7271cfa 100644 --- a/src/mlpack/bindings/julia/mlpack/cli.jl.in +++ b/src/mlpack/bindings/julia/mlpack/cli.jl.in @@ -66,7 +66,7 @@ function CLIRestoreSettings(programName::String) end function CLISetParam(paramName::String, paramValue::Int) - ccall((:CLI_SetParamInt, library), Nothing, (Cstring, Int64), paramName, + ccall((:CLI_SetParamInt, library), Nothing, (Cstring, Int), paramName, paramValue); end @@ -89,15 +89,15 @@ function CLISetParamMat(paramName::String, paramValue, pointsAsRows::Bool) paramMat = to_matrix(paramValue, Float64) - ccall((:CLI_SetParamMat, library), Nothing, (Cstring, Ptr{Float64}, UInt64, - UInt64, Bool), paramName, Base.pointer(paramMat), size(paramMat, 1), + ccall((:CLI_SetParamMat, library), Nothing, (Cstring, Ptr{Float64}, Csize_t, + Csize_t, Bool), paramName, Base.pointer(paramMat), size(paramMat, 1), size(paramMat, 2), pointsAsRows); end function CLISetParamUMat(paramName::String, paramValue, pointsAsRows::Bool) - paramMat = to_matrix(paramValue, Int64) + paramMat = to_matrix(paramValue, Int) # Sanity check. if minimum(paramMat) <= 0 @@ -105,9 +105,9 @@ function CLISetParamUMat(paramName::String, "Must be 1 or greater.")) end - m = convert(Array{UInt64, 2}, paramMat .- 1) - ccall((:CLI_SetParamUMat, library), Nothing, (Cstring, Ptr{UInt64}, UInt64, - UInt64, Bool), paramName, Base.pointer(m), size(paramValue, 1), + m = convert(Array{Csize_t, 2}, paramMat .- 1) + ccall((:CLI_SetParamUMat, library), Nothing, (Cstring, Ptr{Csize_t}, Csize_t, + Csize_t, Bool), paramName, Base.pointer(m), size(paramValue, 1), size(paramValue, 2), pointsAsRows); end @@ -117,25 +117,25 @@ function CLISetParam(paramName::String, # sequentially. I am not sure if this is fully necessary but I have some # reservations about Julia's support for passing arrays of strings correctly # as a const char**. - ccall((:CLI_SetParamVectorStrLen, library), Nothing, (Cstring, UInt64), + ccall((:CLI_SetParamVectorStrLen, library), Nothing, (Cstring, Csize_t), paramName, size(vector, 1)); for i in 1:size(vector, 1) ccall((:CLI_SetParamVectorStrStr, library), Nothing, (Cstring, Cstring, - UInt64), paramName, vector[i], i .- 1); + Csize_t), paramName, vector[i], i .- 1); end end function CLISetParam(paramName::String, - vector::Vector{Int64}) - ccall((:CLI_SetParamVectorInt, library), Nothing, (Cstring, Ptr{Int64}, - Int64), paramName, Base.pointer(vector), size(vector, 1)); + vector::Vector{Int}) + ccall((:CLI_SetParamVectorInt, library), Nothing, (Cstring, Ptr{Int}, + Int), paramName, Base.pointer(vector), size(vector, 1)); end function CLISetParam(paramName::String, matWithInfo::Tuple{Array{Bool, 1}, Array{Float64, 2}}, pointsAsRows::Bool) ccall((:CLI_SetParamMatWithInfo, library), Nothing, (Cstring, Ptr{Bool}, - Ptr{Float64}, Int64, Int64, Bool), paramName, + Ptr{Float64}, Int, Int, Bool), paramName, Base.pointer(matWithInfo[1]), Base.pointer(matWithInfo[2]), size(matWithInfo[2], 1), size(matWithInfo[2], 2), pointsAsRows); end @@ -143,44 +143,44 @@ end function CLISetParamRow(paramName::String, paramValue) paramVec = to_vector(paramValue, Float64) - ccall((:CLI_SetParamRow, library), Nothing, (Cstring, Ptr{Float64}, UInt64), + ccall((:CLI_SetParamRow, library), Nothing, (Cstring, Ptr{Float64}, Csize_t), paramName, Base.pointer(paramVec), size(paramVec, 1)); end function CLISetParamCol(paramName::String, paramValue) paramVec = to_vector(paramValue, Float64) - ccall((:CLI_SetParamCol, library), Nothing, (Cstring, Ptr{Float64}, UInt64), + ccall((:CLI_SetParamCol, library), Nothing, (Cstring, Ptr{Float64}, Csize_t), paramName, Base.pointer(paramVec), size(paramVec, 1)); end function CLISetParamURow(paramName::String, paramValue) - paramVec = to_vector(paramValue, Int64) + paramVec = to_vector(paramValue, Int) # Sanity check. if minimum(paramVec) <= 0 throw(DomainError("Input $(paramName) cannot have 0 or negative values! " * "Must be 1 or greater.")) end - m = convert(Array{UInt64, 1}, paramVec .- 1) + m = convert(Array{Csize_t, 1}, paramVec .- 1) - ccall((:CLI_SetParamURow, library), Nothing, (Cstring, Ptr{UInt64}, UInt64), + ccall((:CLI_SetParamURow, library), Nothing, (Cstring, Ptr{Csize_t}, Csize_t), paramName, Base.pointer(m), size(paramValue, 1)); end function CLISetParamUCol(paramName::String, paramValue) - paramVec = to_vector(paramValue, Int64) + paramVec = to_vector(paramValue, Int) # Sanity check. if minimum(paramVec) <= 0 throw(DomainError("Input $(paramName) cannot have 0 or negative values! " * "Must be 1 or greater.")) end - m = convert(Array{UInt64, 1}, paramValue .- 1) + m = convert(Array{Csize_t, 1}, paramValue .- 1) - ccall((:CLI_SetParamUCol, library), Nothing, (Cstring, Ptr{UInt64}, UInt64), + ccall((:CLI_SetParamUCol, library), Nothing, (Cstring, Ptr{Csize_t}, Csize_t), paramName, Base.pointer(m), size(paramValue, 1)); end @@ -189,7 +189,7 @@ function CLIGetParamBool(paramName::String) end function CLIGetParamInt(paramName::String) - return ccall((:CLI_GetParamInt, library), Int64, (Cstring,), paramName) + return ccall((:CLI_GetParamInt, library), Int, (Cstring,), paramName) end function CLIGetParamDouble(paramName::String) @@ -201,15 +201,15 @@ function CLIGetParamString(paramName::String) end function CLIGetParamVectorStr(paramName::String) - local size::UInt64 + local size::Csize_t local ptr::Ptr{String} # Get the size of the vector, then each element. - size = ccall((:CLI_GetParamVectorStrLen, library), UInt64, (Cstring,), + size = ccall((:CLI_GetParamVectorStrLen, library), Csize_t, (Cstring,), paramName); out = Array{String, 1}() for i = 1:size - s = ccall((:CLI_GetParamVectorStrStr, library), Cstring, (Cstring, UInt64), + s = ccall((:CLI_GetParamVectorStrStr, library), Cstring, (Cstring, Csize_t), paramName, i .- 1) push!(out, Base.unsafe_string(s)) end @@ -218,28 +218,28 @@ function CLIGetParamVectorStr(paramName::String) end function CLIGetParamVectorInt(paramName::String) - local size::UInt64 - local ptr::Ptr{Int64} + local size::Csize_t + local ptr::Ptr{Int} # Get the size of the vector, then the pointer to it. We will own the # pointer. - size = ccall((:CLI_GetParamVectorIntLen, library), UInt64, (Cstring,), + size = ccall((:CLI_GetParamVectorIntLen, library), Csize_t, (Cstring,), paramName); - ptr = ccall((:CLI_GetParamVectorIntPtr, library), Ptr{Int64}, (Cstring,), + ptr = ccall((:CLI_GetParamVectorIntPtr, library), Ptr{Int}, (Cstring,), paramName); - return Base.unsafe_wrap(Array{Int64, 1}, ptr, (size), own=true) + return Base.unsafe_wrap(Array{Int, 1}, ptr, (size), own=true) end function CLIGetParamMat(paramName::String, pointsAsRows::Bool) # Can we return different return types? For now let's restrict to a matrix to # make it easy... local ptr::Ptr{Float64} - local rows::UInt64, cols::UInt64; + local rows::Csize_t, cols::Csize_t; # I suppose it would be possible to do this all in one call, but this seems # easy enough. - rows = ccall((:CLI_GetParamMatRows, library), UInt64, (Cstring,), paramName); - cols = ccall((:CLI_GetParamMatCols, library), UInt64, (Cstring,), paramName); + rows = ccall((:CLI_GetParamMatRows, library), Csize_t, (Cstring,), paramName); + cols = ccall((:CLI_GetParamMatCols, library), Csize_t, (Cstring,), paramName); ptr = ccall((:CLI_GetParamMat, library), Ptr{Float64}, (Cstring,), paramName); if pointsAsRows @@ -255,30 +255,30 @@ end function CLIGetParamUMat(paramName::String, pointsAsRows::Bool) # Can we return different return types? For now let's restrict to a matrix to # make it easy... - local ptr::Ptr{UInt64} - local rows::UInt64, cols::UInt64; + local ptr::Ptr{Csize_t} + local rows::Csize_t, cols::Csize_t; # I suppose it would be possible to do this all in one call, but this seems # easy enough. - rows = ccall((:CLI_GetParamUMatRows, library), UInt64, (Cstring,), paramName); - cols = ccall((:CLI_GetParamUMatCols, library), UInt64, (Cstring,), paramName); - ptr = ccall((:CLI_GetParamUMat, library), Ptr{UInt64}, (Cstring,), paramName); + rows = ccall((:CLI_GetParamUMatRows, library), Csize_t, (Cstring,), paramName); + cols = ccall((:CLI_GetParamUMatCols, library), Csize_t, (Cstring,), paramName); + ptr = ccall((:CLI_GetParamUMat, library), Ptr{Csize_t}, (Cstring,), paramName); if pointsAsRows # In this case we have to transpose, unfortunately. - m = Base.unsafe_wrap(Array{UInt64, 2}, ptr, (rows, cols), own=true); - return convert(Array{Int64, 2}, m' .+ 1) # Add 1 because these are indexes. + m = Base.unsafe_wrap(Array{Csize_t, 2}, ptr, (rows, cols), own=true); + return convert(Array{Int, 2}, m' .+ 1) # Add 1 because these are indexes. else # Here no transpose is necessary. - m = Base.unsafe_wrap(Array{UInt64, 2}, ptr, (rows, cols), own=true); - return convert(Array{Int64, 2}, m .+ 1) + m = Base.unsafe_wrap(Array{Csize_t, 2}, ptr, (rows, cols), own=true); + return convert(Array{Int, 2}, m .+ 1) end end function CLIGetParamCol(paramName::String) local ptr::Ptr{Float64}; - local rows::UInt64; + local rows::Csize_t; - rows = ccall((:CLI_GetParamColRows, library), UInt64, (Cstring,), paramName); + rows = ccall((:CLI_GetParamColRows, library), Csize_t, (Cstring,), paramName); ptr = ccall((:CLI_GetParamCol, library), Ptr{Float64}, (Cstring,), paramName); return Base.unsafe_wrap(Array{Float64, 1}, ptr, rows, own=true); @@ -286,45 +286,45 @@ end function CLIGetParamRow(paramName::String) local ptr::Ptr{Float64}; - local cols::UInt64; + local cols::Csize_t; - cols = ccall((:CLI_GetParamRowCols, library), UInt64, (Cstring,), paramName); + cols = ccall((:CLI_GetParamRowCols, library), Csize_t, (Cstring,), paramName); ptr = ccall((:CLI_GetParamRow, library), Ptr{Float64}, (Cstring,), paramName); return Base.unsafe_wrap(Array{Float64, 1}, ptr, cols, own=true); end function CLIGetParamUCol(paramName::String) - local ptr::Ptr{UInt64}; - local rows::UInt64; + local ptr::Ptr{Csize_t}; + local rows::Csize_t; - rows = ccall((:CLI_GetParamUColRows, library), UInt64, (Cstring,), paramName); - ptr = ccall((:CLI_GetParamUCol, library), Ptr{UInt64}, (Cstring,), paramName); + rows = ccall((:CLI_GetParamUColRows, library), Csize_t, (Cstring,), paramName); + ptr = ccall((:CLI_GetParamUCol, library), Ptr{Csize_t}, (Cstring,), paramName); - m = Base.unsafe_wrap(Array{UInt64, 1}, ptr, rows, own=true); - return convert(Array{Int64, 1}, m .+ 1) + m = Base.unsafe_wrap(Array{Csize_t, 1}, ptr, rows, own=true); + return convert(Array{Int, 1}, m .+ 1) end function CLIGetParamURow(paramName::String) - local ptr::Ptr{UInt64}; - local cols::UInt64; + local ptr::Ptr{Csize_t}; + local cols::Csize_t; - cols = ccall((:CLI_GetParamURowCols, library), UInt64, (Cstring,), paramName); - ptr = ccall((:CLI_GetParamURow, library), Ptr{UInt64}, (Cstring,), paramName); + cols = ccall((:CLI_GetParamURowCols, library), Csize_t, (Cstring,), paramName); + ptr = ccall((:CLI_GetParamURow, library), Ptr{Csize_t}, (Cstring,), paramName); - m = Base.unsafe_wrap(Array{UInt64, 1}, ptr, cols, own=true); - return convert(Array{Int64, 1}, m .+ 1) + m = Base.unsafe_wrap(Array{Csize_t, 1}, ptr, cols, own=true); + return convert(Array{Int, 1}, m .+ 1) end function CLIGetParamMatWithInfo(paramName::String, pointsAsRows::Bool) local ptrBool::Ptr{Bool}; local ptrData::Ptr{Float64}; - local rows::UInt64; - local cols::UInt64; + local rows::Csize_t; + local cols::Csize_t; - rows = ccall((:CLI_GetParamMatWithInfoRows, library), UInt64, (Cstring,), + rows = ccall((:CLI_GetParamMatWithInfoRows, library), Csize_t, (Cstring,), paramName); - cols = ccall((:CLI_GetParamMatWithInfoCols, library), UInt64, (Cstring,), + cols = ccall((:CLI_GetParamMatWithInfoCols, library), Csize_t, (Cstring,), paramName); ptrBool = ccall((:CLI_GetParamMatWithInfoBoolPtr, library), Ptr{Bool}, (Cstring,), paramName); diff --git a/src/mlpack/bindings/julia/print_doc_functions_impl.hpp b/src/mlpack/bindings/julia/print_doc_functions_impl.hpp index c3c20ffffc..8fb2534f72 100644 --- a/src/mlpack/bindings/julia/print_doc_functions_impl.hpp +++ b/src/mlpack/bindings/julia/print_doc_functions_impl.hpp @@ -124,7 +124,7 @@ inline std::string CreateInputArguments(const std::string& paramName, d.cppType == "arma::Col") { oss << "julia> " << value << " = CSV.read(\"" << value - << ".csv\"; type=Int64)" << std::endl; + << ".csv\"; type=Int)" << std::endl; } } diff --git a/src/mlpack/bindings/julia/print_type_doc_impl.hpp b/src/mlpack/bindings/julia/print_type_doc_impl.hpp index 9e81099bfd..e6522d36d2 100644 --- a/src/mlpack/bindings/julia/print_type_doc_impl.hpp +++ b/src/mlpack/bindings/julia/print_type_doc_impl.hpp @@ -109,15 +109,15 @@ std::string PrintTypeDoc( if (T::is_col || T::is_row) { return "A 1-d vector-like containing `Int` data (elements should be " - "greater than or equal to 0). Could be an `Array{Int64, 1}`, an " - "`Array{Int64, 2}` with one dimension of size 1, or anything " - "convertible to `Array{Int64, 1}`."; + "greater than or equal to 0). Could be an `Array{Int, 1}`, an " + "`Array{Int, 2}` with one dimension of size 1, or anything " + "convertible to `Array{Int, 1}`."; } else { return "A 2-d matrix-like containing `Int` data (elements should be " - "greater than or equal to 0). Could be an `Array{Int64, 2}` or a " - "`DataFrame` or anything convertible to an `Array{Int64, 2}`. It is " + "greater than or equal to 0). Could be an `Array{Int, 2}` or a " + "`DataFrame` or anything convertible to an `Array{Int, 2}`. It is " "expected that each row of the matrix corresponds to a data point, " "unless `points_are_rows` is set to `false` when calling mlpack " "bindings."; diff --git a/src/mlpack/bindings/julia/tests/runtests.jl b/src/mlpack/bindings/julia/tests/runtests.jl index 616a0c2804..7a859063a0 100644 --- a/src/mlpack/bindings/julia/tests/runtests.jl +++ b/src/mlpack/bindings/julia/tests/runtests.jl @@ -129,7 +129,7 @@ end # Same as TestMatrix but with an unsigned matrix. @testset "TestUMatrix" begin # Generate a random matrix of integers. - x = convert(Array{Int64, 2}, rand(1:500, (100, 5))) + x = convert(Array{Int, 2}, rand(1:500, (100, 5))) _, _, _, _, _, _, _, _, _, _, _, umatOut, _, _ = test_julia_binding(4.0, 12, "hello", @@ -138,7 +138,7 @@ end @test size(umatOut, 1) == 100 @test size(umatOut, 2) == 4 - @test typeof(umatOut[1, 1]) == Int64 + @test typeof(umatOut[1, 1]) == Int for i in [0, 1, 3] for j in 1:100 @test umatOut[j, i + 1] == x[j, i + 1] @@ -155,7 +155,7 @@ end # Same as TestMatrix but with an unsigned column major matrix. @testset "TestUMatrixColMajor" begin # Generate a random matrix of integers. - x = convert(Array{Int64, 2}, rand(1:500, (5, 100))) + x = convert(Array{Int, 2}, rand(1:500, (5, 100))) _, _, _, _, _, _, _, _, _, _, _, umatOut, _, _ = test_julia_binding(4.0, 12, "hello", @@ -164,7 +164,7 @@ end @test size(umatOut, 1) == 4 @test size(umatOut, 2) == 100 - @test typeof(umatOut[1, 1]) == Int64 + @test typeof(umatOut[1, 1]) == Int for i in 1:100 for j in [0, 1, 3] @test umatOut[j + 1, i] == x[j + 1, i] @@ -196,14 +196,14 @@ end # Test an unsigned column vector input parameter. @testset "TestUCol" begin - x = convert(Array{Int64, 1}, rand(1:500, 100)) + x = convert(Array{Int, 1}, rand(1:500, 100)) _, _, _, _, _, _, _, _, _, _, ucolOut, _, _, _ = test_julia_binding(4.0, 12, "hello", ucol_in=x) @test size(ucolOut, 1) == 100 - @test typeof(ucolOut) == Array{Int64, 1} + @test typeof(ucolOut) == Array{Int, 1} for i in 1:100 # Since we subtract one when we convert to C++, and then add one when we # convert back, we get a slightly different result here. @@ -228,14 +228,14 @@ end # Test an unsigned row vector input parameter. @testset "TestURow" begin - x = convert(Array{Int64, 1}, rand(1:500, 100)) + x = convert(Array{Int, 1}, rand(1:500, 100)) _, _, _, _, _, _, _, _, _, _, _, _, urowOut, _ = test_julia_binding(4.0, 12, "hello", urow_in=x) @test size(urowOut, 1) == 100 - @test typeof(urowOut) == Array{Int64, 1} + @test typeof(urowOut) == Array{Int, 1} for i in 1:100 # Since we subtract one when we convert to C++, and then add one when we # convert back, we get a slightly different result here. From 758cb5a6811a012188db05ad07c5e5dd85d8e932 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Fri, 6 Mar 2020 08:33:55 -0500 Subject: [PATCH 062/265] Oops, fix declaration of s. --- src/mlpack/bindings/markdown/print_doc_functions_impl.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp b/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp index 8ed10c97e6..0e9da65798 100644 --- a/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp +++ b/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp @@ -451,6 +451,7 @@ inline std::string PrintModel(const std::string& model) template std::string ProgramCall(const std::string& programName, Args... args) { + std::string s; if (BindingInfo::Language() == "cli") { s += "```bash\n"; From c85d7ec5e9c4b2e78491597b8a4f0130c1705a3a Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Fri, 6 Mar 2020 17:28:53 -0500 Subject: [PATCH 063/265] Switch int64_t to int. --- src/mlpack/bindings/julia/julia_util.cpp | 14 +++++++------- src/mlpack/bindings/julia/julia_util.h | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/mlpack/bindings/julia/julia_util.cpp b/src/mlpack/bindings/julia/julia_util.cpp index 81b17155cd..268aadd452 100644 --- a/src/mlpack/bindings/julia/julia_util.cpp +++ b/src/mlpack/bindings/julia/julia_util.cpp @@ -24,9 +24,9 @@ void CLI_RestoreSettings(const char* programName) /** * Call CLI::SetParam(). Julia always gives us an int64. */ -void CLI_SetParamInt(const char* paramName, int64_t paramValue) +void CLI_SetParamInt(const char* paramName, int paramValue) { - CLI::GetParam(paramName) = int(paramValue); + CLI::GetParam(paramName) = paramValue; CLI::SetPassed(paramName); } @@ -83,7 +83,7 @@ void CLI_SetParamVectorStrStr(const char* paramName, * Call CLI::SetParam>(). Julia always gives us int64s. */ void CLI_SetParamVectorInt(const char* paramName, - int64_t* ints, + int* ints, const size_t length) { // Create a std::vector object; unfortunately this requires copying the @@ -91,7 +91,7 @@ void CLI_SetParamVectorInt(const char* paramName, std::vector vec; vec.resize(length); for (size_t i = 0; i < length; ++i) - vec[i] = int(ints[i]); + vec[i] = ints[i]; CLI::GetParam>(paramName) = std::move(vec); CLI::SetPassed(paramName); @@ -205,9 +205,9 @@ void CLI_SetParamMatWithInfo(const char* paramName, /** * Call CLI::GetParam(). */ -int64_t CLI_GetParamInt(const char* paramName) +int CLI_GetParamInt(const char* paramName) { - return int64_t(CLI::GetParam(paramName)); + return CLI::GetParam(paramName); } /** @@ -246,7 +246,7 @@ size_t CLI_GetParamVectorStrLen(const char* paramName) /** * Call CLI::GetParam>() and get the i'th string. */ -const char* CLI_GetParamVectorStrStr(const char* paramName, const int64_t i) +const char* CLI_GetParamVectorStrStr(const char* paramName, const int i) { return CLI::GetParam>(paramName)[int(i)].c_str(); } diff --git a/src/mlpack/bindings/julia/julia_util.h b/src/mlpack/bindings/julia/julia_util.h index 9e15601640..5039f8c0c9 100644 --- a/src/mlpack/bindings/julia/julia_util.h +++ b/src/mlpack/bindings/julia/julia_util.h @@ -27,7 +27,7 @@ void CLI_RestoreSettings(const char* programName); /** * Call CLI::SetParam(). */ -void CLI_SetParamInt(const char* paramName, int64_t paramValue); +void CLI_SetParamInt(const char* paramName, int paramValue); /** * Call CLI::SetParam(). @@ -61,7 +61,7 @@ void CLI_SetParamVectorStrStr(const char* paramName, * Call CLI::SetParam>(). */ void CLI_SetParamVectorInt(const char* paramName, - int64_t* ints, + int* ints, const size_t length); /** @@ -123,7 +123,7 @@ void CLI_SetParamMatWithInfo(const char* paramName, /** * Call CLI::GetParam(). */ -int64_t CLI_GetParamInt(const char* paramName); +int CLI_GetParamInt(const char* paramName); /** * Call CLI::GetParam(). @@ -149,7 +149,7 @@ size_t CLI_GetParamVectorStrLen(const char* paramName); /** * Call CLI::GetParam>() and get the i'th string. */ -const char* CLI_GetParamVectorStrStr(const char* paramName, const int64_t i); +const char* CLI_GetParamVectorStrStr(const char* paramName, const int i); /** * Call CLI::GetParam>() and get the length of the vector. From 7ddf7ba366137f2f6b2b107769613fb631f4b5de Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Fri, 6 Mar 2020 17:29:45 -0500 Subject: [PATCH 064/265] Remove inaccurate comments. --- src/mlpack/bindings/julia/julia_util.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/bindings/julia/julia_util.cpp b/src/mlpack/bindings/julia/julia_util.cpp index 268aadd452..f2138e91c2 100644 --- a/src/mlpack/bindings/julia/julia_util.cpp +++ b/src/mlpack/bindings/julia/julia_util.cpp @@ -22,7 +22,7 @@ void CLI_RestoreSettings(const char* programName) } /** - * Call CLI::SetParam(). Julia always gives us an int64. + * Call CLI::SetParam(). */ void CLI_SetParamInt(const char* paramName, int paramValue) { @@ -80,7 +80,7 @@ void CLI_SetParamVectorStrStr(const char* paramName, } /** - * Call CLI::SetParam>(). Julia always gives us int64s. + * Call CLI::SetParam>(). */ void CLI_SetParamVectorInt(const char* paramName, int* ints, From cab675bd21d41c7c70bf34b94f847598b4f17506 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Fri, 6 Mar 2020 17:32:12 -0500 Subject: [PATCH 065/265] Fix a few other minor issues. --- src/mlpack/bindings/julia/julia_util.cpp | 12 ++++++------ src/mlpack/bindings/julia/julia_util.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/mlpack/bindings/julia/julia_util.cpp b/src/mlpack/bindings/julia/julia_util.cpp index f2138e91c2..7d089bde50 100644 --- a/src/mlpack/bindings/julia/julia_util.cpp +++ b/src/mlpack/bindings/julia/julia_util.cpp @@ -248,7 +248,7 @@ size_t CLI_GetParamVectorStrLen(const char* paramName) */ const char* CLI_GetParamVectorStrStr(const char* paramName, const int i) { - return CLI::GetParam>(paramName)[int(i)].c_str(); + return CLI::GetParam>(paramName)[i].c_str(); } /** @@ -264,13 +264,13 @@ size_t CLI_GetParamVectorIntLen(const char* paramName) * The vector will be created in-place and it is expected that the calling * function will take ownership. */ -size_t* CLI_GetParamVectorIntPtr(const char* paramName) +int* CLI_GetParamVectorIntPtr(const char* paramName) { const size_t size = CLI::GetParam>(paramName).size(); - size_t* ints = new size_t[size]; + int* ints = new int[size]; for (size_t i = 0; i < size; ++i) - ints[i] = size_t(CLI::GetParam>(paramName)[i]); + ints[i] = CLI::GetParam>(paramName)[i]; return ints; } @@ -493,7 +493,7 @@ size_t* CLI_GetParamURow(const char* paramName) */ size_t CLI_GetParamMatWithInfoRows(const char* paramName) { - return std::get<1>( CLI::GetParam>( + return std::get<1>(CLI::GetParam>( paramName)).n_rows; } @@ -502,7 +502,7 @@ size_t CLI_GetParamMatWithInfoRows(const char* paramName) */ size_t CLI_GetParamMatWithInfoCols(const char* paramName) { - return std::get<1>( CLI::GetParam>( + return std::get<1>(CLI::GetParam>( paramName)).n_cols; } diff --git a/src/mlpack/bindings/julia/julia_util.h b/src/mlpack/bindings/julia/julia_util.h index 5039f8c0c9..87111c99e1 100644 --- a/src/mlpack/bindings/julia/julia_util.h +++ b/src/mlpack/bindings/julia/julia_util.h @@ -161,7 +161,7 @@ size_t CLI_GetParamVectorIntLen(const char* paramName); * The vector will be created in-place and it is expected that the calling * function will take ownership. */ -size_t* CLI_GetParamVectorIntPtr(const char* paramName); +int* CLI_GetParamVectorIntPtr(const char* paramName); /** * Get the number of rows in a matrix parameter. From 491e0308421525a0f313fcce5bde7e160ff0fabd Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Sat, 7 Mar 2020 12:37:13 +0200 Subject: [PATCH 066/265] Implemented margin ranking loss --- .../methods/ann/loss_functions/CMakeLists.txt | 2 + .../loss_functions/margin_ranking_loss.hpp | 96 +++++++++++++++++++ .../margin_ranking_loss_impl.hpp | 74 ++++++++++++++ src/mlpack/tests/loss_functions_test.cpp | 54 +++++++++++ 4 files changed, 226 insertions(+) create mode 100644 src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp create mode 100644 src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp diff --git a/src/mlpack/methods/ann/loss_functions/CMakeLists.txt b/src/mlpack/methods/ann/loss_functions/CMakeLists.txt index 5a74ca4a2e..d7e311bf55 100644 --- a/src/mlpack/methods/ann/loss_functions/CMakeLists.txt +++ b/src/mlpack/methods/ann/loss_functions/CMakeLists.txt @@ -9,6 +9,8 @@ set(SOURCES earth_mover_distance_impl.hpp kl_divergence.hpp kl_divergence_impl.hpp + margin_ranking_loss.hpp + margin_ranking_loss_impl.hpp mean_bias_error.hpp mean_bias_error_impl.hpp mean_squared_error.hpp diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp new file mode 100644 index 0000000000..764c884bea --- /dev/null +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -0,0 +1,96 @@ +/** + * @file margin_ranking_loss.hpp + * @author Andrei Mihalea + * + * Definition of the Margin Ranking Loss function. + * + * 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_ANN_LOSS_FUNCTION_MARGIN_RANKING_LOSS_HPP +#define MLPACK_ANN_LOSS_FUNCTION_MARGIN_RANKING_LOSS_HPP + +#include + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +template < + typename InputDataType = arma::mat, + typename OutputDataType = arma::mat +> +class MarginRankingLoss +{ + public: + /** + * Create the MarginRankingLoss object with Hyperparameter margin. + */ + MarginRankingLoss(const double margin = 1.0); + + /** + * Computes the Margin Ranking Loss function. + * Measures the loss between two intputs and a label with -1 and 1 values. + * a value of 1 in the label means the first input should be ranked higher + * and a value of -1 means the second input should be ranked higher. + * + * @param x1 First input data used for evaluating the specified function. + * @param x2 Second input data used for evaluating the specified function. + * @param y The label vector which contains -1 or 1 values. + */ + template + double Forward(const FirstInputType&& x1, + const SecondInputType&& x2, + const ThirdInputType&& y); + + /** + * Ordinary feed backward pass of a neural network. + * + * @param x1 The propagated first input activation. + * @param x2 The propagated second input activation. + * @param y The label vector which contains -1 or 1 values. + * @param output The calculated error. + */ + template < + typename FirstInputType, + typename SecondInputType, + typename ThirdInputType, + typename OutputType + > + void Backward(const FirstInputType&& x1, + const SecondInputType&& x2, + const ThirdInputType&& y, + OutputType&& output); + + //! Get the output parameter. + OutputDataType& OutputParameter() const { return outputParameter; } + //! Modify the output parameter. + OutputDataType& OutputParameter() { return outputParameter; } + + //! Get the output parameter. + double Margin() const { return margin; } + //! Modify the output parameter. + double& Margin() { return margin; } + + /** + * Serialize the layer. + */ + template + void serialize(Archive& ar, const unsigned int /* version */); + + private: + //! Locally-stored output parameter object. + OutputDataType outputParameter; + + //! The margin value used in calculating Margin Ranking Loss. + double margin; +}; // class MarginRankingLoss + +} // namespace ann +} // namespace mlpack + +// include implementation. +#include "margin_ranking_loss_impl.hpp" + +#endif diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp new file mode 100644 index 0000000000..b36bb58212 --- /dev/null +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp @@ -0,0 +1,74 @@ +/** + * @file margin_ranking_loss_impl.hpp + * @author Andrei Mihalea + * + * Implementation of the Margin Ranking Loss function. + * + * 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_METHODS_ANN_LOSS_FUNCTION_MARGIN_IMPL_LOSS_HPP +#define MLPACK_METHODS_ANN_LOSS_FUNCTION_MARGIN_IMPL_LOSS_HPP + +// In case it hasn't been included. +#include "margin_ranking_loss.hpp" + +namespace mlpack { +namespace ann /** Artifical Neural Network. */ { + +template +MarginRankingLoss::MarginRankingLoss( + const double margin) : margin(margin) +{ + // Nothing to do here. +} + +template +template < + typename FirstInputType, + typename SecondInputType, + typename ThirdInputType +> +double MarginRankingLoss::Forward( + const FirstInputType&& x1, + const SecondInputType&& x2, + const ThirdInputType&& y) +{ + return arma::accu(arma::max(arma::zeros(size(y)), + -y % (x1 - x2) + margin)) / y.n_cols; +} + +template +template < + typename FirstInputType, + typename SecondInputType, + typename ThirdInputType, + typename OutputType +> +void MarginRankingLoss::Backward( + const FirstInputType&& x1, + const SecondInputType&& x2, + const ThirdInputType&& y, + OutputType&& output) +{ + output = -y % (x1 - x2) + margin; + output.elem(arma::find(output >= 0)).ones(); + output.elem(arma::find(output < 0)).zeros(); + output = (x2 - x1) % output / y.n_cols; +} + +template +template +void MarginRankingLoss::serialize( + Archive& ar, + const unsigned int /* version */) +{ + ar & BOOST_SERIALIZATION_NVP(margin); +} + +} // namespace ann +} // namespace mlpack + +#endif diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index 7988a961d0..ed08a844de 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -471,4 +472,57 @@ BOOST_AUTO_TEST_CASE(SimpleMeanBiasErrorTest) BOOST_REQUIRE_EQUAL(output.n_elem, 1); } +/* + * Simple test for the Margin Ranking Loss function. + */ +BOOST_AUTO_TEST_CASE(MarginRankingLossTest) +{ + arma::mat x1, x2, y, output; + MarginRankingLoss<> module; + + // Test the Forward function on a user generator input and compare it against + // the manually calculated result. + x1 = arma::mat("1 2 5 7 -1 -3"); + x2 = arma::mat("-1 3 -4 11 3 -3"); + y = arma::mat("1 -1 -1 1 -1 1"); + double error = module.Forward(std::move(x1), std::move(x2), std::move(y)); + // Computed using PyTorch + // >>> import torch + // >>> import torch.nn.functional as F + // >>> x1 = torch.tensor([1., 2., 5., 7., -1., -3.]) + // >>> x2 = torch.tensor([-1., 3., -4., 11., 3., -3.]) + // >>> y = torch.tensor([1., -1., -1., 1., -1., 1.], requires_grad=True) + // >>> loss = F.margin_ranking_loss(x1, x2, y, margin=1.) + // >>> loss.item() + // 2.6666667461395264 + // >>> y.grad + // tensor([-0.0000, 0.1667, -1.5000, 0.6667, 0.0000, -0.0000]) + BOOST_REQUIRE_CLOSE(error, 2.66667, 1e-3); + + // Test the Backward function. + module.Backward(std::move(x1), std::move(x2), std::move(y), + std::move(output)); + + CheckMatrices(output, arma::mat("-0.000000 0.166667 -1.500000 0.666667 " + "0.000000 -0.000000"), 1e-3); + BOOST_REQUIRE_EQUAL(output.n_rows, y.n_rows); + BOOST_REQUIRE_EQUAL(output.n_cols, y.n_cols); + + // Test the error function on another input. + x1 = arma::mat("0.4287 -1.6208 -1.5006 -0.4473 1.5208 -4.5184 9.3574 " + "-4.8090 4.3455 5.2070"); + x2 = arma::mat("-4.5288 -9.2766 -0.5882 -5.6643 -6.0175 8.8506 3.4759 " + "-9.4886 2.2755 8.4951"); + y = arma::mat("1 1 -1 1 -1 1 1 1 -1 1"); + error = module.Forward(std::move(x1), std::move(x2), std::move(y)); + BOOST_REQUIRE_CLOSE(error, 3.03530, 1e-3); + + // Test the Backward function on the second input. + module.Backward(std::move(x1), std::move(x2), std::move(y), + std::move(output)); + + CheckMatrices(output, arma::mat("0.000000 0.000000 0.091240 0.000000 " + "-0.753830 1.336900 0.000000 0.000000 -0.207000 0.328810"), 1e-6); +} + BOOST_AUTO_TEST_SUITE_END(); From a820fd919d86efe3fddcc38cc46b5e8920977967 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Sat, 7 Mar 2020 12:50:31 +0200 Subject: [PATCH 067/265] Pulled the latest update to loss_functions_test --- src/mlpack/tests/loss_functions_test.cpp | 41 +++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index ed08a844de..02f618bf75 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -23,8 +23,8 @@ #include #include #include -#include #include +#include #include #include @@ -472,6 +472,45 @@ BOOST_AUTO_TEST_CASE(SimpleMeanBiasErrorTest) BOOST_REQUIRE_EQUAL(output.n_elem, 1); } +/** + * Simple test for the Log-Hyperbolic-Cosine loss function. + */ +BOOST_AUTO_TEST_CASE(LogCoshLossTest) +{ + arma::mat input, target, output; + double loss; + LogCoshLoss<> module(2); + + // Test the Forward function. Loss should be 0 if input = target. + input = arma::ones(10, 1); + target = arma::ones(10, 1); + loss = module.Forward(std::move(input), std::move(target)); + BOOST_REQUIRE_EQUAL(loss, 0); + + // Test the Backward function for input = target. + module.Backward(std::move(input), std::move(target), std::move(output)); + for (double el : output) + { + // For input = target we should get 0.0 everywhere. + BOOST_REQUIRE_CLOSE(el, 0.0, 1e-5); + } + + BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); + BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); + + // Test the Forward function. Loss should be 0.546621. + input = arma::mat("1 2 3 4 5"); + target = arma::mat("1 2.4 3.4 4.2 5.5"); + loss = module.Forward(std::move(input), std::move(target)); + BOOST_REQUIRE_CLOSE(loss, 0.546621, 1e-3); + + // Test the Backward function. + module.Backward(std::move(input), std::move(target), std::move(output)); + BOOST_REQUIRE_CLOSE(arma::accu(output), 2.46962, 1e-3); + BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); + BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); +} + /* * Simple test for the Margin Ranking Loss function. */ From fa5f2e9d88458ecd1e0ae5d7a7c7450151a6f5ee Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Sat, 7 Mar 2020 12:57:35 +0200 Subject: [PATCH 068/265] Added the margin ranking loss import --- src/mlpack/tests/loss_functions_test.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index 02f618bf75..8db9a75917 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include From 6f72b588a7a515e7b1b4848ca7ea791727ca5811 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Sat, 7 Mar 2020 13:08:23 +0200 Subject: [PATCH 069/265] Solved some style checks --- .../methods/ann/loss_functions/margin_ranking_loss.hpp | 2 +- .../methods/ann/loss_functions/margin_ranking_loss_impl.hpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index 764c884bea..72198396df 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -93,4 +93,4 @@ class MarginRankingLoss // include implementation. #include "margin_ranking_loss_impl.hpp" -#endif +#endif diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp index b36bb58212..4950eb6b94 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp @@ -28,7 +28,7 @@ MarginRankingLoss::MarginRankingLoss( template template < typename FirstInputType, - typename SecondInputType, + typename SecondInputType, typename ThirdInputType > double MarginRankingLoss::Forward( @@ -36,7 +36,7 @@ double MarginRankingLoss::Forward( const SecondInputType&& x2, const ThirdInputType&& y) { - return arma::accu(arma::max(arma::zeros(size(y)), + return arma::accu(arma::max(arma::zeros(size(y)), -y % (x1 - x2) + margin)) / y.n_cols; } @@ -71,4 +71,4 @@ void MarginRankingLoss::serialize( } // namespace ann } // namespace mlpack -#endif +#endif From e0b76b7a595ee5cc92e381e575f0c951700f6044 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Sat, 7 Mar 2020 13:21:32 +0200 Subject: [PATCH 070/265] Solved the rest of style checks --- .../methods/ann/loss_functions/margin_ranking_loss.hpp | 7 ++++++- src/mlpack/tests/loss_functions_test.cpp | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index 72198396df..7f02c15ceb 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -39,7 +39,12 @@ class MarginRankingLoss * @param x2 Second input data used for evaluating the specified function. * @param y The label vector which contains -1 or 1 values. */ - template + template < + typename FirstInputType, + typename SecondInputType, + typename ThirdInputType + > + double Forward(const FirstInputType&& x1, const SecondInputType&& x2, const ThirdInputType&& y); diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index 8db9a75917..3136a1a0ca 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -540,7 +540,7 @@ BOOST_AUTO_TEST_CASE(MarginRankingLossTest) BOOST_REQUIRE_CLOSE(error, 2.66667, 1e-3); // Test the Backward function. - module.Backward(std::move(x1), std::move(x2), std::move(y), + module.Backward(std::move(x1), std::move(x2), std::move(y), std::move(output)); CheckMatrices(output, arma::mat("-0.000000 0.166667 -1.500000 0.666667 " @@ -560,7 +560,7 @@ BOOST_AUTO_TEST_CASE(MarginRankingLossTest) // Test the Backward function on the second input. module.Backward(std::move(x1), std::move(x2), std::move(y), std::move(output)); - + CheckMatrices(output, arma::mat("0.000000 0.000000 0.091240 0.000000 " "-0.753830 1.336900 0.000000 0.000000 -0.207000 0.328810"), 1e-6); } From 9949edc365ad1f55e586a33172ea53c071220436 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Sat, 7 Mar 2020 14:51:01 +0200 Subject: [PATCH 071/265] Added description for hyperparameter margin and a template description for input / output --- .../ann/loss_functions/margin_ranking_loss.hpp | 10 +++++++++- src/mlpack/tests/loss_functions_test.cpp | 12 +----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index 7f02c15ceb..cb0f7ca1d3 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -17,6 +17,13 @@ namespace mlpack { namespace ann /** Artificial Neural Network. */ { +/** + * @tparam InputDataType Type of the input data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + * @tparam OutputDataType Type of the output data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + */ + template < typename InputDataType = arma::mat, typename OutputDataType = arma::mat @@ -26,6 +33,8 @@ class MarginRankingLoss public: /** * Create the MarginRankingLoss object with Hyperparameter margin. + * Hyperparameter margin defines a minimum distance between correctly ranked + * samples. */ MarginRankingLoss(const double margin = 1.0); @@ -44,7 +53,6 @@ class MarginRankingLoss typename SecondInputType, typename ThirdInputType > - double Forward(const FirstInputType&& x1, const SecondInputType&& x2, const ThirdInputType&& y); diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index 3136a1a0ca..1640206357 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -526,17 +526,7 @@ BOOST_AUTO_TEST_CASE(MarginRankingLossTest) x2 = arma::mat("-1 3 -4 11 3 -3"); y = arma::mat("1 -1 -1 1 -1 1"); double error = module.Forward(std::move(x1), std::move(x2), std::move(y)); - // Computed using PyTorch - // >>> import torch - // >>> import torch.nn.functional as F - // >>> x1 = torch.tensor([1., 2., 5., 7., -1., -3.]) - // >>> x2 = torch.tensor([-1., 3., -4., 11., 3., -3.]) - // >>> y = torch.tensor([1., -1., -1., 1., -1., 1.], requires_grad=True) - // >>> loss = F.margin_ranking_loss(x1, x2, y, margin=1.) - // >>> loss.item() - // 2.6666667461395264 - // >>> y.grad - // tensor([-0.0000, 0.1667, -1.5000, 0.6667, 0.0000, -0.0000]) + // Computed using torch.nn.functional.margin_ranking_loss() BOOST_REQUIRE_CLOSE(error, 2.66667, 1e-3); // Test the Backward function. From e38ee128fec30a3e9a92a2e8eb6c24110a22a48d Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Sat, 7 Mar 2020 17:57:05 +0530 Subject: [PATCH 072/265] Added Tests for MaxPooling --- src/mlpack/tests/ann_layer_test.cpp | 85 +++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/src/mlpack/tests/ann_layer_test.cpp b/src/mlpack/tests/ann_layer_test.cpp index 136460c2f1..729f0f0640 100644 --- a/src/mlpack/tests/ann_layer_test.cpp +++ b/src/mlpack/tests/ann_layer_test.cpp @@ -3032,4 +3032,89 @@ BOOST_AUTO_TEST_CASE(TransposedConvolutionLayerPaddingTest) module6.Backward(std::move(input), std::move(output), std::move(delta)); BOOST_REQUIRE_EQUAL(arma::accu(delta), 0.0); } + +/** + * Simple test for Max Pooling layer. + */ +BOOST_AUTO_TEST_CASE(MaxPoolingTestCase) +{ + // For rectangular input. + arma::mat input = arma::mat(12, 1); + arma::mat output; + input.zeros(); + input(0) = 1; + input(1) = 2; + input(2) = 3; + input(3) = input(8) = 7; + input(4) = 4; + input(5) = 5; + input(6) = input(7) = 6; + input(10) = 8; + input(11) = 9; + // Output-Size should be 2 x 2. + // Square output. + MaxPooling<> module1(2, 2, 2, 1); + module1.InputHeight() = 3; + module1.InputWidth() = 4; + module1.Forward(std::move(input), std::move(output)); + // Calculated using torch.nn.MaxPool2d(). + BOOST_REQUIRE_EQUAL(arma::accu(output), 28); + BOOST_REQUIRE_EQUAL(output.n_elem, 4); + BOOST_REQUIRE_EQUAL(output.n_cols, 1); + + // For Square input. + input = arma::mat(9, 1); + input.zeros(); + input(0) = 6; + input(1) = 3; + input(2) = 9; + input(3) = 3; + input(6) = 3; + // Output-Size should be 1 x 2. + // Rectangular output. + MaxPooling<> module2(2, 2, 1, 1); + module2.InputHeight() = 3; + module2.InputWidth() = 3; + module2.Forward(std::move(input), std::move(output)); + // Calculated using torch.nn.MaxPool2d(). + BOOST_REQUIRE_EQUAL(arma::accu(output), 15.0); + BOOST_REQUIRE_EQUAL(output.n_elem, 2); + BOOST_REQUIRE_EQUAL(output.n_cols, 1); + + // For Square input. + input = arma::mat(16, 1); + input.zeros(); + input(0) = 6; + input(1) = 3; + input(2) = 9; + input(4) = 3; + input(8) = 3; + // Output-Size should be 3 x 3. + // Square output. + MaxPooling<> module3(2, 2, 1, 1); + module3.InputHeight() = 4; + module3.InputWidth() = 4; + module3.Forward(std::move(input), std::move(output)); + // Calculated using torch.nn.MaxPool2d(). + BOOST_REQUIRE_EQUAL(arma::accu(output), 30.0); + BOOST_REQUIRE_EQUAL(output.n_elem, 9); + BOOST_REQUIRE_EQUAL(output.n_cols, 1); + + // For Rectangular input. + input = arma::mat(6, 1); + input.zeros(); + input(0) = 1; + input(1) = 1; + input(3) = 1; + // Output-Size should be 2 x 2. + // Square output. + MaxPooling<> module4(2, 1, 1, 1); + module4.InputHeight() = 2; + module4.InputWidth() = 3; + module4.Forward(std::move(input), std::move(output)); + // Calculated using torch.nn.AdaptiveMaxPool2d(). + BOOST_REQUIRE_EQUAL(arma::accu(output), 3); + BOOST_REQUIRE_EQUAL(output.n_elem, 4); + BOOST_REQUIRE_EQUAL(output.n_cols, 1); +} BOOST_AUTO_TEST_SUITE_END(); From 2a3cd354bc92774a0b5d1aab24985bf084c7358b Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Sat, 7 Mar 2020 18:29:11 +0530 Subject: [PATCH 073/265] Changes in Pooling Layers --- src/mlpack/methods/ann/layer/max_pooling.hpp | 22 ++++++++++---------- src/mlpack/tests/ann_layer_test.cpp | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/mlpack/methods/ann/layer/max_pooling.hpp b/src/mlpack/methods/ann/layer/max_pooling.hpp index e0793c2ba2..71cfb9b524 100644 --- a/src/mlpack/methods/ann/layer/max_pooling.hpp +++ b/src/mlpack/methods/ann/layer/max_pooling.hpp @@ -179,29 +179,29 @@ class MaxPooling arma::Mat& output, arma::Mat& poolingIndices) { + const size_t rStep = kernelWidth; + const size_t cStep = kernelHeight; for (size_t j = 0, colidx = 0; j < output.n_cols; - ++j, colidx += strideWidth) + ++j, colidx += strideHeight) { for (size_t i = 0, rowidx = 0; i < output.n_rows; - ++i, rowidx += strideHeight) + ++i, rowidx += strideWidth) { arma::mat subInput = input( - arma::span(rowidx, rowidx + kernelWidth - 1 - offset), - arma::span(colidx, colidx + kernelHeight - 1 - offset)); - + arma::span(rowidx, rowidx + rStep - 1 - offset), + arma::span(colidx, colidx + cStep - 1 - offset)); const size_t idx = pooling.Pooling(subInput); output(i, j) = subInput(idx); if (!deterministic) { arma::Mat subIndices = indices(arma::span(rowidx, - rowidx + kernelWidth - 1 - offset), - arma::span(colidx, colidx + kernelHeight - 1 - offset)); - + rowidx + rStep - 1), + arma::span(colidx, colidx + cStep - 1)); poolingIndices(i, j) = subIndices(idx); - } - } - } + } + } + } } /** diff --git a/src/mlpack/tests/ann_layer_test.cpp b/src/mlpack/tests/ann_layer_test.cpp index 729f0f0640..c4d09a199c 100644 --- a/src/mlpack/tests/ann_layer_test.cpp +++ b/src/mlpack/tests/ann_layer_test.cpp @@ -3072,12 +3072,12 @@ BOOST_AUTO_TEST_CASE(MaxPoolingTestCase) input(6) = 3; // Output-Size should be 1 x 2. // Rectangular output. - MaxPooling<> module2(2, 2, 1, 1); + MaxPooling<> module2(3, 2, 3, 1); module2.InputHeight() = 3; module2.InputWidth() = 3; module2.Forward(std::move(input), std::move(output)); // Calculated using torch.nn.MaxPool2d(). - BOOST_REQUIRE_EQUAL(arma::accu(output), 15.0); + BOOST_REQUIRE_EQUAL(arma::accu(output), 12.0); BOOST_REQUIRE_EQUAL(output.n_elem, 2); BOOST_REQUIRE_EQUAL(output.n_cols, 1); From 98b09fd08cf2063ab2598d534d1a5a95431dc745 Mon Sep 17 00:00:00 2001 From: kartikdutt18 <39593019+kartikdutt18@users.noreply.github.com> Date: Sat, 7 Mar 2020 18:34:51 +0530 Subject: [PATCH 074/265] Typo fix. --- src/mlpack/tests/ann_layer_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/tests/ann_layer_test.cpp b/src/mlpack/tests/ann_layer_test.cpp index c4d09a199c..47d2e3baa8 100644 --- a/src/mlpack/tests/ann_layer_test.cpp +++ b/src/mlpack/tests/ann_layer_test.cpp @@ -3112,7 +3112,7 @@ BOOST_AUTO_TEST_CASE(MaxPoolingTestCase) module4.InputHeight() = 2; module4.InputWidth() = 3; module4.Forward(std::move(input), std::move(output)); - // Calculated using torch.nn.AdaptiveMaxPool2d(). + // Calculated using torch.nn.MaxPool2d(). BOOST_REQUIRE_EQUAL(arma::accu(output), 3); BOOST_REQUIRE_EQUAL(output.n_elem, 4); BOOST_REQUIRE_EQUAL(output.n_cols, 1); From 2dd1ad8a395abc6f1a772cdd76facc6085d5f1c3 Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Sun, 8 Mar 2020 10:48:43 +0530 Subject: [PATCH 075/265] Style Fix --- src/mlpack/tests/ann_layer_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/tests/ann_layer_test.cpp b/src/mlpack/tests/ann_layer_test.cpp index 47d2e3baa8..5d7e622235 100644 --- a/src/mlpack/tests/ann_layer_test.cpp +++ b/src/mlpack/tests/ann_layer_test.cpp @@ -3034,8 +3034,8 @@ BOOST_AUTO_TEST_CASE(TransposedConvolutionLayerPaddingTest) } /** - * Simple test for Max Pooling layer. - */ + * Simple test for Max Pooling layer. + */ BOOST_AUTO_TEST_CASE(MaxPoolingTestCase) { // For rectangular input. From d8c10c4f87afb562e993179fe57dfe288d3778ec Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Sun, 8 Mar 2020 11:02:25 +0530 Subject: [PATCH 076/265] Add offset --- src/mlpack/methods/ann/layer/max_pooling.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/ann/layer/max_pooling.hpp b/src/mlpack/methods/ann/layer/max_pooling.hpp index 71cfb9b524..7617474218 100644 --- a/src/mlpack/methods/ann/layer/max_pooling.hpp +++ b/src/mlpack/methods/ann/layer/max_pooling.hpp @@ -196,8 +196,8 @@ class MaxPooling if (!deterministic) { arma::Mat subIndices = indices(arma::span(rowidx, - rowidx + rStep - 1), - arma::span(colidx, colidx + cStep - 1)); + rowidx + rStep - 1 - offset), + arma::span(colidx, colidx + cStep - 1 - offset)); poolingIndices(i, j) = subIndices(idx); } } From 03a924cab6bfea1864bf75c51bc7eae19ff687da Mon Sep 17 00:00:00 2001 From: Saksham Date: Mon, 9 Mar 2020 08:12:31 +0530 Subject: [PATCH 077/265] links for references --- src/mlpack/methods/ann/layer/alpha_dropout.hpp | 3 ++- src/mlpack/methods/ann/layer/c_relu.hpp | 3 ++- src/mlpack/methods/ann/layer/dropconnect.hpp | 3 ++- src/mlpack/methods/ann/layer/dropout.hpp | 1 + src/mlpack/methods/ann/layer/elu.hpp | 6 ++++-- src/mlpack/methods/ann/layer/fast_lstm.hpp | 3 ++- src/mlpack/methods/ann/layer/glimpse.hpp | 1 + src/mlpack/methods/ann/layer/gru.hpp | 3 ++- src/mlpack/methods/ann/layer/recurrent_attention.hpp | 3 ++- src/mlpack/methods/ann/layer/reparametrization.hpp | 3 ++- src/mlpack/methods/ann/layer/weight_norm.hpp | 3 ++- 11 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/mlpack/methods/ann/layer/alpha_dropout.hpp b/src/mlpack/methods/ann/layer/alpha_dropout.hpp index 4edbf3031c..5d3a01c390 100644 --- a/src/mlpack/methods/ann/layer/alpha_dropout.hpp +++ b/src/mlpack/methods/ann/layer/alpha_dropout.hpp @@ -35,7 +35,8 @@ namespace ann /** Artificial Neural Network. */ { * Andreas Mayr}, * title = {Self-Normalizing Neural Networks}, * journal = {Advances in Neural Information Processing Systems}, - * year = {2017} + * year = {2017}, + * url = {https://arxiv.org/abs/1706.02515} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/c_relu.hpp b/src/mlpack/methods/ann/layer/c_relu.hpp index 5108646116..37358a534d 100644 --- a/src/mlpack/methods/ann/layer/c_relu.hpp +++ b/src/mlpack/methods/ann/layer/c_relu.hpp @@ -33,7 +33,8 @@ namespace ann /** Artificial Neural Network. */ { * title = {Understanding and Improving Convolutional Neural Networks * via Concatenated Rectified Linear Units}, * author = {LWenling Shang, Kihyuk Sohn, Diogo Almeida, Honglak Lee}, - * year = {2016} + * year = {2016}, + * url = {https://arxiv.org/abs/1603.05201} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/dropconnect.hpp b/src/mlpack/methods/ann/layer/dropconnect.hpp index 1ed6f2ce0c..b92b297afd 100644 --- a/src/mlpack/methods/ann/layer/dropconnect.hpp +++ b/src/mlpack/methods/ann/layer/dropconnect.hpp @@ -46,7 +46,8 @@ namespace ann /** Artificial Neural Network. */ { * Learning(ICML - 13)}, * author = {Li Wan and Matthew Zeiler and Sixin Zhang and Yann L. Cun and * Rob Fergus}, - * year = {2013} + * year = {2013}, + * url = {http://proceedings.mlr.press/v28/wan13.pdf} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/dropout.hpp b/src/mlpack/methods/ann/layer/dropout.hpp index 696d92869c..94bd288626 100644 --- a/src/mlpack/methods/ann/layer/dropout.hpp +++ b/src/mlpack/methods/ann/layer/dropout.hpp @@ -39,6 +39,7 @@ namespace ann /** Artificial Neural Network. */ { * journal = {CoRR}, * volume = {abs/1207.0580}, * year = {2012}, + * url = {https://arxiv.org/abs/1207.0580} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/elu.hpp b/src/mlpack/methods/ann/layer/elu.hpp index 575b99e4d5..58024c52d0 100644 --- a/src/mlpack/methods/ann/layer/elu.hpp +++ b/src/mlpack/methods/ann/layer/elu.hpp @@ -56,7 +56,8 @@ namespace ann /** Artificial Neural Network. */ { * title = {Fast and Accurate Deep Network Learning by Exponential Linear * Units (ELUs)}, * journal = {CoRR}, - * year = {2015} + * year = {2015}, + * url = {https://arxiv.org/abs/1511.07289} * } * @endcode * @@ -86,7 +87,8 @@ namespace ann /** Artificial Neural Network. */ { * Andreas Mayr}, * title = {Self-Normalizing Neural Networks}, * journal = {Advances in Neural Information Processing Systems}, - * year = {2017} + * year = {2017}, + * url = {https://arxiv.org/abs/1706.02515} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/fast_lstm.hpp b/src/mlpack/methods/ann/layer/fast_lstm.hpp index e73906a5ba..b4eeba634e 100644 --- a/src/mlpack/methods/ann/layer/fast_lstm.hpp +++ b/src/mlpack/methods/ann/layer/fast_lstm.hpp @@ -47,7 +47,8 @@ namespace ann /** Artificial Neural Network. */ { * author = {Hochreiter, Sepp and Schmidhuber, J\"{u}rgen}, * title = {Long Short-term Memory}, * journal = {Neural Comput.}, - * year = {1997} + * year = {1997}, + * url = {https://www.bioinf.jku.at/publications/older/2604.pdf} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/glimpse.hpp b/src/mlpack/methods/ann/layer/glimpse.hpp index 357fbce726..6a747c14e0 100644 --- a/src/mlpack/methods/ann/layer/glimpse.hpp +++ b/src/mlpack/methods/ann/layer/glimpse.hpp @@ -15,6 +15,7 @@ * journal = {CoRR}, * volume = {abs/1406.6247}, * year = {2014}, + * url = {https://arxiv.org/abs/1406.6247} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/gru.hpp b/src/mlpack/methods/ann/layer/gru.hpp index 63fee576be..cf60a0fb8c 100644 --- a/src/mlpack/methods/ann/layer/gru.hpp +++ b/src/mlpack/methods/ann/layer/gru.hpp @@ -13,7 +13,8 @@ Kyunghyun and Bengio, Yoshua}, * booktitle = {ICML}, * pages = {2067--2075}, - * year = {2015} + * year = {2015}, + * url = {https://arxiv.org/abs/1502.02367} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/recurrent_attention.hpp b/src/mlpack/methods/ann/layer/recurrent_attention.hpp index 087b6bdb6b..5b507e0b69 100644 --- a/src/mlpack/methods/ann/layer/recurrent_attention.hpp +++ b/src/mlpack/methods/ann/layer/recurrent_attention.hpp @@ -39,7 +39,8 @@ namespace ann /** Artificial Neural Network. */ { * author={Volodymyr Mnih, Nicolas Heess, Alex Graves, Koray Kavukcuoglu}, * journal={CoRR}, * volume={abs/1406.6247}, - * year={2014} + * year={2014}, + * url = {https://arxiv.org/abs/1406.6247} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/reparametrization.hpp b/src/mlpack/methods/ann/layer/reparametrization.hpp index 1fc299e2ba..2c56832ad7 100644 --- a/src/mlpack/methods/ann/layer/reparametrization.hpp +++ b/src/mlpack/methods/ann/layer/reparametrization.hpp @@ -39,7 +39,8 @@ namespace ann /** Artificial Neural Network. */ { * Xavier Glorot, Matthew Botvinick, Shakir Mohamed and * Alexander Lerchner | Google DeepMind}, * journal = {2017 International Conference on Learning Representations(ICLR)}, - * year = {2017} + * year = {2017}, + * url = {https://openreview.net/references/pdf?id=Sy2fzU9gl} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/weight_norm.hpp b/src/mlpack/methods/ann/layer/weight_norm.hpp index c4762c22d4..f5fd92c6cb 100644 --- a/src/mlpack/methods/ann/layer/weight_norm.hpp +++ b/src/mlpack/methods/ann/layer/weight_norm.hpp @@ -42,7 +42,8 @@ namespace ann /** Artificial Neural Network. */ { * Training of Deep Neural Networks}, * author = {Tim Salimans, Diederik P. Kingma}, * booktitle = {Neural Information Processing Systems 2016}, - * year = {2016} + * year = {2016}, + * url = {https://arxiv.org/abs/1602.07868}, * } * @endcode * From 012011e3a2ad34eac747fcf6247377318aaf189d Mon Sep 17 00:00:00 2001 From: Saksham Date: Mon, 9 Mar 2020 08:27:54 +0530 Subject: [PATCH 078/265] fixed formatting --- src/mlpack/methods/ann/layer/alpha_dropout.hpp | 2 +- src/mlpack/methods/ann/layer/c_relu.hpp | 2 +- src/mlpack/methods/ann/layer/dropconnect.hpp | 2 +- src/mlpack/methods/ann/layer/dropout.hpp | 2 +- src/mlpack/methods/ann/layer/elu.hpp | 2 +- src/mlpack/methods/ann/layer/fast_lstm.hpp | 2 +- src/mlpack/methods/ann/layer/glimpse.hpp | 2 +- src/mlpack/methods/ann/layer/gru.hpp | 2 +- src/mlpack/methods/ann/layer/recurrent_attention.hpp | 12 ++++++------ src/mlpack/methods/ann/layer/reparametrization.hpp | 2 +- src/mlpack/methods/ann/layer/weight_norm.hpp | 2 +- 11 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/mlpack/methods/ann/layer/alpha_dropout.hpp b/src/mlpack/methods/ann/layer/alpha_dropout.hpp index 5d3a01c390..4623dc11f6 100644 --- a/src/mlpack/methods/ann/layer/alpha_dropout.hpp +++ b/src/mlpack/methods/ann/layer/alpha_dropout.hpp @@ -36,7 +36,7 @@ namespace ann /** Artificial Neural Network. */ { * title = {Self-Normalizing Neural Networks}, * journal = {Advances in Neural Information Processing Systems}, * year = {2017}, - * url = {https://arxiv.org/abs/1706.02515} + * url = {https://arxiv.org/abs/1706.02515} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/c_relu.hpp b/src/mlpack/methods/ann/layer/c_relu.hpp index 37358a534d..9b839768fe 100644 --- a/src/mlpack/methods/ann/layer/c_relu.hpp +++ b/src/mlpack/methods/ann/layer/c_relu.hpp @@ -34,7 +34,7 @@ namespace ann /** Artificial Neural Network. */ { * via Concatenated Rectified Linear Units}, * author = {LWenling Shang, Kihyuk Sohn, Diogo Almeida, Honglak Lee}, * year = {2016}, - * url = {https://arxiv.org/abs/1603.05201} + * url = {https://arxiv.org/abs/1603.05201} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/dropconnect.hpp b/src/mlpack/methods/ann/layer/dropconnect.hpp index b92b297afd..4cacf33fce 100644 --- a/src/mlpack/methods/ann/layer/dropconnect.hpp +++ b/src/mlpack/methods/ann/layer/dropconnect.hpp @@ -47,7 +47,7 @@ namespace ann /** Artificial Neural Network. */ { * author = {Li Wan and Matthew Zeiler and Sixin Zhang and Yann L. Cun and * Rob Fergus}, * year = {2013}, - * url = {http://proceedings.mlr.press/v28/wan13.pdf} + * url = {http://proceedings.mlr.press/v28/wan13.pdf} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/dropout.hpp b/src/mlpack/methods/ann/layer/dropout.hpp index 94bd288626..878bf80ed7 100644 --- a/src/mlpack/methods/ann/layer/dropout.hpp +++ b/src/mlpack/methods/ann/layer/dropout.hpp @@ -39,7 +39,7 @@ namespace ann /** Artificial Neural Network. */ { * journal = {CoRR}, * volume = {abs/1207.0580}, * year = {2012}, - * url = {https://arxiv.org/abs/1207.0580} + * url = {https://arxiv.org/abs/1207.0580} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/elu.hpp b/src/mlpack/methods/ann/layer/elu.hpp index 58024c52d0..d0f7265370 100644 --- a/src/mlpack/methods/ann/layer/elu.hpp +++ b/src/mlpack/methods/ann/layer/elu.hpp @@ -57,7 +57,7 @@ namespace ann /** Artificial Neural Network. */ { * Units (ELUs)}, * journal = {CoRR}, * year = {2015}, - * url = {https://arxiv.org/abs/1511.07289} + * url = {https://arxiv.org/abs/1511.07289} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/fast_lstm.hpp b/src/mlpack/methods/ann/layer/fast_lstm.hpp index b4eeba634e..dfff05af1b 100644 --- a/src/mlpack/methods/ann/layer/fast_lstm.hpp +++ b/src/mlpack/methods/ann/layer/fast_lstm.hpp @@ -48,7 +48,7 @@ namespace ann /** Artificial Neural Network. */ { * title = {Long Short-term Memory}, * journal = {Neural Comput.}, * year = {1997}, - * url = {https://www.bioinf.jku.at/publications/older/2604.pdf} + * url = {https://www.bioinf.jku.at/publications/older/2604.pdf} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/glimpse.hpp b/src/mlpack/methods/ann/layer/glimpse.hpp index 6a747c14e0..b2a51a6f58 100644 --- a/src/mlpack/methods/ann/layer/glimpse.hpp +++ b/src/mlpack/methods/ann/layer/glimpse.hpp @@ -15,7 +15,7 @@ * journal = {CoRR}, * volume = {abs/1406.6247}, * year = {2014}, - * url = {https://arxiv.org/abs/1406.6247} + * url = {https://arxiv.org/abs/1406.6247} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/gru.hpp b/src/mlpack/methods/ann/layer/gru.hpp index cf60a0fb8c..9c8e6cf367 100644 --- a/src/mlpack/methods/ann/layer/gru.hpp +++ b/src/mlpack/methods/ann/layer/gru.hpp @@ -14,7 +14,7 @@ * booktitle = {ICML}, * pages = {2067--2075}, * year = {2015}, - * url = {https://arxiv.org/abs/1502.02367} + * url = {https://arxiv.org/abs/1502.02367} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/recurrent_attention.hpp b/src/mlpack/methods/ann/layer/recurrent_attention.hpp index 5b507e0b69..e764a7bab5 100644 --- a/src/mlpack/methods/ann/layer/recurrent_attention.hpp +++ b/src/mlpack/methods/ann/layer/recurrent_attention.hpp @@ -35,12 +35,12 @@ namespace ann /** Artificial Neural Network. */ { * * @code * @article{MnihHGK14, - * title={Recurrent Models of Visual Attention}, - * author={Volodymyr Mnih, Nicolas Heess, Alex Graves, Koray Kavukcuoglu}, - * journal={CoRR}, - * volume={abs/1406.6247}, - * year={2014}, - * url = {https://arxiv.org/abs/1406.6247} + * title = {Recurrent Models of Visual Attention}, + * author = {Volodymyr Mnih, Nicolas Heess, Alex Graves, Koray Kavukcuoglu}, + * journal = {CoRR}, + * volume = {abs/1406.6247}, + * year = {2014}, + * url = {https://arxiv.org/abs/1406.6247} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/reparametrization.hpp b/src/mlpack/methods/ann/layer/reparametrization.hpp index 2c56832ad7..bd1e20292d 100644 --- a/src/mlpack/methods/ann/layer/reparametrization.hpp +++ b/src/mlpack/methods/ann/layer/reparametrization.hpp @@ -40,7 +40,7 @@ namespace ann /** Artificial Neural Network. */ { * Alexander Lerchner | Google DeepMind}, * journal = {2017 International Conference on Learning Representations(ICLR)}, * year = {2017}, - * url = {https://openreview.net/references/pdf?id=Sy2fzU9gl} + * url = {https://openreview.net/references/pdf?id=Sy2fzU9gl} * } * @endcode * diff --git a/src/mlpack/methods/ann/layer/weight_norm.hpp b/src/mlpack/methods/ann/layer/weight_norm.hpp index f5fd92c6cb..1b68527118 100644 --- a/src/mlpack/methods/ann/layer/weight_norm.hpp +++ b/src/mlpack/methods/ann/layer/weight_norm.hpp @@ -43,7 +43,7 @@ namespace ann /** Artificial Neural Network. */ { * author = {Tim Salimans, Diederik P. Kingma}, * booktitle = {Neural Information Processing Systems 2016}, * year = {2016}, - * url = {https://arxiv.org/abs/1602.07868}, + * url = {https://arxiv.org/abs/1602.07868}, * } * @endcode * From c600714c413bb30cbd1d3591d22e7fefd3a14fc3 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Mon, 9 Mar 2020 08:55:51 +0200 Subject: [PATCH 079/265] retrigger checks; see if python27 passes now From b823643b91da8d00d803f8bb85b65034b317b985 Mon Sep 17 00:00:00 2001 From: Sriram Date: Mon, 9 Mar 2020 19:41:11 +0530 Subject: [PATCH 080/265] Redirected badge and removed info about NUMfocus --- README.md | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/README.md b/README.md index 17249b8d82..45d0cd88ef 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ src="https://cdn.rawgit.com/mlpack/mlpack.org/e7d36ed8/mlpack-black.svg" style=" Jenkins Coveralls License - NumFOCUS + NumFOCUS

@@ -34,21 +34,6 @@ functions as a "swiss army knife" for machine learning researchers. In addition to its powerful C++ interface, mlpack also provides command-line programs, Python bindings, and Julia bindings. -mlpack uses an [open governance model](./GOVERNANCE.md) and is fiscally -sponsored by [NumFOCUS](https://numfocus.org/). Consider making a -[tax-deductible donation](https://numfocus.org/donate-to-mlpack) to help the -project pay for developer time, professional services, travel, workshops, and a -variety of other needs. - -

-
- ### 0. Contents 1. [Introduction](#1-introduction) From 3a5f29b3500b526d3f1410fe0c63ca544a0095cd Mon Sep 17 00:00:00 2001 From: Sriram Date: Mon, 9 Mar 2020 19:56:09 +0530 Subject: [PATCH 081/265] Reverted deletion and added attribution line --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 45d0cd88ef..698803371a 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,23 @@ functions as a "swiss army knife" for machine learning researchers. In addition to its powerful C++ interface, mlpack also provides command-line programs, Python bindings, and Julia bindings. +[//]: # (numfocus-fiscal-sponsor-attribution) + +mlpack uses an [open governance model](./GOVERNANCE.md) and is fiscally +sponsored by [NumFOCUS](https://numfocus.org/). Consider making a +[tax-deductible donation](https://numfocus.org/donate-to-mlpack) to help the +project pay for developer time, professional services, travel, workshops, and a +variety of other needs. + + +
+ ### 0. Contents 1. [Introduction](#1-introduction) From 9f728e5bebd758e38b00b9ee08f9407b3353b392 Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Tue, 10 Mar 2020 11:52:13 +0530 Subject: [PATCH 082/265] Add involvement and repharse info --- CONTRIBUTING.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0a6c693227..38659571b5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,6 +10,16 @@ If you would like to learn more about how to get started contributing, see the interested in participating in Google Summer of Code, see [mlpack and Google Summer of Code](http://www.mlpack.org/gsoc.html). +## Involving + +It is not necessary to always code and contribute to mlpack codebase, There are several other ways to contribute to, Some of them could be : + +- Triaging a Bug Report: There are many issues raised daily and if you could confirm or create a reproducible code snippet to triage and confirm that issue, that would do a whole good of help + +- Involvement in Messaging Channel: Our messaging channel is very active, There are times when there are queries related to machine learning models or similar such issues, Which could be solved without writing code if you are versed with the topic and hence we would love to have people with good conceptual skills on board + +- Documentation - Pull requests are not judged based on length, Improving the Documentation are often left unnoticed, and hence if you can improve docs in any way, we would be greatly thankful to you + ## Pull request process Once a pull request is submitted, it must be approved by at least one member of @@ -33,9 +43,9 @@ appreciated and encouraged! All mlpack contributors who choose to review and provide feedback on Pull Requests have a responsibility to both the project and the individual making the contribution. -Reviews and feedback must be helpful, insightful, and geared towards improving the contribution. If there are reasons why you feel the PR should not be merged, explain what those are. Do not expect to be able to block a Pull Request from advancing simply because you say "No" without giving an explanation. Be open to having your mind changed. Be open to working with the contributor to make the Pull Request better. +Reviews and feedback must be [helpful, insightful, and geared towards improving the contribution](https://www.youtube.com/watch?v=NNXk_WJzyMI). If there are reasons why you feel the PR should not be merged, explain what those are. Do not expect to be able to block a Pull Request from advancing simply because you say "No" without giving an explanation. Be open to having your mind changed. Be open to working with the contributor to make the Pull Request better. -Reviews that are dismissive or disrespectful of the contributor or any other reviewers are strictly prohibited. +Please don't leave dismissive or disrespectful reviews! It's not helpful for anyone. When reviewing a Pull Request, the primary goals are : @@ -52,8 +62,6 @@ Let's welcome new contributors with <3, and not overwhelm them. ## Pull Request Waiting Time -Since all those associated with the project are working during their free time to improve the project, It sometimes becomes difficult to spare time for a particular pull request and hence please wait patiently until one of the contributors or maintainers reviews your pull request or patches. +Since members of the Contributors team only work on mlpack in their free time, it may take some time for them to review pull requests. While gentle reminders are welcome, please be patient and avoid constantly messaging the contributors. -There is a minimum waiting time which we try to respect, so that people who may have important input in such a huge project are able to respond - -Constant pinging or getting in touch to get the pull request wouldn't be of much help and may overload the maintainer which could potentialy reduce the throughput. +Typically small PRs will be reviewed within a handful of days; larger PRs might take a few weeks for an initial review, and it may be a little bit longer in times of high activity. From ef13d5d78d6f06ed1c30ebda9fb5516c85a7db27 Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Tue, 10 Mar 2020 12:06:43 +0530 Subject: [PATCH 083/265] wrapping lines --- CONTRIBUTING.md | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 38659571b5..c16ff035aa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,16 +10,6 @@ If you would like to learn more about how to get started contributing, see the interested in participating in Google Summer of Code, see [mlpack and Google Summer of Code](http://www.mlpack.org/gsoc.html). -## Involving - -It is not necessary to always code and contribute to mlpack codebase, There are several other ways to contribute to, Some of them could be : - -- Triaging a Bug Report: There are many issues raised daily and if you could confirm or create a reproducible code snippet to triage and confirm that issue, that would do a whole good of help - -- Involvement in Messaging Channel: Our messaging channel is very active, There are times when there are queries related to machine learning models or similar such issues, Which could be solved without writing code if you are versed with the topic and hence we would love to have people with good conceptual skills on board - -- Documentation - Pull requests are not judged based on length, Improving the Documentation are often left unnoticed, and hence if you can improve docs in any way, we would be greatly thankful to you - ## Pull request process Once a pull request is submitted, it must be approved by at least one member of @@ -41,27 +31,48 @@ appreciated and encouraged! ## Reviewing Pull Requests -All mlpack contributors who choose to review and provide feedback on Pull Requests have a responsibility to both the project and the individual making the contribution. +All mlpack contributors who choose to review and provide feedback on Pull +Requests have a responsibility to both the project and the individual making +the contribution. -Reviews and feedback must be [helpful, insightful, and geared towards improving the contribution](https://www.youtube.com/watch?v=NNXk_WJzyMI). If there are reasons why you feel the PR should not be merged, explain what those are. Do not expect to be able to block a Pull Request from advancing simply because you say "No" without giving an explanation. Be open to having your mind changed. Be open to working with the contributor to make the Pull Request better. +Reviews and feedback must be +[helpful, insightful, and geared towards improving the contribution]( + https://www.youtube.com/watch?v=NNXk_WJzyMI). +If there are reasons why you feel the PR should not be merged, explain +what those are. Do not expect to be able to block a Pull Request from advancing +simply because you say "No" without giving an explanation. Be open to having +your mind changed. Be open to working with the contributor to make the Pull +Request better. -Please don't leave dismissive or disrespectful reviews! It's not helpful for anyone. +Please don't leave dismissive or disrespectful reviews! It's not helpful for +anyone. When reviewing a Pull Request, the primary goals are : - For the codebase/project to improve - For the person submitting the request to succeed -Even if a Pull Request does not gets merged, the submitters should come away from the experience feeling like their effort was not wasted or unappreciated. Every Pull Request from a new contributor is an opportunity to grow the community. +Even if a Pull Request does not gets merged, the submitters should come away +from the experience feeling like their effort was not wasted or unappreciated. +Every Pull Request from a new contributor is an opportunity to grow the community. -When changes are necessary, request them, do not demand them, and do not assume that the contributor already knows how to do that. Be there to lend a helping hand in case of need. +When changes are necessary, request them, do not demand them, and do not assume +that the contributor already knows how to do that. Be there to lend a helping +hand in case of need. -Since there is clearly a high difference between pull request being raised and those being reviewed we highly encourage everyone to review each others pull request keeping in mind all the above mentioned points. +Since there is clearly a high difference between pull request being raised and +those being reviewed we highly encourage everyone to review each others pull +request keeping in mind all the above mentioned points. -Let's welcome new contributors with <3, and not overwhelm them. +Let's welcome new contributors with ❤️, and not overwhelm them. ## Pull Request Waiting Time -Since members of the Contributors team only work on mlpack in their free time, it may take some time for them to review pull requests. While gentle reminders are welcome, please be patient and avoid constantly messaging the contributors. +Since members of the Contributors team only work on mlpack in their free time, +it may take some time for them to review pull requests. While gentle reminders +are welcome, please be patient and avoid constantly messaging the contributors or +tagging them on Pull requests. -Typically small PRs will be reviewed within a handful of days; larger PRs might take a few weeks for an initial review, and it may be a little bit longer in times of high activity. +Typically small PRs will be reviewed within a handful of days; larger PRs might +take a few weeks for an initial review, and it may be a little bit longer in +times of high activity. From 72aa1fdd329bba639a6ce853595e90f1400f6b59 Mon Sep 17 00:00:00 2001 From: Saksham Rastogi <40931412+codeboy5@users.noreply.github.com> Date: Tue, 10 Mar 2020 16:35:54 +0530 Subject: [PATCH 084/265] Update alpha_dropout.hpp --- src/mlpack/methods/ann/layer/alpha_dropout.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/layer/alpha_dropout.hpp b/src/mlpack/methods/ann/layer/alpha_dropout.hpp index 4623dc11f6..770a528915 100644 --- a/src/mlpack/methods/ann/layer/alpha_dropout.hpp +++ b/src/mlpack/methods/ann/layer/alpha_dropout.hpp @@ -36,7 +36,7 @@ namespace ann /** Artificial Neural Network. */ { * title = {Self-Normalizing Neural Networks}, * journal = {Advances in Neural Information Processing Systems}, * year = {2017}, - * url = {https://arxiv.org/abs/1706.02515} + * url = {https://deepmind.com/research/publications/beta-VAE-Learning-Basic-Visual-Concepts-with-a-Constrained-Variational-Framework} * } * @endcode * From 26fd7302be0e19f1b81b98801970f52a849281d8 Mon Sep 17 00:00:00 2001 From: Sriram Date: Tue, 10 Mar 2020 17:41:07 +0530 Subject: [PATCH 085/265] Redirected link on NUMfocus logo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 698803371a..1f3024f3fe 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ project pay for developer time, professional services, travel, workshops, and a variety of other needs.
- + From beb684b6d30dc9aa29fd3c7bd4d75dd790461239 Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Tue, 10 Mar 2020 22:25:17 +0530 Subject: [PATCH 086/265] Style Fix --- src/mlpack/methods/ann/layer/max_pooling.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/ann/layer/max_pooling.hpp b/src/mlpack/methods/ann/layer/max_pooling.hpp index 7617474218..abab4a00a6 100644 --- a/src/mlpack/methods/ann/layer/max_pooling.hpp +++ b/src/mlpack/methods/ann/layer/max_pooling.hpp @@ -199,9 +199,9 @@ class MaxPooling rowidx + rStep - 1 - offset), arma::span(colidx, colidx + cStep - 1 - offset)); poolingIndices(i, j) = subIndices(idx); - } - } - } + } + } + } } /** From c40c359b1e04ddbe6b57b4900f8adc2632cedc3e Mon Sep 17 00:00:00 2001 From: Sriram Date: Tue, 10 Mar 2020 22:32:51 +0530 Subject: [PATCH 087/265] Init commit --- doc/tutorials/rl/rl.txt | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 doc/tutorials/rl/rl.txt diff --git a/doc/tutorials/rl/rl.txt b/doc/tutorials/rl/rl.txt new file mode 100644 index 0000000000..5cf76ac623 --- /dev/null +++ b/doc/tutorials/rl/rl.txt @@ -0,0 +1,9 @@ +/*! +@file rl.txt +@author Sriram S K +@brief Tutorial for how to use the Reinforcement Learning module in mlpack + +@page rltutorial Reinforcement Learning Tutorial + +@section intro_rltut Introduction + From 2dda64d269d43655e0f733ed64346d634e937408 Mon Sep 17 00:00:00 2001 From: Saksham Rastogi <40931412+codeboy5@users.noreply.github.com> Date: Tue, 10 Mar 2020 23:56:18 +0530 Subject: [PATCH 088/265] Update alpha_dropout.hpp --- src/mlpack/methods/ann/layer/alpha_dropout.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/layer/alpha_dropout.hpp b/src/mlpack/methods/ann/layer/alpha_dropout.hpp index 770a528915..4623dc11f6 100644 --- a/src/mlpack/methods/ann/layer/alpha_dropout.hpp +++ b/src/mlpack/methods/ann/layer/alpha_dropout.hpp @@ -36,7 +36,7 @@ namespace ann /** Artificial Neural Network. */ { * title = {Self-Normalizing Neural Networks}, * journal = {Advances in Neural Information Processing Systems}, * year = {2017}, - * url = {https://deepmind.com/research/publications/beta-VAE-Learning-Basic-Visual-Concepts-with-a-Constrained-Variational-Framework} + * url = {https://arxiv.org/abs/1706.02515} * } * @endcode * From b585cd7531e45d77185dc461b6375d967e5fe009 Mon Sep 17 00:00:00 2001 From: Saksham Rastogi <40931412+codeboy5@users.noreply.github.com> Date: Tue, 10 Mar 2020 23:57:54 +0530 Subject: [PATCH 089/265] Update reparametrization.hpp --- src/mlpack/methods/ann/layer/reparametrization.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/layer/reparametrization.hpp b/src/mlpack/methods/ann/layer/reparametrization.hpp index bd1e20292d..71b0338004 100644 --- a/src/mlpack/methods/ann/layer/reparametrization.hpp +++ b/src/mlpack/methods/ann/layer/reparametrization.hpp @@ -40,7 +40,7 @@ namespace ann /** Artificial Neural Network. */ { * Alexander Lerchner | Google DeepMind}, * journal = {2017 International Conference on Learning Representations(ICLR)}, * year = {2017}, - * url = {https://openreview.net/references/pdf?id=Sy2fzU9gl} + * url = {https://deepmind.com/research/publications/beta-VAE-Learning-Basic-Visual-Concepts-with-a-Constrained-Variational-Framework} * } * @endcode * From 43f0967781c4e07b55b18e6e8ef2759e71998d02 Mon Sep 17 00:00:00 2001 From: adithya-tp Date: Wed, 11 Mar 2020 14:33:54 +0400 Subject: [PATCH 090/265] Improved ANN Tutorial for FFN - Thyroid Dataset Example --- doc/tutorials/ann/ann.txt | 68 +++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/doc/tutorials/ann/ann.txt b/doc/tutorials/ann/ann.txt index c1e7da31c1..29575a92e3 100644 --- a/doc/tutorials/ann/ann.txt +++ b/doc/tutorials/ann/ann.txt @@ -204,17 +204,19 @@ using namespace mlpack::ann; int main() { - // Load the training set. - arma::mat dataset; - data::Load("thyroid_train.csv", dataset, true); + // Load the training set and testing set. + arma::mat trainData; + data::Load("thyroid_train.csv", trainData, true); + arma::mat testData; + data::Load("thyroid_test.csv", testData, true); - // Split the labels from the training set. - arma::mat trainData = dataset.submat(0, 0, dataset.n_rows - 4, - dataset.n_cols - 1); + // Split the labels from the training set and testing set respectively. + arma::mat trainLabels = trainData.row(trainData.n_rows - 1); + arma::mat testLabels = testData.row(testData.n_rows - 1); - // Split the data from the training set. - arma::mat trainLabels = dataset.submat(dataset.n_rows - 3, 0, - dataset.n_rows - 1, dataset.n_cols - 1); + // Split the data from the training set and testing set respectively. + trainData.shed_row(trainData.n_rows - 1); + testData.shed_row(testData.n_rows - 1); // Initialize the network. FFN<> model; @@ -226,14 +228,52 @@ int main() // Train the model. model.Train(trainData, trainLabels); - // Use the Predict method to get the assignments. - arma::mat assignments; - model.Predict(trainData, assignments); + // Use the Predict method to get the predictions. + arma::mat predictionTemp; + model.Predict(testData, predictionTemp); + + /* + Since the predictionsTemp is of dimensions (3 x number_of_data_points) + with continuous values, we first need to reduce it to a dimension of + (1 x number_of_data_points) with scalar values, to be able to compare with + testLabels. + + The first step towards doing this is to create a matrix of zeros with the + desired dimensions (1 x number_of_data_points). + */ + arma::mat prediction = arma::zeros(1, predictionTemp.n_cols); + + // Find index of max prediction for each data point and store in "prediction" + for (size_t i = 0; i < predictionTemp.n_cols; ++i) + { + // we add 1 to the max index, so that it matches the actual test labels. + prediction(i) = arma::as_scalar(arma::find( + arma::max(predictionTemp.col(i)) == predictionTemp.col(i), 1)) + 1; + } + + /* + Compute the error between predictions and testLabels, + now that we have the desired predictions. + */ + size_t error = 0; + for (size_t i = 0; i < testData.n_cols; i++) + { + if (int(arma::as_scalar(prediction.col(i))) == int(arma::as_scalar(testLabels.col(i)))) + { + error++; + } + } + double classificationError = 1 - double(error) / testData.n_cols; + + // Print out the classification error for the testing dataset. + cout << "Classification Error for the Test set: " << classificationError << endl; + return 0; } @endcode -Now, the matrix assignments holds the classification of each point in the -dataset. +Now, the matrix prediction holds the classification of each point in the +dataset. Subsequently, we find the classfication error by comparing it +with testLabels. In the next example, we create simple noisy sine sequences, which are trained later on, using the RNN class in the `RNNModel()` method. From 34842727d3f62361b54efd53cd4abc261117e748 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Wed, 11 Mar 2020 08:32:00 -0400 Subject: [PATCH 091/265] Fix return type of A(). --- src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp b/src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp index 650c87a94d..788c2ef842 100644 --- a/src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp @@ -75,9 +75,9 @@ class LogCoshLoss OutputDataType& OutputParameter() { return outputParameter; } //! Get the value of hyperparameter a. - bool A() const { return a; } + double A() const { return a; } //! Modify the value of hyperparameter a. - bool& A() { return a; } + double& A() { return a; } /** * Serialize the loss function. From ca9ac315b7ccf9d0790bb74f348274f88277423d Mon Sep 17 00:00:00 2001 From: adithya-tp Date: Wed, 11 Mar 2020 19:23:49 +0400 Subject: [PATCH 092/265] Fixed markdown pipe symbol issue --- src/mlpack/bindings/markdown/print_docs.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mlpack/bindings/markdown/print_docs.cpp b/src/mlpack/bindings/markdown/print_docs.cpp index 8b464baa1e..317396e746 100644 --- a/src/mlpack/bindings/markdown/print_docs.cpp +++ b/src/mlpack/bindings/markdown/print_docs.cpp @@ -126,7 +126,8 @@ void PrintDocs(const std::string& bindingName, cout << "| "; cout << ParamString(it->second.name) << " | "; cout << ParamType(it->second) << " | "; - cout << it->second.desc; // just a string + string desc = boost::replace_all_copy(it->second.desc, "|", "\\|"); + cout << desc << endl; // just a string // Print whether or not it's a "special" language-only parameter. if (it->second.name == "copy_all_inputs" || it->second.name == "help" || it->second.name == "info" || it->second.name == "version") @@ -180,7 +181,8 @@ void PrintDocs(const std::string& bindingName, cout << "{: #" << languages[i] << "_" << bindingName << "_detailed-documentation }" << endl; cout << endl; - cout << programDoc.documentation() << endl; + string doc = boost::replace_all_copy(programDoc.documentation(), "|", "\\|"); + cout << doc << endl; cout << endl; cout << "### See also" << endl; From 2c16179ecde02a2e0a4ff45d9b880c872807ec35 Mon Sep 17 00:00:00 2001 From: adithya-tp Date: Wed, 11 Mar 2020 19:34:41 +0400 Subject: [PATCH 093/265] Style fix --- src/mlpack/bindings/markdown/print_docs.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mlpack/bindings/markdown/print_docs.cpp b/src/mlpack/bindings/markdown/print_docs.cpp index 317396e746..81a80b21ef 100644 --- a/src/mlpack/bindings/markdown/print_docs.cpp +++ b/src/mlpack/bindings/markdown/print_docs.cpp @@ -181,7 +181,8 @@ void PrintDocs(const std::string& bindingName, cout << "{: #" << languages[i] << "_" << bindingName << "_detailed-documentation }" << endl; cout << endl; - string doc = boost::replace_all_copy(programDoc.documentation(), "|", "\\|"); + string doc = boost::replace_all_copy(programDoc.documentation(), + "|", "\\|"); cout << doc << endl; cout << endl; From f050c78a78a1882066a07d9f6f266c35a6fdbe6b Mon Sep 17 00:00:00 2001 From: adithya-tp Date: Wed, 11 Mar 2020 19:38:02 +0400 Subject: [PATCH 094/265] Style Fix - Extra white space at the end of line --- src/mlpack/bindings/markdown/print_docs.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/bindings/markdown/print_docs.cpp b/src/mlpack/bindings/markdown/print_docs.cpp index 81a80b21ef..218e5e4c32 100644 --- a/src/mlpack/bindings/markdown/print_docs.cpp +++ b/src/mlpack/bindings/markdown/print_docs.cpp @@ -181,7 +181,7 @@ void PrintDocs(const std::string& bindingName, cout << "{: #" << languages[i] << "_" << bindingName << "_detailed-documentation }" << endl; cout << endl; - string doc = boost::replace_all_copy(programDoc.documentation(), + string doc = boost::replace_all_copy(programDoc.documentation(), "|", "\\|"); cout << doc << endl; cout << endl; From 54e880b1980b915a69586c84ec33b4073cab944e Mon Sep 17 00:00:00 2001 From: Mrityunjay Tripathi Date: Wed, 11 Mar 2020 22:39:16 +0530 Subject: [PATCH 095/265] added mean option to backward function [CI SKIP] --- src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp index f3a0fb8c33..6207446dfc 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp @@ -60,7 +60,10 @@ void HuberLoss::Backward( absError = std::abs(target[i] - input[i]); output[i] = absError > delta ? - delta * (target[i] - input[i]) / absError : input[i] - target[i]; - output[i] /= output.n_elem; + if (mean) + { + output[i] /= output.n_elem; + } } } From 97fd462a98a66e52e41720f9954f2dd7d901c466 Mon Sep 17 00:00:00 2001 From: Ryan Birmingham Date: Wed, 11 Mar 2020 15:18:33 -0400 Subject: [PATCH 096/265] Simplify CMake for Armadillo and its Dependencies (#2247) * start simplify of arma deps * no manual openblas links * slight change to openblas get and order * restore tools directory * Rename * wrong opts renamed * better handle no wrapper case * library -> libraries * move dep links to findArmadillo * minor cleanup of findArma * giving up on cleanup of config get * simplify header read * special HDF5 case * more closely follo hdf5 case * remove standard findblas/lapack * cleanup doc and quiet mode --- .appveyor.yml | 4 +- .ci/windows-steps.yaml | 6 +- CMake/ARMA_FindACML.cmake | 37 --- CMake/ARMA_FindACMLMP.cmake | 37 --- CMake/ARMA_FindARPACK.cmake | 39 --- CMake/ARMA_FindBLAS.cmake | 44 ---- CMake/ARMA_FindCBLAS.cmake | 47 ---- CMake/ARMA_FindCLAPACK.cmake | 48 ---- CMake/ARMA_FindLAPACK.cmake | 44 ---- CMake/ARMA_FindMKL.cmake | 49 ---- CMake/ARMA_FindOpenBLAS.cmake | 37 --- CMake/FindARPACK.cmake | 55 +++++ CMake/FindArmadillo.cmake | 437 +++++++++------------------------- CMakeLists.txt | 45 ---- doc/guide/build_windows.hpp | 2 +- 15 files changed, 179 insertions(+), 752 deletions(-) delete mode 100644 CMake/ARMA_FindACML.cmake delete mode 100644 CMake/ARMA_FindACMLMP.cmake delete mode 100644 CMake/ARMA_FindARPACK.cmake delete mode 100644 CMake/ARMA_FindBLAS.cmake delete mode 100644 CMake/ARMA_FindCBLAS.cmake delete mode 100644 CMake/ARMA_FindCLAPACK.cmake delete mode 100644 CMake/ARMA_FindLAPACK.cmake delete mode 100644 CMake/ARMA_FindMKL.cmake delete mode 100644 CMake/ARMA_FindOpenBLAS.cmake create mode 100644 CMake/FindARPACK.cmake diff --git a/.appveyor.yml b/.appveyor.yml index 068f2e4c76..f0ffa39490 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -85,8 +85,8 @@ build_script: - cd C:\projects\mlpack && mkdir build && cd build - > cmake -G "%VSVER%" - -DBLAS_LIBRARY:FILEPATH=%BLAS_LIBRARY% - -DLAPACK_LIBRARY:FILEPATH=%BLAS_LIBRARY% + -DBLAS_LIBRARIES:FILEPATH=%BLAS_LIBRARY% + -DLAPACK_LIBRARIES:FILEPATH=%BLAS_LIBRARY% -DARMADILLO_INCLUDE_DIR="C:/projects/mlpack/armadillo-8.400.0/include" -DARMADILLO_LIBRARY:FILEPATH=%ARMADILLO_LIBRARY% -DBOOST_INCLUDEDIR:PATH=%BOOST_INCLUDE% diff --git a/.ci/windows-steps.yaml b/.ci/windows-steps.yaml index 4d8b9e7923..8d61b72f91 100644 --- a/.ci/windows-steps.yaml +++ b/.ci/windows-steps.yaml @@ -28,7 +28,7 @@ steps: # Configure armadillo - bash: | git clone --depth 1 https://github.com/mlpack/jenkins-conf.git conf - + curl -O http://masterblaster.mlpack.org:5005/armadillo-8.400.0.tar.gz -o armadillo-8.400.0.tar.gz tar -xzvf armadillo-8.400.0.tar.gz @@ -60,8 +60,8 @@ steps: cmake $(CMakeGenerator) ` $(CMakeArgs) ` - -DBLAS_LIBRARY:FILEPATH=$(Agent.ToolsDirectory)\OpenBLAS.0.2.14.1\lib\native\lib\x64\libopenblas.dll.a ` - -DLAPACK_LIBRARY:FILEPATH=$(Agent.ToolsDirectory)\OpenBLAS.0.2.14.1\lib\native\lib\x64\libopenblas.dll.a ` + -DBLAS_LIBRARIES:FILEPATH=$(Agent.ToolsDirectory)\OpenBLAS.0.2.14.1\lib\native\lib\x64\libopenblas.dll.a ` + -DLAPACK_LIBRARIES:FILEPATH=$(Agent.ToolsDirectory)\OpenBLAS.0.2.14.1\lib\native\lib\x64\libopenblas.dll.a ` -DARMADILLO_INCLUDE_DIR="..\armadillo-8.400.0\include" ` -DARMADILLO_LIBRARY="..\armadillo-8.400.0\Release\armadillo.lib" ` -DBOOST_INCLUDEDIR=$(Agent.ToolsDirectory)\boost.1.60.0.0\lib\native\include ` diff --git a/CMake/ARMA_FindACML.cmake b/CMake/ARMA_FindACML.cmake deleted file mode 100644 index 42561cafda..0000000000 --- a/CMake/ARMA_FindACML.cmake +++ /dev/null @@ -1,37 +0,0 @@ -# - Find AMD's ACML library (no includes) which provides optimised BLAS and LAPACK functions -# This module defines -# ACML_LIBRARIES, the libraries needed to use ACML. -# ACML_FOUND, If false, do not try to use ACML. -# also defined, but not for general use are -# ACML_LIBRARY, where to find the ACML library. - -set(ACML_NAMES ${ACML_NAMES} acml) -find_library(ACML_LIBRARY - NAMES ${ACML_NAMES} - PATHS /usr/lib64 /usr/lib /usr/*/lib64 /usr/*/lib /usr/*/gfortran64/lib/ /usr/*/gfortran32/lib/ /usr/local/lib64 /usr/local/lib /opt/lib64 /opt/lib /opt/*/lib64 /opt/*/lib /opt/*/gfortran64/lib/ /opt/*/gfortran32/lib/ - ) - -if (ACML_LIBRARY) - set(ACML_LIBRARIES ${ACML_LIBRARY}) - set(ACML_FOUND "YES") -else () - set(ACML_FOUND "NO") -endif () - - -if (ACML_FOUND) - if (NOT ACML_FIND_QUIETLY) - message(STATUS "Found the ACML library: ${ACML_LIBRARIES}") - endif () -else () - if (ACML_FIND_REQUIRED) - message(FATAL_ERROR "Could not find the ACML library") - endif () -endif () - -# Deprecated declarations. -get_filename_component (NATIVE_ACML_LIB_PATH ${ACML_LIBRARY} PATH) - -mark_as_advanced( - ACML_LIBRARY - ) diff --git a/CMake/ARMA_FindACMLMP.cmake b/CMake/ARMA_FindACMLMP.cmake deleted file mode 100644 index 47a192ced6..0000000000 --- a/CMake/ARMA_FindACMLMP.cmake +++ /dev/null @@ -1,37 +0,0 @@ -# - Find AMD's ACMLMP library (no includes) which provides optimised and parallelised BLAS and LAPACK functions -# This module defines -# ACMLMP_LIBRARIES, the libraries needed to use ACMLMP. -# ACMLMP_FOUND, If false, do not try to use ACMLMP. -# also defined, but not for general use are -# ACMLMP_LIBRARY, where to find the ACMLMP library. - -set(ACMLMP_NAMES ${ACMLMP_NAMES} acml_mp) -find_library(ACMLMP_LIBRARY - NAMES ${ACMLMP_NAMES} - PATHS /usr/lib64 /usr/lib /usr/*/lib64 /usr/*/lib /usr/*/gfortran64_mp/lib/ /usr/*/gfortran32_mp/lib/ /usr/local/lib64 /usr/local/lib /opt/lib64 /opt/lib /opt/*/lib64 /opt/*/lib /opt/*/gfortran64_mp/lib/ /opt/*/gfortran32_mp/lib/ - ) - -if (ACMLMP_LIBRARY) - set(ACMLMP_LIBRARIES ${ACMLMP_LIBRARY}) - set(ACMLMP_FOUND "YES") -else () - set(ACMLMP_FOUND "NO") -endif () - - -if (ACMLMP_FOUND) - if (NOT ACMLMP_FIND_QUIETLY) - message(STATUS "Found the ACMLMP library: ${ACMLMP_LIBRARIES}") - endif () -else () - if (ACMLMP_FIND_REQUIRED) - message(FATAL_ERROR "Could not find the ACMLMP library") - endif () -endif () - -# Deprecated declarations. -get_filename_component (NATIVE_ACMLMP_LIB_PATH ${ACMLMP_LIBRARY} PATH) - -mark_as_advanced( - ACMLMP_LIBRARY - ) diff --git a/CMake/ARMA_FindARPACK.cmake b/CMake/ARMA_FindARPACK.cmake deleted file mode 100644 index ff1ee22797..0000000000 --- a/CMake/ARMA_FindARPACK.cmake +++ /dev/null @@ -1,39 +0,0 @@ -# - Try to find ARPACK -# Once done this will define -# -# ARPACK_FOUND - system has ARPACK -# ARPACK_LIBRARY - Link this to use ARPACK - - -find_library(ARPACK_LIBRARY - NAMES arpack - PATHS /usr/lib64 /usr/lib /usr/local/lib64 /usr/local/lib - ) - - -if (ARPACK_LIBRARY) - set(ARPACK_FOUND YES) -else () - # Search for PARPACK. - find_library(ARPACK_LIBRARY - NAMES parpack - PATHS /usr/lib64 /usr/lib /usr/local/lib64 /usr/local/lib - ) - - if (ARPACK_LIBRARY) - set(ARPACK_FOUND YES) - else () - set(ARPACK_FOUND NO) - endif () -endif () - - -if (ARPACK_FOUND) - if (NOT ARPACK_FIND_QUIETLY) - message(STATUS "Found an ARPACK library: ${ARPACK_LIBRARY}") - endif () -else () - if (ARPACK_FIND_REQUIRED) - message(FATAL_ERROR "Could not find an ARPACK library") - endif () -endif () diff --git a/CMake/ARMA_FindBLAS.cmake b/CMake/ARMA_FindBLAS.cmake deleted file mode 100644 index ff37b4fc79..0000000000 --- a/CMake/ARMA_FindBLAS.cmake +++ /dev/null @@ -1,44 +0,0 @@ -# - Find a BLAS library (no includes) -# This module defines -# BLAS_LIBRARIES, the libraries needed to use BLAS. -# BLAS_FOUND, If false, do not try to use BLAS. -# also defined, but not for general use are -# BLAS_LIBRARY, where to find the BLAS library. - -set(BLAS_NAMES ${BLAS_NAMES} blas) - -# Find the ATLAS version preferentially. -find_library(BLAS_LIBRARY - NAMES ${BLAS_NAMES} - PATHS /usr/lib64/atlas /usr/lib/atlas /usr/local/lib64/atlas /usr/local/lib/atlas - NO_DEFAULT_PATH) - -find_library(BLAS_LIBRARY - NAMES ${BLAS_NAMES} - PATHS /usr/lib64/atlas /usr/lib/atlas /usr/lib64 /usr/lib /usr/local/lib64 /usr/local/lib - ) - -if (BLAS_LIBRARY) - set(BLAS_LIBRARIES ${BLAS_LIBRARY}) - set(BLAS_FOUND "YES") -else () - set(BLAS_FOUND "NO") -endif () - - -if (BLAS_FOUND) - if (NOT BLAS_FIND_QUIETLY) - message(STATUS "Found BLAS: ${BLAS_LIBRARIES}") - endif () -else () - if (BLAS_FIND_REQUIRED) - message(FATAL_ERROR "Could not find BLAS") - endif () -endif () - -# Deprecated declarations. -get_filename_component (NATIVE_BLAS_LIB_PATH ${BLAS_LIBRARY} PATH) - -mark_as_advanced( - BLAS_LIBRARY - ) diff --git a/CMake/ARMA_FindCBLAS.cmake b/CMake/ARMA_FindCBLAS.cmake deleted file mode 100644 index da84c246b8..0000000000 --- a/CMake/ARMA_FindCBLAS.cmake +++ /dev/null @@ -1,47 +0,0 @@ -# - Find CBLAS (includes and library) -# This module defines -# CBLAS_INCLUDE_DIR -# CBLAS_LIBRARIES -# CBLAS_FOUND -# also defined, but not for general use are -# CBLAS_LIBRARY, where to find the library. - -find_path(CBLAS_INCLUDE_DIR cblas.h -/usr/include/atlas/ -/usr/local/include/atlas/ -/usr/include/ -/usr/local/include/ -) - -set(CBLAS_NAMES ${CBLAS_NAMES} cblas) -find_library(CBLAS_LIBRARY - NAMES ${CBLAS_NAMES} - PATHS /usr/lib64/atlas-sse3 /usr/lib64/atlas /usr/lib64 /usr/local/lib64/atlas /usr/local/lib64 /usr/lib/atlas-sse3 /usr/lib/atlas-sse2 /usr/lib/atlas-sse /usr/lib/atlas-3dnow /usr/lib/atlas /usr/lib /usr/local/lib/atlas /usr/local/lib - ) - -if (CBLAS_LIBRARY AND CBLAS_INCLUDE_DIR) - set(CBLAS_LIBRARIES ${CBLAS_LIBRARY}) - set(CBLAS_FOUND "YES") -else () - set(CBLAS_FOUND "NO") -endif () - - -if (CBLAS_FOUND) - if (NOT CBLAS_FIND_QUIETLY) - message(STATUS "Found a CBLAS library: ${CBLAS_LIBRARIES}") - endif () -else () - if (CBLAS_FIND_REQUIRED) - message(FATAL_ERROR "Could not find a CBLAS library") - endif () -endif () - -# Deprecated declarations. -set (NATIVE_CBLAS_INCLUDE_PATH ${CBLAS_INCLUDE_DIR} ) -get_filename_component (NATIVE_CBLAS_LIB_PATH ${CBLAS_LIBRARY} PATH) - -mark_as_advanced( - CBLAS_LIBRARY - CBLAS_INCLUDE_DIR - ) diff --git a/CMake/ARMA_FindCLAPACK.cmake b/CMake/ARMA_FindCLAPACK.cmake deleted file mode 100644 index 97a9792a31..0000000000 --- a/CMake/ARMA_FindCLAPACK.cmake +++ /dev/null @@ -1,48 +0,0 @@ -# - Find a version of CLAPACK (includes and library) -# This module defines -# CLAPACK_INCLUDE_DIR -# CLAPACK_LIBRARIES -# CLAPACK_FOUND -# also defined, but not for general use are -# CLAPACK_LIBRARY, where to find the library. - -find_path(CLAPACK_INCLUDE_DIR clapack.h -/usr/include/atlas/ -/usr/local/include/atlas/ -/usr/include/ -/usr/local/include/ -) - -set(CLAPACK_NAMES ${CLAPACK_NAMES} lapack_atlas) -set(CLAPACK_NAMES ${CLAPACK_NAMES} clapack) -find_library(CLAPACK_LIBRARY - NAMES ${CLAPACK_NAMES} - PATHS /usr/lib64/atlas-sse3 /usr/lib64/atlas /usr/lib64 /usr/local/lib64/atlas /usr/local/lib64 /usr/lib/atlas-sse3 /usr/lib/atlas-sse2 /usr/lib/atlas-sse /usr/lib/atlas-3dnow /usr/lib/atlas /usr/lib /usr/local/lib/atlas /usr/local/lib - ) - -if (CLAPACK_LIBRARY AND CLAPACK_INCLUDE_DIR) - set(CLAPACK_LIBRARIES ${CLAPACK_LIBRARY}) - set(CLAPACK_FOUND "YES") -else () - set(CLAPACK_FOUND "NO") -endif () - - -if (CLAPACK_FOUND) - if (NOT CLAPACK_FIND_QUIETLY) - message(STATUS "Found a CLAPACK library: ${CLAPACK_LIBRARIES}") - endif () -else () - if (CLAPACK_FIND_REQUIRED) - message(FATAL_ERROR "Could not find a CLAPACK library") - endif () -endif () - -# Deprecated declarations. -set (NATIVE_CLAPACK_INCLUDE_PATH ${CLAPACK_INCLUDE_DIR} ) -get_filename_component (NATIVE_CLAPACK_LIB_PATH ${CLAPACK_LIBRARY} PATH) - -mark_as_advanced( - CLAPACK_LIBRARY - CLAPACK_INCLUDE_DIR - ) diff --git a/CMake/ARMA_FindLAPACK.cmake b/CMake/ARMA_FindLAPACK.cmake deleted file mode 100644 index b40d16d04c..0000000000 --- a/CMake/ARMA_FindLAPACK.cmake +++ /dev/null @@ -1,44 +0,0 @@ -# - Find a LAPACK library (no includes) -# This module defines -# LAPACK_LIBRARIES, the libraries needed to use LAPACK. -# LAPACK_FOUND, If false, do not try to use LAPACK. -# also defined, but not for general use are -# LAPACK_LIBRARY, where to find the LAPACK library. - -set(LAPACK_NAMES ${LAPACK_NAMES} lapack) - -# Check ATLAS paths preferentially, using this necessary hack (I love CMake). -find_library(LAPACK_LIBRARY - NAMES ${LAPACK_NAMES} - PATHS /usr/lib64/atlas /usr/lib/atlas /usr/local/lib64/atlas /usr/local/lib/atlas - NO_DEFAULT_PATH) - -find_library(LAPACK_LIBRARY - NAMES ${LAPACK_NAMES} - PATHS /usr/lib64 /usr/lib /usr/local/lib64 /usr/local/lib - ) - -if (LAPACK_LIBRARY) - set(LAPACK_LIBRARIES ${LAPACK_LIBRARY}) - set(LAPACK_FOUND "YES") -else () - set(LAPACK_FOUND "NO") -endif () - - -if (LAPACK_FOUND) - if (NOT LAPACK_FIND_QUIETLY) - message(STATUS "Found LAPACK: ${LAPACK_LIBRARIES}") - endif () -else () - if (LAPACK_FIND_REQUIRED) - message(FATAL_ERROR "Could not find LAPACK") - endif () -endif () - -# Deprecated declarations. -get_filename_component (NATIVE_LAPACK_LIB_PATH ${LAPACK_LIBRARY} PATH) - -mark_as_advanced( - LAPACK_LIBRARY - ) diff --git a/CMake/ARMA_FindMKL.cmake b/CMake/ARMA_FindMKL.cmake deleted file mode 100644 index 452fa5a643..0000000000 --- a/CMake/ARMA_FindMKL.cmake +++ /dev/null @@ -1,49 +0,0 @@ -# - Find the MKL libraries (no includes) -# This module defines -# MKL_LIBRARIES, the libraries needed to use Intel's implementation of BLAS & LAPACK. -# MKL_FOUND, If false, do not try to use MKL. - -set(MKL_NAMES ${MKL_NAMES} mkl_lapack) -set(MKL_NAMES ${MKL_NAMES} mkl_intel_thread) -set(MKL_NAMES ${MKL_NAMES} mkl_core) -set(MKL_NAMES ${MKL_NAMES} guide) -set(MKL_NAMES ${MKL_NAMES} mkl) -set(MKL_NAMES ${MKL_NAMES} iomp5) -#set(MKL_NAMES ${MKL_NAMES} pthread) - -if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(MKL_NAMES ${MKL_NAMES} mkl_intel_lp64) -else() - set(MKL_NAMES ${MKL_NAMES} mkl_intel) -endif() - -foreach (MKL_NAME ${MKL_NAMES}) - find_library(${MKL_NAME}_LIBRARY - NAMES ${MKL_NAME} - PATHS /usr/lib64 /usr/lib /usr/local/lib64 /usr/local/lib /opt/intel/lib/intel64 /opt/intel/lib/ia32 /opt/intel/mkl/lib/lib64 /opt/intel/mkl/lib/intel64 /opt/intel/mkl/lib/ia32 /opt/intel/mkl/lib /opt/intel/*/mkl/lib/intel64 /opt/intel/*/mkl/lib/ia32/ /opt/mkl/*/lib/em64t /opt/mkl/*/lib/32 /opt/intel/mkl/*/lib/em64t /opt/intel/mkl/*/lib/32 - ) - - set(TMP_LIBRARY ${${MKL_NAME}_LIBRARY}) - - if(TMP_LIBRARY) - set(MKL_LIBRARIES ${MKL_LIBRARIES} ${TMP_LIBRARY}) - endif() -endforeach() - -if (MKL_LIBRARIES) - set(MKL_FOUND "YES") -else () - set(MKL_FOUND "NO") -endif () - -if (MKL_FOUND) - if (NOT MKL_FIND_QUIETLY) - message(STATUS "Found MKL libraries: ${MKL_LIBRARIES}") - endif () -else () - if (MKL_FIND_REQUIRED) - message(FATAL_ERROR "Could not find MKL libraries") - endif () -endif () - -# mark_as_advanced(MKL_LIBRARY) diff --git a/CMake/ARMA_FindOpenBLAS.cmake b/CMake/ARMA_FindOpenBLAS.cmake deleted file mode 100644 index edfa27db83..0000000000 --- a/CMake/ARMA_FindOpenBLAS.cmake +++ /dev/null @@ -1,37 +0,0 @@ -# - Find the OpenBLAS library (no includes) -# This module defines -# OpenBLAS_LIBRARIES, the libraries needed to use OpenBLAS. -# OpenBLAS_FOUND, If false, do not try to use OpenBLAS. -# also defined, but not for general use are -# OpenBLAS_LIBRARY, where to find the OpenBLAS library. - -set(OpenBLAS_NAMES ${OpenBLAS_NAMES} openblas) -find_library(OpenBLAS_LIBRARY - NAMES ${OpenBLAS_NAMES} - PATHS /lib64 /lib /usr/lib64 /usr/lib /usr/local/lib64 /usr/local/lib - ) - -if (OpenBLAS_LIBRARY) - set(OpenBLAS_LIBRARIES ${OpenBLAS_LIBRARY}) - set(OpenBLAS_FOUND "YES") -else () - set(OpenBLAS_FOUND "NO") -endif () - - -if (OpenBLAS_FOUND) - if (NOT OpenBLAS_FIND_QUIETLY) - message(STATUS "Found the OpenBLAS library: ${OpenBLAS_LIBRARIES}") - endif () -else () - if (OpenBLAS_FIND_REQUIRED) - message(FATAL_ERROR "Could not find the OpenBLAS library") - endif () -endif () - -# Deprecated declarations. -get_filename_component (NATIVE_OpenBLAS_LIB_PATH ${OpenBLAS_LIBRARY} PATH) - -mark_as_advanced( - OpenBLAS_LIBRARY - ) diff --git a/CMake/FindARPACK.cmake b/CMake/FindARPACK.cmake new file mode 100644 index 0000000000..08baa3b2b1 --- /dev/null +++ b/CMake/FindARPACK.cmake @@ -0,0 +1,55 @@ +# Searches for an installation of the ARPACK library. On success, it sets the following variables: +# +# ARPACK_FOUND Set to true to indicate the library was found +# ARPACK_LIBRARIES All libraries needed to use ARPACK (with full path) +# +# To specify an additional directory to search, set ARPACK_ROOT. +# +# TODO: Do we need to explicitly search for BLAS and LAPACK as well? The source distribution statically links these to +# libarpack. Are there any installations that don't do this or the equivalent? +# +# Author: Siddhartha Chaudhuri, 2009 +# + +SET(ARPACK_FOUND FALSE) + +# First look in user-provided root directory, then look in system locations +FIND_LIBRARY(ARPACK_LIBRARIES NAMES arpack libarpack ARPACK libARPACK PATHS "${ARPACK_ROOT}" "${ARPACK_ROOT}/lib" + NO_DEFAULT_PATH) +IF(NOT ARPACK_LIBRARIES) + FIND_LIBRARY(ARPACK_LIBRARIES NAMES arpack libarpack ARPACK libARPACK) +ENDIF(NOT ARPACK_LIBRARIES) + +IF(ARPACK_LIBRARIES) + # On OS X we probably also need gfortran and BLAS and LAPACK libraries + IF(APPLE) + FIND_LIBRARY(ARPACK_LAPACK_LIBRARY NAMES lapack LAPACK PATHS "${ARPACK_ROOT}" "${ARPACK_ROOT}/lib") + FIND_LIBRARY(ARPACK_BLAS_LIBRARY NAMES blas BLAS PATHS "${ARPACK_ROOT}" "${ARPACK_ROOT}/lib") + FIND_LIBRARY(ARPACK_GFORTRAN_LIBRARY NAMES gfortran PATHS "${ARPACK_ROOT}" "${ARPACK_ROOT}/lib" + PATH_SUFFIXES "" "gfortran/lib" "../gfortran/lib") + + IF(ARPACK_BLAS_LIBRARY) + SET(ARPACK_LIBRARIES ${ARPACK_LIBRARIES} ${ARPACK_BLAS_LIBRARY}) + ENDIF(ARPACK_BLAS_LIBRARY) + + IF(ARPACK_LAPACK_LIBRARY) + SET(ARPACK_LIBRARIES ${ARPACK_LIBRARIES} ${ARPACK_LAPACK_LIBRARY}) + ENDIF(ARPACK_LAPACK_LIBRARY) + + IF(ARPACK_GFORTRAN_LIBRARY) + SET(ARPACK_LIBRARIES ${ARPACK_LIBRARIES} ${ARPACK_GFORTRAN_LIBRARY}) + ENDIF(ARPACK_GFORTRAN_LIBRARY) + ENDIF(APPLE) + + SET(ARPACK_FOUND TRUE) +ENDIF(ARPACK_LIBRARIES) + +IF(ARPACK_FOUND) + IF(NOT ARPACK_FIND_QUIETLY) + MESSAGE(STATUS "Found ARPACK: libraries at ${ARPACK_LIBRARIES}") + ENDIF(NOT ARPACK_FIND_QUIETLY) +ELSE(ARPACK_FOUND) + IF(ARPACK_FIND_REQUIRED) + MESSAGE(FATAL_ERROR "ARPACK not found") + ENDIF(ARPACK_FIND_REQUIRED) +ENDIF(ARPACK_FOUND) diff --git a/CMake/FindArmadillo.cmake b/CMake/FindArmadillo.cmake index 1244f64936..36ec63b515 100644 --- a/CMake/FindArmadillo.cmake +++ b/CMake/FindArmadillo.cmake @@ -1,41 +1,41 @@ -# - Find Armadillo -# Find the Armadillo C++ library -# -# Using Armadillo: -# find_package(Armadillo REQUIRED) -# include_directories(${ARMADILLO_INCLUDE_DIRS}) -# add_executable(foo foo.cc) -# target_link_libraries(foo ${ARMADILLO_LIBRARIES}) -# This module sets the following variables: -# ARMADILLO_FOUND - set to true if the library is found -# ARMADILLO_INCLUDE_DIRS - list of required include directories -# ARMADILLO_LIBRARIES - list of libraries to be linked -# ARMADILLO_VERSION_MAJOR - major version number -# ARMADILLO_VERSION_MINOR - minor version number -# ARMADILLO_VERSION_PATCH - patch version number -# ARMADILLO_VERSION_STRING - version number as a string (ex: "1.0.4") -# ARMADILLO_VERSION_NAME - name of the version (ex: "Antipodean Antileech") +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. -#============================================================================= -# Copyright 2011 Clement Creusot -# -# Distributed under the OSI-approved BSD License (the "License"); -# see accompanying file Copyright.txt for details. -# -# This software is distributed WITHOUT ANY WARRANTY; without even the -# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -# See the License for more information. -#============================================================================= -# (To distribute this file outside of CMake, substitute the full -# License text for the above reference.) +#[=======================================================================[.rst: +FindArmadillo +------------- +Find the Armadillo C++ library. +Armadillo is a library for linear algebra & scientific computing. + +Using Armadillo: + +:: + + find_package(Armadillo REQUIRED) + include_directories(${ARMADILLO_INCLUDE_DIRS}) + add_executable(foo foo.cc) + target_link_libraries(foo ${ARMADILLO_LIBRARIES}) + +This module sets the following variables: + +:: + + ARMADILLO_FOUND - set to true if the library is found + ARMADILLO_INCLUDE_DIRS - list of required include directories + ARMADILLO_LIBRARIES - list of libraries to be linked + ARMADILLO_VERSION_MAJOR - major version number + ARMADILLO_VERSION_MINOR - minor version number + ARMADILLO_VERSION_PATCH - patch version number + ARMADILLO_VERSION_STRING - version number as a string (ex: "1.0.4") + ARMADILLO_VERSION_NAME - name of the version (ex: "Antipodean Antileech") +#]=======================================================================] find_path(ARMADILLO_INCLUDE_DIR NAMES armadillo PATHS "$ENV{ProgramFiles}/Armadillo/include" ) - if(ARMADILLO_INCLUDE_DIR) # ------------------------------------------------------------------------ # Extract version information from @@ -52,318 +52,117 @@ if(ARMADILLO_INCLUDE_DIR) if(EXISTS "${ARMADILLO_INCLUDE_DIR}/armadillo_bits/arma_version.hpp") # Read and parse armdillo version header file for version number - file(READ "${ARMADILLO_INCLUDE_DIR}/armadillo_bits/arma_version.hpp" _armadillo_HEADER_CONTENTS) - string(REGEX REPLACE ".*#define ARMA_VERSION_MAJOR ([0-9]+).*" "\\1" ARMADILLO_VERSION_MAJOR "${_armadillo_HEADER_CONTENTS}") - string(REGEX REPLACE ".*#define ARMA_VERSION_MINOR ([0-9]+).*" "\\1" ARMADILLO_VERSION_MINOR "${_armadillo_HEADER_CONTENTS}") - string(REGEX REPLACE ".*#define ARMA_VERSION_PATCH ([0-9]+).*" "\\1" ARMADILLO_VERSION_PATCH "${_armadillo_HEADER_CONTENTS}") + file(STRINGS "${ARMADILLO_INCLUDE_DIR}/armadillo_bits/arma_version.hpp" _ARMA_HEADER_CONTENTS REGEX "#define ARMA_VERSION_[A-Z]+ ") + string(REGEX REPLACE ".*#define ARMA_VERSION_MAJOR ([0-9]+).*" "\\1" ARMADILLO_VERSION_MAJOR "${_ARMA_HEADER_CONTENTS}") + string(REGEX REPLACE ".*#define ARMA_VERSION_MINOR ([0-9]+).*" "\\1" ARMADILLO_VERSION_MINOR "${_ARMA_HEADER_CONTENTS}") + string(REGEX REPLACE ".*#define ARMA_VERSION_PATCH ([0-9]+).*" "\\1" ARMADILLO_VERSION_PATCH "${_ARMA_HEADER_CONTENTS}") # WARNING: The number of spaces before the version name is not one. - string(REGEX REPLACE ".*#define ARMA_VERSION_NAME\ +\"([0-9a-zA-Z\ _-]+)\".*" "\\1" ARMADILLO_VERSION_NAME "${_armadillo_HEADER_CONTENTS}") + string(REGEX REPLACE ".*#define ARMA_VERSION_NAME\ +\"([0-9a-zA-Z\ _-]+)\".*" "\\1" ARMADILLO_VERSION_NAME "${_ARMA_HEADER_CONTENTS}") endif() set(ARMADILLO_VERSION_STRING "${ARMADILLO_VERSION_MAJOR}.${ARMADILLO_VERSION_MINOR}.${ARMADILLO_VERSION_PATCH}") endif () - -#====================== - -# Determine what support libraries are being used, and whether or not we need to -# link against them. We need to look in config.hpp. -set(SUPPORT_INCLUDE_DIRS "") -set(SUPPORT_LIBRARIES "") -set(ARMA_NEED_LIBRARY true) # Assume true. if(EXISTS "${ARMADILLO_INCLUDE_DIR}/armadillo_bits/config.hpp") - file(READ "${ARMADILLO_INCLUDE_DIR}/armadillo_bits/config.hpp" _armadillo_CONFIG_CONTENTS) - # ARMA_USE_WRAPPER - string(REGEX MATCH "\r?\n[\t ]*#define[ \t]+ARMA_USE_WRAPPER[ \t]*\r?\n" ARMA_USE_WRAPPER "${_armadillo_CONFIG_CONTENTS}") - - # ARMA_USE_LAPACK - string(REGEX MATCH "\r?\n[\t ]*#if[\t ]+!defined[(]ARMA_USE_LAPACK[)][\t ]*\r?\n[\t ]*#define[ \t]+ARMA_USE_LAPACK[ \t]*\r?\n" ARMA_USE_LAPACK "${_armadillo_CONFIG_CONTENTS}") - - # ARMA_USE_BLAS - string(REGEX MATCH "\r?\n[\t ]*#if[\t ]+!defined[(]ARMA_USE_BLAS[)][\t ]*\r?\n[\t ]*#define[ \t]+ARMA_USE_BLAS[ \t]*\r?\n" ARMA_USE_BLAS "${_armadillo_CONFIG_CONTENTS}") - # ARMA_USE_ARPACK - # ARMA_USE_ARPACK - string(REGEX MATCH "\r?\n[\t ]*#if[\t ]+!defined[(]ARMA_USE_ARPACK[)][\t ]*\r?\n[\t ]*#define[ \t]+ARMA_USE_ARPACK[ \t]*\r?\n" ARMA_USE_ARPACK "${_armadillo_CONFIG_CONTENTS}") - - # Look for #define ARMA_USE_HDF5. - string(REGEX MATCH "\r?\n[\t ]*#if[\t ]+!defined[(]ARMA_USE_HDF5[)][\t ]*\r?\n[\t ]*#define[ \t]+ARMA_USE_HDF5[ \t]*\r?\n" ARMA_USE_HDF5 "${_armadillo_CONFIG_CONTENTS}") - - # If we aren't wrapping, things get a little more complex. - if("${ARMA_USE_WRAPPER}" STREQUAL "") - set(ARMA_NEED_LIBRARY false) - message(STATUS "ARMA_USE_WRAPPER is not defined, so all dependencies of " - "Armadillo must be manually linked.") - - set(HAVE_LAPACK false) - set(HAVE_BLAS false) - - # Search for LAPACK/BLAS (or replacement). - if ((NOT "${ARMA_USE_LAPACK}" STREQUAL "") AND - (NOT "${ARMA_USE_BLAS}" STREQUAL "")) - # In order of preference: MKL, ACML, OpenBLAS, ATLAS - set(MKL_FIND_QUIETLY true) - include(ARMA_FindMKL) - set(ACMLMP_FIND_QUIETLY true) - include(ARMA_FindACMLMP) - set(ACML_FIND_QUIETLY true) - include(ARMA_FindACML) - - if (MKL_FOUND) - message(STATUS "Using MKL for LAPACK/BLAS: ${MKL_LIBRARIES}") - - set(SUPPORT_LIBRARIES "${SUPPORT_LIBRARIES}" "${MKL_LIBRARIES}") - set(HAVE_LAPACK true) - set(HAVE_BLAS true) - elseif (ACMLMP_FOUND) - message(STATUS "Using multi-core ACML libraries for LAPACK/BLAS: - ${ACMLMP_LIBRARIES}") - - set(SUPPORT_LIBRARIES "${SUPPORT_LIBRARIES}" "${ACMLMP_LIBRARIES}") - set(HAVE_LAPACK true) - set(HAVE_BLAS true) - elseif (ACML_FOUND) - message(STATUS "Using ACML for LAPACK/BLAS: ${ACML_LIBRARIES}") - - set(SUPPORT_LIBRARIES "${SUPPORT_LIBRARIES}" "${ACML_LIBRARIES}") - set(HAVE_LAPACK true) - set(HAVE_BLAS true) - endif () - endif () - - # If we haven't found BLAS, try. - if (NOT "${ARMA_USE_BLAS}" STREQUAL "" AND NOT HAVE_BLAS) - # Search for BLAS. - set(OpenBLAS_FIND_QUIETLY true) - include(ARMA_FindOpenBLAS) - set(CBLAS_FIND_QUIETLY true) - include(ARMA_FindCBLAS) - set(BLAS_FIND_QUIETLY true) - include(ARMA_FindBLAS) - - if (OpenBLAS_FOUND) - # Warn if ATLAS is found also. - if (CBLAS_FOUND) - message(STATUS "Warning: both OpenBLAS and ATLAS have been found; " - "ATLAS will not be used.") - endif () - message(STATUS "Using OpenBLAS for BLAS: ${OpenBLAS_LIBRARIES}") - - set(SUPPORT_LIBRARIES "${SUPPORT_LIBRARIES}" "${OpenBLAS_LIBRARIES}") - set(HAVE_BLAS true) - elseif (CBLAS_FOUND) - message(STATUS "Using ATLAS for BLAS: ${CBLAS_LIBRARIES}") - - set(SUPPORT_LIBRARIES "${SUPPORT_LIBRARIES}" "${CBLAS_LIBRARIES}") - set(SUPPORT_INCLUDE_DIRS "${SUPPORT_INCLUDE_DIRS}" - "${CBLAS_INCLUDE_DIR}") - set(HAVE_BLAS true) - elseif (BLAS_FOUND) - message(STATUS "Using standard BLAS: ${BLAS_LIBRARIES}") - - set(SUPPORT_LIBRARIES "${SUPPORT_LIBRARIES}" "${BLAS_LIBRARIES}") - set(HAVE_BLAS true) - endif () - endif () - - # If we haven't found LAPACK, try. - if (NOT "${ARMA_USE_LAPACK}" STREQUAL "" AND NOT HAVE_LAPACK) - # Search for LAPACK. - set(CLAPACK_FIND_QUIETLY true) - include(ARMA_FindCLAPACK) - set(LAPACK_FIND_QUIETLY true) - include(ARMA_FindLAPACK) - - # Only use ATLAS if OpenBLAS isn't being used. - if (CLAPACK_FOUND AND NOT OpenBLAS_FOUND) - message(STATUS "Using ATLAS for LAPACK: ${CLAPACK_LIBRARIES}") - - set(SUPPORT_LIBRARIES "${SUPPORT_LIBRARIES}" "${CLAPACK_LIBRARIES}") - set(SUPPORT_INCLUDE_DIRS "${SUPPORT_INCLUDE_DIRS}" - "${CLAPACK_INCLUDE_DIR}") - set(HAVE_LAPACK true) - elseif (LAPACK_FOUND) - message(STATUS "Using standard LAPACK: ${LAPACK_LIBRARIES}") - - set(SUPPORT_LIBRARIES "${SUPPORT_LIBRARIES}" "${LAPACK_LIBRARIES}") - set(HAVE_LAPACK true) - endif () - endif () - - if (NOT "${ARMA_USE_LAPACK}" STREQUAL "" AND NOT HAVE_LAPACK) - message(FATAL_ERROR "Cannot find LAPACK library, but ARMA_USE_LAPACK is " - "set. Try specifying LAPACK libraries manually by setting the " - "LAPACK_LIBRARY variable.") - endif () - - if (NOT "${ARMA_USE_BLAS}" STREQUAL "" AND NOT HAVE_BLAS) - message(FATAL_ERROR "Cannot find BLAS library, but ARMA_USE_BLAS is set. " - "Try specifying BLAS libraries manually by setting the BLAS_LIBRARY " - "variable.") - endif () - - # Search for ARPACK (or replacement). - if (NOT "${ARMA_USE_ARPACK}" STREQUAL "") - # Use Armadillo ARPACK-finding procedure. - set(ARPACK_FIND_QUIETLY true) - include(ARMA_FindARPACK) - - if (NOT ARPACK_FOUND) - message(FATAL_ERROR "ARMA_USE_ARPACK is defined in " - "armadillo_bits/config.hpp, but ARPACK cannot be found. Try " - "specifying ARPACK_LIBRARY.") - endif () - - set(SUPPORT_LIBRARIES "${SUPPORT_LIBRARIES}" "${ARPACK_LIBRARY}") - endif () - - # Search for HDF5 (or replacement). - if (NOT "${ARMA_USE_HDF5}" STREQUAL "") - find_package(HDF5 QUIET) - - if(NOT HDF5_FOUND) - # On Debian systems, the HDF5 package has been split into multiple - # packages so that it is co-installable. But this may mean that the - # include files are hidden somewhere very odd that the FindHDF5.cmake - # script will not find. Thus, we'll also quickly check pkgconfig to see - # if there is information on what to use there. - find_package(PkgConfig) - if (PKG_CONFIG_FOUND) - pkg_check_modules(HDF5 hdf5) - # But using pkgconfig is a little weird because HDF5_LIBRARIES won't - # be filled with exact library paths, like the other scripts. So - # instead what we get is HDF5_LIBRARY_DIRS which is the equivalent of - # what we'd pass to -L. - if (HDF5_FOUND) - # I'm not sure what I think of doing this here... - link_directories("${HDF5_LIBRARY_DIRS}") - endif() - endif() - endif() - - if(NOT HDF5_FOUND) - # We tried but didn't find it. - message(FATAL_ERROR "Armadillo HDF5 support is enabled, but HDF5 " - "cannot be found on the system. Consider disabling HDF5 support.") - endif() - - set(SUPPORT_INCLUDE_DIRS "${SUPPORT_INCLUDE_DIRS}" "${HDF5_INCLUDE_DIRS}") - set(SUPPORT_LIBRARIES "${SUPPORT_LIBRARIES}" "${HDF5_LIBRARIES}") - endif () - - else() - # Some older versions still require linking against HDF5 since they did not - # wrap libhdf5. This was true for versions older than 4.300. - if(NOT "${ARMA_USE_HDF5}" STREQUAL "" AND - "${ARMADILLO_VERSION_STRING}" VERSION_LESS "4.300.0") - message(STATUS "Armadillo HDF5 support is enabled and manual linking is " - "required.") - # We have HDF5 support and need to link against HDF5. - find_package(HDF5) - - if(NOT HDF5_FOUND) - # On Debian systems, the HDF5 package has been split into multiple - # packages so that it is co-installable. But this may mean that the - # include files are hidden somewhere very odd that the FindHDF5.cmake - # script will not find. Thus, we'll also quickly check pkgconfig to see - # if there is information on what to use there. - find_package(PkgConfig) - if (PKG_CONFIG_FOUND) - pkg_check_modules(HDF5 hdf5) - # But using pkgconfig is a little weird because HDF5_LIBRARIES won't - # be filled with exact library paths, like the other scripts. So - # instead what we get is HDF5_LIBRARY_DIRS which is the equivalent of - # what we'd pass to -L. - if (HDF5_FOUND) - # I'm not sure what I think of doing this here... - link_directories("${HDF5_LIBRARY_DIRS}") - endif() - endif() - endif() - - if(NOT HDF5_FOUND) - # We tried but didn't find it. - message(FATAL_ERROR "Armadillo HDF5 support is enabled, but HDF5 " - "cannot be found on the system. Consider disabling HDF5 support.") - endif() - - set(SUPPORT_INCLUDE_DIRS "${HDF5_INCLUDE_DIRS}") - set(SUPPORT_LIBRARIES "${HDF5_LIBRARIES}") - endif() - - # Versions between 4.300 and 4.500 did successfully wrap HDF5, but didn't have good support for setting the include directory correctly. - if(NOT "${ARMA_USE_HDF5}" STREQUAL "" AND - "${ARMADILLO_VERSION_STRING}" VERSION_GREATER "4.299.0" AND - "${ARMADILLO_VERSION_STRING}" VERSION_LESS "4.450.0") - message(STATUS "Armadillo HDF5 support is enabled and include " - "directories must be found.") - find_package(HDF5) - - if(NOT HDF5_FOUND) - # On Debian systems, the HDF5 package has been split into multiple - # packages so that it is co-installable. But this may mean that the - # include files are hidden somewhere very odd that the FindHDF5.cmake - # script will not find. Thus, we'll also quickly check pkgconfig to see - # if there is information on what to use there. - find_package(PkgConfig) - if (PKG_CONFIG_FOUND) - pkg_check_modules(HDF5 hdf5) - endif() - endif() - - if(NOT HDF5_FOUND) - # We tried but didn't find it. - message(FATAL_ERROR "Armadillo HDF5 support is enabled, but HDF5 " - "cannot be found on the system. Consider disabling HDF5 support.") - endif() - - set(SUPPORT_INCLUDE_DIRS "${HDF5_INCLUDE_DIRS}") - endif() - - endif() -else() - message(FATAL_ERROR "${ARMADILLO_INCLUDE_DIR}/armadillo_bits/config.hpp not " - "found! Cannot determine what to link against.") + file(STRINGS "${ARMADILLO_INCLUDE_DIR}/armadillo_bits/config.hpp" _ARMA_CONFIG_CONTENTS REGEX "^#define ARMA_USE_[A-Z]+") + string(REGEX MATCH "ARMA_USE_WRAPPER" _ARMA_USE_WRAPPER "${_ARMA_CONFIG_CONTENTS}") + string(REGEX MATCH "ARMA_USE_LAPACK" _ARMA_USE_LAPACK "${_ARMA_CONFIG_CONTENTS}") + string(REGEX MATCH "ARMA_USE_BLAS" _ARMA_USE_BLAS "${_ARMA_CONFIG_CONTENTS}") + string(REGEX MATCH "ARMA_USE_ARPACK" _ARMA_USE_ARPACK "${_ARMA_CONFIG_CONTENTS}") + string(REGEX MATCH "ARMA_USE_HDF5" _ARMA_USE_HDF5 "${_ARMA_CONFIG_CONTENTS}") endif() -if (ARMA_NEED_LIBRARY) +include(FindPackageHandleStandardArgs) + +# If _ARMA_USE_WRAPPER is set, then we just link to armadillo, but if it's not then we need support libraries instead +set(_ARMA_SUPPORT_LIBRARIES) + +if(_ARMA_USE_WRAPPER) # UNIX paths are standard, no need to write. find_library(ARMADILLO_LIBRARY NAMES armadillo PATHS "$ENV{ProgramFiles}/Armadillo/lib" "$ENV{ProgramFiles}/Armadillo/lib64" "$ENV{ProgramFiles}/Armadillo" ) + set(_ARMA_REQUIRED_VARS ARMADILLO_LIBRARY ARMADILLO_INCLUDE_DIR VERSION_VAR ARMADILLO_VERSION_STRING) +else() + # don't link to armadillo in this case + set(ARMADILLO_LIBRARY "") + if(_ARMA_USE_LAPACK) + if(ARMADILLO_FIND_QUIETLY OR NOT ARMADILLO_FIND_REQUIRED) + find_package(LAPACK QUIET) + else() + find_package(LAPCK REQUIRED) + endif() + if(LAPACK_FOUND) + set(_ARMA_SUPPORT_LIBRARIES "${_ARMA_SUPPORT_LIBRARIES}" "${LAPACK_LIBRARIES}") + endif() + endif() + if(_ARMA_USE_BLAS) + if(ARMADILLO_FIND_QUIETLY OR NOT ARMADILLO_FIND_REQUIRED) + find_package(BLAS QUIET) + else() + find_package(BLAS REQUIRED) + endif() + if(BLAS_FOUND) + set(_ARMA_SUPPORT_LIBRARIES "${_ARMA_SUPPORT_LIBRARIES}" "${BLAS_LIBRARIES}") + endif() + endif() + if(_ARMA_USE_ARPACK) + if(ARMADILLO_FIND_QUIETLY OR NOT ARMADILLO_FIND_REQUIRED) + find_package(ARPACK QUIET) + else() + find_package(ARPACK REQUIRED) + endif() + if(ARPACK_FOUND) + set(_ARMA_SUPPORT_LIBRARIES "${_ARMA_SUPPORT_LIBRARIES}" "${ARPACK_LIBRARIES}") + endif() + endif() + if(_ARMA_USE_HDF5) + find_package(HDF5 QUIET) + if(NOT HDF5_FOUND) + # On Debian systems, the HDF5 package has been split into multiple + # packages so that it is co-installable. But this may mean that the + # include files are hidden somewhere very odd that FindHDF5.cmake will + # not find. Thus, we'll also quickly check pkgconfig to see if there is + # information on what to use there. + message(WARNING "HDF5 required but not found; using PkgConfig") + find_package(PkgConfig) + if (PKG_CONFIG_FOUND) + pkg_check_modules(HDF5 REQUIRED hdf5) + link_directories("${HDF5_LIBRARY_DIRS}") + else() + message(FATAL_ERROR "PkgConfig (Used to help find HDF5) was not found") + endif() + endif() + set(_ARMA_SUPPORT_INCLUDE_DIRS "${HDF5_INCLUDE_DIRS}") + set(_ARMA_SUPPORT_LIBRARIES "${_ARMA_SUPPORT_LIBRARIES}" "${HDF5_LIBRARIES}") + endif() + set(ARMADILLO_FOUND true) + set(_ARMA_REQUIRED_VARS ARMADILLO_INCLUDE_DIR VERSION_VAR ARMADILLO_VERSION_STRING) +endif() - # Checks 'REQUIRED', 'QUIET' and versions. - include(FindPackageHandleStandardArgs) - find_package_handle_standard_args(Armadillo - REQUIRED_VARS ARMADILLO_LIBRARY ARMADILLO_INCLUDE_DIR - VERSION_VAR ARMADILLO_VERSION_STRING) - # version_var fails with cmake < 2.8.4. -else () - # Checks 'REQUIRED', 'QUIET' and versions. - include(FindPackageHandleStandardArgs) - find_package_handle_standard_args(Armadillo - REQUIRED_VARS ARMADILLO_INCLUDE_DIR - VERSION_VAR ARMADILLO_VERSION_STRING) -endif () +find_package_handle_standard_args(Armadillo REQUIRED_VARS ${_ARMA_REQUIRED_VARS}) if (ARMADILLO_FOUND) - # Also include support include directories. - set(ARMADILLO_INCLUDE_DIRS ${ARMADILLO_INCLUDE_DIR} ${SUPPORT_INCLUDE_DIRS}) - # Also include support libraries to link against. - if (ARMA_NEED_LIBRARY) - set(ARMADILLO_LIBRARIES ${ARMADILLO_LIBRARY} ${SUPPORT_LIBRARIES}) - else () - set(ARMADILLO_LIBRARIES ${SUPPORT_LIBRARIES}) - endif () - message(STATUS "Armadillo libraries: ${ARMADILLO_LIBRARIES}") + set(ARMADILLO_INCLUDE_DIRS ${ARMADILLO_INCLUDE_DIR}) + set(ARMADILLO_LIBRARIES ${ARMADILLO_LIBRARY} ${_ARMA_SUPPORT_LIBRARIES}) endif () +# Clean up internal variables +unset(_ARMA_REQUIRED_VARS) +unset(_ARMA_SUPPORT_LIBRARIES) +unset(_ARMA_USE_WRAPPER) +unset(_ARMA_USE_LAPACK) +unset(_ARMA_USE_BLAS) +unset(_ARMA_USE_ARPACK) +unset(_ARMA_USE_HDF5) +unset(_ARMA_CONFIG_CONTENTS) +unset(_ARMA_HEADER_CONTENTS) +unset(__ARMA_SUPPORT_INCLUDE_DIRS) # Hide internal variables mark_as_advanced( ARMADILLO_INCLUDE_DIR ARMADILLO_LIBRARY) - -#====================== diff --git a/CMakeLists.txt b/CMakeLists.txt index 84e96b4510..18536d4c52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -292,51 +292,6 @@ else() endif () endif() -# On Windows, Armadillo should be using LAPACK and BLAS but we still need to -# link against it. We don't want to use the FindLAPACK or FindBLAS modules -# because then we are required to have a FORTRAN compiler (argh!) so we will try -# and find LAPACK and BLAS ourselves, using a slightly modified variant of the -# script Armadillo uses to find these. -if (WIN32) - find_library(LAPACK_LIBRARY - NAMES lapack liblapack lapack_win32_MT lapack_win32 - PATHS "C:/Program Files/Armadillo" - PATH_SUFFIXES "examples/lib_win32/") - - if (NOT LAPACK_LIBRARY) - message(FATAL_ERROR "Cannot find LAPACK library (.lib)!") - endif () - - find_library(BLAS_LIBRARY - NAMES blas libblas blas_win32_MT blas_win32 - PATHS "C:/Program Files/Armadillo" - PATH_SUFFIXES "examples/lib_win32/") - - if (NOT BLAS_LIBRARY) - message(FATAL_ERROR "Cannot find BLAS library (.lib)!") - endif () - - # Piggyback LAPACK and BLAS linking into Armadillo link. - set(ARMADILLO_LIBRARIES - ${ARMADILLO_LIBRARIES} ${BLAS_LIBRARY} ${LAPACK_LIBRARY}) - - # Ensure that the libraries are added to the MSVC IDE runtime path. - get_filename_component(BLAS_DIR ${BLAS_LIBRARY} DIRECTORY) - get_filename_component(LAPACK_DIR ${LAPACK_LIBRARY} DIRECTORY) - - # Sometimes, especially with an OpenBLAS install via nuget, the DLLs are - # actually in ../../bin/x64/. Automatically add these. - if (EXISTS "${BLAS_DIR}/../../bin/x64/") - get_filename_component(BLAS_DLL_DIR "${BLAS_DIR}/../../bin/x64" ABSOLUTE) - set(DLL_COPY_DIRS ${DLL_COPY_DIRS} "${BLAS_DLL_DIR}") - endif () - - if (EXISTS "${LAPACK_DIR}/../../bin/x64/") - get_filename_component(LAPACK_DLL_DIR "${LAPACK_DIR}/../../bin/x64" ABSOLUTE) - set(DLL_COPY_DIRS ${DLL_COPY_DIRS} "${BLAS_DLL_DIR}") - endif () -endif () - # Include directories for the previous dependencies. set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} ${ARMADILLO_INCLUDE_DIRS}) set(MLPACK_LIBRARIES ${MLPACK_LIBRARIES} ${ARMADILLO_LIBRARIES}) diff --git a/doc/guide/build_windows.hpp b/doc/guide/build_windows.hpp index 469b866066..bb8ce9a1ea 100644 --- a/doc/guide/build_windows.hpp +++ b/doc/guide/build_windows.hpp @@ -118,7 +118,7 @@ compiler version, check if the Visual Studio compiler and Windows SDK are instal - Run cmake: @code -cmake -G "Visual Studio 16 2019" -A x64 -DBLAS_LIBRARY:FILEPATH="C:/mlpack/mlpack/packages/OpenBLAS.0.2.14.1/lib/native/lib/x64/libopenblas.dll.a" -DLAPACK_LIBRARY:FILEPATH="C:/mlpack/mlpack/packages/OpenBLAS.0.2.14.1/lib/native/lib/x64/libopenblas.dll.a" -DARMADILLO_INCLUDE_DIR="C:/mlpack/armadillo/include" -DARMADILLO_LIBRARY:FILEPATH="C:/mlpack/armadillo/build/Debug/armadillo.lib" -DBOOST_INCLUDEDIR:PATH="C:/boost/" -DBOOST_LIBRARYDIR:PATH="C:/boost/lib64-msvc-14.2" -DDEBUG=OFF -DPROFILE=OFF .. +cmake -G "Visual Studio 16 2019" -A x64 -DBLAS_LIBRARIES:FILEPATH="C:/mlpack/mlpack/packages/OpenBLAS.0.2.14.1/lib/native/lib/x64/libopenblas.dll.a" -DLAPACK_LIBRARIES:FILEPATH="C:/mlpack/mlpack/packages/OpenBLAS.0.2.14.1/lib/native/lib/x64/libopenblas.dll.a" -DARMADILLO_INCLUDE_DIR="C:/mlpack/armadillo/include" -DARMADILLO_LIBRARY:FILEPATH="C:/mlpack/armadillo/build/Debug/armadillo.lib" -DBOOST_INCLUDEDIR:PATH="C:/boost/" -DBOOST_LIBRARYDIR:PATH="C:/boost/lib64-msvc-14.2" -DDEBUG=OFF -DPROFILE=OFF .. @endcode @note cmake will attempt to automatically download the ensmallen dependency. If for some reason cmake can't download the dependency, you will need to manually download ensmallen from http://ensmallen.org/ and extract it to "C:\mlpack\mlpack\deps\". Then, specify the path to ensmallen using the flag: -DENSMALLEN_INCLUDE_DIR=C:/mlpack/mlpack/deps/ensmallen/include From 2c27ef792b1276d9c5f6d5d2550e5051bedcb07b Mon Sep 17 00:00:00 2001 From: adithya-tp Date: Thu, 12 Mar 2020 00:26:07 +0400 Subject: [PATCH 097/265] Removed extra endl after printing description --- src/mlpack/bindings/markdown/print_docs.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/bindings/markdown/print_docs.cpp b/src/mlpack/bindings/markdown/print_docs.cpp index 218e5e4c32..2ebb67b8bd 100644 --- a/src/mlpack/bindings/markdown/print_docs.cpp +++ b/src/mlpack/bindings/markdown/print_docs.cpp @@ -127,7 +127,7 @@ void PrintDocs(const std::string& bindingName, cout << ParamString(it->second.name) << " | "; cout << ParamType(it->second) << " | "; string desc = boost::replace_all_copy(it->second.desc, "|", "\\|"); - cout << desc << endl; // just a string + cout << desc; // just a string // Print whether or not it's a "special" language-only parameter. if (it->second.name == "copy_all_inputs" || it->second.name == "help" || it->second.name == "info" || it->second.name == "version") From 91a0a10fb059e15916c0192e8982ecc5c9e7164a Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Thu, 12 Mar 2020 15:41:55 +0530 Subject: [PATCH 098/265] Fix build according to r-value tests --- src/mlpack/tests/ann_layer_test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mlpack/tests/ann_layer_test.cpp b/src/mlpack/tests/ann_layer_test.cpp index 76f02fa322..8992b7904a 100644 --- a/src/mlpack/tests/ann_layer_test.cpp +++ b/src/mlpack/tests/ann_layer_test.cpp @@ -3053,7 +3053,7 @@ BOOST_AUTO_TEST_CASE(MaxPoolingTestCase) MaxPooling<> module1(2, 2, 2, 1); module1.InputHeight() = 3; module1.InputWidth() = 4; - module1.Forward(std::move(input), std::move(output)); + module1.Forward(input, output); // Calculated using torch.nn.MaxPool2d(). BOOST_REQUIRE_EQUAL(arma::accu(output), 28); BOOST_REQUIRE_EQUAL(output.n_elem, 4); @@ -3072,7 +3072,7 @@ BOOST_AUTO_TEST_CASE(MaxPoolingTestCase) MaxPooling<> module2(3, 2, 3, 1); module2.InputHeight() = 3; module2.InputWidth() = 3; - module2.Forward(std::move(input), std::move(output)); + module2.Forward(input, output); // Calculated using torch.nn.MaxPool2d(). BOOST_REQUIRE_EQUAL(arma::accu(output), 12.0); BOOST_REQUIRE_EQUAL(output.n_elem, 2); @@ -3091,7 +3091,7 @@ BOOST_AUTO_TEST_CASE(MaxPoolingTestCase) MaxPooling<> module3(2, 2, 1, 1); module3.InputHeight() = 4; module3.InputWidth() = 4; - module3.Forward(std::move(input), std::move(output)); + module3.Forward(input, output); // Calculated using torch.nn.MaxPool2d(). BOOST_REQUIRE_EQUAL(arma::accu(output), 30.0); BOOST_REQUIRE_EQUAL(output.n_elem, 9); @@ -3108,7 +3108,7 @@ BOOST_AUTO_TEST_CASE(MaxPoolingTestCase) MaxPooling<> module4(2, 1, 1, 1); module4.InputHeight() = 2; module4.InputWidth() = 3; - module4.Forward(std::move(input), std::move(output)); + module4.Forward(input, output); // Calculated using torch.nn.MaxPool2d(). BOOST_REQUIRE_EQUAL(arma::accu(output), 3); BOOST_REQUIRE_EQUAL(output.n_elem, 4); From 1348aa3be3f69f1708aa9e48ff28ecbb1a21d228 Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Thu, 12 Mar 2020 16:02:18 +0530 Subject: [PATCH 099/265] Restart build, changed comment to do so --- src/mlpack/tests/ann_layer_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/tests/ann_layer_test.cpp b/src/mlpack/tests/ann_layer_test.cpp index 8992b7904a..301205dca1 100644 --- a/src/mlpack/tests/ann_layer_test.cpp +++ b/src/mlpack/tests/ann_layer_test.cpp @@ -3035,7 +3035,7 @@ BOOST_AUTO_TEST_CASE(TransposedConvolutionLayerPaddingTest) */ BOOST_AUTO_TEST_CASE(MaxPoolingTestCase) { - // For rectangular input. + // For rectangular input to pooling layers. arma::mat input = arma::mat(12, 1); arma::mat output; input.zeros(); From 934681a1fe99259195672e8d40a37ee9a21c1ee5 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Thu, 12 Mar 2020 13:13:55 +0000 Subject: [PATCH 100/265] Use Cint to match C++'s int type, not Julia's Int. --- src/mlpack/bindings/julia/mlpack/cli.jl.in | 19 ++++++++++--------- .../julia/print_input_processing_impl.hpp | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/mlpack/bindings/julia/mlpack/cli.jl.in b/src/mlpack/bindings/julia/mlpack/cli.jl.in index d4e7271cfa..1659650932 100644 --- a/src/mlpack/bindings/julia/mlpack/cli.jl.in +++ b/src/mlpack/bindings/julia/mlpack/cli.jl.in @@ -66,8 +66,8 @@ function CLIRestoreSettings(programName::String) end function CLISetParam(paramName::String, paramValue::Int) - ccall((:CLI_SetParamInt, library), Nothing, (Cstring, Int), paramName, - paramValue); + ccall((:CLI_SetParamInt, library), Nothing, (Cstring, Cint), paramName, + Cint(paramValue)); end function CLISetParam(paramName::String, paramValue::Float64) @@ -126,9 +126,9 @@ function CLISetParam(paramName::String, end function CLISetParam(paramName::String, - vector::Vector{Int}) - ccall((:CLI_SetParamVectorInt, library), Nothing, (Cstring, Ptr{Int}, - Int), paramName, Base.pointer(vector), size(vector, 1)); + vector::Vector{Cint}) + ccall((:CLI_SetParamVectorInt, library), Nothing, (Cstring, Ptr{Cint}, + Csize_t), paramName, Base.pointer(vector), size(vector, 1)); end function CLISetParam(paramName::String, @@ -189,7 +189,7 @@ function CLIGetParamBool(paramName::String) end function CLIGetParamInt(paramName::String) - return ccall((:CLI_GetParamInt, library), Int, (Cstring,), paramName) + return Int(ccall((:CLI_GetParamInt, library), Cint, (Cstring,), paramName)) end function CLIGetParamDouble(paramName::String) @@ -219,16 +219,17 @@ end function CLIGetParamVectorInt(paramName::String) local size::Csize_t - local ptr::Ptr{Int} + local ptr::Ptr{Cint} # Get the size of the vector, then the pointer to it. We will own the # pointer. size = ccall((:CLI_GetParamVectorIntLen, library), Csize_t, (Cstring,), paramName); - ptr = ccall((:CLI_GetParamVectorIntPtr, library), Ptr{Int}, (Cstring,), + ptr = ccall((:CLI_GetParamVectorIntPtr, library), Ptr{Cint}, (Cstring,), paramName); - return Base.unsafe_wrap(Array{Int, 1}, ptr, (size), own=true) + return convert(Array{Int, 1}, Base.unsafe_wrap(Array{Cint, 1}, ptr, (size), + own=true)) end function CLIGetParamMat(paramName::String, pointsAsRows::Bool) diff --git a/src/mlpack/bindings/julia/print_input_processing_impl.hpp b/src/mlpack/bindings/julia/print_input_processing_impl.hpp index 6839abf29d..444c4dedc7 100644 --- a/src/mlpack/bindings/julia/print_input_processing_impl.hpp +++ b/src/mlpack/bindings/julia/print_input_processing_impl.hpp @@ -47,8 +47,19 @@ void PrintInputProcessing( // CLISetParam("", convert(, )) // end std::cout << " if !ismissing(" << juliaName << ")" << std::endl; - std::cout << " CLISetParam(\"" << d.name << "\", convert(" - << GetJuliaType() << ", " << juliaName << "))" << std::endl; + std::cout << " CLISetParam(\"" << d.name << "\", convert("; + if (std::is_same>::value) + { + // Special case: the type we will call CLISetParam() with for a + // vector is not Julia's "Vector{Int}" type but instead + // "Vector{Cint}", so we have to make that conversion manually. + std::cout << "Vector{Cint}"; + } + else + { + std::cout << GetJuliaType(); + } + std::cout << ", " << juliaName << "))" << std::endl; std::cout << " end" << std::endl; } } From 69ccaab3a6dd3c16aa81f0e0b0b3ab3f3dc6b6bd Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Thu, 12 Mar 2020 13:31:29 +0000 Subject: [PATCH 101/265] Move Vector{Cint} conversion into cli.jl. --- src/mlpack/bindings/julia/mlpack/cli.jl.in | 5 +++-- .../julia/print_input_processing_impl.hpp | 15 ++------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/mlpack/bindings/julia/mlpack/cli.jl.in b/src/mlpack/bindings/julia/mlpack/cli.jl.in index 1659650932..b5f7c2c0a6 100644 --- a/src/mlpack/bindings/julia/mlpack/cli.jl.in +++ b/src/mlpack/bindings/julia/mlpack/cli.jl.in @@ -126,9 +126,10 @@ function CLISetParam(paramName::String, end function CLISetParam(paramName::String, - vector::Vector{Cint}) + vector::Vector{Int}) + cint_vec = convert(Vector{Cint}, vector) ccall((:CLI_SetParamVectorInt, library), Nothing, (Cstring, Ptr{Cint}, - Csize_t), paramName, Base.pointer(vector), size(vector, 1)); + Csize_t), paramName, Base.pointer(cint_vec), size(cint_vec, 1)); end function CLISetParam(paramName::String, diff --git a/src/mlpack/bindings/julia/print_input_processing_impl.hpp b/src/mlpack/bindings/julia/print_input_processing_impl.hpp index 444c4dedc7..6839abf29d 100644 --- a/src/mlpack/bindings/julia/print_input_processing_impl.hpp +++ b/src/mlpack/bindings/julia/print_input_processing_impl.hpp @@ -47,19 +47,8 @@ void PrintInputProcessing( // CLISetParam("", convert(, )) // end std::cout << " if !ismissing(" << juliaName << ")" << std::endl; - std::cout << " CLISetParam(\"" << d.name << "\", convert("; - if (std::is_same>::value) - { - // Special case: the type we will call CLISetParam() with for a - // vector is not Julia's "Vector{Int}" type but instead - // "Vector{Cint}", so we have to make that conversion manually. - std::cout << "Vector{Cint}"; - } - else - { - std::cout << GetJuliaType(); - } - std::cout << ", " << juliaName << "))" << std::endl; + std::cout << " CLISetParam(\"" << d.name << "\", convert(" + << GetJuliaType() << ", " << juliaName << "))" << std::endl; std::cout << " end" << std::endl; } } From f482383a8bb0f915829510879afa604cc514cabd Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Thu, 12 Mar 2020 16:48:49 +0000 Subject: [PATCH 102/265] Don't build Julia bindings for regular Windows builds. --- .ci/windows-steps.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.ci/windows-steps.yaml b/.ci/windows-steps.yaml index 4d8b9e7923..08dfa68f58 100644 --- a/.ci/windows-steps.yaml +++ b/.ci/windows-steps.yaml @@ -66,6 +66,7 @@ steps: -DARMADILLO_LIBRARY="..\armadillo-8.400.0\Release\armadillo.lib" ` -DBOOST_INCLUDEDIR=$(Agent.ToolsDirectory)\boost.1.60.0.0\lib\native\include ` -DBOOST_LIBRARYDIR=$(Agent.ToolsDirectory)\boost_libs ` + -DBUILD_JULIA_BINDINGS=OFF ` -DCMAKE_BUILD_TYPE=Release .. displayName: 'Configure mlpack' From ae91ff7113684e45bddc05b571d79772bb972193 Mon Sep 17 00:00:00 2001 From: Mrityunjay Tripathi Date: Thu, 12 Mar 2020 23:19:46 +0530 Subject: [PATCH 103/265] removed r-value reference --- src/mlpack/methods/ann/loss_functions/huber_loss.hpp | 8 ++++---- src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp | 8 ++++---- src/mlpack/tests/loss_functions_test.cpp | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp index 4b021ca193..f7beff39e1 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp @@ -55,7 +55,7 @@ class HuberLoss * @param target The target vector. */ template - double Forward(const InputType&& input, const TargetType&& target); + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. @@ -65,9 +65,9 @@ class HuberLoss * @param output The calculated error. */ template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp index 6207446dfc..a7c3c6f2bb 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp @@ -31,7 +31,7 @@ HuberLoss::HuberLoss( template template double HuberLoss::Forward( - const InputType&& input, const TargetType&& target) + const InputType& input, const TargetType& target) { double loss = 0; double absError; @@ -48,9 +48,9 @@ double HuberLoss::Forward( template template void HuberLoss::Backward( - const InputType&& input, - const TargetType&& target, - OutputType&& output) + const InputType& input, + const TargetType& target, + OutputType& output) { output.set_size(size(input)); double absError; diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index e9c0df1a18..e086adf1e4 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -45,11 +45,11 @@ BOOST_AUTO_TEST_CASE(HuberLossTest) // Test the Forward function. input = arma::mat("17.45 12.91 13.63 29.01 7.12 15.47 31.52 31.97"); target = arma::mat("16.52 13.11 13.67 29.51 24.31 15.03 30.72 34.07"); - double loss = module.Forward(std::move(input), std::move(target)); + double loss = module.Forward(input, target); BOOST_REQUIRE_CLOSE_FRACTION(loss, 2.410631, 0.00001); // Test the backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input, target, output); // Expected Output: // [0.1162 -0.0250 -0.0050 -0.0625 -0.1250 0.0550 0.1000 -0.1250] From 2e0dc1952200facddde00253e7b83dd070395e53 Mon Sep 17 00:00:00 2001 From: Sriram Date: Fri, 13 Mar 2020 00:41:27 +0530 Subject: [PATCH 104/265] Added background of tutorial --- .../reinforcement_learning.txt | 91 +++++++++++++++++++ doc/tutorials/rl/rl.txt | 9 -- 2 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 doc/tutorials/reinforcement_learning/reinforcement_learning.txt delete mode 100644 doc/tutorials/rl/rl.txt diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt new file mode 100644 index 0000000000..10447414ad --- /dev/null +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -0,0 +1,91 @@ +/*! +@file rl.txt +@author Sriram S K +@brief Tutorial for how to use the Reinforcement Learning module in mlpack. + +@page rltutorial Reinforcement Learning Tutorial + +@section intro_rltut Introduction + +Reinforcement Learning is one of the hottest topics right now, with +interest surging after DeepMind published their article on training +deep neural networks to play Atari games to great success. mlpack +implements a complete end-to-end framework for Reinforcement Learning, +featuring multiple environments, policies and methods. Of course, +custom environments and policies can be used and plugged into the +existing framework with no runtime overhead. + +mlpack implements typical benchmark environments (Acrobot, Mountain car etc.), +commonly used policies and replay methods and supports asynchronous +learning as well. In addition, it can [communicate](https://github.com/zoq/gym_tcp_api) +with the OpenAI Gym toolkit for more environments. + +@section toc_rltut Table of Contents + +This tutorial is split into the following sections: + + - \ref intro_rltut + - \ref toc_rltut + - \ref environment_rltut + - \ref agent_components_rltut + - \ref q_learning_rltut + - \ref async_learning_rltut + +@section environment_rltut Reinforcement Learning Environments + +mlpack implements a number of the most popular environments used for testing +RL agents and algorithms. These include the Cart Pole, Acrobot, Mountain Car +and their variations. Of course, as mentioned above, you can communicate with +OpenAI Gym for other environments, like the Atari video games. + +A key component of mlpack is its extensibility. It is a simple process to create +your own custom environments, specific to your needs, and use it with mlpack's +RL framework. All the environments implement a few specific methods and classes +which are used by the agents while learning. + +- \c State: The State class is a representation of the environment. For the CartPole, + this would involve storing the position, velocity, angle and angular velocity. + +- \c Action: It is an enum naming all the possible actions the agent can take in the + environment. Continuing with the CartPole example, the Action enum would simply + contain the two possible actions, backward and forward. + +- \c Sample: This method is perhaps the heart of the environment, providing rewards to + the agent depending on the state and the action taken, and updates the state based on + the action taken as well. + +Of course, your custom environment will most likely a number of helper methods, depending +on your application, such as the Dsdt method in the Acrobot environment, used in the RK4 +iterative method (also another helper method) to estimate the next state. + +@section agent_components_rltut Components of an RL Agent + +A Reinforcement Learning agent, in general, takes actions in an environment in order +to maximize a cumulative reward. To that end, it requires a way to choose actions (\b policy) +and a way to sample previous experiences (\b replay). + +An example of a simple policy would be an epsilon-greedy policy. Using such a policy, the agent +will choose actions greedily with some probability epsilon. This probability is slowly decreased +over time, balancing the line between exploration and exploitation. + +Similarly, an example of a simple reply would be a random replay. At each time step, the +interactions between the agent and the environment are saved to a memory buffer and previous +experiences are sampled from the buffer to train the agent. + +Instantiating the components of an agent can be easily done by passing the Environment as +a templated argument and the parameters of the policy/replay to the constructor. + +To create a Greedy Policy and Prioritized Replay for the CartPole environment, we would do the +following: + +@code +GreedyPolicy policy(1.0, 1000, 0.1); +PrioritizedReplay replayMethod(10, 10000, 0.6); +@endcode + +The arguments to `policy` are the initial epsilon values, the intenrval of decrease in its value +and the value at which epsilon bottoms out and won't be reduced further. The arguments to +`replayMethod` are size of the batch returned, the number of examples stored in memory, and the +degree of prioritization. + +@section q_learning_rltut Q-Learning in mlpack \ No newline at end of file diff --git a/doc/tutorials/rl/rl.txt b/doc/tutorials/rl/rl.txt deleted file mode 100644 index 5cf76ac623..0000000000 --- a/doc/tutorials/rl/rl.txt +++ /dev/null @@ -1,9 +0,0 @@ -/*! -@file rl.txt -@author Sriram S K -@brief Tutorial for how to use the Reinforcement Learning module in mlpack - -@page rltutorial Reinforcement Learning Tutorial - -@section intro_rltut Introduction - From 3f07bcfef3a3271b873480a06964fc71ff28e9e3 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Thu, 12 Mar 2020 21:04:43 +0000 Subject: [PATCH 105/265] Only add test if BUILD_JULIA_BINDINGS is specified. --- src/mlpack/bindings/julia/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/bindings/julia/CMakeLists.txt b/src/mlpack/bindings/julia/CMakeLists.txt index bfc3faba00..461d7b7a22 100644 --- a/src/mlpack/bindings/julia/CMakeLists.txt +++ b/src/mlpack/bindings/julia/CMakeLists.txt @@ -154,6 +154,6 @@ if (BUILD_JULIA_BINDINGS) endif () endmacro () -if (BUILD_TESTS) +if (BUILD_TESTS AND BUILD_JULIA_BINDINGS) add_subdirectory(tests) endif () From 4bb869a1955bc4c4fcf52ce2445b00af8921e70d Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Thu, 12 Mar 2020 23:37:54 +0000 Subject: [PATCH 106/265] Switch to correct size_t type. --- src/mlpack/bindings/julia/julia_util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/bindings/julia/julia_util.cpp b/src/mlpack/bindings/julia/julia_util.cpp index 7d089bde50..faa639956f 100644 --- a/src/mlpack/bindings/julia/julia_util.cpp +++ b/src/mlpack/bindings/julia/julia_util.cpp @@ -246,7 +246,7 @@ size_t CLI_GetParamVectorStrLen(const char* paramName) /** * Call CLI::GetParam>() and get the i'th string. */ -const char* CLI_GetParamVectorStrStr(const char* paramName, const int i) +const char* CLI_GetParamVectorStrStr(const char* paramName, const size_t i) { return CLI::GetParam>(paramName)[i].c_str(); } From abe5764cc665f7ce521e63abd9fdd20fb5f3ec5a Mon Sep 17 00:00:00 2001 From: Mrityunjay Tripathi Date: Fri, 13 Mar 2020 07:25:47 +0530 Subject: [PATCH 107/265] typo fix --- src/mlpack/methods/ann/layer/recurrent_attention.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/ann/layer/recurrent_attention.hpp b/src/mlpack/methods/ann/layer/recurrent_attention.hpp index 4152b4058f..3c43d75d0e 100644 --- a/src/mlpack/methods/ann/layer/recurrent_attention.hpp +++ b/src/mlpack/methods/ann/layer/recurrent_attention.hpp @@ -65,9 +65,9 @@ class RecurrentAttention /** * Create the RecurrentAttention object using the specified modules. * - * @param start The module output size. - * @param start The recurrent neural network module. - * @param start The action module. + * @param outSize The module output size. + * @param rnn The recurrent neural network module. + * @param action The action module. * @param rho Maximum number of steps to backpropagate through time (BPTT). */ template From 744ddc5c4ddcd6c8292a683f5e4223c40ed5c2dd Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Fri, 13 Mar 2020 16:22:00 +0530 Subject: [PATCH 108/265] Removed r-value references and some style fixes --- src/mlpack/core/metrics/lmetric.hpp | 2 +- .../core/metrics/mahalanobis_distance.hpp | 2 +- .../metrics/mahalanobis_distance_impl.hpp | 2 +- src/mlpack/methods/ann/layer/hardshrink.hpp | 8 +-- .../methods/ann/layer/hardshrink_impl.hpp | 4 +- src/mlpack/methods/ann/layer/max_pooling.hpp | 12 ++-- src/mlpack/methods/ann/layer/mean_pooling.hpp | 7 +-- src/mlpack/methods/ann/layer_names.hpp | 62 +++++++++---------- .../ann/loss_functions/log_cosh_loss.hpp | 8 +-- .../ann/loss_functions/log_cosh_loss_impl.hpp | 8 +-- .../tests/activation_functions_test.cpp | 5 +- src/mlpack/tests/loss_functions_test.cpp | 8 +-- 12 files changed, 62 insertions(+), 66 deletions(-) diff --git a/src/mlpack/core/metrics/lmetric.hpp b/src/mlpack/core/metrics/lmetric.hpp index d92bc0ea8f..370ecc7517 100644 --- a/src/mlpack/core/metrics/lmetric.hpp +++ b/src/mlpack/core/metrics/lmetric.hpp @@ -63,7 +63,7 @@ template class LMetric { public: - /*** + /** * Default constructor does nothing, but is required to satisfy the Metric * policy. */ diff --git a/src/mlpack/core/metrics/mahalanobis_distance.hpp b/src/mlpack/core/metrics/mahalanobis_distance.hpp index 3547202eee..9c251cbeb3 100644 --- a/src/mlpack/core/metrics/mahalanobis_distance.hpp +++ b/src/mlpack/core/metrics/mahalanobis_distance.hpp @@ -1,4 +1,4 @@ -/*** +/** * @file mahalanobis_distance.hpp * @author Ryan Curtin * diff --git a/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp b/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp index 58aea1ab5d..16763cc4c5 100644 --- a/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp +++ b/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp @@ -1,4 +1,4 @@ -/*** +/** * @file mahalanobis_distance_impl.hpp * @author Ryan Curtin * diff --git a/src/mlpack/methods/ann/layer/hardshrink.hpp b/src/mlpack/methods/ann/layer/hardshrink.hpp index 170eb21524..aa7f51b550 100644 --- a/src/mlpack/methods/ann/layer/hardshrink.hpp +++ b/src/mlpack/methods/ann/layer/hardshrink.hpp @@ -66,7 +66,7 @@ class HardShrink * @param output Resulting output activation. */ template - void Forward(const InputType&& input, OutputType&& output); + void Forward(const InputType& input, OutputType& output); /** * Ordinary feed backward pass of a neural network, calculating the function @@ -78,9 +78,9 @@ class HardShrink * @param g The calculated gradient. */ template - void Backward(const DataType&& input, - DataType&& gy, - DataType&& g); + void Backward(const DataType& input, + DataType& gy, + DataType& g); //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/layer/hardshrink_impl.hpp b/src/mlpack/methods/ann/layer/hardshrink_impl.hpp index 669d4f13d2..d1f9112eaa 100644 --- a/src/mlpack/methods/ann/layer/hardshrink_impl.hpp +++ b/src/mlpack/methods/ann/layer/hardshrink_impl.hpp @@ -30,7 +30,7 @@ HardShrink::HardShrink(const double lambda) : template template void HardShrink::Forward( - const InputType&& input, OutputType&& output) + const InputType& input, OutputType& output) { output = ((input > lambda) + (input < -lambda)) % input; } @@ -38,7 +38,7 @@ void HardShrink::Forward( template template void HardShrink::Backward( - const DataType&& input, DataType&& gy, DataType&& g) + const DataType& input, DataType& gy, DataType& g) { DataType derivative; derivative = (arma::ones(arma::size(input)) - (input == 0)); diff --git a/src/mlpack/methods/ann/layer/max_pooling.hpp b/src/mlpack/methods/ann/layer/max_pooling.hpp index ddaa45d42b..3f3af81949 100644 --- a/src/mlpack/methods/ann/layer/max_pooling.hpp +++ b/src/mlpack/methods/ann/layer/max_pooling.hpp @@ -179,8 +179,6 @@ class MaxPooling arma::Mat& output, arma::Mat& poolingIndices) { - const size_t rStep = kernelWidth; - const size_t cStep = kernelHeight; for (size_t j = 0, colidx = 0; j < output.n_cols; ++j, colidx += strideHeight) { @@ -188,16 +186,18 @@ class MaxPooling ++i, rowidx += strideWidth) { arma::mat subInput = input( - arma::span(rowidx, rowidx + rStep - 1 - offset), - arma::span(colidx, colidx + cStep - 1 - offset)); + arma::span(rowidx, rowidx + kernelWidth - 1 - offset), + arma::span(colidx, colidx + kernelHeight - 1 - offset)); + const size_t idx = pooling.Pooling(subInput); output(i, j) = subInput(idx); if (!deterministic) { arma::Mat subIndices = indices(arma::span(rowidx, - rowidx + rStep - 1 - offset), - arma::span(colidx, colidx + cStep - 1 - offset)); + rowidx + kernelWidth - 1 - offset), + arma::span(colidx, colidx + kernelHeight - 1 - offset)); + poolingIndices(i, j) = subIndices(idx); } } diff --git a/src/mlpack/methods/ann/layer/mean_pooling.hpp b/src/mlpack/methods/ann/layer/mean_pooling.hpp index a3da66e441..34c3a811e2 100644 --- a/src/mlpack/methods/ann/layer/mean_pooling.hpp +++ b/src/mlpack/methods/ann/layer/mean_pooling.hpp @@ -156,9 +156,6 @@ class MeanPooling template void Pooling(const arma::Mat& input, arma::Mat& output) { - const size_t rStep = kernelWidth; - const size_t cStep = kernelHeight; - for (size_t j = 0, colidx = 0; j < output.n_cols; ++j, colidx += strideHeight) { @@ -166,8 +163,8 @@ class MeanPooling ++i, rowidx += strideWidth) { arma::mat subInput = input( - arma::span(rowidx, rowidx + rStep - 1 - offset), - arma::span(colidx, colidx + cStep - 1 - offset)); + arma::span(rowidx, rowidx + kernelWidth - 1 - offset), + arma::span(colidx, colidx + kernelHeight - 1 - offset)); output(i, j) = arma::mean(arma::mean(subInput)); } diff --git a/src/mlpack/methods/ann/layer_names.hpp b/src/mlpack/methods/ann/layer_names.hpp index 64775c1d6d..ea1150b14e 100644 --- a/src/mlpack/methods/ann/layer_names.hpp +++ b/src/mlpack/methods/ann/layer_names.hpp @@ -30,7 +30,7 @@ class LayerNameVisitor : public boost::static_visitor { } - /* + /** * Return the name of the given layer of type AtrousConvolution as a string. * * @param Given layer of type AtrousConvolution. @@ -41,7 +41,7 @@ class LayerNameVisitor : public boost::static_visitor return "atrousconvolution"; } - /* + /** * Return the name of the given layer of type AlphaDropout as a string. * * @param Given layer of type AlphaDropout. @@ -52,7 +52,7 @@ class LayerNameVisitor : public boost::static_visitor return "alphadropout"; } - /* + /** * Return the name of the given layer of type BatchNorm as a string. * * @param Given layer of type BatchNorm. @@ -63,7 +63,7 @@ class LayerNameVisitor : public boost::static_visitor return "batchnorm"; } - /* + /** * Return the name of the given layer of type Constant as a string. * * @param Given layer of type Constant. @@ -74,7 +74,7 @@ class LayerNameVisitor : public boost::static_visitor return "constant"; } - /* + /** * Return the name of the given layer of type Convolution as a string. * * @param Given layer of type Convolution. @@ -85,7 +85,7 @@ class LayerNameVisitor : public boost::static_visitor return "convolution"; } - /* + /** * Return the name of the given layer of type DropConnect as a string. * * @param Given layer of type DropConnect. @@ -96,7 +96,7 @@ class LayerNameVisitor : public boost::static_visitor return "dropconnect"; } - /* + /** * Return the name of the given layer of type Dropout as a string. * * @param Given layer of type Dropout. @@ -107,7 +107,7 @@ class LayerNameVisitor : public boost::static_visitor return "dropout"; } - /* + /** * Return the name of the given layer of type FlexibleReLU as a string. * * @param Given layer of type FlexibleReLU. @@ -118,7 +118,7 @@ class LayerNameVisitor : public boost::static_visitor return "flexiblerelu"; } - /* + /** * Return the name of the given layer of type LayerNorm as a string. * * @param Given layer of type LayerNorm. @@ -129,7 +129,7 @@ class LayerNameVisitor : public boost::static_visitor return "layernorm"; } - /* + /** * Return the name of the given layer of type Linear as a string. * * @param Given layer of type Linear. @@ -140,7 +140,7 @@ class LayerNameVisitor : public boost::static_visitor return "linear"; } - /* + /** * Return the name of the given layer of type LinearNoBias as a string. * * @param Given layer of type LinearNoBias. @@ -151,7 +151,7 @@ class LayerNameVisitor : public boost::static_visitor return "linearnobias"; } - /* + /** * Return the name of the given layer of type MaxPooling as a string. * * @param Given layer of type MaxPooling. @@ -162,7 +162,7 @@ class LayerNameVisitor : public boost::static_visitor return "maxpooling"; } - /* + /** * Return the name of the given layer of type MeanPooling as a string. * * @param Given layer of type MeanPooling. @@ -173,7 +173,7 @@ class LayerNameVisitor : public boost::static_visitor return "meanpooling"; } - /* + /** * Return the name of the given layer of type MultiplyConstant as a string. * * @param Given layer of type MultiplyConstant. @@ -184,7 +184,7 @@ class LayerNameVisitor : public boost::static_visitor return "multiplyconstant"; } - /* + /** * Return the name of the given layer of type ReLULayer as a string. * * @param Given layer of type ReLULayer. @@ -195,7 +195,7 @@ class LayerNameVisitor : public boost::static_visitor return "relu"; } - /* + /** * Return the name of the given layer of type TransposedConvolution as a * string. * @@ -207,7 +207,7 @@ class LayerNameVisitor : public boost::static_visitor return "transposedconvolution"; } - /* + /** * Return the name of the given layer of type IdentityLayer as a string. * * @param Given layer of type IdentityLayer. @@ -218,7 +218,7 @@ class LayerNameVisitor : public boost::static_visitor return "identity"; } - /* + /** * Return the name of the given layer of type TanHLayer as a string. * * @param Given layer of type TanHLayer. @@ -229,7 +229,7 @@ class LayerNameVisitor : public boost::static_visitor return "tanh"; } - /* + /** * Return the name of the given layer of type ELU as a string. * * @param Given layer of type ELU. @@ -240,7 +240,7 @@ class LayerNameVisitor : public boost::static_visitor return "elu"; } - /* + /** * Return the name of the given layer of type HardTanH as a string. * * @param Given layer of type HardTanH. @@ -251,7 +251,7 @@ class LayerNameVisitor : public boost::static_visitor return "hardtanh"; } - /* + /** * Return the name of the given layer of type LeakyReLU as a string. * * @param Given layer of type LeakyReLU. @@ -262,7 +262,7 @@ class LayerNameVisitor : public boost::static_visitor return "leakyrelu"; } - /* + /** * Return the name of the given layer of type PReLU as a string. * * @param Given layer of type PReLU. @@ -273,7 +273,7 @@ class LayerNameVisitor : public boost::static_visitor return "prelu"; } - /* + /** * Return the name of the given layer of type SigmoidLayer as a string. * * @param Given layer of type SigmoidLayer. @@ -284,7 +284,7 @@ class LayerNameVisitor : public boost::static_visitor return "sigmoid"; } - /* + /** * Return the name of the given layer of type LogSoftMax as a string. * * @param Given layer of type LogSoftMax. @@ -306,7 +306,7 @@ class LayerNameVisitor : public boost::static_visitor return "lstm"; } - /* + /** * Return the name of the given layer of type CReLU as a string. * * @param Given layer of type CReLU. @@ -317,7 +317,7 @@ class LayerNameVisitor : public boost::static_visitor return "crelu"; } - /* + /** * Return the name of the given layer of type Highway as a string. * * @param Given layer of type Highway. @@ -328,7 +328,7 @@ class LayerNameVisitor : public boost::static_visitor return "highway"; } - /* + /** * Return the name of the given layer of type GRU as a string. * * @param Given layer of type GRU. @@ -339,7 +339,7 @@ class LayerNameVisitor : public boost::static_visitor return "gru"; } - /* + /** * Return the name of the given layer of type Glimpse as a string. * * @param Given layer of type Glimpse. @@ -350,7 +350,7 @@ class LayerNameVisitor : public boost::static_visitor return "glimpse"; } - /* + /** * Return the name of the given layer of type FastLSTM as a string. * * @param Given layer of type FastLSTM. @@ -361,7 +361,7 @@ class LayerNameVisitor : public boost::static_visitor return "fastlstm"; } - /* + /** * Return the name of the given layer of type WeightNorm as a string. * * @param Given layer of type WeightNorm. @@ -372,7 +372,7 @@ class LayerNameVisitor : public boost::static_visitor return "weightnorm"; } - /* + /** * Return the name of the layer of specified type as a string. * * @param Given layer of any type. diff --git a/src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp b/src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp index 788c2ef842..d528157b11 100644 --- a/src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp @@ -55,7 +55,7 @@ class LogCoshLoss * @param target Target data to compare with. */ template - double Forward(const InputType&& input, const TargetType&& target); + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. @@ -65,9 +65,9 @@ class LogCoshLoss * @param output The calculated error. */ template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp index 9dc427ded3..a74aeb2e59 100644 --- a/src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp @@ -29,7 +29,7 @@ LogCoshLoss::LogCoshLoss(const double a) : template template double LogCoshLoss::Forward( - const InputType&& input, const TargetType&& target) + const InputType& input, const TargetType& target) { return arma::accu(arma::log(arma::cosh(a * (target - input)))) / a; } @@ -37,9 +37,9 @@ double LogCoshLoss::Forward( template template void LogCoshLoss::Backward( - const InputType&& input, - const TargetType&& target, - OutputType&& output) + const InputType& input, + const TargetType& target, + OutputType& output) { output = arma::tanh(a * (target - input)); } diff --git a/src/mlpack/tests/activation_functions_test.cpp b/src/mlpack/tests/activation_functions_test.cpp index 89d9f8bf98..0776ce60d1 100644 --- a/src/mlpack/tests/activation_functions_test.cpp +++ b/src/mlpack/tests/activation_functions_test.cpp @@ -346,7 +346,7 @@ void CheckHardShrinkActivationCorrect(const arma::colvec input, // Test the activation function using the entire vector as input. arma::colvec activations; - hardshrink.Forward(std::move(input), std::move(activations)); + hardshrink.Forward(input, activations); for (size_t i = 0; i < activations.n_elem; i++) { BOOST_REQUIRE_CLOSE(activations.at(i), target.at(i), 1e-3); @@ -371,8 +371,7 @@ void CheckHardShrinkDerivativeCorrect(const arma::colvec input, // This error vector will be set to 1 to get the derivatives. arma::colvec error = arma::ones(input.n_elem); - hardshrink.Backward(std::move(input), std::move(error), std::move( - derivatives)); + hardshrink.Backward(input, error, derivatives); for (size_t i = 0; i < derivatives.n_elem; i++) { BOOST_REQUIRE_CLOSE(derivatives.at(i), target.at(i), 1e-3); diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index 9c0c5004d8..ce902f4e43 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -484,11 +484,11 @@ BOOST_AUTO_TEST_CASE(LogCoshLossTest) // Test the Forward function. Loss should be 0 if input = target. input = arma::ones(10, 1); target = arma::ones(10, 1); - loss = module.Forward(std::move(input), std::move(target)); + loss = module.Forward(input, target); BOOST_REQUIRE_EQUAL(loss, 0); // Test the Backward function for input = target. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input, target, output); for (double el : output) { // For input = target we should get 0.0 everywhere. @@ -501,11 +501,11 @@ BOOST_AUTO_TEST_CASE(LogCoshLossTest) // Test the Forward function. Loss should be 0.546621. input = arma::mat("1 2 3 4 5"); target = arma::mat("1 2.4 3.4 4.2 5.5"); - loss = module.Forward(std::move(input), std::move(target)); + loss = module.Forward(input, target); BOOST_REQUIRE_CLOSE(loss, 0.546621, 1e-3); // Test the Backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input, target, output); BOOST_REQUIRE_CLOSE(arma::accu(output), 2.46962, 1e-3); BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); From 3c78cac24399103e659d4cdef2989c2238d0ffb2 Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Thu, 12 Mar 2020 21:13:38 +0530 Subject: [PATCH 109/265] Improve contrib docs - repharasing --- CONTRIBUTING.md | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c16ff035aa..85627e650e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,47 +31,45 @@ appreciated and encouraged! ## Reviewing Pull Requests -All mlpack contributors who choose to review and provide feedback on Pull -Requests have a responsibility to both the project and the individual making +All mlpack contributors who choose to review and provide feedback on pull +requests have a responsibility to both the project and the individual making the contribution. -Reviews and feedback must be +Reviews and feedback should be [helpful, insightful, and geared towards improving the contribution]( https://www.youtube.com/watch?v=NNXk_WJzyMI). If there are reasons why you feel the PR should not be merged, explain -what those are. Do not expect to be able to block a Pull Request from advancing -simply because you say "No" without giving an explanation. Be open to having -your mind changed. Be open to working with the contributor to make the Pull -Request better. +what those are. Be open to having your mind changed. Be open to +working with the contributor to make the pull request better. Please don't leave dismissive or disrespectful reviews! It's not helpful for anyone. -When reviewing a Pull Request, the primary goals are : +When reviewing a pull request, the primary goals are: - For the codebase/project to improve - For the person submitting the request to succeed -Even if a Pull Request does not gets merged, the submitters should come away +Even if a pull request does not get merged, the submitters should come away from the experience feeling like their effort was not wasted or unappreciated. -Every Pull Request from a new contributor is an opportunity to grow the community. +Every pull request from a new contributor is an opportunity to grow the community. When changes are necessary, request them, do not demand them, and do not assume that the contributor already knows how to do that. Be there to lend a helping hand in case of need. -Since there is clearly a high difference between pull request being raised and -those being reviewed we highly encourage everyone to review each others pull -request keeping in mind all the above mentioned points. +Since there can sometimes be a lot more pull requests being opened than +reviewed, we highly encourage everyone to review each others pull request +keeping in mind all the above mentioned points. -Let's welcome new contributors with ❤️, and not overwhelm them. +Let's welcome new contributors with ❤️. ## Pull Request Waiting Time -Since members of the Contributors team only work on mlpack in their free time, -it may take some time for them to review pull requests. While gentle reminders -are welcome, please be patient and avoid constantly messaging the contributors or -tagging them on Pull requests. +mlpack is a community-driven project, so everyone only works on it in their +free time, this means it may take some time for them to review pull requests. +While gentle reminders are welcome, please be patient and avoid constantly +messaging contributors or tagging them on pull requests. Typically small PRs will be reviewed within a handful of days; larger PRs might take a few weeks for an initial review, and it may be a little bit longer in From d1188bdb1a1dd5b3c32d4478f4b57b3ac2341d12 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Fri, 13 Mar 2020 08:47:19 -0400 Subject: [PATCH 110/265] Update CONTRIBUTING.md --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 85627e650e..c9ac5e5aa7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -67,7 +67,7 @@ Let's welcome new contributors with ❤️. ## Pull Request Waiting Time mlpack is a community-driven project, so everyone only works on it in their -free time, this means it may take some time for them to review pull requests. +free time; this means it may take some time for them to review pull requests. While gentle reminders are welcome, please be patient and avoid constantly messaging contributors or tagging them on pull requests. From b6a7f1c2fab0437ebc5c6dcd6ca9775d5bab55d9 Mon Sep 17 00:00:00 2001 From: Sriram Date: Fri, 13 Mar 2020 18:34:37 +0530 Subject: [PATCH 111/265] Completed tutorial v1.0 --- .../reinforcement_learning.txt | 151 +++++++++++++++++- 1 file changed, 150 insertions(+), 1 deletion(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index 10447414ad..a6b7d9ee40 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -30,6 +30,7 @@ This tutorial is split into the following sections: - \ref agent_components_rltut - \ref q_learning_rltut - \ref async_learning_rltut + - \ref further_rltut @section environment_rltut Reinforcement Learning Environments @@ -88,4 +89,152 @@ and the value at which epsilon bottoms out and won't be reduced further. The arg `replayMethod` are size of the batch returned, the number of examples stored in memory, and the degree of prioritization. -@section q_learning_rltut Q-Learning in mlpack \ No newline at end of file +In addition to the above components, an RL agent requires many hyper-parameters to be tuned during + it's training period. These parameters include everything from the discount rate of the future +reward to whether Double Q-learning should be used or not. The `TrainingConfig` class can be +instantiated and configured as follows: + +@code + TrainingConfig config; + config.StepSize() = 0.01; + config.Discount() = 0.9; + config.TargetNetworkSyncInterval() = 100; + config.ExplorationSteps() = 100; + config.DoubleQLearning() = false; + config.StepLimit() = 200; +@endcode + +The object `config` describes an RL agent, using a step size of 0.01 for the optimization process, +a discount factor of 0.9, sync interval of 200 episodes. This agent only starts learning after storing +100 exploration steps, has a step limit of 200, and does not utilize double q-learning. + +In this way, we can easily configure an RL agent with the desired hyperparameters. + +@section q_learning_rltut Q-Learning in mlpack + +Here, we demonstrate Q-Learning in mlpack through the use of a simple example, the training of a Q-Learning +agent on the CartPole environment. The code has been broken into chunks for easy understanding. + +@code +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace mlpack; +using namespace mlpack::ann; +using namespace ens; +using namespace mlpack::rl; +@endcode + +We include all the necessary components of our toy example and declare namespaces for convenience. + +@code +int main() +{ + // Set up the network. + FFN, GaussianInitialization> model(MeanSquaredError<>(), + GaussianInitialization(0, 0.001)); + model.Add>(4, 128); + model.Add>(); + model.Add>(128, 128); + model.Add>(); + model.Add>(128, 2); + +@endcode + +The first step in setting our Q-learning agent is to setup the network for it to use. Here, +we use mlpack's ann module to setup a simple FFN network, consisting of a single hidden layer. + +@note +The network constructed here has an input shape of 4 and output shape of 2. This corresponds to +the structure of the CartPole environment, where each state is represented as a column vector with +4 data members (position, velocity, angle, angular velocity). Similarly, the output shape is represented +by the number of possible actions, which in this case, is only 2 (foward and backward). + +The next step would be to setup the other components of the Q-learning agent, namely its policy, replay +method and hyperparameters. + +@code + // Set up the policy and replay method. + GreedyPolicy policy(1.0, 1000, 0.1, 0.99); + RandomReplay replayMethod(10, 10000); + + TrainingConfig config; + config.StepSize() = 0.01; + config.Discount() = 0.9; + config.TargetNetworkSyncInterval() = 100; + config.ExplorationSteps() = 100; + config.DoubleQLearning() = false; + config.StepLimit() = 200; +@endcode + +And now, we get to the heart of the program, declaring a Q-Learning agent. + +@code + QLearning + agent(std::move(config), std::move(model), std::move(policy), + std::move(replayMethod)); +@endcode + +Here, we call the `QLearning` constructor, passing in the type of environment, +network, updater, policy and replay. We use `decltype(var)` as a shorthand for +the variable, saving us the trouble of copying the lengthy templated type. + +Similarly, `std::move` is called for convenience, moving the components instead of +duplicating them and copying them over. + +We have our Q-Learning agent `agent` ready to be trained on the Cart Pole environment. + +@code + arma::running_stat averageReturn; + size_t episodes = 0; + bool converged = true; + while (true) + { + double episodeReturn = agent.Episode(); + averageReturn(episodeReturn); + episodes += 1; + + if (episodes > 1000) + { + std::cout << "Cart Pole with DQN failed." << std::endl; + converged = false; + break; + } + + /** + * Reaching running average return 35 is enough to show it works. + */ + std::cout << "Average return: " << averageReturn.mean() + << " Episode return: " << episodeReturn << std::endl; + if (averageReturn.mean() > 35) + break; + } + if (converged) + std::cout << "Hooray! Q-Learning agent successfully trained" << std::endl; + + return 0; +} +@endcode + +We set up a loop to train the agent. The exit condition is determined by the average +reward which can be computed with `arma::running_stat`. It is used for storing running +statistics of scalars, which in this case is the reward signal. The agent can be said +to have converged when the average return reaches a predetermined value (i.e. > 35). + +Conversely, if the average return does not go beyond that amount even after a thousands, +we can conclude that the agent will not converge and exit the training loop. + +@section further_rltut Further documentation + +For further documentation on the rl classes, consult the \ref mlpack::rl +"complete API documentation". + +*/ \ No newline at end of file From 24d3e98da2625e03e9ba898e1d16301439044e23 Mon Sep 17 00:00:00 2001 From: adithya-tp Date: Fri, 13 Mar 2020 17:52:55 +0400 Subject: [PATCH 112/265] Incorporating review changes --- doc/tutorials/ann/ann.txt | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/doc/tutorials/ann/ann.txt b/doc/tutorials/ann/ann.txt index 29575a92e3..43188d422d 100644 --- a/doc/tutorials/ann/ann.txt +++ b/doc/tutorials/ann/ann.txt @@ -213,8 +213,6 @@ int main() // Split the labels from the training set and testing set respectively. arma::mat trainLabels = trainData.row(trainData.n_rows - 1); arma::mat testLabels = testData.row(testData.n_rows - 1); - - // Split the data from the training set and testing set respectively. trainData.shed_row(trainData.n_rows - 1); testData.shed_row(testData.n_rows - 1); @@ -240,6 +238,9 @@ int main() The first step towards doing this is to create a matrix of zeros with the desired dimensions (1 x number_of_data_points). + + In predictionsTemp, the 3 dimensions for each data point correspond to the + probabilities of belonging to the three possible classes. */ arma::mat prediction = arma::zeros(1, predictionTemp.n_cols); @@ -255,24 +256,21 @@ int main() Compute the error between predictions and testLabels, now that we have the desired predictions. */ - size_t error = 0; + size_t correct = 0; for (size_t i = 0; i < testData.n_cols; i++) { - if (int(arma::as_scalar(prediction.col(i))) == int(arma::as_scalar(testLabels.col(i)))) - { - error++; - } + correct = arma::accu(prediction == testLabels); } - double classificationError = 1 - double(error) / testData.n_cols; + double classificationError = 1 - double(correct) / testData.n_cols; // Print out the classification error for the testing dataset. - cout << "Classification Error for the Test set: " << classificationError << endl; + std::cout << "Classification Error for the Test set: " << classificationError << std::endl; return 0; } @endcode Now, the matrix prediction holds the classification of each point in the -dataset. Subsequently, we find the classfication error by comparing it +dataset. Subsequently, we find the classification error by comparing it with testLabels. In the next example, we create simple noisy sine sequences, which are trained From 778d61a17ea8b9cab6fb8020f79d8480ab5bc8ef Mon Sep 17 00:00:00 2001 From: Sriram Date: Fri, 13 Mar 2020 20:17:24 +0530 Subject: [PATCH 113/265] Removed async learning header --- doc/tutorials/reinforcement_learning/reinforcement_learning.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index a6b7d9ee40..1128030344 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -29,7 +29,6 @@ This tutorial is split into the following sections: - \ref environment_rltut - \ref agent_components_rltut - \ref q_learning_rltut - - \ref async_learning_rltut - \ref further_rltut @section environment_rltut Reinforcement Learning Environments From add98c70b2be1be65d42262ec494f83d8f17eca2 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Fri, 13 Mar 2020 16:45:12 +0000 Subject: [PATCH 114/265] Fix unchanged header definition (oops). --- src/mlpack/bindings/julia/julia_util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/bindings/julia/julia_util.h b/src/mlpack/bindings/julia/julia_util.h index 87111c99e1..06ec97ad0a 100644 --- a/src/mlpack/bindings/julia/julia_util.h +++ b/src/mlpack/bindings/julia/julia_util.h @@ -149,7 +149,7 @@ size_t CLI_GetParamVectorStrLen(const char* paramName); /** * Call CLI::GetParam>() and get the i'th string. */ -const char* CLI_GetParamVectorStrStr(const char* paramName, const int i); +const char* CLI_GetParamVectorStrStr(const char* paramName, const size_t i); /** * Call CLI::GetParam>() and get the length of the vector. From 718e3d522b7c8eb8e11da2138534a96166a1c59d Mon Sep 17 00:00:00 2001 From: Sriram Date: Sat, 14 Mar 2020 00:09:06 +0530 Subject: [PATCH 115/265] Grammatical errors --- .../reinforcement_learning.txt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index 1128030344..1c837ef070 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -16,7 +16,7 @@ custom environments and policies can be used and plugged into the existing framework with no runtime overhead. mlpack implements typical benchmark environments (Acrobot, Mountain car etc.), -commonly used policies and replay methods and supports asynchronous +commonly used policies, replay methods and supports asynchronous learning as well. In addition, it can [communicate](https://github.com/zoq/gym_tcp_api) with the OpenAI Gym toolkit for more environments. @@ -52,10 +52,10 @@ which are used by the agents while learning. - \c Sample: This method is perhaps the heart of the environment, providing rewards to the agent depending on the state and the action taken, and updates the state based on - the action taken as well. + the action taken as well. -Of course, your custom environment will most likely a number of helper methods, depending -on your application, such as the Dsdt method in the Acrobot environment, used in the RK4 +Of course, your custom environment will most likely make use of a number of helper methods, depending +on your application, such as the \c Dsdt method in the \c Acrobot environment, used in the \c RK4 iterative method (also another helper method) to estimate the next state. @section agent_components_rltut Components of an RL Agent @@ -68,7 +68,7 @@ An example of a simple policy would be an epsilon-greedy policy. Using such a po will choose actions greedily with some probability epsilon. This probability is slowly decreased over time, balancing the line between exploration and exploitation. -Similarly, an example of a simple reply would be a random replay. At each time step, the +Similarly, an example of a simple replay would be a random replay. At each time step, the interactions between the agent and the environment are saved to a memory buffer and previous experiences are sampled from the buffer to train the agent. @@ -83,12 +83,12 @@ GreedyPolicy policy(1.0, 1000, 0.1); PrioritizedReplay replayMethod(10, 10000, 0.6); @endcode -The arguments to `policy` are the initial epsilon values, the intenrval of decrease in its value +The arguments to `policy` are the initial epsilon values, the interval of decrease in its value and the value at which epsilon bottoms out and won't be reduced further. The arguments to `replayMethod` are size of the batch returned, the number of examples stored in memory, and the degree of prioritization. -In addition to the above components, an RL agent requires many hyper-parameters to be tuned during +In addition to the above components, an RL agent requires many hyperparameters to be tuned during it's training period. These parameters include everything from the discount rate of the future reward to whether Double Q-learning should be used or not. The `TrainingConfig` class can be instantiated and configured as follows: @@ -228,8 +228,8 @@ reward which can be computed with `arma::running_stat`. It is used for storing r statistics of scalars, which in this case is the reward signal. The agent can be said to have converged when the average return reaches a predetermined value (i.e. > 35). -Conversely, if the average return does not go beyond that amount even after a thousands, -we can conclude that the agent will not converge and exit the training loop. +Conversely, if the average return does not go beyond that amount even after a thousand +episodes, we can conclude that the agent will not converge and exit the training loop. @section further_rltut Further documentation From 84de117e58755cf0c5cacac82102450655e5cdad Mon Sep 17 00:00:00 2001 From: Mrityunjay Tripathi Date: Sat, 14 Mar 2020 00:57:30 +0530 Subject: [PATCH 116/265] style fix --- src/mlpack/methods/ann/loss_functions/huber_loss.hpp | 5 +---- src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp | 8 ++------ 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp index f7beff39e1..9a18cfea1b 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp @@ -24,7 +24,6 @@ namespace ann /** Artificial Neural Network. */ { * and linear for large values, with equal values and slopes of the different * sections at the two points where \f$ |y - f(x)| = delta \f$. * - * @tparam ActivationFunction Activation function used for the embedding layer. * @tparam InputDataType Type of the input data (arma::colvec, arma::mat, * arma::sp_mat or arma::cube). * @tparam OutputDataType Type of the output data (arma::colvec, arma::mat, @@ -44,9 +43,7 @@ class HuberLoss * after which absolute error is considered. * @param mean If true then mean loss is computed otherwise sum. */ - HuberLoss( - const double delta = 1.0, - const bool mean = true); + HuberLoss(const double delta = 1.0, const bool mean = true); /** * Computes the Huber Loss function. diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp index a7c3c6f2bb..356da2633c 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp @@ -34,11 +34,9 @@ double HuberLoss::Forward( const InputType& input, const TargetType& target) { double loss = 0; - double absError; - for (size_t i = 0; i < input.n_elem; ++i) { - absError = std::abs(target[i] - input[i]); + const double absError = std::abs(target[i] - input[i]); loss += absError > delta ? delta * (absError - 0.5 * delta) : 0.5 * std::pow(absError, 2); } @@ -53,11 +51,9 @@ void HuberLoss::Backward( OutputType& output) { output.set_size(size(input)); - double absError; - for (size_t i = 0; i < output.n_elem; ++i) { - absError = std::abs(target[i] - input[i]); + const double absError = std::abs(target[i] - input[i]); output[i] = absError > delta ? - delta * (target[i] - input[i]) / absError : input[i] - target[i]; if (mean) From 219696656a5bd4fab685794998366215215b5e4d Mon Sep 17 00:00:00 2001 From: Lakshya Ojha <57477999+ojhalakshya@users.noreply.github.com> Date: Sat, 14 Mar 2020 01:07:22 +0530 Subject: [PATCH 117/265] Adding soft shrink function (#2174) * soft shrinkage activation function * changes * new changes * new changes * void Fn complete * comments * changes * changes * lambda as param * added derivate also * changes * complete function * changes in ss function * new changes * soft shrinkage activation function * changes * new changes * new changes * void Fn complete * comments * changes * changes * lambda as param * added derivate also * changes * complete function * changes in ss function * new changes * style corrections * style corrections 2 * corrections * corrections * final corrections * base layer changes * cmake softshrink function * softshrink.hpp in layer * softshrink_impl.hpp added * layer_types.hpp updated * updating activation_functions_test.cpp * soft shrink changes * commit * style corrections * style changes * changes style * small style changes * changes * style checks * final checks * changes * doxygen syntax for comment * soft shrink comments * style checks for 80 characters * dummy commit * softshrink.hpp to softshrink_impl.hpp fn to forward * bug fixes in build * style fixes * updating lambda definition Co-Authored-By: Ryan Birmingham * Use _Internal module, not util. * Print code snippets in ```julia blocks. * Always pass 64-bit integers back and forth. * Additional i386 fixes. * Remove unneeded REQUIRE. * Add docstrings to the mlpack module. * New NumFOCUS badge mockup * Match style of other badges * Minor changes reflecting discusssion * Use Csize_t instead of uint64_t, and UInt/Int. * Oops, fix declaration of s. * Switch int64_t to int. * Remove inaccurate comments. * Fix a few other minor issues. * Redirected badge and removed info about NUMfocus * Reverted deletion and added attribution line * added @tparam and rvalue correction according to recent refactor pr Co-authored-by: Ryan Birmingham Co-authored-by: Ryan Curtin Co-authored-by: Sriram --- src/mlpack/methods/ann/layer/CMakeLists.txt | 2 + src/mlpack/methods/ann/layer/layer_types.hpp | 1 + src/mlpack/methods/ann/layer/softshrink.hpp | 131 ++++++++++++++++++ .../methods/ann/layer/softshrink_impl.hpp | 61 ++++++++ .../tests/activation_functions_test.cpp | 61 ++++++++ 5 files changed, 256 insertions(+) create mode 100644 src/mlpack/methods/ann/layer/softshrink.hpp create mode 100644 src/mlpack/methods/ann/layer/softshrink_impl.hpp diff --git a/src/mlpack/methods/ann/layer/CMakeLists.txt b/src/mlpack/methods/ann/layer/CMakeLists.txt index 0da69fc02b..a101444157 100644 --- a/src/mlpack/methods/ann/layer/CMakeLists.txt +++ b/src/mlpack/methods/ann/layer/CMakeLists.txt @@ -96,6 +96,8 @@ set(SOURCES weight_norm_impl.hpp hardshrink.hpp hardshrink_impl.hpp + softshrink.hpp + softshrink_impl.hpp ) # Add directory name to sources. diff --git a/src/mlpack/methods/ann/layer/layer_types.hpp b/src/mlpack/methods/ann/layer/layer_types.hpp index 810c5b6b93..3682ed98cd 100644 --- a/src/mlpack/methods/ann/layer/layer_types.hpp +++ b/src/mlpack/methods/ann/layer/layer_types.hpp @@ -42,6 +42,7 @@ #include #include #include +#include // Convolution modules. #include diff --git a/src/mlpack/methods/ann/layer/softshrink.hpp b/src/mlpack/methods/ann/layer/softshrink.hpp new file mode 100644 index 0000000000..cc117bf361 --- /dev/null +++ b/src/mlpack/methods/ann/layer/softshrink.hpp @@ -0,0 +1,131 @@ +/** + * @file softshrink.hpp + * @author Lakshya Ojha + * + * The soft shrink function has threshold proportional to the noise level given + * by the user. + * The use of a Soft Shrink activation function provides adaptive denoising at + * various noise levels using a single CNN(Convolution Neural) without a + * requirement to train a unique CNN for each noise level. + * + * 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_METHODS_ANN_LAYER_SOFTSHRINK_HPP +#define MLPACK_METHODS_ANN_LAYER_SOFTSHRINK_HPP + +#include + +namespace mlpack { +namespace ann /** Artifical Neural Network. */ { + +/** + * Soft Shrink operator is defined as, + * @f{eqnarray*}{ + * f(x) &=& \left\{ + * \begin{array}{lr} + * x - lambda & : x > lambda \\ + * x + lambda & : x < -lambda \\ + * 0 & : otherwise + * \end{array} \\ + * \right. + * f'(x) &=& \left\{ + * \begin{array}{lr} + * 1 & : x > lambda \\ + * 1 & : x < -lambda \\ + * 0 & : otherwise + * \end{array} + * \right. + * @f} + * + * @tparam InputDataType Type of the input data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + * @tparam OutputDataType Type of the output data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + */ +template < + typename InputDataType = arma::mat, + typename OutputDataType = arma::mat +> +class SoftShrink +{ + public: + /** + * Create Soft Shrink object using specified hyperparameter lambda. + * + * @param lambda The noise level of an image depends on settings of an + * imaging device. The settings can be used to select appropriate + * parameters for denoising methods. It is proportional to the noise + * level entered by the user. + * And it is calculated by multiplying the + * noise level sigma of the input(noisy image) and a + * coefficient 'a' which is one of the training parameters. + * Default value of lambda is 0.5. + */ + SoftShrink(const double lambda = 0.5); + + /** + * Ordinary feed forward pass of a neural network, evaluating the function + * f(x) by propagating the activity forward through f. + * + * @param input Input data used for evaluating the Soft Shrink function. + * @param output Resulting output activation + */ + template + void Forward(const InputType& input, OutputType& output); + + /** + * Ordinary feed backward pass of a neural network, calculating the function + * f(x) by propagating x backwards through f. Using the results from the feed + * forward pass. + * + * @param input The propagated input activation f(x). + * @param gy The backpropagated error. + * @param g The calculated gradient + */ + template + void Backward(const DataType& input, + DataType& gy, + DataType& g); + + //! Get the output parameter. + OutputDataType const& OutputParameter() const { return outputParameter; } + //! Modify the output parameter. + OutputDataType& OutputParameter() { return outputParameter; } + + //! Get the delta. + OutputDataType const& Delta() const { return delta; } + //! Modify the delta. + OutputDataType& Delta() { return delta; } + + //! Get the hyperparameter lambda. + double const& Lambda() const { return lambda; } + //! Modify the hyperparameter lambda. + double& Lambda() { return lambda; } + + /** + * Serialize the layer. + */ + template + void serialize(Archive& ar, const unsigned int /* version */); + + private: + //! Locally-stored delta object. + OutputDataType delta; + + //! Locally-stored output parameter object. + OutputDataType outputParameter; + + //! Locally-stored hyperparamater lambda. + double lambda; +}; // class SoftShrink + +} // namespace ann +} // namespace mlpack + +// Include implementation. +#include "softshrink_impl.hpp" + +#endif diff --git a/src/mlpack/methods/ann/layer/softshrink_impl.hpp b/src/mlpack/methods/ann/layer/softshrink_impl.hpp new file mode 100644 index 0000000000..99876c89c8 --- /dev/null +++ b/src/mlpack/methods/ann/layer/softshrink_impl.hpp @@ -0,0 +1,61 @@ +/** + * @file softshrink.hpp + * @author Lakshya Ojha + * + * Implementation of Soft Shrink activation function. + * + * 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_METHODS_ANN_LAYER_SOFTSHRINK_IMPL_HPP +#define MLPACK_METHODS_ANN_LAYER_SOFTSHRINK_IMPL_HPP + +// In case it hasn't yet been included +#include "softshrink.hpp" + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +// This constructor is called for Soft Shrink activation function. +// lambda is a hyperparameter. +template +SoftShrink::SoftShrink(const double lambda) : + lambda(lambda) +{ + // Nothing to do here. +} + +template +template +void SoftShrink::Forward( + const InputType& input, OutputType& output) +{ + output = (input > lambda) % (input - lambda) + ( + input < -lambda) % (input + lambda); +} + +template +template +void SoftShrink::Backward( + const DataType& input, DataType& gy, DataType& g) +{ + DataType derivative; + derivative = (arma::ones(arma::size(input)) - (input == 0)); + g = gy % derivative; +} + +template +template +void SoftShrink::serialize( + Archive& ar, + const unsigned int /* version */) +{ + ar & BOOST_SERIALIZATION_NVP(lambda); +} + +} // namespace ann +} // namespace mlpack + +#endif diff --git a/src/mlpack/tests/activation_functions_test.cpp b/src/mlpack/tests/activation_functions_test.cpp index 0776ce60d1..758974e301 100644 --- a/src/mlpack/tests/activation_functions_test.cpp +++ b/src/mlpack/tests/activation_functions_test.cpp @@ -378,6 +378,52 @@ void CheckHardShrinkDerivativeCorrect(const arma::colvec input, } } +/* + * Implementation of the Soft Shrink activation function test. The function is + * implemented as Soft Shrink layer in the file softshrink.hpp. + * + * @param input Input data used for evaluating the Soft Shrink activation function. + * @param target Target data used to evaluate the Soft Shrink activation. + */ +void CheckSoftShrinkActivationCorrect(const arma::colvec input, + const arma::colvec target) +{ + SoftShrink<> softshrink; + + // Test the activation function using the entire vector as input. + arma::colvec activations; + softshrink.Forward(input, activations); + for (size_t i = 0; i < activations.n_elem; i++) + { + BOOST_REQUIRE_CLOSE(activations.at(i), target.at(i), 1e-3); + } +} + +/* + * Implementation of the Soft Shrink activation function derivative test. + * The derivative function is implemented as Soft Shrink layer in the file + * softshrink.hpp + * + * @param input Input data used for evaluating the Soft Shrink activation function. + * @param target Target data used to evaluate the Soft Shrink activation. + */ +void CheckSoftShrinkDerivativeCorrect(const arma::colvec input, + const arma::colvec target) +{ + SoftShrink<> softshrink; + + // Test the calculation of the derivatives using the entire vector as input. + arma::colvec derivatives; + + // This error vector will be set to 1 to get the derivatives. + arma::colvec error = arma::ones(input.n_elem); + softshrink.Backward(input, error, derivatives); + for (size_t i = 0; i < derivatives.n_elem; i++) + { + BOOST_REQUIRE_CLOSE(derivatives.at(i), target.at(i), 1e-3); + } +} + /* * Simple SELU activation test to check whether the mean and variance remain * invariant after passing normalized inputs through the function. @@ -767,4 +813,19 @@ BOOST_AUTO_TEST_CASE(HardShrinkFunctionTest) desiredDerivatives); } +/** + * Basic test of the Soft Shrink function. + */ +BOOST_AUTO_TEST_CASE(SoftShrinkFunctionTest) +{ + const arma::colvec desiredActivations("-1.5 2.7 4 -99.7 0.5 -0.5 1.5 0"); + + const arma::colvec desiredDerivatives("1 1 1 1 1 1 1 0"); + + CheckSoftShrinkActivationCorrect(activationData, + desiredActivations); + CheckSoftShrinkDerivativeCorrect(desiredActivations, + desiredDerivatives); +} + BOOST_AUTO_TEST_SUITE_END(); From ba25b8e139649c8c64e83a0331e1e4388f1088ee Mon Sep 17 00:00:00 2001 From: Rahul Prabhu Date: Fri, 13 Mar 2020 16:00:01 +0530 Subject: [PATCH 118/265] Added Deterministic() function --- src/mlpack/methods/ann/layer/elu.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/mlpack/methods/ann/layer/elu.hpp b/src/mlpack/methods/ann/layer/elu.hpp index 43d6105c82..c837f6a955 100644 --- a/src/mlpack/methods/ann/layer/elu.hpp +++ b/src/mlpack/methods/ann/layer/elu.hpp @@ -165,6 +165,11 @@ class ELU //! Modify the non zero gradient. double& Alpha() { return alpha; } + //! Get the value of deterministic parameter. + bool Deterministic() const { return deterministic; } + //! Modify the value of deterministic parameter. + bool& Deterministic() { return deterministic; } + //! Get the lambda parameter. double const& Lambda() const { return lambda; } From dd9b0bca2468adf6fcacbbea39041b9570f112e8 Mon Sep 17 00:00:00 2001 From: adithya-tp Date: Sat, 14 Mar 2020 19:01:09 +0400 Subject: [PATCH 119/265] Cleaner code for a few test files --- src/mlpack/tests/adaboost_test.cpp | 114 ++++-------------- .../tests/convolutional_network_test.cpp | 8 +- src/mlpack/tests/feedforward_network_test.cpp | 24 +--- 3 files changed, 26 insertions(+), 120 deletions(-) diff --git a/src/mlpack/tests/adaboost_test.cpp b/src/mlpack/tests/adaboost_test.cpp index 86d74034c4..0065877abe 100644 --- a/src/mlpack/tests/adaboost_test.cpp +++ b/src/mlpack/tests/adaboost_test.cpp @@ -58,10 +58,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundIris) arma::Row predictedLabels; a.Classify(inputData, predictedLabels); - size_t countError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != predictedLabels(i)) - countError++; + size_t countError = arma::accu(labels != predictedLabels); double hammingLoss = (double) countError / labels.n_cols; // Check that ztProduct is finite. @@ -96,10 +93,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorIris) Perceptron<> p(inputData, labels.row(0), numClasses, perceptronIter); p.Classify(inputData, perceptronPrediction); - size_t countWeakLearnerError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != perceptronPrediction(i)) - countWeakLearnerError++; + size_t countWeakLearnerError = arma::accu(labels != perceptronPrediction); double weakLearnerErrorRate = (double) countWeakLearnerError / labels.n_cols; // Define parameters for AdaBoost. @@ -110,10 +104,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorIris) arma::Row predictedLabels; a.Classify(inputData, predictedLabels); - size_t countError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != predictedLabels(i)) - countError++; + size_t countError = arma::accu(labels != predictedLabels);; double error = (double) countError / labels.n_cols; BOOST_REQUIRE_LE(error, weakLearnerErrorRate); @@ -151,10 +142,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundVertebralColumn) arma::Row predictedLabels; a.Classify(inputData, predictedLabels); - size_t countError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != predictedLabels(i)) - countError++; + size_t countError = arma::accu(labels != predictedLabels); double hammingLoss = (double) countError / labels.n_cols; // Check that ztProduct is finite. @@ -187,10 +175,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorVertebralColumn) Perceptron<> p(inputData, labels.row(0), numClasses, perceptronIter); p.Classify(inputData, perceptronPrediction); - size_t countWeakLearnerError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != perceptronPrediction(i)) - countWeakLearnerError++; + size_t countWeakLearnerError = arma::accu(labels != perceptronPrediction); double weakLearnerErrorRate = (double) countWeakLearnerError / labels.n_cols; // Define parameters for AdaBoost. @@ -201,10 +186,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorVertebralColumn) arma::Row predictedLabels; a.Classify(inputData, predictedLabels); - size_t countError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != predictedLabels(i)) - countError++; + size_t countError = arma::accu(labels != predictedLabels); double error = (double) countError / labels.n_cols; BOOST_REQUIRE_LE(error, weakLearnerErrorRate); @@ -242,10 +224,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundNonLinearSepData) arma::Row predictedLabels; a.Classify(inputData, predictedLabels); - size_t countError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != predictedLabels(i)) - countError++; + size_t countError = arma::accu(labels == predictedLabels); double hammingLoss = (double) countError / labels.n_cols; // Check that ztProduct is finite. @@ -278,10 +257,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorNonLinearSepData) Perceptron<> p(inputData, labels.row(0), numClasses, perceptronIter); p.Classify(inputData, perceptronPrediction); - size_t countWeakLearnerError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != perceptronPrediction(i)) - countWeakLearnerError++; + size_t countWeakLearnerError = arma::accu(labels != perceptronPrediction); double weakLearnerErrorRate = (double) countWeakLearnerError / labels.n_cols; // Define parameters for AdaBoost. @@ -292,10 +268,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorNonLinearSepData) arma::Row predictedLabels; a.Classify(inputData, predictedLabels); - size_t countError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != predictedLabels(i)) - countError++; + size_t countError = arma::accu(labels != predictedLabels); double error = (double) countError / labels.n_cols; BOOST_REQUIRE_LE(error, weakLearnerErrorRate); @@ -332,10 +305,7 @@ BOOST_AUTO_TEST_CASE(HammingLossIris_DS) arma::Row predictedLabels; a.Classify(inputData, predictedLabels); - size_t countError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != predictedLabels(i)) - countError++; + size_t countError = arma::accu(labels != predictedLabels); double hammingLoss = (double) countError / labels.n_cols; // Check that ztProduct is finite. @@ -371,10 +341,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorIris_DS) ID3DecisionStump ds(inputData, labelsvec, numClasses, inpBucketSize); ds.Classify(inputData, dsPrediction); - size_t countWeakLearnerError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != dsPrediction(i)) - countWeakLearnerError++; + size_t countWeakLearnerError = arma::accu(labels != dsPrediction); double weakLearnerErrorRate = (double) countWeakLearnerError / labels.n_cols; // Define parameters for AdaBoost. @@ -387,10 +354,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorIris_DS) arma::Row predictedLabels; a.Classify(inputData, predictedLabels); - size_t countError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != predictedLabels(i)) - countError++; + size_t countError = arma::accu(labels != predictedLabels); double error = (double) countError / labels.n_cols; BOOST_REQUIRE_LE(error, weakLearnerErrorRate); @@ -430,10 +394,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundVertebralColumn_DS) arma::Row predictedLabels; a.Classify(inputData, predictedLabels); - size_t countError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != predictedLabels(i)) - countError++; + size_t countError = arma::accu(labels != predictedLabels); double hammingLoss = (double) countError / labels.n_cols; // Check that ztProduct is finite. @@ -466,11 +427,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorVertebralColumn_DS) ID3DecisionStump ds(inputData, labelsvec, numClasses, inpBucketSize); ds.Classify(inputData, dsPrediction); - size_t countWeakLearnerError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != dsPrediction(i)) - countWeakLearnerError++; - + size_t countWeakLearnerError = arma::accu(labels != dsPrediction); double weakLearnerErrorRate = (double) countWeakLearnerError / labels.n_cols; // Define parameters for AdaBoost. @@ -482,10 +439,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorVertebralColumn_DS) arma::Row predictedLabels; a.Classify(inputData, predictedLabels); - size_t countError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != predictedLabels(i)) - countError++; + size_t countError = arma::accu(labels != predictedLabels); double error = (double) countError / labels.n_cols; BOOST_REQUIRE_LE(error, weakLearnerErrorRate); @@ -524,10 +478,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundNonLinearSepData_DS) arma::Row predictedLabels; a.Classify(inputData, predictedLabels); - size_t countError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != predictedLabels(i)) - countError++; + size_t countError = arma::accu(labels != predictedLabels); double hammingLoss = (double) countError / labels.n_cols; // Check that ztProduct is finite. @@ -561,10 +512,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorNonLinearSepData_DS) ID3DecisionStump ds(inputData, labelsvec, numClasses, inpBucketSize); ds.Classify(inputData, dsPrediction); - size_t countWeakLearnerError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != dsPrediction(i)) - countWeakLearnerError++; + size_t countWeakLearnerError = arma::accu(labels != dsPrediction); double weakLearnerErrorRate = (double) countWeakLearnerError / labels.n_cols; // Define parameters for AdaBoost. @@ -577,10 +525,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorNonLinearSepData_DS) arma::Row predictedLabels; a.Classify(inputData, predictedLabels); - size_t countError = 0; - for (size_t i = 0; i < labels.n_cols; i++) - if (labels(i) != predictedLabels(i)) - countError++; + size_t countError = arma::accu(labels != predictedLabels); double error = (double) countError / labels.n_cols; BOOST_REQUIRE_LE(error, weakLearnerErrorRate); @@ -650,11 +595,7 @@ BOOST_AUTO_TEST_CASE(ClassifyTest_VERTEBRALCOL) BOOST_REQUIRE_CLOSE(arma::accu(probabilities.col(i)), 1, 1e-5); } - size_t localError = 0; - for (size_t i = 0; i < trueTestLabels.n_cols; i++) - if (trueTestLabels(i) != predictedLabels1(i)) - localError++; - + size_t localError = arma::accu(trueTestLabels != predictedLabels1); double lError = (double) localError / trueTestLabels.n_cols; BOOST_REQUIRE_LE(lError, 0.30); } @@ -722,11 +663,7 @@ BOOST_AUTO_TEST_CASE(ClassifyTest_NONLINSEP) BOOST_REQUIRE_CLOSE(arma::accu(probabilities.col(i)), 1, 1e-5); } - size_t localError = 0; - for (size_t i = 0; i < trueTestLabels.n_cols; i++) - if (trueTestLabels(i) != predictedLabels1(i)) - localError++; - + size_t localError = arma::accu(trueTestLabels != predictedLabels1); double lError = (double) localError / trueTestLabels.n_cols; BOOST_REQUIRE_LE(lError, 0.30); } @@ -793,10 +730,7 @@ BOOST_AUTO_TEST_CASE(ClassifyTest_IRIS) BOOST_REQUIRE_CLOSE(arma::accu(probabilities.col(i)), 1, 1e-5); } - size_t localError = 0; - for (size_t i = 0; i < trueTestLabels.n_cols; i++) - if (trueTestLabels(i) != predictedLabels1(i)) - localError++; + size_t localError = arma::accu(trueTestLabels != predictedLabels1); double lError = (double) localError / labels.n_cols; BOOST_REQUIRE_LE(lError, 0.30); } @@ -851,11 +785,7 @@ BOOST_AUTO_TEST_CASE(TrainTest) arma::Row predictedLabels(testData.n_cols); a.Classify(testData, predictedLabels); - int localError = 0; - for (size_t i = 0; i < trueTestLabels.n_cols; i++) - if (trueTestLabels(i) != predictedLabels(i)) - localError++; - + int localError = arma::accu(trueTestLabels != predictedLabels); double lError = (double) localError / trueTestLabels.n_cols; BOOST_REQUIRE_LE(lError, 0.30); diff --git a/src/mlpack/tests/convolutional_network_test.cpp b/src/mlpack/tests/convolutional_network_test.cpp index 6c0f0059f1..a7c3e41442 100644 --- a/src/mlpack/tests/convolutional_network_test.cpp +++ b/src/mlpack/tests/convolutional_network_test.cpp @@ -114,13 +114,7 @@ BOOST_AUTO_TEST_CASE(VanillaNetworkTest) arma::max(predictionTemp.col(i)) == predictionTemp.col(i), 1)) + 1; } - size_t correct = 0; - for (size_t i = 0; i < X.n_cols; i++) - { - if (prediction(i) == Y(i)) - correct++; - } - + size_t correct = arma::accu(prediction == Y); double classificationError = 1 - double(correct) / X.n_cols; if (classificationError <= 0.25) { diff --git a/src/mlpack/tests/feedforward_network_test.cpp b/src/mlpack/tests/feedforward_network_test.cpp index b84f2a557b..68ff897a1c 100644 --- a/src/mlpack/tests/feedforward_network_test.cpp +++ b/src/mlpack/tests/feedforward_network_test.cpp @@ -53,17 +53,8 @@ void TestNetwork(ModelType& model, arma::max(predictionTemp.col(i)) == predictionTemp.col(i), 1)) + 1; } - size_t error = 0; - for (size_t i = 0; i < testData.n_cols; i++) - { - if (int(arma::as_scalar(prediction.col(i))) == - int(arma::as_scalar(testLabels.col(i)))) - { - error++; - } - } - - double classificationError = 1 - double(error) / testData.n_cols; + size_t correct = arma::accu(prediction == testLabels); + double classificationError = 1 - double(correct) / testData.n_cols; BOOST_REQUIRE_LE(classificationError, classificationErrorThreshold); } @@ -199,16 +190,7 @@ BOOST_AUTO_TEST_CASE(ForwardBackwardTest) arma::max(currentResuls.col(i)) == currentResuls.col(i), 1)) + 1; } - size_t correct = 0; - for (size_t i = 0; i < currentLabels.n_cols; i++) - { - if (int(arma::as_scalar(prediction.col(i))) == - int(arma::as_scalar(currentLabels.col(i)))) - { - correct++; - } - } - + size_t correct = arma::accu(prediction == currentLabels); error(1 - (double) correct / batchSize); } Log::Debug << "Current training error: " << error.mean() << std::endl; From f0e73c73d672b227f43f466cd931fefc885a77fe Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Sun, 15 Mar 2020 00:22:34 +0530 Subject: [PATCH 120/265] Rename LoadSave to ImageConverter --- src/mlpack/methods/preprocess/CMakeLists.txt | 6 +++--- ...image_main.cpp => image_converter_main.cpp} | 16 +++++----------- src/mlpack/tests/CMakeLists.txt | 2 +- ...image_test.cpp => image_converter_test.cpp} | 18 ++++++++++-------- 4 files changed, 19 insertions(+), 23 deletions(-) rename src/mlpack/methods/preprocess/{load_save_image_main.cpp => image_converter_main.cpp} (93%) rename src/mlpack/tests/main_tests/{load_save_image_test.cpp => image_converter_test.cpp} (93%) diff --git a/src/mlpack/methods/preprocess/CMakeLists.txt b/src/mlpack/methods/preprocess/CMakeLists.txt index 39810a1c87..48d0378614 100644 --- a/src/mlpack/methods/preprocess/CMakeLists.txt +++ b/src/mlpack/methods/preprocess/CMakeLists.txt @@ -43,7 +43,7 @@ add_python_binding(preprocess_scale) add_markdown_docs(preprocess_scale "cli;python" "preprocessing") if (NOT HAS_STB) - add_cli_executable(load_save_image) - add_python_binding(load_save_image) - add_markdown_docs(load_save_image "cli;python" "preprocessing") + add_cli_executable(image_converter) + add_python_binding(limage_converter) + add_markdown_docs(image_converter "cli;python" "preprocessing") endif () \ No newline at end of file diff --git a/src/mlpack/methods/preprocess/load_save_image_main.cpp b/src/mlpack/methods/preprocess/image_converter_main.cpp similarity index 93% rename from src/mlpack/methods/preprocess/load_save_image_main.cpp rename to src/mlpack/methods/preprocess/image_converter_main.cpp index 8673861e42..d5ee142c4d 100644 --- a/src/mlpack/methods/preprocess/load_save_image_main.cpp +++ b/src/mlpack/methods/preprocess/image_converter_main.cpp @@ -1,5 +1,5 @@ /** - * @file load_save_image_main.cpp + * @file image_converter_main.cpp * @author Jeffin Sam * * A CLI executable to load and save a image dataset. @@ -21,7 +21,7 @@ using namespace std; using namespace mlpack::data; -PROGRAM_INFO("Load Save Image", +PROGRAM_INFO("Image Converter", // Short description. "A utility to load an image or set of images into a single dataset that" "can then be used by other mlpack methods and utilities. This can also" @@ -39,17 +39,17 @@ PROGRAM_INFO("Load Save Image", PRINT_PARAM_STRING("dataset") + " and " + PRINT_PARAM_STRING("save") + " as an parameter. An example to load an image : " + "\n\n" + - PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, + PRINT_CALL("image_converter", "input", "X", "height", 256, "width", 256, "channel", 3, "output", "Y") + "\n\n" + " An example to save an image is :" + "\n\n" + - PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, + PRINT_CALL("image_converter", "input", "X", "height", 256, "width", 256, "channel", 3, "dataset", "Y", "save", true) + "\n\n" + " An example to load an image and also flipping it while loading is :" + "\n\n" + - PRINT_CALL("load_save_image", "input", "X", "height", 256, "width", 256, + PRINT_CALL("image_converter", "input", "X", "height", 256, "width", 256, "channel", 3, "output", "Y", "transpose", true), SEE_ALSO("@preprocess_binarize", "#preprocess_binarize"), SEE_ALSO("@preprocess_describe", "#preprocess_describe"), @@ -77,8 +77,6 @@ PARAM_MATRIX_IN("dataset", "Input matrix to save as images.", "I"); PARAM_MODEL_IN(ImageInfo, "input_model", "Input Image Info model.", "m"); PARAM_MODEL_OUT(ImageInfo, "output_model", "Output Image Info model.", "M"); -#ifdef HAS_STB // Compile this only if stb is present. - static void mlpackMain() { // Parse command line options. @@ -138,8 +136,4 @@ static void mlpackMain() if (CLI::HasParam("output_model")) CLI::GetParam ("output_model") = info; } -#else -static void mlpackMain() {} - -#endif // HAS_STB. diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index c23405426c..77e4fcc2e4 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -137,7 +137,7 @@ add_executable(mlpack_test main_tests/kfn_test.cpp main_tests/knn_test.cpp main_tests/linear_regression_test.cpp - main_tests/load_save_image_test.cpp + main_tests/image_converter_test.cpp main_tests/linear_svm_test.cpp main_tests/logistic_regression_test.cpp main_tests/local_coordinate_coding_test.cpp diff --git a/src/mlpack/tests/main_tests/load_save_image_test.cpp b/src/mlpack/tests/main_tests/image_converter_test.cpp similarity index 93% rename from src/mlpack/tests/main_tests/load_save_image_test.cpp rename to src/mlpack/tests/main_tests/image_converter_test.cpp index 016b3424e3..bd34831c72 100644 --- a/src/mlpack/tests/main_tests/load_save_image_test.cpp +++ b/src/mlpack/tests/main_tests/image_converter_test.cpp @@ -12,10 +12,10 @@ #define BINDING_TYPE BINDING_TYPE_TEST #include -static const std::string testName = "LoadSaveImage"; +static const std::string testName = "ImageConverter"; #include -#include +#include #include "test_helper.hpp" #include @@ -23,25 +23,27 @@ static const std::string testName = "LoadSaveImage"; using namespace mlpack; -struct LoadSaveImageTestFixture +struct ImageConverterTestFixture { public: - LoadSaveImageTestFixture() + ImageConverterTestFixture() { // Cache in the options for this program. CLI::RestoreSettings(testName); } - ~LoadSaveImageTestFixture() + ~ImageConverterTestFixture() { // Clear the settings. + remove("test_image777.png"); + remove("test_image999.png"); bindings::tests::CleanMemory(); CLI::ClearSettings(); } }; -BOOST_FIXTURE_TEST_SUITE(LoadSaveImageMainTest, - LoadSaveImageTestFixture); +BOOST_FIXTURE_TEST_SUITE(ImageConverterMainTest, + ImageConverterTestFixture); BOOST_AUTO_TEST_CASE(LoadImageTest) { @@ -197,7 +199,7 @@ BOOST_AUTO_TEST_CASE(InvalidChannelTest) /** * Check for invalid input values. */ -BOOST_AUTO_TEST_CASE(EmptyinputTest) +BOOST_AUTO_TEST_CASE(EmptyInputTest) { SetInputParam>("input", {}); SetInputParam("height", 50); From 36eb28eff73e86cf0acd3916879f382234dd8f7e Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Sun, 15 Mar 2020 12:48:46 +0530 Subject: [PATCH 121/265] change variable "power" to "duration" I think that "duration"(indicating time duration in which force is applied) instead of "power" is more appropriate. --- .../environment/continuous_mountain_car.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp b/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp index 5837e1b147..4d5eccce56 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp @@ -96,7 +96,7 @@ class ContinuousMountainCar * @param positionGoal Final target position. * @param velocityMin Minimum legal velocity. * @param velocityMax Maximum legal velocity. - * @param power Power generated by car. + * @param duration time duration for which force is apllied on the car. * @param doneReward Reward recieved by the agent on success. * @param maxSteps The number of steps after which the episode * terminates. If the value is 0, there is no limit. @@ -106,7 +106,7 @@ class ContinuousMountainCar const double positionGoal = 0.45, const double velocityMin = -0.07, const double velocityMax = 0.07, - const double power = 0.0015, + const double duration = 0.0015, const double doneReward = 100, const size_t maxSteps = 0) : positionMin(positionMin), @@ -114,7 +114,7 @@ class ContinuousMountainCar positionGoal(positionGoal), velocityMin(velocityMin), velocityMax(velocityMax), - power(power), + duration(duration), doneReward(doneReward), maxSteps(maxSteps), stepsPerformed(0) @@ -139,7 +139,7 @@ class ContinuousMountainCar double force = std::min(std::max(action.action[0], -1.0), 1.0); // Update states. - nextState.Velocity() = state.Velocity() + force * power - 0.0025 * + nextState.Velocity() = state.Velocity() + force * duration - 0.0025 * std::cos(3 * state.Position()); nextState.Velocity() = std::min( std::max(nextState.Velocity(), velocityMin), velocityMax); @@ -236,8 +236,8 @@ class ContinuousMountainCar //! Locally-stored maximum legal velocity. double velocityMax; - //! Locally-stored power. - double power; + //! Locally-stored duration. + double duration; //! Locally-stored done reward. double doneReward; From e22b85d15b8c931c94572f6c5e203c6cbcf4b47c Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Sun, 15 Mar 2020 12:56:05 +0530 Subject: [PATCH 122/265] Update continuous_mountain_car.hpp --- .../environment/continuous_mountain_car.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp b/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp index 4d5eccce56..24cdc3cc04 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp @@ -96,7 +96,7 @@ class ContinuousMountainCar * @param positionGoal Final target position. * @param velocityMin Minimum legal velocity. * @param velocityMax Maximum legal velocity. - * @param duration time duration for which force is apllied on the car. + * @param duration Time Duration for which force is applied on the car. * @param doneReward Reward recieved by the agent on success. * @param maxSteps The number of steps after which the episode * terminates. If the value is 0, there is no limit. From 27ceb51793baa28a3576469d3a68970052dc5670 Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Sun, 15 Mar 2020 13:52:13 +0530 Subject: [PATCH 123/265] use the ClampRange() instead of std::max,min mlpack/core/math/clamp.hpp already contains a function to clip values between a minimum value and a maximum value. --- .../environment/continuous_mountain_car.hpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp b/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp index 24cdc3cc04..4d59e91d87 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp @@ -18,6 +18,7 @@ #define MLPACK_METHODS_RL_ENVIRONMENT_CONTINUOUS_MOUNTAIN_CAR_HPP #include +#include namespace mlpack { namespace rl { @@ -136,16 +137,14 @@ class ContinuousMountainCar stepsPerformed++; // Calculate acceleration. - double force = std::min(std::max(action.action[0], -1.0), 1.0); + double force = math::ClampRange(action.action[0], -1.0, 1.0); // Update states. nextState.Velocity() = state.Velocity() + force * duration - 0.0025 * std::cos(3 * state.Position()); - nextState.Velocity() = std::min( - std::max(nextState.Velocity(), velocityMin), velocityMax); + nextState.Velocity() = math::ClampRange(nextState.Velocity(), velocityMin, velocityMax); nextState.Position() = state.Position() + nextState.Velocity(); - nextState.Position() = std::min( - std::max(nextState.Position(), positionMin), positionMax); + nextState.Position() = math::ClampRange(nextState.Position(), positionMin, positionMax); if (nextState.Position() == positionMin && nextState.Velocity() < 0) nextState.Velocity() = 0.0; From 924bc562e96d1a3a9b58dc78e06362daf027652a Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Sun, 15 Mar 2020 13:55:40 +0530 Subject: [PATCH 124/265] using clamprange --- .../methods/reinforcement_learning/environment/acrobot.hpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/acrobot.hpp b/src/mlpack/methods/reinforcement_learning/environment/acrobot.hpp index 62bb0633fc..7b71b0a996 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/acrobot.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/acrobot.hpp @@ -169,10 +169,8 @@ class Acrobot nextState.Theta2() = Wrap(currentNextState[1], -M_PI, M_PI); //! The value of angular velocity is bounded in min and max value. - nextState.AngularVelocity1() = std::min( - std::max(currentNextState[2], -maxVel1), maxVel1); - nextState.AngularVelocity2() = std::min( - std::max(currentNextState[3], -maxVel2), maxVel2); + nextState.AngularVelocity1() = math::ClampRange(currentNextState[2], -maxVel1, maxVel1); + nextState.AngularVelocity2() = math::ClampRange(currentNextState[3], -maxVel2, maxVel2); // Check if the episode has terminated. bool done = IsTerminal(nextState); From 3daa7e25b8fa5dfb67ed1d609b71a979ce5e7fee Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Sun, 15 Mar 2020 13:58:40 +0530 Subject: [PATCH 125/265] using clamprange --- .../reinforcement_learning/environment/mountain_car.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/mountain_car.hpp b/src/mlpack/methods/reinforcement_learning/environment/mountain_car.hpp index b02b631cd4..8f79358e01 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/mountain_car.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/mountain_car.hpp @@ -17,6 +17,7 @@ #define MLPACK_METHODS_RL_ENVIRONMENT_MOUNTAIN_CAR_HPP #include +#include namespace mlpack { namespace rl { @@ -134,13 +135,11 @@ class MountainCar int direction = action - 1; nextState.Velocity() = state.Velocity() + 0.001 * direction - 0.0025 * std::cos(3 * state.Position()); - nextState.Velocity() = std::min( - std::max(nextState.Velocity(), velocityMin), velocityMax); + nextState.Velocity() = math::ClampRange(nextState.Velocity(), velocityMin, velocityMax); // Update states. nextState.Position() = state.Position() + nextState.Velocity(); - nextState.Position() = std::min( - std::max(nextState.Position(), positionMin), positionMax); + nextState.Position() = math::ClampRange(nextState.Position(), positionMin, positionMax); if (nextState.Position() == positionMin && nextState.Velocity() < 0) nextState.Velocity() = 0.0; From e2a904bac510ca23dc231a48e091cc5948045f21 Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Sun, 15 Mar 2020 14:00:46 +0530 Subject: [PATCH 126/265] using clamprange --- .../reinforcement_learning/environment/pendulum.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/pendulum.hpp b/src/mlpack/methods/reinforcement_learning/environment/pendulum.hpp index e7133bdf6b..1864d711c6 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/pendulum.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/pendulum.hpp @@ -17,6 +17,7 @@ #define MLPACK_METHODS_RL_ENVIRONMENT_PENDULUM_HPP #include +#include namespace mlpack { namespace rl { @@ -140,8 +141,7 @@ class Pendulum const double length = 1.0; // Get action and clip the values between max and min limits. - double torque = std::min( - std::max(action.action[0], -maxTorque), maxTorque); + double torque = math::ClampRange(action.action[0], -maxTorque, maxTorque); // Calculate costs of taking this action in the current state. double costs = std::pow(AngleNormalize(theta), 2) + 0.1 * @@ -151,8 +151,7 @@ class Pendulum double newAngularVelocity = angularVelocity + (-3.0 * gravity / (2 * length) * std::sin(theta + M_PI) + 3.0 / std::pow(mass * length, 2) * torque) * dt; - nextState.AngularVelocity() = std::min(std::max(newAngularVelocity, - -maxAngularVelocity), maxAngularVelocity); + nextState.AngularVelocity() = math::ClampRange(newAngularVelocity, -maxAngularVelocity, maxAngularVelocity); nextState.Theta() = theta + newAngularVelocity * dt; // Check if the episode has terminated From f99b2d517ff621e8bef4e6f4a5658cabfa26e68e Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Sun, 15 Mar 2020 14:01:58 +0530 Subject: [PATCH 127/265] using cliprange --- .../reinforcement_learning/environment/reward_clipping.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/reward_clipping.hpp b/src/mlpack/methods/reinforcement_learning/environment/reward_clipping.hpp index a7ce90038e..765519c89e 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/reward_clipping.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/reward_clipping.hpp @@ -13,6 +13,7 @@ #define MLPACK_METHODS_RL_ENVIRONMENT_REWARD_CLIPPING_HPP #include +#include namespace mlpack { namespace rl { @@ -91,7 +92,7 @@ class RewardClipping // Get original unclipped reward from base environment. double unclippedReward = environment.Sample(state, action, nextState); // Clip rewards according to the min and max limit and return. - return std::min(std::max(unclippedReward, minReward), maxReward); + return math::ClampRange(unclippedReward, minReward, maxReward); } /** From c5784d5acfce13f9696d219849f2ed422b96a91a Mon Sep 17 00:00:00 2001 From: Nishant Kumar Date: Sun, 15 Mar 2020 18:16:47 +0530 Subject: [PATCH 128/265] DoubleDQN doesn't utilize DoubleQLearning 1) Changed config.DoubleQLearning() = true; for Double DQN 2) Comment changes --- src/mlpack/tests/q_learning_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/tests/q_learning_test.cpp b/src/mlpack/tests/q_learning_test.cpp index e7c30b38f8..95a26f40ad 100644 --- a/src/mlpack/tests/q_learning_test.cpp +++ b/src/mlpack/tests/q_learning_test.cpp @@ -199,7 +199,7 @@ BOOST_AUTO_TEST_CASE(CartPoleWithDoubleDQN) config.Discount() = 0.9; config.TargetNetworkSyncInterval() = 100; config.ExplorationSteps() = 100; - config.DoubleQLearning() = false; + config.DoubleQLearning() = true; config.StepLimit() = 200; // Set up the DQN agent. @@ -215,7 +215,7 @@ BOOST_AUTO_TEST_CASE(CartPoleWithDoubleDQN) averageReturn(episodeReturn); /** - * Reaching running average return 35 is enough to show it works. + * Reaching running average return 40 is enough to show it works. * For the speed of the test case, I didn't set high criterion. */ Log::Debug << "Average return: " << averageReturn.mean() From f8786d0d6c33ee26dc003c20952f59db46ff2101 Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Sun, 15 Mar 2020 18:40:35 +0530 Subject: [PATCH 129/265] fixed the styling issue --- .../methods/reinforcement_learning/environment/acrobot.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/acrobot.hpp b/src/mlpack/methods/reinforcement_learning/environment/acrobot.hpp index 7b71b0a996..51fbe8c603 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/acrobot.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/acrobot.hpp @@ -169,8 +169,10 @@ class Acrobot nextState.Theta2() = Wrap(currentNextState[1], -M_PI, M_PI); //! The value of angular velocity is bounded in min and max value. - nextState.AngularVelocity1() = math::ClampRange(currentNextState[2], -maxVel1, maxVel1); - nextState.AngularVelocity2() = math::ClampRange(currentNextState[3], -maxVel2, maxVel2); + nextState.AngularVelocity1() = math::ClampRange(currentNextState[2], + -maxVel1, maxVel1); + nextState.AngularVelocity2() = math::ClampRange(currentNextState[3], + -maxVel2, maxVel2); // Check if the episode has terminated. bool done = IsTerminal(nextState); From 1243fa0fc1d6594814bece069f4a34680c192595 Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Sun, 15 Mar 2020 18:41:47 +0530 Subject: [PATCH 130/265] Update acrobot.hpp --- .../methods/reinforcement_learning/environment/acrobot.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/acrobot.hpp b/src/mlpack/methods/reinforcement_learning/environment/acrobot.hpp index 51fbe8c603..b61cc561e7 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/acrobot.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/acrobot.hpp @@ -170,7 +170,7 @@ class Acrobot //! The value of angular velocity is bounded in min and max value. nextState.AngularVelocity1() = math::ClampRange(currentNextState[2], - -maxVel1, maxVel1); + -maxVel1, maxVel1); nextState.AngularVelocity2() = math::ClampRange(currentNextState[3], -maxVel2, maxVel2); From aa39d630bdecd464d7f56f5bed9af87f955418fe Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Sun, 15 Mar 2020 18:43:13 +0530 Subject: [PATCH 131/265] Update mountain_car.hpp --- .../reinforcement_learning/environment/mountain_car.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/mountain_car.hpp b/src/mlpack/methods/reinforcement_learning/environment/mountain_car.hpp index 8f79358e01..a83e44d4fc 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/mountain_car.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/mountain_car.hpp @@ -135,11 +135,13 @@ class MountainCar int direction = action - 1; nextState.Velocity() = state.Velocity() + 0.001 * direction - 0.0025 * std::cos(3 * state.Position()); - nextState.Velocity() = math::ClampRange(nextState.Velocity(), velocityMin, velocityMax); + nextState.Velocity() = math::ClampRange(nextState.Velocity(), + velocityMin, velocityMax); // Update states. nextState.Position() = state.Position() + nextState.Velocity(); - nextState.Position() = math::ClampRange(nextState.Position(), positionMin, positionMax); + nextState.Position() = math::ClampRange(nextState.Position(), + positionMin, positionMax); if (nextState.Position() == positionMin && nextState.Velocity() < 0) nextState.Velocity() = 0.0; From 21b7e3240f39ad06ce8eb2821f96b2f67c84d2bf Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Sun, 15 Mar 2020 18:44:35 +0530 Subject: [PATCH 132/265] Update pendulum.hpp --- .../methods/reinforcement_learning/environment/pendulum.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/pendulum.hpp b/src/mlpack/methods/reinforcement_learning/environment/pendulum.hpp index 1864d711c6..55c02668cd 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/pendulum.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/pendulum.hpp @@ -151,7 +151,8 @@ class Pendulum double newAngularVelocity = angularVelocity + (-3.0 * gravity / (2 * length) * std::sin(theta + M_PI) + 3.0 / std::pow(mass * length, 2) * torque) * dt; - nextState.AngularVelocity() = math::ClampRange(newAngularVelocity, -maxAngularVelocity, maxAngularVelocity); + nextState.AngularVelocity() = math::ClampRange(newAngularVelocity, + -maxAngularVelocity, maxAngularVelocity); nextState.Theta() = theta + newAngularVelocity * dt; // Check if the episode has terminated From b268531dafb7992ba6dd66ae499d22074985e3b0 Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Sun, 15 Mar 2020 02:40:29 +0530 Subject: [PATCH 133/265] Style-fixes for New policy --- src/mlpack/core/data/string_encoding.hpp | 7 +- src/mlpack/core/data/string_encoding_impl.hpp | 3 +- .../bag_of_words_encoding_policy.hpp | 95 +++++------ .../dictionary_encoding_policy.hpp | 58 +++---- .../tf_idf_encoding_policy.hpp | 149 +++++++++--------- src/mlpack/tests/string_encoding_test.cpp | 67 ++++---- 6 files changed, 190 insertions(+), 189 deletions(-) diff --git a/src/mlpack/core/data/string_encoding.hpp b/src/mlpack/core/data/string_encoding.hpp index 1457fa92c7..ac9d67c366 100644 --- a/src/mlpack/core/data/string_encoding.hpp +++ b/src/mlpack/core/data/string_encoding.hpp @@ -153,9 +153,7 @@ class StringEncoding * 2. IsTokenEmpty() that accepts a token and returns true if the given * token is empty. */ - template + template void EncodeHelper(const std::vector& input, OutputType& output, const TokenizerType& tokenizer, @@ -182,8 +180,7 @@ class StringEncoding * 2. IsTokenEmpty() that accepts a token and returns true if the given * token is empty. */ - template + template void EncodeHelper(const std::vector& input, std::vector>& output, const TokenizerType& tokenizer, diff --git a/src/mlpack/core/data/string_encoding_impl.hpp b/src/mlpack/core/data/string_encoding_impl.hpp index b233fba6e5..16a533245b 100644 --- a/src/mlpack/core/data/string_encoding_impl.hpp +++ b/src/mlpack/core/data/string_encoding_impl.hpp @@ -152,8 +152,7 @@ EncodeHelper(const std::vector& input, } template -template +template void StringEncoding:: EncodeHelper(const std::vector& input, std::vector>& output, diff --git a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index 01e113b64d..a2225aafd4 100644 --- a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -18,6 +18,7 @@ namespace mlpack { namespace data { + /** * Definition of the BagOfWordsEncodingPolicy class. * @@ -30,15 +31,15 @@ class BagOfWordsEncodingPolicy { public: /** - * The function initializes the output matrix. - * - * @param output Output matrix to store the encoded results (sp_mat or mat). - * @param datasetSize The number of strings in the input dataset. - * @param maxNumTokens The maximum number of tokens in the strings of the - input dataset (not used). - * @param dictionarySize The size of the dictionary. - * @tparam MatType The type of output matrix. - */ + * The function initializes the output matrix. + * + * @param output Output matrix to store the encoded results (sp_mat or mat). + * @param datasetSize The number of strings in the input dataset. + * @param maxNumTokens The maximum number of tokens in the strings of the + input dataset (not used). + * @param dictionarySize The size of the dictionary. + * @tparam MatType The type of output matrix. + */ template static void InitMatrix(MatType& output, size_t datasetSize, @@ -49,64 +50,64 @@ class BagOfWordsEncodingPolicy } /** - * The function initializes the output matrix. - * Overloaded function to store result in vector> - * - * @param output Output matrix to store the encoded results. - * @param datasetSize The number of strings in the input dataset. - * @param maxNumTokens The maximum number of tokens in the strings of the - input dataset. - * @param dictionarySize The size of the dictionary (not used). - * @tparam OutputType The type of output vector. - */ + * The function initializes the output matrix. + * Overloaded function to store result in vector> + * + * @param output Output matrix to store the encoded results. + * @param datasetSize The number of strings in the input dataset. + * @param maxNumTokens The maximum number of tokens in the strings of the + input dataset. + * @param dictionarySize The size of the dictionary (not used). + * @tparam OutputType The type of output vector. + */ template static void InitMatrix(std::vector >& output, size_t datasetSize, size_t /*maxNumTokens*/, size_t dictionarySize) { - output.resize(datasetSize, std::vector (dictionarySize, 0)); + output.resize(datasetSize, std::vector(dictionarySize, 0)); } /** - * The function performs the bag of words encoding algorithm i.e. it writes - * the encoded token to the ouput. - * - * @param output Output matrix to store the encoded results (sp_mat or mat). - * @param value The encoded token. - * @param row The row number at which the encoding is performed. - * @param col The row token number at which the encoding is performed. - * @tparam MatType The type of output matrix. - */ + * The function performs the bag of words encoding algorithm i.e. it writes + * the encoded token to the output. + * + * @param output Output matrix to store the encoded results (sp_mat or mat). + * @param value The encoded token. + * @param row The row number at which the encoding is performed. + * @param col The row token number at which the encoding is performed. + * @tparam MatType The type of output matrix. + */ template static void Encode(MatType& output, size_t value, size_t row, size_t /*col*/) { - // Important since Mapping of words,Dcitionary Encoding starts from 1, + // Important since Mapping of words, Dictionary Encoding starts from 1, // whereas allowed column value is 0. output(row, value - 1) = 1; } /** - * The function performs the bag of words encoding algorithm i.e. it writes - * the encoded token to the ouput. - * Overload function to accepted vector> as output type. - * - * @param output Output matrix to store the encoded results. - * @param value The encoded token. - * @param row The row number at which the encoding is performed. - * @param col The row token number at which the encoding is performed. - * @tparam OutputType The type of output vector. - */ + * The function performs the bag of words encoding algorithm i.e. it writes + * the encoded token to the output. + * Overload function to accepted vector> as output type. + * + * @param output Output matrix to store the encoded results. + * @param value The encoded token. + * @param row The row number at which the encoding is performed. + * @param col The row token number at which the encoding is performed. + * @tparam OutputType The type of output vector. + */ template static void Encode(std::vector >& output, size_t value, size_t row, size_t /*col*/) { - // Important since Mapping of words,Dcitionary Encoding starts from 1, + // Important since Mapping of words in Dictionary Encoding starts from 1, // whereas allowed column value is 0. output[row][value - 1] = 1; } @@ -121,12 +122,12 @@ class BagOfWordsEncodingPolicy } /** - * Empty function, Important for tf-idf encoding policy - * - * @param row The row number at which the encoding is performed. - * @param numToken The count of token parsed till now. - * @param value The encoded token. - */ + * Empty function, Important for tf-idf encoding policy. + * + * @param row The row number at which the encoding is performed. + * @param numToken The count of token parsed till now. + * @param value The encoded token. + */ static void PreprocessToken(size_t /*row*/, size_t /*numTokens*/, size_t /*value*/) { } diff --git a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp index f1eb26313b..3af0bf8277 100644 --- a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp @@ -31,17 +31,17 @@ class DictionaryEncodingPolicy { public: /** - * The function initializes the output matrix. - * - * @tparam MatType The output matrix type. - * - * @param output Output matrix to store the encoded results (sp_mat or mat). - * @param datasetSize The number of strings in the input dataset. - * @param maxNumTokens The maximum number of tokens in the strings of the - input dataset. - * @param dictionarySize The size of the dictionary (not used). - * @tparam MatType The type of output matrix. - */ + * The function initializes the output matrix. + * + * @tparam MatType The output matrix type. + * + * @param output Output matrix to store the encoded results (sp_mat or mat). + * @param datasetSize The number of strings in the input dataset. + * @param maxNumTokens The maximum number of tokens in the strings of the + input dataset. + * @param dictionarySize The size of the dictionary (not used). + * @tparam MatType The type of output matrix. + */ template static void InitMatrix(MatType& output, const size_t datasetSize, @@ -52,17 +52,17 @@ class DictionaryEncodingPolicy } /** - * The function performs the dictionary encoding algorithm i.e. it writes - * the encoded token to the ouput. - * - * @tparam MatType The output matrix type. - * - * @param output Output matrix to store the encoded results (sp_mat or mat). - * @param value The encoded token. - * @param row The row number at which the encoding is performed. - * @param col The token index in the row. - * @tparam MatType The type of output matrix. - */ + * The function performs the dictionary encoding algorithm i.e. it writes + * the encoded token to the ouput. + * + * @tparam MatType The output matrix type. + * + * @param output Output matrix to store the encoded results (sp_mat or mat). + * @param value The encoded token. + * @param row The row number at which the encoding is performed. + * @param col The token index in the row. + * @tparam MatType The type of output matrix. + */ template static void Encode(MatType& output, const size_t value, @@ -88,12 +88,14 @@ class DictionaryEncodingPolicy } /** - * Empty function, Important for tf-idf encoding policy - * - * @param row The row number at which the encoding is performed. - * @param numToken The count of token parsed till now. - * @param value The encoded token. - */ + * Empty function, Important for tf-idf encoding policy. + * This function has been only used in tf-idf encoding policy and has no + * relevance here. + * + * @param row The row number at which the encoding is performed. + * @param numToken The count of token parsed till now. + * @param value The encoded token. + */ static void PreprocessToken(size_t /*row*/, size_t /*numTokens*/, size_t /*value*/) {} diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index d1724ccd3d..8f80ab73d8 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -1,5 +1,5 @@ /** - * @file td_idf_encoding_policy.hpp + * @file tf_idf_encoding_policy.hpp * @author Jeffin Sam * * Definition of the TfIdfEncodingPolicy class. @@ -15,12 +15,14 @@ #include #include #include + namespace mlpack { namespace data { + /** * Definition of the TfIdfEncodingPolicy class. * - * Tf-idf is weighing scheme that stands for term-frequency multiplied by + * Tf-idf is a weighting scheme that stands for term-frequency multiplied by * inverse document-frequency. * The goal of using tf-idf is to scale down the impact of tokens that occur * very frequently in a given corpus while using the type of Term-Frequency @@ -35,15 +37,15 @@ class TfIdfEncodingPolicy { public: /** - * Enum class used to identify the type of tf encoding - * - * Follwing are the defination of the types - * BINARY : binary weighting scheme (0,1) - * RAW_COUNT : raw count weighting scheme (count of token for every row) - * TERM_FREQUENCY : term frequency weighting scheme (count / length(row)) - * SUBLINEAR_TF : logarthimic weighting scheme (log(tf) + 1) - * - */ + * Enum class used to identify the type of tf encoding. + * + * Following are the type definitions: + * BINARY : binary weighting scheme (0,1) + * RAW_COUNT : raw count weighting scheme (count of token for every row) + * TERM_FREQUENCY : term frequency weighting scheme (count / length(row)) + * SUBLINEAR_TF : logarithmic weighting scheme (log(tf) + 1) + * + */ enum class TfTypes { RAW_COUNT, @@ -53,33 +55,34 @@ class TfIdfEncodingPolicy }; /** - * A constructor for the class which is use to set the type of term frequency - * and also the value for somoothIdf. - * - * @param tfType The type of term frequency, The avialbale option are - * RAW_COUNT : The count of a specific token - * BINARY : 1 if token occurs in document and 0 otherwise; - * TERM_FREQUENCY : Raw_count ÷ (number of words in document) - * SUBLINEAR_TF : log(Raw_Count) + 1 - * - * @param smoothIdf Used to indicate whether to use smooth idf or not. - */ + * A constructor for the class which is use to set the type of term frequency + * and also the value for smoothIdf. + * + * @param tfType The type of term frequency, The avialbale option are + * RAW_COUNT : The count of a specific token + * BINARY : 1 if token occurs in document and 0 otherwise; + * TERM_FREQUENCY : Raw_count ÷ (number of words in document) + * SUBLINEAR_TF : log(Raw_Count) + 1 + * + * @param smoothIdf Used to indicate whether to use smooth idf or not. + */ TfIdfEncodingPolicy(TfTypes tfType = TfTypes::RAW_COUNT, bool smoothIdf = true) : tfType(tfType), smoothIdf(smoothIdf) { } + /** - * The function initializes the output matrix. - * - * @param output Output matrix to store the encoded results (sp_mat or mat). - * @param datasetSize The number of strings in the input dataset. - * @param maxNumTokens The maximum number of tokens in the strings of the + * The function initializes the output matrix. + * + * @param output Output matrix to store the encoded results (sp_mat or mat). + * @param datasetSize The number of strings in the input dataset. + * @param maxNumTokens The maximum number of tokens in the strings of the input dataset. - * @param dictionarySize The size of the dictionary (not used). - * @tparam MatType The type of output matrix. - */ + * @param dictionarySize The size of the dictionary (not used). + * @tparam MatType The type of output matrix. + */ template static void InitMatrix(MatType& output, size_t datasetSize, @@ -90,43 +93,41 @@ class TfIdfEncodingPolicy } /** - * The function initializes the output matrix. - * Overloaded function to store result in vector> - * - * @param output Output matrix to store the encoded results. - * @param datasetSize The number of strings in the input dataset. - * @param maxNumTokens The maximum number of tokens in the strings of the - input dataset. - * @param dictionarySize The size of the dictionary (not used). - * @tparam OutputType The type of output vector. - */ + * The function initializes the output matrix. + * Overloaded function to store result in vector> + * + * @param output Output matrix to store the encoded results. + * @param datasetSize The number of strings in the input dataset. + * @param maxNumTokens The maximum number of tokens in the strings of the + input dataset. + * @param dictionarySize The size of the dictionary (not used). + * @tparam OutputType The type of output vector. + */ template static void InitMatrix(std::vector >& output, size_t datasetSize, size_t /*maxNumTokens*/, size_t dictionarySize) { - output.resize(datasetSize, std::vector (dictionarySize, 0)); + output.resize(datasetSize, std::vector(dictionarySize, 0)); } /** - * The function performs the TfIdf encoding algorithm i.e. it writes - * the encoded token to the ouput. - * - * @param output Output matrix to store the encoded results (sp_mat or mat). - * @param value The encoded token. - * @param row The row number at which the encoding is performed. - * @param col The row token number at which the encoding is performed. - * @tparam MatType The type of output matrix. - */ + * The function performs the TfIdf encoding algorithm i.e. it writes + * the encoded token to the output. + * + * @param output Output matrix to store the encoded results (sp_mat or mat). + * @param value The encoded token. + * @param row The row number at which the encoding is performed. + * @param col The row token number at which the encoding is performed. + * @tparam MatType The type of output matrix. + */ template void Encode(MatType& output, size_t value, size_t row, size_t /*col*/) { - // Important since Mapping of words,Dcitionary Encoding starts from 1, - // whereas allowed column value is 0. double idf, tf; if (smoothIdf) idf = std::log((output.n_rows + 1) / (1 + idfdict[value - 1])) + 1; @@ -142,29 +143,27 @@ class TfIdfEncodingPolicy else tf = tokenCount[row][value - 1]; - output(row, value-1) = tf * idf; + output(row, value - 1) = tf * idf; } /** - * The function performs the TfIdf encoding algorithm i.e. it writes - * the encoded token to the ouput. - * Overload function to accepted vector> as output type. - * - * @param output Output matrix to store the encoded results. - * @param value The encoded token. - * @param row The row number at which the encoding is performed. - * @param col The row token number at which the encoding is performed. - * @tparam OutputType The type of output vector. - * @tparam OutputType The type of output vector. - */ + * The function performs the TfIdf encoding algorithm i.e. it writes + * the encoded token to the output. + * Overload function to accepted vector> as output type. + * + * @param output Output matrix to store the encoded results. + * @param value The encoded token. + * @param row The row number at which the encoding is performed. + * @param col The row token number at which the encoding is performed. + * @tparam OutputType The type of output vector. + * @tparam OutputType The type of output vector. + */ template void Encode(std::vector >& output, size_t value, size_t row, size_t /*col*/) { - // Important since Mapping of words,Dcitionary Encoding starts from 1, - // whereas allowed column value is 0. double idf, tf; if (smoothIdf) idf = std::log((output.size() + 1) / (1 + idfdict[value - 1])) + 1; @@ -180,27 +179,27 @@ class TfIdfEncodingPolicy else tf = tokenCount[row][value - 1]; - output[row][value-1] = tf * idf; + output[row][value - 1] = tf * idf; } /** * Serialize the class to the given archive. */ template - void serialize(Archive& ar , const unsigned int /* version */) + void serialize(Archive& ar, const unsigned int /* version */) { ar & BOOST_SERIALIZATION_NVP(tfType); ar & BOOST_SERIALIZATION_NVP(smoothIdf); } /* - * The function is used to create the datastrcutre will be important to find - * out idfvalue of words, and then wrtiting the output based on their count. - * - * @param row The row number at which the encoding is performed. - * @param numToken The count of token parsed till now. - * @param value The encoded token. - */ + * The function is used to create the datastrcutre will be important to find + * out idfvalue of words, and then wrtiting the output based on their count. + * + * @param row The row number at which the encoding is performed. + * @param numToken The count of token parsed till now. + * @param value The encoded token. + */ void PreprocessToken(size_t row, size_t /*numTokens*/, size_t value) diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 866acb54ba..8e4b8107b9 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -224,8 +224,8 @@ BOOST_AUTO_TEST_CASE(SplitByAnyOfTokenizerUnicodeTest) } /** -* Test the CharExtract tokenizer. -*/ + * Test the CharExtract tokenizer. + */ BOOST_AUTO_TEST_CASE(DictionaryEncodingIndividualCharactersTest) { vector input = { @@ -521,6 +521,9 @@ BOOST_AUTO_TEST_CASE(CharExtractDictionaryEncodingSerialization) CheckMatrices(output, xmlOutput, textOutput, binaryOutput); } +/** + * Test for Bag of Words encoding algorithm. + */ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) { using DictionaryType = StringEncodingDictionary; @@ -532,12 +535,12 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) encoder.Encode(stringEncodingInput, output, tokenizer); const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers + // Checking that everything is mapped to different numbers. std::unordered_map keysCount; for (auto& keyValue : dictionary.Mapping()) { keysCount[keyValue.second]++; - // Every token should be mapped only once + // Every token should be mapped only once. BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } arma::mat expected = { @@ -552,8 +555,8 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) } /** -* Bag of Words encoding algorithm output saved in a vector. -*/ + * Bag of Words encoding algorithm output saved in a vector. + */ BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingTest) { using DictionaryType = StringEncodingDictionary; @@ -567,12 +570,12 @@ BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingTest) const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers + // Checking that everything is mapped to different numbers. std::unordered_map keysCount; for (auto& keyValue : dictionary.Mapping()) { keysCount[keyValue.second]++; - // Every token should be mapped only once + // Every token should be mapped only once. BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } @@ -589,8 +592,8 @@ BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingTest) } /** -* Test Bag of Words encoding for characters. -*/ + * Test Bag of Words encoding for characters. + */ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) { vector input = { @@ -602,7 +605,7 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) arma::mat output; BagOfWordsEncoding encoder; - // Passing a empty string to encode characters + // Passing a empty string to encode characters. encoder.Encode(input, output, CharExtract()); arma::mat target = { { 1, 1, 1, 0, 0 }, @@ -628,7 +631,7 @@ BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingIndividualCharactersTest) vector> output; BagOfWordsEncoding encoder; - // Passing a empty string to encode characters + // Passing a empty string to encode characters. encoder.Encode(input, output, CharExtract()); vector> expected = { @@ -642,7 +645,7 @@ BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingIndividualCharactersTest) /** * Test the Tf-Idf Encoding using rawcount type and smoothidf as true, - * which is the deafult values used for algorithim. + * which is the default value used for algorithm. */ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) { @@ -655,12 +658,12 @@ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) encoder.Encode(stringEncodingInput, output, tokenizer); const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers + // Checking that everything is mapped to different numbers. std::unordered_map keysCount; for (auto& keyValue : dictionary.Mapping()) { keysCount[keyValue.second]++; - // Every token should be mapped only once + // Every token should be mapped only once. BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } arma::mat expected = { @@ -703,12 +706,12 @@ BOOST_AUTO_TEST_CASE(VectorRawCountSmoothIdfEncodingTest) const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers + // Checking that everything is mapped to different numbers. std::unordered_map keysCount; for (auto& keyValue : dictionary.Mapping()) { keysCount[keyValue.second]++; - // Every token should be mapped only once + // Every token should be mapped only once. BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } @@ -752,7 +755,7 @@ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) arma::mat output; TfIdfEncoding encoder; - // Passing a empty string to encode charactersrawcountsmoothidftrue + // Passing a empty string to encode characters. encoder.Encode(input, output, CharExtract()); arma::mat target = { { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, @@ -777,7 +780,7 @@ BOOST_AUTO_TEST_CASE(VectorRawCountSmoothIdfEncodingIndividualCharactersTest) vector> output; TfIdfEncoding encoder; - // Passing a empty string to encode characters + // Passing a empty string to encode characters. encoder.Encode(input, output, CharExtract()); vector> expected = { { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, @@ -805,12 +808,12 @@ BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers + // Checking that everything is mapped to different numbers. std::unordered_map keysCount; for (auto& keyValue : dictionary.Mapping()) { keysCount[keyValue.second]++; - // Every token should be mapped only once + // Every token should be mapped only once. BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } @@ -854,12 +857,12 @@ BOOST_AUTO_TEST_CASE(VectorTfIdfRawCountEncodingTest) const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers + // Checking that everything is mapped to different numbers. std::unordered_map keysCount; for (auto& keyValue : dictionary.Mapping()) { keysCount[keyValue.second]++; - // Every token should be mapped only once + // Every token should be mapped only once. BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } @@ -904,7 +907,7 @@ BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) TfIdfEncoding encoder(TfIdfEncodingPolicy::TfTypes::RAW_COUNT, false); - // Passing a empty string to encode charactersrawcountsmoothidftrue + // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); arma::mat target = { { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, @@ -931,7 +934,7 @@ BOOST_AUTO_TEST_CASE(VectorRawcountEncodingIndividualCharactersTest) TfIdfEncoding encoder(TfIdfEncodingPolicy::TfTypes::RAW_COUNT, false); - // Passing a empty string to encode characters + // Passing a empty string to encode characters. encoder.Encode(input, output, CharExtract()); vector> expected = { { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, @@ -959,7 +962,7 @@ BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) TfIdfEncoding encoder(TfIdfEncodingPolicy::TfTypes::BINARY, true); - // Passing a empty string to encode charactersrawcountsmoothidftrue + // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); arma::mat target = { { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, @@ -985,7 +988,7 @@ BOOST_AUTO_TEST_CASE(VectorBnarySmoothIdfEncodingIndividualCharactersTest) TfIdfEncoding encoder(TfIdfEncodingPolicy::TfTypes::BINARY, true); - // Passing a empty string to encode characters + // Passing a empty string to encode characters. encoder.Encode(input, output, CharExtract()); vector> expected = { { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, @@ -1013,7 +1016,7 @@ BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) TfIdfEncoding encoder(TfIdfEncodingPolicy::TfTypes::BINARY, false); - // Passing a empty string to encode charactersrawcountsmoothidftrue + // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); arma::mat target = { { 1.4054651081081644, 1, 1.4054651081081644, 0, 0 }, @@ -1039,7 +1042,7 @@ BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) TfIdfEncoding encoder(TfIdfEncodingPolicy::TfTypes::SUBLINEAR_TF, true); - // Passing a empty string to encode charactersrawcountsmoothidftrue + // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); arma::mat target = { { 1.2876820724517808, 1.6931471805599454, 2.1802352704293200, 0, 0 }, @@ -1066,7 +1069,7 @@ BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) TfIdfEncoding encoder(TfIdfEncodingPolicy::TfTypes::SUBLINEAR_TF, false); - // Passing a empty string to encode charactersrawcountsmoothidftrue + // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); arma::mat target = { { 1.4054651081081644, 1.6931471805599454, 2.3796592851687173, 0, 0 }, @@ -1093,7 +1096,7 @@ BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) TfIdfEncoding encoder(TfIdfEncodingPolicy::TfTypes::TERM_FREQUENCY, true); - // Passing a empty string to encode charactersrawcountsmoothidftrue + // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); arma::mat target = { { 0.2575364144903562, 0.4, 0.5150728289807124, 0, 0 }, @@ -1120,7 +1123,7 @@ BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) TfIdfEncoding encoder(TfIdfEncodingPolicy::TfTypes::TERM_FREQUENCY, false); - // Passing a empty string to encode charactersrawcountsmoothidftrue + // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); arma::mat target = { { 0.2810930216216329, 0.4, 0.5621860432432658, 0, 0 }, From 357df3cdd40f59c229da03cf418fdfe4514fc080 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 16 Mar 2020 13:06:44 +0000 Subject: [PATCH 134/265] Force minimum boost to 1.58 (and a workaround for CMake < 3.6). --- CMakeLists.txt | 23 +++++++++++------------ README.md | 8 ++++---- doc/guide/build.hpp | 2 +- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 18536d4c52..8c21581fa1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -370,7 +370,12 @@ if (NOT ENSMALLEN_FOUND) # Get the name of the directory. file (GLOB ENS_DIRECTORIES RELATIVE "${CMAKE_BINARY_DIR}/deps/" "${CMAKE_BINARY_DIR}/deps/ensmallen-[0-9]*.[0-9]*.[0-9]*") - list(FILTER ENS_DIRECTORIES EXCLUDE REGEX "ensmallen-.*\.tar\.gz") + # list(FILTER) is not available on 3.5 or older, but try to keep + # configuring without filtering the list anyway (it might work if only + # the file ensmallen-latest.tar.gz is present. + if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.6.0") + list(FILTER ENS_DIRECTORIES EXCLUDE REGEX "ensmallen-.*\.tar\.gz") + endif () list(LENGTH ENS_DIRECTORIES ENS_DIRECTORIES_LEN) if (ENS_DIRECTORIES_LEN EQUAL 1) list(GET ENS_DIRECTORIES 0 ENSMALLEN_INCLUDE_DIR) @@ -408,6 +413,9 @@ endif () # Unfortunately this configuration variable is necessary and will need to be # updated as time goes on and new versions are released. set(Boost_ADDITIONAL_VERSIONS + "1.72.0" "1.72" + "1.71.0" "1.71" + "1.70.0" "1.70" "1.69.0" "1.69" "1.68.0" "1.68" "1.67.0" "1.67" @@ -419,23 +427,14 @@ set(Boost_ADDITIONAL_VERSIONS "1.61.1" "1.61.0" "1.61" "1.60.1" "1.60.0" "1.60" "1.59.1" "1.59.0" "1.59" - "1.58.1" "1.58.0" "1.58" - "1.57.1" "1.57.0" "1.57" - "1.56.1" "1.56.0" "1.56" - "1.55.1" "1.55.0" "1.55" - "1.54.1" "1.54.0" "1.54" - "1.53.1" "1.53.0" "1.53" - "1.52.1" "1.52.0" "1.52" - "1.51.1" "1.51.0" "1.51" - "1.50.1" "1.50.0" "1.50" - "1.49.1" "1.49.0" "1.49") + "1.58.1" "1.58.0" "1.58") # Disable forced config-mode CMake search for Boost, which only imports targets # and does not set the variables that we need. # # TODO for the brave: transition all mlpack's CMake to 'target-based modern # CMake'. Good luck! You'll need it. set(Boost_NO_BOOST_CMAKE 1) -find_package(Boost 1.49 +find_package(Boost 1.58 COMPONENTS program_options unit_test_framework diff --git a/README.md b/README.md index f66437b25e..d204bb4b3a 100644 --- a/README.md +++ b/README.md @@ -100,11 +100,11 @@ Citations are beneficial for the growth and improvement of mlpack. mlpack has the following dependencies: - Armadillo >= 8.400.0 + Armadillo >= 8.400.0 Boost (program_options, math_c99, unit_test_framework, serialization, - spirit) - CMake >= 3.3.2 - ensmallen >= 2.10.0 + spirit) >= 1.58.0 + CMake >= 3.3.2 + ensmallen >= 2.10.0 All of those should be available in your distribution's package manager. If not, you will have to compile each of them by hand. See the documentation for diff --git a/doc/guide/build.hpp b/doc/guide/build.hpp index e0577c39ba..df5e2edb82 100644 --- a/doc/guide/build.hpp +++ b/doc/guide/build.hpp @@ -79,7 +79,7 @@ system and have headers present: - Armadillo >= 8.400.0 (with LAPACK support) - Boost (math_c99, program_options, serialization, unit_test_framework, heap, - spirit) >= 1.49 + spirit) >= 1.58 - ensmallen >= 2.10.0 (will be downloaded if not found) In addition, mlpack has the following optional dependencies: From 3f5ffcf4e026df9dbc1ad49182dd686b110f9a1d Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 16 Mar 2020 13:06:59 +0000 Subject: [PATCH 135/265] Update HISTORY. --- HISTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 3fb7cce8fa..96a1543073 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -52,6 +52,8 @@ * Change neural network types to avoid unnecessary use of rvalue references (#2259). + * Bump minimum Boost version to 1.58 (#????). + ### mlpack 3.2.2 ###### 2019-11-26 * Add `valid` and `same` padding option in `Convolution` and `Atrous From 93bc884a19bea322a1135d9921eccfb8acb0bdce Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 16 Mar 2020 13:08:58 +0000 Subject: [PATCH 136/265] Remove now-unneeded boost_backport_math.hpp. --- src/mlpack/core/boost_backport/CMakeLists.txt | 3 - .../boost_backport/boost_backport_math.hpp | 29 -- src/mlpack/core/boost_backport/polygamma.hpp | 94 ---- src/mlpack/core/boost_backport/trigamma.hpp | 469 ------------------ src/mlpack/core/dists/gamma_distribution.cpp | 4 +- 5 files changed, 2 insertions(+), 597 deletions(-) delete mode 100644 src/mlpack/core/boost_backport/boost_backport_math.hpp delete mode 100644 src/mlpack/core/boost_backport/polygamma.hpp delete mode 100644 src/mlpack/core/boost_backport/trigamma.hpp diff --git a/src/mlpack/core/boost_backport/CMakeLists.txt b/src/mlpack/core/boost_backport/CMakeLists.txt index c2b15d5120..9df7408ca1 100644 --- a/src/mlpack/core/boost_backport/CMakeLists.txt +++ b/src/mlpack/core/boost_backport/CMakeLists.txt @@ -2,15 +2,12 @@ # Anything not in this list will not be compiled into mlpack. set(SOURCES bernoulli.hpp - boost_backport_math.hpp boost_backport_serialization.hpp detail/bernoulli_details.hpp detail/polygamma.hpp detail/unchecked_bernoulli.hpp math_fwd.hpp policy.hpp - polygamma.hpp - trigamma.hpp unordered_collections_load_imp.hpp unordered_collections_save_imp.hpp unordered_map.hpp diff --git a/src/mlpack/core/boost_backport/boost_backport_math.hpp b/src/mlpack/core/boost_backport/boost_backport_math.hpp deleted file mode 100644 index f3d7f3cc32..0000000000 --- a/src/mlpack/core/boost_backport/boost_backport_math.hpp +++ /dev/null @@ -1,29 +0,0 @@ -/** - * @file boost_backport.hpp - * @author Yannis Mentekidis - * - * Centralized control of what boost files to include. We have backported the - * following boost functionality here: - * - * * trigamma and polygamma function evaluation (added in boost 1.58.0) - * - * For versions 1.56, 1.57 we include the backported polygamma and trigamma - * functions. Anything newer, we include from Boost. - */ -#ifndef MLPACK_CORE_BOOST_BACKPORT_MATH_HPP -#define MLPACK_CORE_BOOST_BACKPORT_MATH_HPP - -#include - -#if BOOST_VERSION < 105800 - // Backported trigamma and polygamma. - #include "mlpack/core/boost_backport/trigamma.hpp" - #include "mlpack/core/boost_backport/polygamma.hpp" -#else - // Boost's version. - #include - #include -#endif - -#endif // MLPACK_CORE_BOOST_BACKPORT_HPP - diff --git a/src/mlpack/core/boost_backport/polygamma.hpp b/src/mlpack/core/boost_backport/polygamma.hpp deleted file mode 100644 index 3ff77836b3..0000000000 --- a/src/mlpack/core/boost_backport/polygamma.hpp +++ /dev/null @@ -1,94 +0,0 @@ - -/////////////////////////////////////////////////////////////////////////////// -// Copyright 2013 Nikhar Agrawal -// Copyright 2013 Christopher Kormanyos -// Copyright 2014 John Maddock -// Copyright 2013 Paul Bristow -// Distributed under the Boost -// Software License, Version 1.0. (See accompanying file -// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#ifndef _BOOST_POLYGAMMA_2013_07_30_HPP_ - #define _BOOST_POLYGAMMA_2013_07_30_HPP_ - -#include "detail/polygamma.hpp" -#include "trigamma.hpp" -#include - -// Forward declarations -namespace boost { namespace math { - template - inline typename tools::promote_args::type - trigamma(T x, const Policy&); - - template - inline typename tools::promote_args::type - trigamma(T x); -}} - -namespace boost { namespace math { - - - template - inline typename tools::promote_args::type polygamma(const int n, T x, const Policy& pol) - { - // - // Filter off special cases right at the start: - // - if(n == 0) - return boost::math::digamma(x, pol); - if(n == 1) - return boost::math::trigamma(x, pol); - // - // We've found some standard library functions to misbehave if any FPU exception flags - // are set prior to their call, this code will clear those flags, then reset them - // on exit: - // - BOOST_FPU_EXCEPTION_GUARD - // - // The type of the result - the common type of T and U after - // any integer types have been promoted to double: - // - typedef typename tools::promote_args::type result_type; - // - // The type used for the calculation. This may be a wider type than - // the result in order to ensure full precision: - // - typedef typename policies::evaluation::type value_type; - // - // The type of the policy to forward to the actual implementation. - // We disable promotion of float and double as that's [possibly] - // happened already in the line above. Also reset to the default - // any policies we don't use (reduces code bloat if we're called - // multiple times with differing policies we don't actually use). - // Also normalise the type, again to reduce code bloat in case we're - // called multiple times with functionally identical policies that happen - // to be different types. - // - typedef typename policies::normalise< - Policy, - policies::promote_float, - policies::promote_double, - policies::discrete_quantile<>, - policies::assert_undefined<> >::type forwarding_policy; - // - // Whew. Now we can make the actual call to the implementation. - // Arguments are explicitly cast to the evaluation type, and the result - // passed through checked_narrowing_cast which handles things like overflow - // according to the policy passed: - // - return policies::checked_narrowing_cast( - detail::polygamma_imp(n, static_cast(x), forwarding_policy()), - "boost::math::polygamma<%1%>(int, %1%)"); - } - - template - inline typename tools::promote_args::type polygamma(const int n, T x) - { - return boost::math::polygamma(n, x, policies::policy<>()); - } - -} } // namespace boost::math - -#endif // _BOOST_BERNOULLI_2013_05_30_HPP_ - diff --git a/src/mlpack/core/boost_backport/trigamma.hpp b/src/mlpack/core/boost_backport/trigamma.hpp deleted file mode 100644 index ebf435289e..0000000000 --- a/src/mlpack/core/boost_backport/trigamma.hpp +++ /dev/null @@ -1,469 +0,0 @@ -// (C) Copyright John Maddock 2006. -// Use, modification and distribution are subject to the -// Boost Software License, Version 1.0. (See accompanying file -// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#ifndef BOOST_MATH_SF_TRIGAMMA_HPP -#define BOOST_MATH_SF_TRIGAMMA_HPP - -#ifdef _MSC_VER -#pragma once -#endif - -#include "math_fwd.hpp" -#include "polygamma.hpp" -#include -#include -#include -#include -#include -#include -#include - -namespace boost{ -namespace math{ -namespace detail{ - -template -T polygamma_imp(const int n, T x, const Policy &pol); - -template -T trigamma_prec(T x, const mpl::int_<53>*, const Policy&) -{ - // Max error in interpolated form: 3.736e-017 - static const T offset = BOOST_MATH_BIG_CONSTANT(T, 53, 2.1093254089355469); - static const T P_1_2[] = { - BOOST_MATH_BIG_CONSTANT(T, 53, -1.1093280605946045), - BOOST_MATH_BIG_CONSTANT(T, 53, -3.8310674472619321), - BOOST_MATH_BIG_CONSTANT(T, 53, -3.3703848401898283), - BOOST_MATH_BIG_CONSTANT(T, 53, 0.28080574467981213), - BOOST_MATH_BIG_CONSTANT(T, 53, 1.6638069578676164), - BOOST_MATH_BIG_CONSTANT(T, 53, 0.64468386819102836), - }; - static const T Q_1_2[] = { - BOOST_MATH_BIG_CONSTANT(T, 53, 1.0), - BOOST_MATH_BIG_CONSTANT(T, 53, 3.4535389668541151), - BOOST_MATH_BIG_CONSTANT(T, 53, 4.5208926987851437), - BOOST_MATH_BIG_CONSTANT(T, 53, 2.7012734178351534), - BOOST_MATH_BIG_CONSTANT(T, 53, 0.64468798399785611), - BOOST_MATH_BIG_CONSTANT(T, 53, -0.20314516859987728e-6), - }; - // Max error in interpolated form: 1.159e-017 - static const T P_2_4[] = { - BOOST_MATH_BIG_CONSTANT(T, 53, -0.13803835004508849e-7), - BOOST_MATH_BIG_CONSTANT(T, 53, 0.50000049158540261), - BOOST_MATH_BIG_CONSTANT(T, 53, 1.6077979838469348), - BOOST_MATH_BIG_CONSTANT(T, 53, 2.5645435828098254), - BOOST_MATH_BIG_CONSTANT(T, 53, 2.0534873203680393), - BOOST_MATH_BIG_CONSTANT(T, 53, 0.74566981111565923), - }; - static const T Q_2_4[] = { - BOOST_MATH_BIG_CONSTANT(T, 53, 1.0), - BOOST_MATH_BIG_CONSTANT(T, 53, 2.8822787662376169), - BOOST_MATH_BIG_CONSTANT(T, 53, 4.1681660554090917), - BOOST_MATH_BIG_CONSTANT(T, 53, 2.7853527819234466), - BOOST_MATH_BIG_CONSTANT(T, 53, 0.74967671848044792), - BOOST_MATH_BIG_CONSTANT(T, 53, -0.00057069112416246805), - }; - // Maximum Deviation Found: 6.896e-018 - // Expected Error Term : -6.895e-018 - // Maximum Relative Change in Control Points : 8.497e-004 - static const T P_4_inf[] = { - static_cast(0.68947581948701249e-17L), - static_cast(0.49999999999998975L), - static_cast(1.0177274392923795L), - static_cast(2.498208511343429L), - static_cast(2.1921221359427595L), - static_cast(1.5897035272532764L), - static_cast(0.40154388356961734L), - }; - static const T Q_4_inf[] = { - static_cast(1.0L), - static_cast(1.7021215452463932L), - static_cast(4.4290431747556469L), - static_cast(2.9745631894384922L), - static_cast(2.3013614809773616L), - static_cast(0.28360399799075752L), - static_cast(0.022892987908906897L), - }; - - if(x <= 2) - { - return (offset + boost::math::tools::evaluate_polynomial(P_1_2, x) / tools::evaluate_polynomial(Q_1_2, x)) / (x * x); - } - else if(x <= 4) - { - T y = 1 / x; - return (1 + tools::evaluate_polynomial(P_2_4, y) / tools::evaluate_polynomial(Q_2_4, y)) / x; - } - T y = 1 / x; - return (1 + tools::evaluate_polynomial(P_4_inf, y) / tools::evaluate_polynomial(Q_4_inf, y)) / x; -} - -template -T trigamma_prec(T x, const mpl::int_<64>*, const Policy&) -{ - // Max error in interpolated form: 1.178e-020 - static const T offset_1_2 = BOOST_MATH_BIG_CONSTANT(T, 64, 2.109325408935546875); - static const T P_1_2[] = { - BOOST_MATH_BIG_CONSTANT(T, 64, -1.10932535608960258341), - BOOST_MATH_BIG_CONSTANT(T, 64, -4.18793841543017129052), - BOOST_MATH_BIG_CONSTANT(T, 64, -4.63865531898487734531), - BOOST_MATH_BIG_CONSTANT(T, 64, -0.919832884430500908047), - BOOST_MATH_BIG_CONSTANT(T, 64, 1.68074038333180423012), - BOOST_MATH_BIG_CONSTANT(T, 64, 1.21172611429185622377), - BOOST_MATH_BIG_CONSTANT(T, 64, 0.259635673503366427284), - }; - static const T Q_1_2[] = { - BOOST_MATH_BIG_CONSTANT(T, 64, 1.0), - BOOST_MATH_BIG_CONSTANT(T, 64, 3.77521119359546982995), - BOOST_MATH_BIG_CONSTANT(T, 64, 5.664338024578956321), - BOOST_MATH_BIG_CONSTANT(T, 64, 4.25995134879278028361), - BOOST_MATH_BIG_CONSTANT(T, 64, 1.62956638448940402182), - BOOST_MATH_BIG_CONSTANT(T, 64, 0.259635512844691089868), - BOOST_MATH_BIG_CONSTANT(T, 64, 0.629642219810618032207e-8), - }; - // Max error in interpolated form: 3.912e-020 - static const T P_2_8[] = { - BOOST_MATH_BIG_CONSTANT(T, 64, -0.387540035162952880976e-11), - BOOST_MATH_BIG_CONSTANT(T, 64, 0.500000000276430504), - BOOST_MATH_BIG_CONSTANT(T, 64, 3.21926880986360957306), - BOOST_MATH_BIG_CONSTANT(T, 64, 10.2550347708483445775), - BOOST_MATH_BIG_CONSTANT(T, 64, 18.9002075150709144043), - BOOST_MATH_BIG_CONSTANT(T, 64, 21.0357215832399705625), - BOOST_MATH_BIG_CONSTANT(T, 64, 13.4346512182925923978), - BOOST_MATH_BIG_CONSTANT(T, 64, 3.98656291026448279118), - }; - static const T Q_2_8[] = { - BOOST_MATH_BIG_CONSTANT(T, 64, 1.0), - BOOST_MATH_BIG_CONSTANT(T, 64, 6.10520430478613667724), - BOOST_MATH_BIG_CONSTANT(T, 64, 18.475001060603645512), - BOOST_MATH_BIG_CONSTANT(T, 64, 31.7087534567758405638), - BOOST_MATH_BIG_CONSTANT(T, 64, 31.908814523890465398), - BOOST_MATH_BIG_CONSTANT(T, 64, 17.4175479039227084798), - BOOST_MATH_BIG_CONSTANT(T, 64, 3.98749106958394941276), - BOOST_MATH_BIG_CONSTANT(T, 64, -0.000115917322224411128566), - }; - // Maximum Deviation Found: 2.635e-020 - // Expected Error Term : 2.635e-020 - // Maximum Relative Change in Control Points : 1.791e-003 - static const T P_8_inf[] = { - BOOST_MATH_BIG_CONSTANT(T, 64, -0.263527875092466899848e-19), - BOOST_MATH_BIG_CONSTANT(T, 64, 0.500000000000000058145), - BOOST_MATH_BIG_CONSTANT(T, 64, 0.0730121433777364138677), - BOOST_MATH_BIG_CONSTANT(T, 64, 1.94505878379957149534), - BOOST_MATH_BIG_CONSTANT(T, 64, 0.0517092358874932620529), - BOOST_MATH_BIG_CONSTANT(T, 64, 1.07995383547483921121), - }; - static const T Q_8_inf[] = { - BOOST_MATH_BIG_CONSTANT(T, 64, 1.0), - BOOST_MATH_BIG_CONSTANT(T, 64, -0.187309046577818095504), - BOOST_MATH_BIG_CONSTANT(T, 64, 3.95255391645238842975), - BOOST_MATH_BIG_CONSTANT(T, 64, -1.14743283327078949087), - BOOST_MATH_BIG_CONSTANT(T, 64, 2.52989799376344914499), - BOOST_MATH_BIG_CONSTANT(T, 64, -0.627414303172402506396), - BOOST_MATH_BIG_CONSTANT(T, 64, 0.141554248216425512536), - }; - - if(x <= 2) - { - return (offset_1_2 + boost::math::tools::evaluate_polynomial(P_1_2, x) / tools::evaluate_polynomial(Q_1_2, x)) / (x * x); - } - else if(x <= 8) - { - T y = 1 / x; - return (1 + tools::evaluate_polynomial(P_2_8, y) / tools::evaluate_polynomial(Q_2_8, y)) / x; - } - T y = 1 / x; - return (1 + tools::evaluate_polynomial(P_8_inf, y) / tools::evaluate_polynomial(Q_8_inf, y)) / x; -} - -template -T trigamma_prec(T x, const mpl::int_<113>*, const Policy&) -{ - // Max error in interpolated form: 1.916e-035 - - static const T P_1_2[] = { - BOOST_MATH_BIG_CONSTANT(T, 113, -0.999999999999999082554457936871832533), - BOOST_MATH_BIG_CONSTANT(T, 113, -4.71237311120865266379041700054847734), - BOOST_MATH_BIG_CONSTANT(T, 113, -7.94125711970499027763789342500817316), - BOOST_MATH_BIG_CONSTANT(T, 113, -5.74657746697664735258222071695644535), - BOOST_MATH_BIG_CONSTANT(T, 113, -0.404213349456398905981223965160595687), - BOOST_MATH_BIG_CONSTANT(T, 113, 2.47877781178642876561595890095758896), - BOOST_MATH_BIG_CONSTANT(T, 113, 2.07714151702455125992166949812126433), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.858877899162360138844032265418028567), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.20499222604410032375789018837922397), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.0272103140348194747360175268778415049), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.0015764849020876949848954081173520686), - }; - static const T Q_1_2[] = { - BOOST_MATH_BIG_CONSTANT(T, 113, 1.0), - BOOST_MATH_BIG_CONSTANT(T, 113, 4.71237311120863419878375031457715223), - BOOST_MATH_BIG_CONSTANT(T, 113, 9.58619118655339853449127952145877467), - BOOST_MATH_BIG_CONSTANT(T, 113, 11.0940067269829372437561421279054968), - BOOST_MATH_BIG_CONSTANT(T, 113, 8.09075424749327792073276309969037885), - BOOST_MATH_BIG_CONSTANT(T, 113, 3.87705890159891405185343806884451286), - BOOST_MATH_BIG_CONSTANT(T, 113, 1.22758678701914477836330837816976782), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.249092040606385004109672077814668716), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.0295750413900655597027079600025569048), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.00157648490200498142247694709728858139), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.161264050344059471721062360645432809e-14), - }; - - // Max error in interpolated form: 8.958e-035 - static const T P_2_4[] = { - BOOST_MATH_BIG_CONSTANT(T, 113, -2.55843734739907925764326773972215085), - BOOST_MATH_BIG_CONSTANT(T, 113, -12.2830208240542011967952466273455887), - BOOST_MATH_BIG_CONSTANT(T, 113, -23.9195022162767993526575786066414403), - BOOST_MATH_BIG_CONSTANT(T, 113, -24.9256431504823483094158828285470862), - BOOST_MATH_BIG_CONSTANT(T, 113, -14.7979122765478779075108064826412285), - BOOST_MATH_BIG_CONSTANT(T, 113, -4.46654453928610666393276765059122272), - BOOST_MATH_BIG_CONSTANT(T, 113, -0.0191439033405649675717082465687845002), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.515412052554351265708917209749037352), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.195378348786064304378247325360320038), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.0334761282624174313035014426794245393), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.002373665205942206348500250056602687), - }; - static const T Q_2_4[] = { - BOOST_MATH_BIG_CONSTANT(T, 113, 1.0), - BOOST_MATH_BIG_CONSTANT(T, 113, 4.80098558454419907830670928248659245), - BOOST_MATH_BIG_CONSTANT(T, 113, 9.99220727843170133895059300223445265), - BOOST_MATH_BIG_CONSTANT(T, 113, 11.8896146167631330735386697123464976), - BOOST_MATH_BIG_CONSTANT(T, 113, 8.96613256683809091593793565879092581), - BOOST_MATH_BIG_CONSTANT(T, 113, 4.47254136149624110878909334574485751), - BOOST_MATH_BIG_CONSTANT(T, 113, 1.48600982028196527372434773913633152), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.319570735766764237068541501137990078), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.0407358345787680953107374215319322066), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.00237366520593271641375755486420859837), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.239554887903526152679337256236302116e-15), - BOOST_MATH_BIG_CONSTANT(T, 113, -0.294749244740618656265237072002026314e-17), - }; - - static const T y_offset_2_4 = BOOST_MATH_BIG_CONSTANT(T, 113, 3.558437347412109375); - - // Max error in interpolated form: 4.319e-035 - static const T P_4_8[] = { - BOOST_MATH_BIG_CONSTANT(T, 113, 0.166626112697021464248967707021688845e-16), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.499999999999997739552090249208808197), - BOOST_MATH_BIG_CONSTANT(T, 113, 6.40270945019053817915772473771553187), - BOOST_MATH_BIG_CONSTANT(T, 113, 41.3833374155000608013677627389343329), - BOOST_MATH_BIG_CONSTANT(T, 113, 166.803341854562809335667241074035245), - BOOST_MATH_BIG_CONSTANT(T, 113, 453.39964786925369319960722793414521), - BOOST_MATH_BIG_CONSTANT(T, 113, 851.153712317697055375935433362983944), - BOOST_MATH_BIG_CONSTANT(T, 113, 1097.70657567285059133109286478004458), - BOOST_MATH_BIG_CONSTANT(T, 113, 938.431232478455316020076349367632922), - BOOST_MATH_BIG_CONSTANT(T, 113, 487.268001604651932322080970189930074), - BOOST_MATH_BIG_CONSTANT(T, 113, 119.953445242335730062471193124820659), - }; - static const T Q_4_8[] = { - BOOST_MATH_BIG_CONSTANT(T, 113, 1.0), - BOOST_MATH_BIG_CONSTANT(T, 113, 12.4720855670474488978638945855932398), - BOOST_MATH_BIG_CONSTANT(T, 113, 78.6093129753298570701376952709727391), - BOOST_MATH_BIG_CONSTANT(T, 113, 307.470246050318322489781182863190127), - BOOST_MATH_BIG_CONSTANT(T, 113, 805.140686101151538537565264188630079), - BOOST_MATH_BIG_CONSTANT(T, 113, 1439.12019760292146454787601409644413), - BOOST_MATH_BIG_CONSTANT(T, 113, 1735.6105285756048831268586001383127), - BOOST_MATH_BIG_CONSTANT(T, 113, 1348.32500712856328019355198611280536), - BOOST_MATH_BIG_CONSTANT(T, 113, 607.225985860570846699704222144650563), - BOOST_MATH_BIG_CONSTANT(T, 113, 119.952317857277045332558673164517227), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.000140165918355036060868680809129436084), - }; - - // Maximum Deviation Found: 2.867e-035 - // Expected Error Term : 2.866e-035 - // Maximum Relative Change in Control Points : 2.662e-004 - static const T P_8_16[] = { - BOOST_MATH_BIG_CONSTANT(T, 113, -0.184828315274146610610872315609837439e-19), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.500000000000000004122475157735807738), - BOOST_MATH_BIG_CONSTANT(T, 113, 3.02533865247313349284875558880415875), - BOOST_MATH_BIG_CONSTANT(T, 113, 13.5995927517457371243039532492642734), - BOOST_MATH_BIG_CONSTANT(T, 113, 35.3132224283087906757037999452941588), - BOOST_MATH_BIG_CONSTANT(T, 113, 67.1639424550714159157603179911505619), - BOOST_MATH_BIG_CONSTANT(T, 113, 83.5767733658513967581959839367419891), - BOOST_MATH_BIG_CONSTANT(T, 113, 71.073491212235705900866411319363501), - BOOST_MATH_BIG_CONSTANT(T, 113, 35.8621515614725564575893663483998663), - BOOST_MATH_BIG_CONSTANT(T, 113, 8.72152231639983491987779743154333318), - }; - static const T Q_8_16[] = { - BOOST_MATH_BIG_CONSTANT(T, 113, 1.0), - BOOST_MATH_BIG_CONSTANT(T, 113, 5.71734397161293452310624822415866372), - BOOST_MATH_BIG_CONSTANT(T, 113, 25.293404179620438179337103263274815), - BOOST_MATH_BIG_CONSTANT(T, 113, 62.2619767967468199111077640625328469), - BOOST_MATH_BIG_CONSTANT(T, 113, 113.955048909238993473389714972250235), - BOOST_MATH_BIG_CONSTANT(T, 113, 130.807138328938966981862203944329408), - BOOST_MATH_BIG_CONSTANT(T, 113, 102.423146902337654110717764213057753), - BOOST_MATH_BIG_CONSTANT(T, 113, 44.0424772805245202514468199602123565), - BOOST_MATH_BIG_CONSTANT(T, 113, 8.89898032477904072082994913461386099), - BOOST_MATH_BIG_CONSTANT(T, 113, -0.0296627336872039988632793863671456398), - }; - // Maximum Deviation Found: 1.079e-035 - // Expected Error Term : -1.079e-035 - // Maximum Relative Change in Control Points : 7.884e-003 - static const T P_16_inf[] = { - BOOST_MATH_BIG_CONSTANT(T, 113, 0.0), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.500000000000000000000000000000087317), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.345625669885456215194494735902663968), - BOOST_MATH_BIG_CONSTANT(T, 113, 9.62895499360842232127552650044647769), - BOOST_MATH_BIG_CONSTANT(T, 113, 3.5936085382439026269301003761320812), - BOOST_MATH_BIG_CONSTANT(T, 113, 49.459599118438883265036646019410669), - BOOST_MATH_BIG_CONSTANT(T, 113, 7.77519237321893917784735690560496607), - BOOST_MATH_BIG_CONSTANT(T, 113, 74.4536074488178075948642351179304121), - BOOST_MATH_BIG_CONSTANT(T, 113, 2.75209340397069050436806159297952699), - BOOST_MATH_BIG_CONSTANT(T, 113, 23.9292359711471667884504840186561598), - }; - static const T Q_16_inf[] = { - BOOST_MATH_BIG_CONSTANT(T, 113, 1.0), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.357918006437579097055656138920742037), - BOOST_MATH_BIG_CONSTANT(T, 113, 19.1386039850709849435325005484512944), - BOOST_MATH_BIG_CONSTANT(T, 113, 0.874349081464143606016221431763364517), - BOOST_MATH_BIG_CONSTANT(T, 113, 98.6516097434855572678195488061432509), - BOOST_MATH_BIG_CONSTANT(T, 113, -16.1051972833382893468655223662534306), - BOOST_MATH_BIG_CONSTANT(T, 113, 154.316860216253720989145047141653727), - BOOST_MATH_BIG_CONSTANT(T, 113, -40.2026880424378986053105969312264534), - BOOST_MATH_BIG_CONSTANT(T, 113, 60.1679136674264778074736441126810223), - BOOST_MATH_BIG_CONSTANT(T, 113, -13.3414844622256422644504472438320114), - BOOST_MATH_BIG_CONSTANT(T, 113, 2.53795636200649908779512969030363442), - }; - - if(x <= 2) - { - return (2 + boost::math::tools::evaluate_polynomial(P_1_2, x) / tools::evaluate_polynomial(Q_1_2, x)) / (x * x); - } - else if(x <= 4) - { - return (y_offset_2_4 + boost::math::tools::evaluate_polynomial(P_2_4, x) / tools::evaluate_polynomial(Q_2_4, x)) / (x * x); - } - else if(x <= 8) - { - T y = 1 / x; - return (1 + tools::evaluate_polynomial(P_4_8, y) / tools::evaluate_polynomial(Q_4_8, y)) / x; - } - else if(x <= 16) - { - T y = 1 / x; - return (1 + tools::evaluate_polynomial(P_8_16, y) / tools::evaluate_polynomial(Q_8_16, y)) / x; - } - T y = 1 / x; - return (1 + tools::evaluate_polynomial(P_16_inf, y) / tools::evaluate_polynomial(Q_16_inf, y)) / x; -} - -template -T trigamma_imp(T x, const Tag* t, const Policy& pol) -{ - // - // This handles reflection of negative arguments, and all our - // error handling, then forwards to the T-specific approximation. - // - BOOST_MATH_STD_USING // ADL of std functions. - - T result = 0; - // - // Check for negative arguments and use reflection: - // - if(x <= 0) - { - // Reflect: - T z = 1 - x; - // Argument reduction for tan: - if(floor(x) == x) - { - return policies::raise_pole_error("boost::math::trigamma<%1%>(%1%)", 0, (1-x), pol); - } - T s = fabs(x) < fabs(z) ? boost::math::sin_pi(x, pol) : boost::math::sin_pi(z, pol); - return -trigamma_imp(z, t, pol) + boost::math::pow<2>(constants::pi()) / (s * s); - } - if(x < 1) - { - result = 1 / (x * x); - x += 1; - } - return result + trigamma_prec(x, t, pol); -} - -template -T trigamma_imp(T x, const mpl::int_<0>*, const Policy& pol) -{ - return polygamma_imp(1, x, pol); -} -// -// Initializer: ensure all our constants are initialized prior to the first call of main: -// -template -struct trigamma_initializer -{ - struct init - { - init() - { - typedef typename policies::precision::type precision_type; - do_init(mpl::bool_()); - } - void do_init(const mpl::true_&) - { - boost::math::trigamma(T(2.5), Policy()); - } - void do_init(const mpl::false_&){} - void force_instantiate()const{} - }; - static const init initializer; - static void force_instantiate() - { - initializer.force_instantiate(); - } -}; - -template -const typename trigamma_initializer::init trigamma_initializer::initializer; - -} // namespace detail - -template -inline typename tools::promote_args::type - trigamma(T x, const Policy&) -{ - typedef typename tools::promote_args::type result_type; - typedef typename policies::evaluation::type value_type; - typedef typename policies::precision::type precision_type; - typedef typename mpl::if_< - mpl::or_< - mpl::less_equal >, - mpl::greater > - >, - mpl::int_<0>, - typename mpl::if_< - mpl::less >, - mpl::int_<53>, - typename mpl::if_< - mpl::less >, - mpl::int_<64>, - mpl::int_<113> - >::type - >::type - >::type tag_type; - - typedef typename policies::normalise< - Policy, - policies::promote_float, - policies::promote_double, - policies::discrete_quantile<>, - policies::assert_undefined<> >::type forwarding_policy; - - // Force initialization of constants: - detail::trigamma_initializer::force_instantiate(); - - return policies::checked_narrowing_cast(detail::trigamma_imp( - static_cast(x), - static_cast(0), forwarding_policy()), "boost::math::trigamma<%1%>(%1%)"); -} - -template -inline typename tools::promote_args::type - trigamma(T x) -{ - return trigamma(x, policies::policy<>()); -} - -} // namespace math -} // namespace boost -#endif - diff --git a/src/mlpack/core/dists/gamma_distribution.cpp b/src/mlpack/core/dists/gamma_distribution.cpp index 54097c1fd0..63f7d7e29c 100644 --- a/src/mlpack/core/dists/gamma_distribution.cpp +++ b/src/mlpack/core/dists/gamma_distribution.cpp @@ -11,8 +11,8 @@ * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ #include "gamma_distribution.hpp" -// This will include digamma and trigamma. -#include +#include +#include using namespace mlpack; using namespace mlpack::distribution; From 4bfc97f2e2ea5d63c631ff2e0792614db41aa52f Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 16 Mar 2020 13:09:36 +0000 Subject: [PATCH 137/265] Remove unnecessary boost backport for serialization. --- .../boost_backport_serialization.hpp | 9 +- .../core/boost_backport/unordered_map.hpp | 234 ------------------ 2 files changed, 1 insertion(+), 242 deletions(-) delete mode 100644 src/mlpack/core/boost_backport/unordered_map.hpp diff --git a/src/mlpack/core/boost_backport/boost_backport_serialization.hpp b/src/mlpack/core/boost_backport/boost_backport_serialization.hpp index 173761f5f7..2b6c2cdd03 100644 --- a/src/mlpack/core/boost_backport/boost_backport_serialization.hpp +++ b/src/mlpack/core/boost_backport/boost_backport_serialization.hpp @@ -16,14 +16,7 @@ #define MLPACK_CORE_BOOST_BACKPORT_SERIALIZATION_HPP #include - -#if BOOST_VERSION < 105600 - // Backported unordered_map. - #include "mlpack/core/boost_backport/unordered_map.hpp" -#else - // Boost's version. - #include -#endif +#include #if BOOST_VERSION == 105800 /** diff --git a/src/mlpack/core/boost_backport/unordered_map.hpp b/src/mlpack/core/boost_backport/unordered_map.hpp deleted file mode 100644 index 4ae37d22c9..0000000000 --- a/src/mlpack/core/boost_backport/unordered_map.hpp +++ /dev/null @@ -1,234 +0,0 @@ -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -#ifndef BOOST_SERIALIZATION_UNORDERED_MAP_HPP -#define BOOST_SERIALIZATION_UNORDERED_MAP_HPP - -// MS compatible compilers support #pragma once -#if defined(_MSC_VER) && (_MSC_VER >= 1020) -# pragma once -#endif - -/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 -// serialization/unordered_map.hpp: -// serialization for stl unordered_map templates - -// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . -// (C) Copyright 2014 Jim Bell -// Use, modification and distribution is subject to the Boost Software -// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -// See http://www.boost.org for updates, documentation, and revision history. - -#include - -#include - -#include -#include "unordered_collections_save_imp.hpp" -#include "unordered_collections_load_imp.hpp" -#include - -namespace boost { -namespace serialization { - -namespace stl { - -// map input -template -struct archive_input_unordered_map -{ - inline void operator()( - Archive &ar, - Container &s, - const unsigned int v - ){ - typedef typename Container::value_type type; - detail::stack_construct t(ar, v); - // borland fails silently w/o full namespace - ar >> boost::serialization::make_nvp("item", t.reference()); - std::pair result = - s.insert(t.reference()); - // note: the following presumes that the map::value_type was NOT tracked - // in the archive. This is the usual case, but here there is no way - // to determine that. - if(result.second){ - ar.reset_object_address( - & (result.first->second), - & t.reference().second - ); - } - } -}; - -// multimap input -template -struct archive_input_unordered_multimap -{ - inline void operator()( - Archive &ar, - Container &s, - const unsigned int v - ){ - typedef typename Container::value_type type; - detail::stack_construct t(ar, v); - // borland fails silently w/o full namespace - ar >> boost::serialization::make_nvp("item", t.reference()); - typename Container::const_iterator result - = s.insert(t.reference()); - // note: the following presumes that the map::value_type was NOT tracked - // in the archive. This is the usual case, but here there is no way - // to determine that. - ar.reset_object_address( - & result->second, - & t.reference() - ); - } -}; - -} // stl - -template< - class Archive, - class Key, - class HashFcn, - class EqualKey, - class Allocator -> -inline void save( - Archive & ar, - const std::unordered_map< - Key, HashFcn, EqualKey, Allocator - > &t, - const unsigned int /*file_version*/ -){ - boost::serialization::stl::save_unordered_collection< - Archive, - std::unordered_map< - Key, HashFcn, EqualKey, Allocator - > - >(ar, t); -} - -template< - class Archive, - class Key, - class HashFcn, - class EqualKey, - class Allocator -> -inline void load( - Archive & ar, - std::unordered_map< - Key, HashFcn, EqualKey, Allocator - > &t, - const unsigned int /*file_version*/ -){ - boost::serialization::stl::load_unordered_collection< - Archive, - std::unordered_map< - Key, HashFcn, EqualKey, Allocator - >, - boost::serialization::stl::archive_input_unordered_map< - Archive, - std::unordered_map< - Key, HashFcn, EqualKey, Allocator - > - > - >(ar, t); -} - -// split non-intrusive serialization function member into separate -// non intrusive save/load member functions -template< - class Archive, - class Key, - class HashFcn, - class EqualKey, - class Allocator -> -inline void serialize( - Archive & ar, - std::unordered_map< - Key, HashFcn, EqualKey, Allocator - > &t, - const unsigned int file_version -){ - boost::serialization::split_free(ar, t, file_version); -} - -// unordered_multimap -template< - class Archive, - class Key, - class HashFcn, - class EqualKey, - class Allocator -> -inline void save( - Archive & ar, - const std::unordered_multimap< - Key, HashFcn, EqualKey, Allocator - > &t, - const unsigned int /*file_version*/ -){ - boost::serialization::stl::save_unordered_collection< - Archive, - std::unordered_multimap< - Key, HashFcn, EqualKey, Allocator - > - >(ar, t); -} - -template< - class Archive, - class Key, - class HashFcn, - class EqualKey, - class Allocator -> -inline void load( - Archive & ar, - std::unordered_multimap< - Key, HashFcn, EqualKey, Allocator - > &t, - const unsigned int /*file_version*/ -){ - boost::serialization::stl::load_unordered_collection< - Archive, - std::unordered_multimap< - Key, HashFcn, EqualKey, Allocator - >, - boost::serialization::stl::archive_input_unordered_multimap< - Archive, - std::unordered_multimap< - Key, HashFcn, EqualKey, Allocator - > - > - >(ar, t); -} - -// split non-intrusive serialization function member into separate -// non intrusive save/load member functions -template< - class Archive, - class Key, - class HashFcn, - class EqualKey, - class Allocator -> -inline void serialize( - Archive & ar, - std::unordered_multimap< - Key, HashFcn, EqualKey, Allocator - > &t, - const unsigned int file_version -){ - boost::serialization::split_free(ar, t, file_version); -} - -} // namespace serialization -} // namespace boost - -#endif // BOOST_SERIALIZATION_UNORDERED_MAP_HPP From 36c2fde91a670d928a4d2a84febc80e4696e5d7b Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 16 Mar 2020 13:10:16 +0000 Subject: [PATCH 138/265] Remove unnecessary Boost version check. --- src/mlpack/tests/mlpack_test.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/mlpack/tests/mlpack_test.cpp b/src/mlpack/tests/mlpack_test.cpp index 1518d0afd9..0d7cdae5b7 100644 --- a/src/mlpack/tests/mlpack_test.cpp +++ b/src/mlpack/tests/mlpack_test.cpp @@ -14,11 +14,6 @@ #include -// We only need to do this for old Boost versions. -#if BOOST_VERSION < 103600 - #define BOOST_AUTO_TEST_MAIN -#endif - #if BOOST_VERSION >= 105900 #include #include From c97e58b8db914b378d336472ca764307c8deff7b Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 16 Mar 2020 13:16:58 +0000 Subject: [PATCH 139/265] Update with PR number. --- HISTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 96a1543073..b45285d8cf 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -52,7 +52,7 @@ * Change neural network types to avoid unnecessary use of rvalue references (#2259). - * Bump minimum Boost version to 1.58 (#????). + * Bump minimum Boost version to 1.58 (#2305). ### mlpack 3.2.2 ###### 2019-11-26 From 6fa652edbaa4df0afcd31a1248209e5a1f1b28c2 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 16 Mar 2020 13:17:32 +0000 Subject: [PATCH 140/265] Remove reference to deleted file. --- src/mlpack/core/boost_backport/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/mlpack/core/boost_backport/CMakeLists.txt b/src/mlpack/core/boost_backport/CMakeLists.txt index 9df7408ca1..5edfe13cab 100644 --- a/src/mlpack/core/boost_backport/CMakeLists.txt +++ b/src/mlpack/core/boost_backport/CMakeLists.txt @@ -10,7 +10,6 @@ set(SOURCES policy.hpp unordered_collections_load_imp.hpp unordered_collections_save_imp.hpp - unordered_map.hpp vector.hpp string_view.hpp string_view_fwd.hpp From 9f37d3a5f83407036bb9eb7b3083b59106479f74 Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Mon, 16 Mar 2020 20:22:49 +0530 Subject: [PATCH 141/265] Update continuous_mountain_car.hpp --- .../environment/continuous_mountain_car.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp b/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp index 4d59e91d87..c1336428a6 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/continuous_mountain_car.hpp @@ -142,9 +142,11 @@ class ContinuousMountainCar // Update states. nextState.Velocity() = state.Velocity() + force * duration - 0.0025 * std::cos(3 * state.Position()); - nextState.Velocity() = math::ClampRange(nextState.Velocity(), velocityMin, velocityMax); + nextState.Velocity() = math::ClampRange(nextState.Velocity(), + velocityMin, velocityMax); nextState.Position() = state.Position() + nextState.Velocity(); - nextState.Position() = math::ClampRange(nextState.Position(), positionMin, positionMax); + nextState.Position() = math::ClampRange(nextState.Position(), + positionMin, positionMax); if (nextState.Position() == positionMin && nextState.Velocity() < 0) nextState.Velocity() = 0.0; From 5a746abd70781c0e587b7cfa255de9fde3b85bd5 Mon Sep 17 00:00:00 2001 From: Mikhail Lozhnikov Date: Mon, 16 Mar 2020 18:43:10 +0300 Subject: [PATCH 142/265] Style fixes for the following files: - src/mlpack/core/data/string_encoding_impl.hpp - src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp - src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp - src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp --- src/mlpack/core/data/string_encoding_impl.hpp | 1 + .../bag_of_words_encoding_policy.hpp | 92 ++--- .../dictionary_encoding_policy.hpp | 20 +- .../tf_idf_encoding_policy.hpp | 331 +++++++++++------- 4 files changed, 272 insertions(+), 172 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_impl.hpp b/src/mlpack/core/data/string_encoding_impl.hpp index 16a533245b..2194486f23 100644 --- a/src/mlpack/core/data/string_encoding_impl.hpp +++ b/src/mlpack/core/data/string_encoding_impl.hpp @@ -126,6 +126,7 @@ EncodeHelper(const std::vector& input, { if (!dictionary.HasToken(token)) dictionary.AddToken(std::move(token)); + policy.PreprocessToken(i, numTokens, dictionary.Value(token)); token = tokenizer(strView); diff --git a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index a2225aafd4..4ca76b4a08 100644 --- a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -9,8 +9,8 @@ * 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_ENCODING_POLICIES_BAG_OF_WORDS_ENCODING_POLICY_HPP -#define MLPACK_CORE_DATA_ENCODING_POLICIES_BAG_OF_WORDS_ENCODING_POLICY_HPP +#ifndef MLPACK_CORE_DATA_STRING_ENCODING_POLICIES_BAG_OF_WORDS_ENCODING_POLICY_HPP +#define MLPACK_CORE_DATA_STRING_ENCODING_POLICIES_BAG_OF_WORDS_ENCODING_POLICY_HPP #include #include @@ -22,10 +22,12 @@ namespace data { /** * Definition of the BagOfWordsEncodingPolicy class. * - * BagOfWords is used as a helper class for StringEncoding. - * The encoder creates a vector of all the unique token and then assigns - * 1 if the token is present in the document, 0 if not present. The tokens - * are labeled in the order of their occurrence in the input dataset. + * BagOfWords is used as a helper class for StringEncoding. The encoder maps + * each dataset item to a vector of size N, where N is equal to the total number + * of tokens. If an item of the dataset has the i-th token, then the i-th + * coordinate of the corresponding vector is equal to 1, otherwise it's equal to + * zero. The order in which the tokens are labeled is defined by the dictionary + * used by the StringEncoding class. */ class BagOfWordsEncodingPolicy { @@ -33,18 +35,19 @@ class BagOfWordsEncodingPolicy /** * The function initializes the output matrix. * + * @tparam MatType The output matrix type. + * * @param output Output matrix to store the encoded results (sp_mat or mat). * @param datasetSize The number of strings in the input dataset. * @param maxNumTokens The maximum number of tokens in the strings of the input dataset (not used). * @param dictionarySize The size of the dictionary. - * @tparam MatType The type of output matrix. */ template static void InitMatrix(MatType& output, - size_t datasetSize, - size_t /*maxNumTokens*/, - size_t dictionarySize) + const size_t datasetSize, + const size_t /* maxNumTokens */, + const size_t dictionarySize) { output.zeros(datasetSize, dictionarySize); } @@ -53,18 +56,19 @@ class BagOfWordsEncodingPolicy * The function initializes the output matrix. * Overloaded function to store result in vector> * + * @tparam OutputType Type of the output vector. + * * @param output Output matrix to store the encoded results. * @param datasetSize The number of strings in the input dataset. * @param maxNumTokens The maximum number of tokens in the strings of the - input dataset. - * @param dictionarySize The size of the dictionary (not used). - * @tparam OutputType The type of output vector. + input dataset (not used). + * @param dictionarySize The size of the dictionary. */ template - static void InitMatrix(std::vector >& output, - size_t datasetSize, - size_t /*maxNumTokens*/, - size_t dictionarySize) + static void InitMatrix(std::vector>& output, + const size_t datasetSize, + const size_t /* maxNumTokens */, + const size_t dictionarySize) { output.resize(datasetSize, std::vector(dictionarySize, 0)); } @@ -73,20 +77,20 @@ class BagOfWordsEncodingPolicy * The function performs the bag of words encoding algorithm i.e. it writes * the encoded token to the output. * + * @tparam MatType The output matrix type. + * * @param output Output matrix to store the encoded results (sp_mat or mat). * @param value The encoded token. * @param row The row number at which the encoding is performed. - * @param col The row token number at which the encoding is performed. - * @tparam MatType The type of output matrix. + * @param col The token index in the row. */ template static void Encode(MatType& output, - size_t value, - size_t row, - size_t /*col*/) + const size_t value, + const size_t row, + const size_t /* col */) { - // Important since Mapping of words, Dictionary Encoding starts from 1, - // whereas allowed column value is 0. + // The labels are assigned sequentially starting from one. output(row, value - 1) = 1; } @@ -102,16 +106,27 @@ class BagOfWordsEncodingPolicy * @tparam OutputType The type of output vector. */ template - static void Encode(std::vector >& output, - size_t value, - size_t row, - size_t /*col*/) + static void Encode(std::vector>& output, + const size_t value, + const size_t row, + const size_t /* col */) { - // Important since Mapping of words in Dictionary Encoding starts from 1, - // whereas allowed column value is 0. + // The labels are assigned sequentially starting from one. output[row][value - 1] = 1; } + /** + * The function is not used by the bag of words encoding policy. + * + * @param row The row number at which the encoding is performed. + * @param col The token sequence number in the row. + * @param value The encoded token. + */ + static void PreprocessToken(size_t /* row */, + size_t /* col */, + size_t /* value */) + { } + /** * Serialize the class to the given archive. */ @@ -120,19 +135,14 @@ class BagOfWordsEncodingPolicy { // Nothing to serialize. } - - /** - * Empty function, Important for tf-idf encoding policy. - * - * @param row The row number at which the encoding is performed. - * @param numToken The count of token parsed till now. - * @param value The encoded token. - */ - static void PreprocessToken(size_t /*row*/, - size_t /*numTokens*/, - size_t /*value*/) { } }; +/** + * A convenient alias for the StringEncoding class with BagOfWordsEncodingPolicy + * and the default dictionary for the given token type. + * + * @tparam TokenType Type of the tokens. + */ template using BagOfWordsEncoding = StringEncoding>; diff --git a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp index 3af0bf8277..e3de62e526 100644 --- a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp @@ -40,13 +40,12 @@ class DictionaryEncodingPolicy * @param maxNumTokens The maximum number of tokens in the strings of the input dataset. * @param dictionarySize The size of the dictionary (not used). - * @tparam MatType The type of output matrix. */ template static void InitMatrix(MatType& output, const size_t datasetSize, const size_t maxNumTokens, - const size_t /*dictionarySize*/) + const size_t /* dictionarySize */) { output.zeros(datasetSize, maxNumTokens); } @@ -61,7 +60,6 @@ class DictionaryEncodingPolicy * @param value The encoded token. * @param row The row number at which the encoding is performed. * @param col The token index in the row. - * @tparam MatType The type of output matrix. */ template static void Encode(MatType& output, @@ -77,9 +75,10 @@ class DictionaryEncodingPolicy * the encoded token to the ouput. This is an overload function which saves * the result into the given vector to avoid padding. * + * @tparam OutputType Type of the output vector. + * * @param output Output vector to store the encoded results. * @param value The encoded token. - * @tparam OutputType The type of output vector. */ template static void Encode(std::vector& output, size_t value) @@ -88,17 +87,16 @@ class DictionaryEncodingPolicy } /** - * Empty function, Important for tf-idf encoding policy. - * This function has been only used in tf-idf encoding policy and has no - * relevance here. + * The function is not used by the dictionary encoding policy. * * @param row The row number at which the encoding is performed. - * @param numToken The count of token parsed till now. + * @param col The token sequence number in the row. * @param value The encoded token. */ - static void PreprocessToken(size_t /*row*/, - size_t /*numTokens*/, - size_t /*value*/) {} + static void PreprocessToken(const size_t /* row */, + const size_t /* col */, + const size_t /* value */) + { } /** * Serialize the class to the given archive. diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 8f80ab73d8..ecba9f1a8d 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -9,8 +9,8 @@ * 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_ENCODING_POLICIES_TF_IDF_ENCODING_POLICY_HPP -#define MLPACK_CORE_DATA_ENCODING_POLICIES_TF_IDF_ENCODING_POLICY_HPP +#ifndef MLPACK_CORE_DATA_STRING_ENCODING_POLICIES_TF_IDF_ENCODING_POLICY_HPP +#define MLPACK_CORE_DATA_STRING_ENCODING_POLICIES_TF_IDF_ENCODING_POLICY_HPP #include #include @@ -20,94 +20,98 @@ namespace mlpack { namespace data { /** - * Definition of the TfIdfEncodingPolicy class. + * Definition of the TfIdfEncodingPolicy class. TfIdfEncodingPolicy is used + * as a helper class for StringEncoding. * - * Tf-idf is a weighting scheme that stands for term-frequency multiplied by - * inverse document-frequency. - * The goal of using tf-idf is to scale down the impact of tokens that occur - * very frequently in a given corpus while using the type of Term-Frequency - * of a token and that are hence empirically less informative than features - * that occur in a small fraction of the training corpus. - * TfIdfEncodingPolicy is used as a helper class for StringEncoding. - * The encoder assigns a tf-idf number to each unique token and treat - * the dataset as categorical. The tokens are labeled in the order of their - * occurrence in the input dataset. + * Tf-idf is a weighting scheme that takes into account the importance of + * encoded tokens. The tf-idf statistics is equal to term frequency (tf) + * multiplied by inverse document frequency (idf). + * The encoder assigns the corresponding tf-idf value to each token. The order + * in which the tokens are labeled is defined by the dictionary used by the + * StringEncoding class. */ class TfIdfEncodingPolicy { public: /** - * Enum class used to identify the type of tf encoding. + * Enum class used to identify the type of term frequency encoding. * - * Following are the type definitions: - * BINARY : binary weighting scheme (0,1) - * RAW_COUNT : raw count weighting scheme (count of token for every row) - * TERM_FREQUENCY : term frequency weighting scheme (count / length(row)) - * SUBLINEAR_TF : logarithmic weighting scheme (log(tf) + 1) - * + * The present implementation supports the following types: + * BINARY Term frequency equals 1 if the row contains the encoded + * token and 0 otherwise. + * RAW_COUNT Term frequency equals the number of times when the encoded + * token occurs in the row. + * TERM_FREQUENCY Term frequency equals the number of times when the encoded + * token occurs in the row divided by the row size. + * SUBLINEAR_TF Term frequency equals \f$ 1 + log(rawCount), \f$ where + rawCount is equal to the number of times when the encoded + * token occurs in the row. */ enum class TfTypes { - RAW_COUNT, BINARY, - SUBLINEAR_TF, + RAW_COUNT, TERM_FREQUENCY, + SUBLINEAR_TF, }; /** * A constructor for the class which is use to set the type of term frequency * and also the value for smoothIdf. * - * @param tfType The type of term frequency, The avialbale option are - * RAW_COUNT : The count of a specific token - * BINARY : 1 if token occurs in document and 0 otherwise; - * TERM_FREQUENCY : Raw_count ÷ (number of words in document) - * SUBLINEAR_TF : log(Raw_Count) + 1 - * + * @param tfType Type of the term frequency statistics. * @param smoothIdf Used to indicate whether to use smooth idf or not. + * If idf is smooth it's calculated by the following formula: + * \f$ idf(T) = \log \frac{1 + N}{1 + df(T)} + 1, \f$ where + * \f$ N \f$ is the total number of strings in the document, + * \f$ T \f$ is the current encoded token, \f$ df(T) \f$ + * equals the number of strings which contain the token. + * If idf isn't smooth then the following rule applies: + * \f$ idf(T) = \log \frac{N}{df(T)} + 1. \f$ */ - TfIdfEncodingPolicy(TfTypes tfType = TfTypes::RAW_COUNT, - bool smoothIdf = true) : - tfType(tfType), - smoothIdf(smoothIdf) - { - } + TfIdfEncodingPolicy(const TfTypes tfType = TfTypes::RAW_COUNT, + const bool smoothIdf = true) : + tfType(tfType), + smoothIdf(smoothIdf) + { } /** * The function initializes the output matrix. * + * @tparam MatType The output matrix type. + * * @param output Output matrix to store the encoded results (sp_mat or mat). * @param datasetSize The number of strings in the input dataset. * @param maxNumTokens The maximum number of tokens in the strings of the - input dataset. - * @param dictionarySize The size of the dictionary (not used). - * @tparam MatType The type of output matrix. + input dataset (not used). + * @param dictionarySize The size of the dictionary. */ template static void InitMatrix(MatType& output, - size_t datasetSize, - size_t /*maxNumTokens*/, - size_t dictionarySize) + const size_t datasetSize, + const size_t /* maxNumTokens */, + const size_t dictionarySize) { output.zeros(datasetSize, dictionarySize); } /** * The function initializes the output matrix. - * Overloaded function to store result in vector> + * Overloaded function to store result in vector>. * + * @tparam OutputType Type of the output vector. + * * @param output Output matrix to store the encoded results. * @param datasetSize The number of strings in the input dataset. * @param maxNumTokens The maximum number of tokens in the strings of the - input dataset. - * @param dictionarySize The size of the dictionary (not used). - * @tparam OutputType The type of output vector. + input dataset (not used). + * @param dictionarySize The size of the dictionary. */ template - static void InitMatrix(std::vector >& output, - size_t datasetSize, - size_t /*maxNumTokens*/, - size_t dictionarySize) + static void InitMatrix(std::vector>& output, + const size_t datasetSize, + const size_t /* maxNumTokens */, + const size_t dictionarySize) { output.resize(datasetSize, std::vector(dictionarySize, 0)); } @@ -116,32 +120,26 @@ class TfIdfEncodingPolicy * The function performs the TfIdf encoding algorithm i.e. it writes * the encoded token to the output. * + * @tparam MatType The output matrix type. + * * @param output Output matrix to store the encoded results (sp_mat or mat). * @param value The encoded token. * @param row The row number at which the encoding is performed. - * @param col The row token number at which the encoding is performed. - * @tparam MatType The type of output matrix. + * @param col The token index in the row. */ template void Encode(MatType& output, - size_t value, - size_t row, - size_t /*col*/) + const size_t value, + const size_t row, + const size_t /* col */) { - double idf, tf; - if (smoothIdf) - idf = std::log((output.n_rows + 1) / (1 + idfdict[value - 1])) + 1; - else - idf = std::log(output.n_rows / idfdict[value - 1]) + 1; + const typename MatType::elem_type tf = + TermFrequency( + tokensFrequences[row][value], rowsSizes[row]); - if (tfType == TfTypes::TERM_FREQUENCY) - tf = tokenCount[row][value - 1] / row_size[row]; - else if (tfType == TfTypes::SUBLINEAR_TF) - tf = std::log(tokenCount[row][value - 1]) + 1; - else if (tfType == TfTypes::BINARY) - tf = tokenCount[row][value - 1] > 0 ? 1 : 0; - else - tf = tokenCount[row][value - 1]; + const typename MatType::elem_type idf = + InverseDocumentFrequency( + output.n_rows, numContainingStrings[value]); output(row, value - 1) = tf * idf; } @@ -149,39 +147,93 @@ class TfIdfEncodingPolicy /** * The function performs the TfIdf encoding algorithm i.e. it writes * the encoded token to the output. - * Overload function to accepted vector> as output type. + * Overloaded function to accept vector> as the output + * type. + * + * @tparam OutputType Type of the output vector. * * @param output Output matrix to store the encoded results. * @param value The encoded token. * @param row The row number at which the encoding is performed. - * @param col The row token number at which the encoding is performed. - * @tparam OutputType The type of output vector. - * @tparam OutputType The type of output vector. + * @param col The token index in the row. */ template void Encode(std::vector >& output, - size_t value, - size_t row, - size_t /*col*/) + const size_t value, + const size_t row, + const size_t /* col */) { - double idf, tf; - if (smoothIdf) - idf = std::log((output.size() + 1) / (1 + idfdict[value - 1])) + 1; - else - idf = std::log(output.size() / idfdict[value - 1]) + 1; + const OutputType tf = TermFrequency( + tokensFrequences[row][value], rowsSizes[row]); - if (tfType == TfTypes::TERM_FREQUENCY) - tf = tokenCount[row][value - 1] / row_size[row]; - else if (tfType == TfTypes::SUBLINEAR_TF) - tf = std::log(tokenCount[row][value - 1]) + 1; - else if (tfType == TfTypes::BINARY) - tf = tokenCount[row][value - 1] > 0 ? 1 : 0; - else - tf = tokenCount[row][value - 1]; + const OutputType idf = InverseDocumentFrequency( + output.size(), numContainingStrings[value]); output[row][value - 1] = tf * idf; } + /* + * The function calculates the necessary statistics for the purpose + * of the tf-idf algorithm during the first pass through the dataset. + * + * @param row The row number at which the encoding is performed. + * @param col The token sequence number in the row. + * @param value The encoded token. + */ + void PreprocessToken(const size_t row, + const size_t /* col */, + const size_t value) + { + if (row >= tokensFrequences.size()) + { + rowsSizes.resize(row + 1); + tokensFrequences.resize(row + 1); + } + + tokensFrequences[row][value]++; + + if (tokensFrequences[row][value] == 1) + numContainingStrings[value]++; + + rowsSizes[row]++; + } + + //! Return token frequencies. + const std::vector>& + TokensFrequences() const { return tokensFrequences; } + //! Modify token frequencies. + std::vector>& TokensFrequences() + { + return tokensFrequences; + } + + //! Get the number of containing strings depending on the given token. + const std::unordered_map& NumContainingStrings() const + { + return numContainingStrings; + } + + //! Modify the number of containing strings depending on the given token. + std::unordered_map& NumContainingStrings() + { + return numContainingStrings; + } + + //! Return the rows sizes. + const std::vector& RowsSizes() const { return rowsSizes; } + //! Modify the rows sizes. + std::vector& RowsSizes() { return rowsSizes; } + + //! Return the term frequency type. + TfTypes TfType() const { return tfType; } + //! Modify the term frequency type. + TfTypes& TfType() { return tfType; } + + //! Determine the idf algorithm type (whether it's smooth or not). + bool SmoothIdf() const { return smoothIdf; } + //! Modify the idf algorithm type (whether it's smooth or not). + bool& SmoothIdf() { return smoothIdf; } + /** * Serialize the class to the given archive. */ @@ -192,46 +244,85 @@ class TfIdfEncodingPolicy ar & BOOST_SERIALIZATION_NVP(smoothIdf); } - /* - * The function is used to create the datastrcutre will be important to find - * out idfvalue of words, and then wrtiting the output based on their count. - * - * @param row The row number at which the encoding is performed. - * @param numToken The count of token parsed till now. - * @param value The encoded token. - */ - void PreprocessToken(size_t row, - size_t /*numTokens*/, - size_t value) - { - if (row >= tokenCount.size()) - { - row_size.push_back(0); - tokenCount.emplace_back(); - } - tokenCount.back()[value - 1]++; - - if (tokenCount.back()[value - 1] == 1) - idfdict[value - 1]++; - - row_size.back()++; - } private: - // Used to store the count of token for each row. - std::vector> tokenCount; - // Used to store the idf values. - std::unordered_map idfdict; - // Used to store the number of tokens in each row. - std::vector row_size; - // smoothIdf variable to indicate smoothining. - bool smoothIdf; - // Type of Term Frequency to use. + /** + * The function calculates the term frequency statistics. + * + * @tparam ValueType Type of the returned value. + * + * @param numOccurrences The number of the given token occurrences in the row. + * @param numTokens The total number of tokens in the row. + */ + template + ValueType TermFrequency(const size_t numOccurrences, + const size_t numTokens) + { + switch (tfType) + { + case TfTypes::BINARY: + return numOccurrences > 0; + case TfTypes::RAW_COUNT: + return numOccurrences; + case TfTypes::TERM_FREQUENCY: + return static_cast(numOccurrences) / numTokens; + case TfTypes::SUBLINEAR_TF: + return std::log(static_cast(numOccurrences)) + 1; + default: + Log::Fatal << "Incorrect term frequency type!"; + return 0; + } + } + + /** + * The function calculates the inverse document frequency statistics. + * + * @tparam ValueType Type of the returned value. + * + * @param totalNumRows The total number of strings in the input dataset. + * @param numOccurrences The number of strings in the input dataset + * which contain the current token. + */ + template + ValueType InverseDocumentFrequency(const size_t totalNumRows, + const size_t numOccurrences) + { + if (smoothIdf) + { + return std::log(static_cast(totalNumRows + 1) / + (1 + numOccurrences)) + 1.0; + } + else + { + return std::log(static_cast(totalNumRows) / + numOccurrences) + 1.0; + } + } + + private: + //! Used to store the total number of tokens for each row. + std::vector> tokensFrequences; + /** + * Used to store the number of strings which contain a token depending + * on the given token. + */ + std::unordered_map numContainingStrings; + //! Used to store the number of tokens in each row. + std::vector rowsSizes; + //! Type of the term frequency scheme. TfTypes tfType; + //! Indicates whether the idf scheme is smooth or not. + bool smoothIdf; }; +/** + * A convenient alias for the StringEncoding class with TfIdfEncodingPolicy + * and the default dictionary for the given token type. + * + * @tparam TokenType Type of the tokens. + */ template using TfIdfEncoding = StringEncoding>; + StringEncodingDictionary>; } // namespace data } // namespace mlpack From d2abb44332ac8282b813fb521a726b011baa8cd2 Mon Sep 17 00:00:00 2001 From: Mikhail Lozhnikov Date: Mon, 16 Mar 2020 18:48:51 +0300 Subject: [PATCH 143/265] Fixed the automatic style checks. --- .../string_encoding_policies/bag_of_words_encoding_policy.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index 4ca76b4a08..b6f164db36 100644 --- a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -9,8 +9,8 @@ * 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_STRING_ENCODING_POLICIES_BAG_OF_WORDS_ENCODING_POLICY_HPP -#define MLPACK_CORE_DATA_STRING_ENCODING_POLICIES_BAG_OF_WORDS_ENCODING_POLICY_HPP +#ifndef MLPACK_CORE_DATA_STR_ENCODING_POLICIES_BAG_OF_WORDS_ENCODING_POLICY_HPP +#define MLPACK_CORE_DATA_STR_ENCODING_POLICIES_BAG_OF_WORDS_ENCODING_POLICY_HPP #include #include From a0cc5abd9f16ccb6c90f1070b10971662466845c Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Mon, 16 Mar 2020 17:18:37 +0100 Subject: [PATCH 144/265] Azure-pipelines: walkaround - for more information see https://status.dev.azure.com/_event/179641421. --- .ci/ci.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.ci/ci.yaml b/.ci/ci.yaml index be1ca4b4c1..e886c4f6dc 100644 --- a/.ci/ci.yaml +++ b/.ci/ci.yaml @@ -1,3 +1,12 @@ +trigger: + branches: + include: + - '*' +pr: + branches: + include: + - '*' + jobs: - job: Linux timeoutInMinutes: 360 From 3199df34fef4eb10ba26487561d7d678b04e6fe9 Mon Sep 17 00:00:00 2001 From: Gaurav Singh Date: Tue, 17 Mar 2020 10:21:31 +0530 Subject: [PATCH 145/265] Adding a warning in ffn_impl.hpp, rnn_impl.hpp and brnn_impl.hpp regarding the MaxIterations() parameter for the issue #2226. (#2238) * add-celu * add-celu * celu activation function added. * Add CELU activation function * minor style changes * Minor style changes. * Removing deterministic parameter and correcting typos * Solving syntax issue. * HISTORY.md and documentation correction. * Reintroducing the deterministic parameter and minor style changes in other files. * Minor changes. * Minor changes in elu.hpp * Changes in layer_types.hpp. * Style changes in celu.hpp and celu_impl.hpp. * Minor changes in celu_impl.hpp. * Changes in activation_functions_test.cpp. * Initial commit with changes in ffn_impl.hpp. * Correcting the relation and adding the same test to rnn_impl.hpp and brnn_impl.hpp. * Changes in ffn_impl.hpp. * Changes only in ffn to check if everything works correctly. * Minor changes in ffn_impl.hpp. * Relevant changes in RNN and BRNN. * Changing std::endl to n. * Changes in rnn_impl.hpp. * Minor Changes. * Minor changes. * Minor changes in the warning message. * Changes in HISTORY.md * Minor changes in HISTORY.md to change the PR address. * Update src/mlpack/methods/ann/brnn.hpp Co-Authored-By: Marcus Edel * Update src/mlpack/methods/ann/brnn.hpp Co-Authored-By: Marcus Edel * Update src/mlpack/methods/ann/ffn.hpp Co-Authored-By: Marcus Edel * Update src/mlpack/methods/ann/rnn.hpp Co-Authored-By: Marcus Edel * Update src/mlpack/methods/ann/ffn.hpp Co-Authored-By: Marcus Edel * Update src/mlpack/methods/ann/rnn.hpp Co-Authored-By: Marcus Edel * Style Changes. * Resolving errors. * Minor style changes. * Update src/mlpack/methods/ann/brnn_impl.hpp Co-Authored-By: Ryan Curtin * Update src/mlpack/methods/ann/ffn_impl.hpp Co-Authored-By: Ryan Curtin * Update src/mlpack/methods/ann/rnn_impl.hpp Co-Authored-By: Ryan Curtin * Final Changes. Co-authored-by: Marcus Edel Co-authored-by: Ryan Curtin --- COPYRIGHT.txt | 1 + src/mlpack/methods/ann/brnn.hpp | 30 +++++++++++++ src/mlpack/methods/ann/brnn_impl.hpp | 42 +++++++++++++++++++ src/mlpack/methods/ann/ffn.hpp | 30 +++++++++++++ src/mlpack/methods/ann/ffn_impl.hpp | 38 +++++++++++++++++ src/mlpack/methods/ann/layer/layer_traits.hpp | 4 ++ src/mlpack/methods/ann/rnn.hpp | 30 +++++++++++++ src/mlpack/methods/ann/rnn_impl.hpp | 38 +++++++++++++++++ .../tests/activation_functions_test.cpp | 2 +- src/mlpack/tests/feedforward_network_test.cpp | 29 +++++++++++++ 10 files changed, 243 insertions(+), 1 deletion(-) diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt index 60fba03e0a..11c82cda4b 100644 --- a/COPYRIGHT.txt +++ b/COPYRIGHT.txt @@ -128,6 +128,7 @@ Copyright: Copyright 2020, Sriram S K Copyright 2020, Manoranjan Kumar Bharti ( Nakul Bharti ) Copyright 2020, Saraansh Tandon + Copyright 2020, Gaurav Singh License: BSD-3-clause All rights reserved. diff --git a/src/mlpack/methods/ann/brnn.hpp b/src/mlpack/methods/ann/brnn.hpp index cddc9c8544..57789336a9 100644 --- a/src/mlpack/methods/ann/brnn.hpp +++ b/src/mlpack/methods/ann/brnn.hpp @@ -24,6 +24,7 @@ #include "init_rules/network_init.hpp" #include #include +#include #include #include @@ -76,6 +77,35 @@ class BRNN MergeOutputType mergeOutput = MergeOutputType(), InitializationRuleType initializeRule = InitializationRuleType()); + /** + * Check if the optimizer has MaxIterations() parameter, if it does + * then check if it's value is less than the number of datapoints + * in the dataset. + * + * @tparam OptimizerType Type of optimizer to use to train the model. + * @param optimizer optimizer used in the training process. + * @param samples Number of datapoints in the dataset. + */ + template + typename std::enable_if< + HasMaxIterations + ::value, void>::type + WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const; + + /** + * Check if the optimizer has MaxIterations() parameter, if it + * doesn't then simply return from the function. + * + * @tparam OptimizerType Type of optimizer to use to train the model. + * @param optimizer optimizer used in the training process. + * @param samples Number of datapoints in the dataset. + */ + template + typename std::enable_if< + !HasMaxIterations + ::value, void>::type + WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const; + /** * Train the bidirectional recurrent neural network on the given input data * using the given optimizer. diff --git a/src/mlpack/methods/ann/brnn_impl.hpp b/src/mlpack/methods/ann/brnn_impl.hpp index be97702932..697bad7dfa 100644 --- a/src/mlpack/methods/ann/brnn_impl.hpp +++ b/src/mlpack/methods/ann/brnn_impl.hpp @@ -60,6 +60,44 @@ BRNN +template +typename std::enable_if< + HasMaxIterations + ::value, void>::type +BRNN::WarnMessageMaxIterations +(OptimizerType& optimizer, size_t samples) const +{ + if (optimizer.MaxIterations() < samples && + optimizer.MaxIterations() != 0) + { + Log::Warn << "The optimizer's maximum number of iterations " + << "is less than the size of the dataset; the " + << "optimizer will not pass over the entire " + << "dataset. To fix this, modify the maximum " + << "number of iterations to be at least equal " + << "to the number of points of your dataset " + << "(" << samples << ")." << std::endl; + } +} + +template +template +typename std::enable_if< + !HasMaxIterations + ::value, void>::type +BRNN::WarnMessageMaxIterations +(OptimizerType& optimizer, size_t samples) const +{ + return; +} + template @@ -83,6 +121,8 @@ double BRNN(optimizer, this->predictors.n_cols); + // Train the model. Timer::Start("BRNN_optimization"); const double out = optimizer.Optimize(*this, parameter); @@ -117,6 +157,8 @@ double BRNN(optimizer, this->predictors.n_cols); + // Train the model. const double out = optimizer.Optimize(*this, parameter); diff --git a/src/mlpack/methods/ann/ffn.hpp b/src/mlpack/methods/ann/ffn.hpp index ada76165cf..70875f2149 100644 --- a/src/mlpack/methods/ann/ffn.hpp +++ b/src/mlpack/methods/ann/ffn.hpp @@ -30,6 +30,7 @@ #include #include #include +#include #include namespace mlpack { @@ -82,6 +83,35 @@ class FFN //! Destructor to release allocated memory. ~FFN(); + /** + * Check if the optimizer has MaxIterations() parameter, if it does + * then check if it's value is less than the number of datapoints + * in the dataset. + * + * @tparam OptimizerType Type of optimizer to use to train the model. + * @param optimizer optimizer used in the training process. + * @param samples Number of datapoints in the dataset. + */ + template + typename std::enable_if< + HasMaxIterations + ::value, void>::type + WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const; + + /** + * Check if the optimizer has MaxIterations() parameter, if it + * doesn't then simply return from the function. + * + * @tparam OptimizerType Type of optimizer to use to train the model. + * @param optimizer optimizer used in the training process. + * @param samples Number of datapoints in the dataset. + */ + template + typename std::enable_if< + !HasMaxIterations + ::value, void>::type + WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const; + /** * Train the feedforward network on the given input data using the given * optimizer. diff --git a/src/mlpack/methods/ann/ffn_impl.hpp b/src/mlpack/methods/ann/ffn_impl.hpp index df0a07f75d..68f3ddd653 100644 --- a/src/mlpack/methods/ann/ffn_impl.hpp +++ b/src/mlpack/methods/ann/ffn_impl.hpp @@ -67,6 +67,40 @@ void FFN::ResetData( ResetParameters(); } +template +template +typename std::enable_if< + HasMaxIterations + ::value, void>::type +FFN:: +WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const +{ + if (optimizer.MaxIterations() < samples && + optimizer.MaxIterations() != 0) + { + Log::Warn << "The optimizer's maximum number of iterations " + << "is less than the size of the dataset; the " + << "optimizer will not pass over the entire " + << "dataset. To fix this, modify the maximum " + << "number of iterations to be at least equal " + << "to the number of points of your dataset " + << "(" << samples << ")." << std::endl; + } +} + +template +template +typename std::enable_if< + !HasMaxIterations + ::value, void>::type +FFN:: +WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const +{ + return; +} + template template @@ -78,6 +112,8 @@ double FFN::Train( { ResetData(std::move(predictors), std::move(responses)); + WarnMessageMaxIterations(optimizer, this->predictors.n_cols); + // Train the model. Timer::Start("ffn_optimization"); const double out = optimizer.Optimize(*this, parameter, callbacks...); @@ -100,6 +136,8 @@ double FFN::Train( OptimizerType optimizer; + WarnMessageMaxIterations(optimizer, this->predictors.n_cols); + // Train the model. Timer::Start("ffn_optimization"); const double out = optimizer.Optimize(*this, parameter, callbacks...); diff --git a/src/mlpack/methods/ann/layer/layer_traits.hpp b/src/mlpack/methods/ann/layer/layer_traits.hpp index 1955235f87..665adb9b2b 100644 --- a/src/mlpack/methods/ann/layer/layer_traits.hpp +++ b/src/mlpack/methods/ann/layer/layer_traits.hpp @@ -116,6 +116,10 @@ HAS_MEM_FUNC(Run, HasRunCheck); // can use with SFINAE to catch when a type has a Bias() function. HAS_MEM_FUNC(Bias, HasBiasCheck); +// This gives us a HasMaxIterationsC type (where U is a function pointer) +// we can use with SFINAE to catch when a type has a MaxIterations() function. +HAS_MEM_FUNC(MaxIterations, HasMaxIterations); + } // namespace ann } // namespace mlpack diff --git a/src/mlpack/methods/ann/rnn.hpp b/src/mlpack/methods/ann/rnn.hpp index 5c6bdf2b74..11cb8a8001 100644 --- a/src/mlpack/methods/ann/rnn.hpp +++ b/src/mlpack/methods/ann/rnn.hpp @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -72,6 +73,35 @@ class RNN //! Destructor to release allocated memory. ~RNN(); + /** + * Check if the optimizer has MaxIterations() parameter, if it does + * then check if it's value is less than the number of datapoints + * in the dataset. + * + * @tparam OptimizerType Type of optimizer to use to train the model. + * @param optimizer optimizer used in the training process. + * @param samples Number of datapoints in the dataset. + */ + template + typename std::enable_if< + HasMaxIterations + ::value, void>::type + WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const; + + /** + * Check if the optimizer has MaxIterations() parameter, if it + * doesn't then simply return from the function. + * + * @tparam OptimizerType Type of optimizer to use to train the model. + * @param optimizer optimizer used in the training process. + * @param samples Number of datapoints in the dataset. + */ + template + typename std::enable_if< + !HasMaxIterations + ::value, void>::type + WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const; + /** * Train the recurrent neural network on the given input data using the given * optimizer. diff --git a/src/mlpack/methods/ann/rnn_impl.hpp b/src/mlpack/methods/ann/rnn_impl.hpp index ba2d903a3f..688f76d654 100644 --- a/src/mlpack/methods/ann/rnn_impl.hpp +++ b/src/mlpack/methods/ann/rnn_impl.hpp @@ -61,6 +61,40 @@ RNN::~RNN() } } +template +template +typename std::enable_if< + HasMaxIterations + ::value, void>::type +RNN:: +WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const +{ + if (optimizer.MaxIterations() < samples && + optimizer.MaxIterations() != 0) + { + Log::Warn << "The optimizer's maximum number of iterations " + << "is less than the size of the dataset; the " + << "optimizer will not pass over the entire " + << "dataset. To fix this, modify the maximum " + << "number of iterations to be at least equal " + << "to the number of points of your dataset " + << "(" << samples << ")." << std::endl; + } +} + +template +template +typename std::enable_if< + !HasMaxIterations + ::value, void>::type +RNN:: +WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const +{ + return; +} + template template @@ -83,6 +117,8 @@ double RNN::Train( ResetParameters(); } + WarnMessageMaxIterations(optimizer, this->predictors.n_cols); + // Train the model. Timer::Start("rnn_optimization"); const double out = optimizer.Optimize(*this, parameter, callbacks...); @@ -127,6 +163,8 @@ double RNN::Train( OptimizerType optimizer; + WarnMessageMaxIterations(optimizer, this->predictors.n_cols); + // Train the model. Timer::Start("rnn_optimization"); const double out = optimizer.Optimize(*this, parameter, callbacks...); diff --git a/src/mlpack/tests/activation_functions_test.cpp b/src/mlpack/tests/activation_functions_test.cpp index 758974e301..9913088ec5 100644 --- a/src/mlpack/tests/activation_functions_test.cpp +++ b/src/mlpack/tests/activation_functions_test.cpp @@ -310,7 +310,7 @@ void CheckPReLUDerivativeCorrect(const arma::colvec input, /* * Implementation of the PReLU activation function gradient test. * The function is implemented as PReLU layer in the file - * perametric_relu.hpp + * parametric_relu.hpp * * @param input Input data used for evaluating the PReLU activation * function. diff --git a/src/mlpack/tests/feedforward_network_test.cpp b/src/mlpack/tests/feedforward_network_test.cpp index 0e3f3314e7..4773325ee0 100644 --- a/src/mlpack/tests/feedforward_network_test.cpp +++ b/src/mlpack/tests/feedforward_network_test.cpp @@ -592,4 +592,33 @@ BOOST_AUTO_TEST_CASE(FFNReturnModel) CheckMatrices(linearB->Parameters(), arma::zeros(3 * 4 + 4, 1)); } +/** + * Test to see if the FFN code compiles when the Optimizer + * doesn't have the MaxIterations() method. + */ +BOOST_AUTO_TEST_CASE(OptimizerTest) +{ + // Load the dataset. + arma::mat trainData; + data::Load("thyroid_train.csv", trainData, true); + + arma::mat trainLabels = trainData.row(trainData.n_rows - 1); + trainData.shed_row(trainData.n_rows - 1); + + arma::mat testData; + data::Load("thyroid_test.csv", testData, true); + + arma::mat testLabels = testData.row(testData.n_rows - 1); + testData.shed_row(testData.n_rows - 1); + + FFN, RandomInitialization, CustomLayer<> > model; + model.Add >(trainData.n_rows, 8); + model.Add >(); + model.Add >(8, 3); + model.Add >(); + + ens::DE opt(200, 1000, 0.6, 0.8, 1e-5); + model.Train(trainData, trainLabels, opt); +} + BOOST_AUTO_TEST_SUITE_END(); From 18ef15c70500cf90377664904949cb8b5526c747 Mon Sep 17 00:00:00 2001 From: Yashwant Singh Parihar Date: Fri, 14 Jun 2019 21:29:19 +0530 Subject: [PATCH 146/265] Fix Azure Failure --- .ci/ci.yaml | 2 +- .ci/macos-steps.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.ci/ci.yaml b/.ci/ci.yaml index e886c4f6dc..739ba9906c 100644 --- a/.ci/ci.yaml +++ b/.ci/ci.yaml @@ -37,7 +37,7 @@ jobs: - job: macOS timeoutInMinutes: 360 pool: - vmImage: macOS-10.13 + vmImage: macOS-10.14 strategy: matrix: Plain: diff --git a/.ci/macos-steps.yaml b/.ci/macos-steps.yaml index bdc11fe3f7..99c11c5df1 100644 --- a/.ci/macos-steps.yaml +++ b/.ci/macos-steps.yaml @@ -15,8 +15,8 @@ steps: sudo xcode-select --switch /Applications/Xcode_10.1.app/Contents/Developer unset BOOST_ROOT pip install cython numpy pandas zipp - brew update brew install openblas armadillo boost + brew update if [ "a$(julia.version)" != "a" ]; then brew cask install julia From 47ef392cc6b60a46472ebf04caedeb685faa6890 Mon Sep 17 00:00:00 2001 From: Yashwant Date: Tue, 17 Mar 2020 11:06:08 +0530 Subject: [PATCH 147/265] Remove brew update as suggested. --- .ci/macos-steps.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.ci/macos-steps.yaml b/.ci/macos-steps.yaml index 99c11c5df1..7439d0b2b2 100644 --- a/.ci/macos-steps.yaml +++ b/.ci/macos-steps.yaml @@ -16,7 +16,6 @@ steps: unset BOOST_ROOT pip install cython numpy pandas zipp brew install openblas armadillo boost - brew update if [ "a$(julia.version)" != "a" ]; then brew cask install julia From 7233ca19d325b9c0e75e39a5694656aebb6e5ba0 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Tue, 17 Mar 2020 16:19:01 +0200 Subject: [PATCH 148/265] Removed rvalues references --- .vscode/settings.json | 68 ++++++++++++++ .../loss_functions/margin_ranking_loss.hpp | 14 +-- .../margin_ranking_loss_impl.hpp | 14 +-- src/mlpack/tests/loss_functions_test.cpp | 88 +++++++++---------- 4 files changed, 125 insertions(+), 59 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..45fd170b7b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,68 @@ +{ + "files.associations": { + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "atomic": "cpp", + "strstream": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "chrono": "cpp", + "codecvt": "cpp", + "complex": "cpp", + "condition_variable": "cpp", + "cstdint": "cpp", + "deque": "cpp", + "list": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "map": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "regex": "cpp", + "set": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "cfenv": "cpp", + "cinttypes": "cpp", + "typeindex": "cpp", + "typeinfo": "cpp", + "valarray": "cpp", + "variant": "cpp" + } +} \ No newline at end of file diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index cb0f7ca1d3..cd9e71ba9f 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -53,9 +53,9 @@ class MarginRankingLoss typename SecondInputType, typename ThirdInputType > - double Forward(const FirstInputType&& x1, - const SecondInputType&& x2, - const ThirdInputType&& y); + double Forward(const FirstInputType& x1, + const SecondInputType& x2, + const ThirdInputType& y); /** * Ordinary feed backward pass of a neural network. @@ -71,10 +71,10 @@ class MarginRankingLoss typename ThirdInputType, typename OutputType > - void Backward(const FirstInputType&& x1, - const SecondInputType&& x2, - const ThirdInputType&& y, - OutputType&& output); + void Backward(const FirstInputType& x1, + const SecondInputType& x2, + const ThirdInputType& y, + OutputType& output); //! Get the output parameter. OutputDataType& OutputParameter() const { return outputParameter; } diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp index 4950eb6b94..f99af349f6 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp @@ -32,9 +32,9 @@ template < typename ThirdInputType > double MarginRankingLoss::Forward( - const FirstInputType&& x1, - const SecondInputType&& x2, - const ThirdInputType&& y) + const FirstInputType& x1, + const SecondInputType& x2, + const ThirdInputType& y) { return arma::accu(arma::max(arma::zeros(size(y)), -y % (x1 - x2) + margin)) / y.n_cols; @@ -48,10 +48,10 @@ template < typename OutputType > void MarginRankingLoss::Backward( - const FirstInputType&& x1, - const SecondInputType&& x2, - const ThirdInputType&& y, - OutputType&& output) + const FirstInputType& x1, + const SecondInputType& x2, + const ThirdInputType& y, + OutputType& output) { output = -y % (x1 - x2) + margin; output.elem(arma::find(output >= 0)).ones(); diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index 1640206357..bbeca9addf 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE(SimpleKLDivergenceTest) // Test the Forward function. Loss should be 0 if input = target. input = arma::ones(10, 1); target = arma::ones(10, 1); - loss = module.Forward(std::move(input), std::move(target)); + loss = module.Forward(input), target)); BOOST_REQUIRE_SMALL(loss, 0.00001); } @@ -66,11 +66,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanSquaredLogarithmicErrorTest) // the manually calculated result. input = arma::zeros(1, 8); target = arma::zeros(1, 8); - double error = module.Forward(std::move(input), std::move(target)); + double error = module.Forward(input), target)); BOOST_REQUIRE_SMALL(error, 0.00001); // Test the Backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input), target), output)); // The output should be equal to 0. CheckMatrices(input, output); BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); @@ -79,11 +79,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanSquaredLogarithmicErrorTest) // Test the error function on a single input. input = arma::mat("2"); target = arma::mat("3"); - error = module.Forward(std::move(input), std::move(target)); + error = module.Forward(input), target)); BOOST_REQUIRE_CLOSE(error, 0.082760974810151655, 0.001); // Test the Backward function on a single input. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input), target), output)); BOOST_REQUIRE_CLOSE(arma::accu(output), -0.1917880483011872, 0.001); BOOST_REQUIRE_EQUAL(output.n_elem, 1); } @@ -101,11 +101,11 @@ BOOST_AUTO_TEST_CASE(KLDivergenceMeanTest) input = arma::mat("1 1 1 1 1 1 1 1 1 1"); target = arma::exp(arma::mat("2 1 1 1 1 1 1 1 1 1")); - loss = module.Forward(std::move(input), std::move(target)); + loss = module.Forward(input), target)); BOOST_REQUIRE_CLOSE_FRACTION(loss, -1.1 , 0.00001); // Test the Backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input), target), output)); BOOST_REQUIRE_CLOSE_FRACTION(arma::as_scalar(output), -0.1, 0.00001); } @@ -122,11 +122,11 @@ BOOST_AUTO_TEST_CASE(KLDivergenceNoMeanTest) input = arma::mat("1 1 1 1 1 1 1 1 1 1"); target = arma::exp(arma::mat("2 1 1 1 1 1 1 1 1 1")); - loss = module.Forward(std::move(input), std::move(target)); + loss = module.Forward(input), target)); BOOST_REQUIRE_CLOSE_FRACTION(loss, -11, 0.00001); // Test the Backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input), target), output)); BOOST_REQUIRE_CLOSE_FRACTION(arma::as_scalar(output), -1, 0.00001); } @@ -142,11 +142,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanSquaredErrorTest) // the manually calculated result. input = arma::mat("1.0 0.0 1.0 0.0 -1.0 0.0 -1.0 0.0"); target = arma::zeros(1, 8); - double error = module.Forward(std::move(input), std::move(target)); + double error = module.Forward(input), target)); BOOST_REQUIRE_EQUAL(error, 0.5); // Test the Backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input), target), output)); // We subtract a zero vector, so according to the used backward formula: // output = 2 * (input - target) / target.n_cols, // output * nofColumns / 2 should be equal to input. @@ -157,11 +157,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanSquaredErrorTest) // Test the error function on a single input. input = arma::mat("2"); target = arma::mat("3"); - error = module.Forward(std::move(input), std::move(target)); + error = module.Forward(input), target)); BOOST_REQUIRE_EQUAL(error, 1.0); // Test the Backward function on a single input. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input), target), output)); // Test whether the output is negative. BOOST_REQUIRE_EQUAL(arma::accu(output), -2); BOOST_REQUIRE_EQUAL(output.n_elem, 1); @@ -179,16 +179,16 @@ BOOST_AUTO_TEST_CASE(SimpleCrossEntropyErrorTest) // the manually calculated result. input1 = arma::mat("0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5"); target1 = arma::zeros(1, 8); - double error1 = module.Forward(std::move(input1), std::move(target1)); + double error1 = module.Forward(input1), target1)); BOOST_REQUIRE_SMALL(error1 - 8 * std::log(2), 2e-5); input2 = arma::mat("0 1 1 0 1 0 0 1"); target2 = arma::mat("0 1 1 0 1 0 0 1"); - double error2 = module.Forward(std::move(input2), std::move(target2)); + double error2 = module.Forward(input2), target2)); BOOST_REQUIRE_SMALL(error2, 1e-5); // Test the Backward function. - module.Backward(std::move(input1), std::move(target1), std::move(output)); + module.Backward(input1), target1), output)); for (double el : output) { // For the 0.5 constant vector we should get 1 / (1 - 0.5) = 2 everywhere. @@ -197,7 +197,7 @@ BOOST_AUTO_TEST_CASE(SimpleCrossEntropyErrorTest) BOOST_REQUIRE_EQUAL(output.n_rows, input1.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input1.n_cols); - module.Backward(std::move(input2), std::move(target2), std::move(output)); + module.Backward(input2), target2), output)); for (size_t i = 0; i < 8; ++i) { double el = output.at(0, i); @@ -223,25 +223,25 @@ BOOST_AUTO_TEST_CASE(SimpleSigmoidCrossEntropyErrorTest) // the calculated result. input1 = arma::mat("0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5"); target1 = arma::zeros(1, 8); - double error1 = module.Forward(std::move(input1), std::move(target1)); + double error1 = module.Forward(input1), target1)); double expected = 0.97407699; // Value computed using tensorflow. BOOST_REQUIRE_SMALL(error1 / input1.n_elem - expected, 1e-7); input2 = arma::mat("1 2 3 4 5"); target2 = arma::mat("0 0 1 0 1"); - double error2 = module.Forward(std::move(input2), std::move(target2)); + double error2 = module.Forward(input2), target2)); expected = 1.5027283; BOOST_REQUIRE_SMALL(error2 / input2.n_elem - expected, 1e-6); input3 = arma::mat("0 -1 -1 0 -1 0 0 -1"); target3 = arma::mat("0 -1 -1 0 -1 0 0 -1"); - double error3 = module.Forward(std::move(input3), std::move(target3)); + double error3 = module.Forward(input3), target3)); expected = 0.00320443; BOOST_REQUIRE_SMALL(error3 / input3.n_elem - expected, 1e-6); // Test the Backward function. - module.Backward(std::move(input1), std::move(target1), std::move(output)); + module.Backward(input1), target1), output)); expected = 0.62245929; for (size_t i = 0; i < output.n_elem; i++) BOOST_REQUIRE_SMALL(output(i) - expected, 1e-5); @@ -250,13 +250,13 @@ BOOST_AUTO_TEST_CASE(SimpleSigmoidCrossEntropyErrorTest) expectedOutput = arma::mat( "0.7310586 0.88079709 -0.04742587 0.98201376 -0.00669285"); - module.Backward(std::move(input2), std::move(target2), std::move(output)); + module.Backward(input2), target2), output)); for (size_t i = 0; i < output.n_elem; i++) BOOST_REQUIRE_SMALL(output(i) - expectedOutput(i), 1e-5); BOOST_REQUIRE_EQUAL(output.n_rows, input2.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input2.n_cols); - module.Backward(std::move(input3), std::move(target3), std::move(output)); + module.Backward(input3), target3), output)); expectedOutput = arma::mat("0.5 1.2689414"); for (size_t i = 0; i < 8; ++i) { @@ -282,18 +282,18 @@ BOOST_AUTO_TEST_CASE(SimpleEarthMoverDistanceLayerTest) // the manually calculated result. input1 = arma::mat("0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5"); target1 = arma::zeros(1, 8); - double error1 = module.Forward(std::move(input1), std::move(target1)); + double error1 = module.Forward(input1), target1)); double expected = 0.0; BOOST_REQUIRE_SMALL(error1 / input1.n_elem - expected, 1e-7); input2 = arma::mat("1 2 3 4 5"); target2 = arma::mat("1 0 1 0 1"); - double error2 = module.Forward(std::move(input2), std::move(target2)); + double error2 = module.Forward(input2), target2)); expected = -1.8; BOOST_REQUIRE_SMALL(error2 / input2.n_elem - expected, 1e-6); // Test the Backward function. - module.Backward(std::move(input1), std::move(target1), std::move(output)); + module.Backward(input1), target1), output)); expected = 0.0; for (size_t i = 0; i < output.n_elem; i++) BOOST_REQUIRE_SMALL(output(i) - expected, 1e-5); @@ -301,7 +301,7 @@ BOOST_AUTO_TEST_CASE(SimpleEarthMoverDistanceLayerTest) BOOST_REQUIRE_EQUAL(output.n_cols, input1.n_cols); expectedOutput = arma::mat("-1 0 -1 0 -1"); - module.Backward(std::move(input2), std::move(target2), std::move(output)); + module.Backward(input2), target2), output)); for (size_t i = 0; i < output.n_elem; i++) BOOST_REQUIRE_SMALL(output(i) - expectedOutput(i), 1e-5); BOOST_REQUIRE_EQUAL(output.n_rows, input2.n_rows); @@ -406,16 +406,16 @@ BOOST_AUTO_TEST_CASE(DiceLossTest) // Test the Forward function. Loss should be 0 if input = target. input1 = arma::ones(10, 1); target = arma::ones(10, 1); - loss = module.Forward(std::move(input1), std::move(target)); + loss = module.Forward(input1), target)); BOOST_REQUIRE_SMALL(loss, 0.00001); // Test the Forward function. Loss should be 0.185185185. input2 = arma::ones(10, 1) * 0.5; - loss = module.Forward(std::move(input2), std::move(target)); + loss = module.Forward(input2), target)); BOOST_REQUIRE_CLOSE(loss, 0.185185185, 0.00001); // Test the Backward function for input = target. - module.Backward(std::move(input1), std::move(target), std::move(output)); + module.Backward(input1), target), output)); for (double el : output) { // For input = target we should get 0.0 everywhere. @@ -425,7 +425,7 @@ BOOST_AUTO_TEST_CASE(DiceLossTest) BOOST_REQUIRE_EQUAL(output.n_cols, input1.n_cols); // Test the Backward function. - module.Backward(std::move(input2), std::move(target), std::move(output)); + module.Backward(input2), target), output)); for (double el : output) { // For the 0.5 constant vector we should get -0.0877914951989026 everywhere. @@ -447,11 +447,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanBiasErrorTest) // the manually calculated result. input = arma::mat("1.0 0.0 1.0 -1.0 -1.0 0.0 -1.0 0.0"); target = arma::zeros(1, 8); - double error = module.Forward(std::move(input), std::move(target)); + double error = module.Forward(input), target)); BOOST_REQUIRE_EQUAL(error, 0.125); // Test the Backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input), target), output)); // We should get a vector with -1 everywhere. for (double el : output) { @@ -463,11 +463,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanBiasErrorTest) // Test the error function on a single input. input = arma::mat("2"); target = arma::mat("3"); - error = module.Forward(std::move(input), std::move(target)); + error = module.Forward(input), target)); BOOST_REQUIRE_EQUAL(error, 1.0); // Test the Backward function on a single input. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input), target), output)); // Test whether the output is negative. BOOST_REQUIRE_EQUAL(arma::accu(output), -1); BOOST_REQUIRE_EQUAL(output.n_elem, 1); @@ -485,11 +485,11 @@ BOOST_AUTO_TEST_CASE(LogCoshLossTest) // Test the Forward function. Loss should be 0 if input = target. input = arma::ones(10, 1); target = arma::ones(10, 1); - loss = module.Forward(std::move(input), std::move(target)); + loss = module.Forward(input), target)); BOOST_REQUIRE_EQUAL(loss, 0); // Test the Backward function for input = target. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input), target), output)); for (double el : output) { // For input = target we should get 0.0 everywhere. @@ -502,11 +502,11 @@ BOOST_AUTO_TEST_CASE(LogCoshLossTest) // Test the Forward function. Loss should be 0.546621. input = arma::mat("1 2 3 4 5"); target = arma::mat("1 2.4 3.4 4.2 5.5"); - loss = module.Forward(std::move(input), std::move(target)); + loss = module.Forward(input), target)); BOOST_REQUIRE_CLOSE(loss, 0.546621, 1e-3); // Test the Backward function. - module.Backward(std::move(input), std::move(target), std::move(output)); + module.Backward(input), target), output)); BOOST_REQUIRE_CLOSE(arma::accu(output), 2.46962, 1e-3); BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); @@ -525,13 +525,12 @@ BOOST_AUTO_TEST_CASE(MarginRankingLossTest) x1 = arma::mat("1 2 5 7 -1 -3"); x2 = arma::mat("-1 3 -4 11 3 -3"); y = arma::mat("1 -1 -1 1 -1 1"); - double error = module.Forward(std::move(x1), std::move(x2), std::move(y)); + double error = module.Forward(x1, x2, y); // Computed using torch.nn.functional.margin_ranking_loss() BOOST_REQUIRE_CLOSE(error, 2.66667, 1e-3); // Test the Backward function. - module.Backward(std::move(x1), std::move(x2), std::move(y), - std::move(output)); + module.Backward(x1, x2, y, output); CheckMatrices(output, arma::mat("-0.000000 0.166667 -1.500000 0.666667 " "0.000000 -0.000000"), 1e-3); @@ -544,12 +543,11 @@ BOOST_AUTO_TEST_CASE(MarginRankingLossTest) x2 = arma::mat("-4.5288 -9.2766 -0.5882 -5.6643 -6.0175 8.8506 3.4759 " "-9.4886 2.2755 8.4951"); y = arma::mat("1 1 -1 1 -1 1 1 1 -1 1"); - error = module.Forward(std::move(x1), std::move(x2), std::move(y)); + error = module.Forward(x1, x2, y); BOOST_REQUIRE_CLOSE(error, 3.03530, 1e-3); // Test the Backward function on the second input. - module.Backward(std::move(x1), std::move(x2), std::move(y), - std::move(output)); + module.Backward(x1, x2, y, output); CheckMatrices(output, arma::mat("0.000000 0.000000 0.091240 0.000000 " "-0.753830 1.336900 0.000000 0.000000 -0.207000 0.328810"), 1e-6); From 5559e4f4eaca59a2b452cd27fb0a40c2993f593f Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Tue, 17 Mar 2020 16:27:42 +0200 Subject: [PATCH 149/265] Removed rvalues references --- src/mlpack/tests/loss_functions_test.cpp | 78 ++++++++++++------------ 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index bbeca9addf..58ad1f3826 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE(SimpleKLDivergenceTest) // Test the Forward function. Loss should be 0 if input = target. input = arma::ones(10, 1); target = arma::ones(10, 1); - loss = module.Forward(input), target)); + loss = module.Forward(input, target); BOOST_REQUIRE_SMALL(loss, 0.00001); } @@ -66,11 +66,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanSquaredLogarithmicErrorTest) // the manually calculated result. input = arma::zeros(1, 8); target = arma::zeros(1, 8); - double error = module.Forward(input), target)); + double error = module.Forward(input, target); BOOST_REQUIRE_SMALL(error, 0.00001); // Test the Backward function. - module.Backward(input), target), output)); + module.Backward(input, target, output); // The output should be equal to 0. CheckMatrices(input, output); BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); @@ -79,11 +79,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanSquaredLogarithmicErrorTest) // Test the error function on a single input. input = arma::mat("2"); target = arma::mat("3"); - error = module.Forward(input), target)); + error = module.Forward(input, target); BOOST_REQUIRE_CLOSE(error, 0.082760974810151655, 0.001); // Test the Backward function on a single input. - module.Backward(input), target), output)); + module.Backward(input, target, output); BOOST_REQUIRE_CLOSE(arma::accu(output), -0.1917880483011872, 0.001); BOOST_REQUIRE_EQUAL(output.n_elem, 1); } @@ -101,11 +101,11 @@ BOOST_AUTO_TEST_CASE(KLDivergenceMeanTest) input = arma::mat("1 1 1 1 1 1 1 1 1 1"); target = arma::exp(arma::mat("2 1 1 1 1 1 1 1 1 1")); - loss = module.Forward(input), target)); + loss = module.Forward(input, target); BOOST_REQUIRE_CLOSE_FRACTION(loss, -1.1 , 0.00001); // Test the Backward function. - module.Backward(input), target), output)); + module.Backward(input, target, output); BOOST_REQUIRE_CLOSE_FRACTION(arma::as_scalar(output), -0.1, 0.00001); } @@ -122,11 +122,11 @@ BOOST_AUTO_TEST_CASE(KLDivergenceNoMeanTest) input = arma::mat("1 1 1 1 1 1 1 1 1 1"); target = arma::exp(arma::mat("2 1 1 1 1 1 1 1 1 1")); - loss = module.Forward(input), target)); + loss = module.Forward(input, target); BOOST_REQUIRE_CLOSE_FRACTION(loss, -11, 0.00001); // Test the Backward function. - module.Backward(input), target), output)); + module.Backward(input, target, output); BOOST_REQUIRE_CLOSE_FRACTION(arma::as_scalar(output), -1, 0.00001); } @@ -142,11 +142,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanSquaredErrorTest) // the manually calculated result. input = arma::mat("1.0 0.0 1.0 0.0 -1.0 0.0 -1.0 0.0"); target = arma::zeros(1, 8); - double error = module.Forward(input), target)); + double error = module.Forward(input, target); BOOST_REQUIRE_EQUAL(error, 0.5); // Test the Backward function. - module.Backward(input), target), output)); + module.Backward(input, target, output); // We subtract a zero vector, so according to the used backward formula: // output = 2 * (input - target) / target.n_cols, // output * nofColumns / 2 should be equal to input. @@ -157,11 +157,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanSquaredErrorTest) // Test the error function on a single input. input = arma::mat("2"); target = arma::mat("3"); - error = module.Forward(input), target)); + error = module.Forward(input, target); BOOST_REQUIRE_EQUAL(error, 1.0); // Test the Backward function on a single input. - module.Backward(input), target), output)); + module.Backward(input, target, output); // Test whether the output is negative. BOOST_REQUIRE_EQUAL(arma::accu(output), -2); BOOST_REQUIRE_EQUAL(output.n_elem, 1); @@ -179,16 +179,16 @@ BOOST_AUTO_TEST_CASE(SimpleCrossEntropyErrorTest) // the manually calculated result. input1 = arma::mat("0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5"); target1 = arma::zeros(1, 8); - double error1 = module.Forward(input1), target1)); + double error1 = module.Forward(input1, target1); BOOST_REQUIRE_SMALL(error1 - 8 * std::log(2), 2e-5); input2 = arma::mat("0 1 1 0 1 0 0 1"); target2 = arma::mat("0 1 1 0 1 0 0 1"); - double error2 = module.Forward(input2), target2)); + double error2 = module.Forward(input2, target2); BOOST_REQUIRE_SMALL(error2, 1e-5); // Test the Backward function. - module.Backward(input1), target1), output)); + module.Backward(input1, target1, output); for (double el : output) { // For the 0.5 constant vector we should get 1 / (1 - 0.5) = 2 everywhere. @@ -197,7 +197,7 @@ BOOST_AUTO_TEST_CASE(SimpleCrossEntropyErrorTest) BOOST_REQUIRE_EQUAL(output.n_rows, input1.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input1.n_cols); - module.Backward(input2), target2), output)); + module.Backward(input2, target2, output); for (size_t i = 0; i < 8; ++i) { double el = output.at(0, i); @@ -223,25 +223,25 @@ BOOST_AUTO_TEST_CASE(SimpleSigmoidCrossEntropyErrorTest) // the calculated result. input1 = arma::mat("0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5"); target1 = arma::zeros(1, 8); - double error1 = module.Forward(input1), target1)); + double error1 = module.Forward(input1, target1); double expected = 0.97407699; // Value computed using tensorflow. BOOST_REQUIRE_SMALL(error1 / input1.n_elem - expected, 1e-7); input2 = arma::mat("1 2 3 4 5"); target2 = arma::mat("0 0 1 0 1"); - double error2 = module.Forward(input2), target2)); + double error2 = module.Forward(input2, target2); expected = 1.5027283; BOOST_REQUIRE_SMALL(error2 / input2.n_elem - expected, 1e-6); input3 = arma::mat("0 -1 -1 0 -1 0 0 -1"); target3 = arma::mat("0 -1 -1 0 -1 0 0 -1"); - double error3 = module.Forward(input3), target3)); + double error3 = module.Forward(input3, target3); expected = 0.00320443; BOOST_REQUIRE_SMALL(error3 / input3.n_elem - expected, 1e-6); // Test the Backward function. - module.Backward(input1), target1), output)); + module.Backward(input1, target1, output); expected = 0.62245929; for (size_t i = 0; i < output.n_elem; i++) BOOST_REQUIRE_SMALL(output(i) - expected, 1e-5); @@ -250,13 +250,13 @@ BOOST_AUTO_TEST_CASE(SimpleSigmoidCrossEntropyErrorTest) expectedOutput = arma::mat( "0.7310586 0.88079709 -0.04742587 0.98201376 -0.00669285"); - module.Backward(input2), target2), output)); + module.Backward(input2, target2, output); for (size_t i = 0; i < output.n_elem; i++) BOOST_REQUIRE_SMALL(output(i) - expectedOutput(i), 1e-5); BOOST_REQUIRE_EQUAL(output.n_rows, input2.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input2.n_cols); - module.Backward(input3), target3), output)); + module.Backward(input3, target3, output); expectedOutput = arma::mat("0.5 1.2689414"); for (size_t i = 0; i < 8; ++i) { @@ -282,18 +282,18 @@ BOOST_AUTO_TEST_CASE(SimpleEarthMoverDistanceLayerTest) // the manually calculated result. input1 = arma::mat("0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5"); target1 = arma::zeros(1, 8); - double error1 = module.Forward(input1), target1)); + double error1 = module.Forward(input1, target1); double expected = 0.0; BOOST_REQUIRE_SMALL(error1 / input1.n_elem - expected, 1e-7); input2 = arma::mat("1 2 3 4 5"); target2 = arma::mat("1 0 1 0 1"); - double error2 = module.Forward(input2), target2)); + double error2 = module.Forward(input2, target2); expected = -1.8; BOOST_REQUIRE_SMALL(error2 / input2.n_elem - expected, 1e-6); // Test the Backward function. - module.Backward(input1), target1), output)); + module.Backward(input1, target1, output); expected = 0.0; for (size_t i = 0; i < output.n_elem; i++) BOOST_REQUIRE_SMALL(output(i) - expected, 1e-5); @@ -301,7 +301,7 @@ BOOST_AUTO_TEST_CASE(SimpleEarthMoverDistanceLayerTest) BOOST_REQUIRE_EQUAL(output.n_cols, input1.n_cols); expectedOutput = arma::mat("-1 0 -1 0 -1"); - module.Backward(input2), target2), output)); + module.Backward(input2, target2, output); for (size_t i = 0; i < output.n_elem; i++) BOOST_REQUIRE_SMALL(output(i) - expectedOutput(i), 1e-5); BOOST_REQUIRE_EQUAL(output.n_rows, input2.n_rows); @@ -406,16 +406,16 @@ BOOST_AUTO_TEST_CASE(DiceLossTest) // Test the Forward function. Loss should be 0 if input = target. input1 = arma::ones(10, 1); target = arma::ones(10, 1); - loss = module.Forward(input1), target)); + loss = module.Forward(input1, target); BOOST_REQUIRE_SMALL(loss, 0.00001); // Test the Forward function. Loss should be 0.185185185. input2 = arma::ones(10, 1) * 0.5; - loss = module.Forward(input2), target)); + loss = module.Forward(input2, target); BOOST_REQUIRE_CLOSE(loss, 0.185185185, 0.00001); // Test the Backward function for input = target. - module.Backward(input1), target), output)); + module.Backward(input1, target, output); for (double el : output) { // For input = target we should get 0.0 everywhere. @@ -425,7 +425,7 @@ BOOST_AUTO_TEST_CASE(DiceLossTest) BOOST_REQUIRE_EQUAL(output.n_cols, input1.n_cols); // Test the Backward function. - module.Backward(input2), target), output)); + module.Backward(input2, target, output); for (double el : output) { // For the 0.5 constant vector we should get -0.0877914951989026 everywhere. @@ -447,11 +447,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanBiasErrorTest) // the manually calculated result. input = arma::mat("1.0 0.0 1.0 -1.0 -1.0 0.0 -1.0 0.0"); target = arma::zeros(1, 8); - double error = module.Forward(input), target)); + double error = module.Forward(input, target); BOOST_REQUIRE_EQUAL(error, 0.125); // Test the Backward function. - module.Backward(input), target), output)); + module.Backward(input, target, output); // We should get a vector with -1 everywhere. for (double el : output) { @@ -463,11 +463,11 @@ BOOST_AUTO_TEST_CASE(SimpleMeanBiasErrorTest) // Test the error function on a single input. input = arma::mat("2"); target = arma::mat("3"); - error = module.Forward(input), target)); + error = module.Forward(input, target); BOOST_REQUIRE_EQUAL(error, 1.0); // Test the Backward function on a single input. - module.Backward(input), target), output)); + module.Backward(input, target, output); // Test whether the output is negative. BOOST_REQUIRE_EQUAL(arma::accu(output), -1); BOOST_REQUIRE_EQUAL(output.n_elem, 1); @@ -485,11 +485,11 @@ BOOST_AUTO_TEST_CASE(LogCoshLossTest) // Test the Forward function. Loss should be 0 if input = target. input = arma::ones(10, 1); target = arma::ones(10, 1); - loss = module.Forward(input), target)); + loss = module.Forward(input, target); BOOST_REQUIRE_EQUAL(loss, 0); // Test the Backward function for input = target. - module.Backward(input), target), output)); + module.Backward(input, target, output); for (double el : output) { // For input = target we should get 0.0 everywhere. @@ -502,11 +502,11 @@ BOOST_AUTO_TEST_CASE(LogCoshLossTest) // Test the Forward function. Loss should be 0.546621. input = arma::mat("1 2 3 4 5"); target = arma::mat("1 2.4 3.4 4.2 5.5"); - loss = module.Forward(input), target)); + loss = module.Forward(input, target); BOOST_REQUIRE_CLOSE(loss, 0.546621, 1e-3); // Test the Backward function. - module.Backward(input), target), output)); + module.Backward(input, target, output); BOOST_REQUIRE_CLOSE(arma::accu(output), 2.46962, 1e-3); BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); From 8e34c3a1e219f437d72485fd6f812a939d8e2fc0 Mon Sep 17 00:00:00 2001 From: Andrei Mihalea Date: Tue, 17 Mar 2020 16:29:50 +0200 Subject: [PATCH 150/265] Delete settings.json Deleted .vscode --- .vscode/settings.json | 68 ------------------------------------------- 1 file changed, 68 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 45fd170b7b..0000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,68 +0,0 @@ -{ - "files.associations": { - "cctype": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "array": "cpp", - "atomic": "cpp", - "strstream": "cpp", - "*.tcc": "cpp", - "bitset": "cpp", - "chrono": "cpp", - "codecvt": "cpp", - "complex": "cpp", - "condition_variable": "cpp", - "cstdint": "cpp", - "deque": "cpp", - "list": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "map": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "ratio": "cpp", - "regex": "cpp", - "set": "cpp", - "string": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "fstream": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "mutex": "cpp", - "new": "cpp", - "ostream": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "thread": "cpp", - "cfenv": "cpp", - "cinttypes": "cpp", - "typeindex": "cpp", - "typeinfo": "cpp", - "valarray": "cpp", - "variant": "cpp" - } -} \ No newline at end of file From bd892fe07b06a1dcbc11dd47578ab99f3185a46d Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Tue, 17 Mar 2020 18:25:09 +0200 Subject: [PATCH 151/265] Modified the parameter names and the comments before the margin getter/setter --- .../loss_functions/margin_ranking_loss.hpp | 31 +++++++++---------- .../margin_ranking_loss_impl.hpp | 24 +++++++------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index cd9e71ba9f..54d8928b19 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -23,7 +23,6 @@ namespace ann /** Artificial Neural Network. */ { * @tparam OutputDataType Type of the output data (arma::colvec, arma::mat, * arma::sp_mat or arma::cube). */ - template < typename InputDataType = arma::mat, typename OutputDataType = arma::mat @@ -44,36 +43,36 @@ class MarginRankingLoss * a value of 1 in the label means the first input should be ranked higher * and a value of -1 means the second input should be ranked higher. * - * @param x1 First input data used for evaluating the specified function. - * @param x2 Second input data used for evaluating the specified function. + * @param input1 First input data used for evaluating the specified function. + * @param input2 Second input data used for evaluating the specified function. * @param y The label vector which contains -1 or 1 values. */ template < typename FirstInputType, typename SecondInputType, - typename ThirdInputType + typename TargetType > - double Forward(const FirstInputType& x1, - const SecondInputType& x2, - const ThirdInputType& y); + double Forward(const FirstInputType& input1, + const SecondInputType& input2, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. * - * @param x1 The propagated first input activation. - * @param x2 The propagated second input activation. - * @param y The label vector which contains -1 or 1 values. + * @param input1 The propagated first input activation. + * @param input2 The propagated second input activation. + * @param target The label vector which contains -1 or 1 values. * @param output The calculated error. */ template < typename FirstInputType, typename SecondInputType, - typename ThirdInputType, + typename TargetType, typename OutputType > - void Backward(const FirstInputType& x1, - const SecondInputType& x2, - const ThirdInputType& y, + void Backward(const FirstInputType& input1, + const SecondInputType& input2, + const TargetType& target, OutputType& output); //! Get the output parameter. @@ -81,9 +80,9 @@ class MarginRankingLoss //! Modify the output parameter. OutputDataType& OutputParameter() { return outputParameter; } - //! Get the output parameter. + //! Get the margin parameter. double Margin() const { return margin; } - //! Modify the output parameter. + //! Modify the margin parameter. double& Margin() { return margin; } /** diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp index f99af349f6..5e908dfede 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp @@ -29,34 +29,34 @@ template template < typename FirstInputType, typename SecondInputType, - typename ThirdInputType + typename TargetType > double MarginRankingLoss::Forward( - const FirstInputType& x1, - const SecondInputType& x2, - const ThirdInputType& y) + const FirstInputType& input1, + const SecondInputType& input2, + const TargetType& target) { - return arma::accu(arma::max(arma::zeros(size(y)), - -y % (x1 - x2) + margin)) / y.n_cols; + return arma::accu(arma::max(arma::zeros(size(target)), + -target % (input1 - input2) + margin)) / target.n_cols; } template template < typename FirstInputType, typename SecondInputType, - typename ThirdInputType, + typename TargetType, typename OutputType > void MarginRankingLoss::Backward( - const FirstInputType& x1, - const SecondInputType& x2, - const ThirdInputType& y, + const FirstInputType& input1, + const SecondInputType& input2, + const TargetType& target, OutputType& output) { - output = -y % (x1 - x2) + margin; + output = -target % (input1 - input2) + margin; output.elem(arma::find(output >= 0)).ones(); output.elem(arma::find(output < 0)).zeros(); - output = (x2 - x1) % output / y.n_cols; + output = (input2 - input1) % output / target.n_cols; } template From 32d6d89dccd9e11f62383542e872d8dec38fcb6d Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Tue, 17 Mar 2020 18:37:03 +0200 Subject: [PATCH 152/265] Modified y to target --- src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index 54d8928b19..ec9c78b11f 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -45,7 +45,7 @@ class MarginRankingLoss * * @param input1 First input data used for evaluating the specified function. * @param input2 Second input data used for evaluating the specified function. - * @param y The label vector which contains -1 or 1 values. + * @param target The label vector which contains -1 or 1 values. */ template < typename FirstInputType, From 1e4110c6090f433cefd1e68d67d16ece8af6ccc6 Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Sat, 4 Jan 2020 01:04:47 +0530 Subject: [PATCH 153/265] Adding computerror function --- src/mlpack/methods/lars/lars.cpp | 8 ++++++++ src/mlpack/methods/lars/lars.hpp | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/mlpack/methods/lars/lars.cpp b/src/mlpack/methods/lars/lars.cpp index 527645a554..dcc4ecdc26 100644 --- a/src/mlpack/methods/lars/lars.cpp +++ b/src/mlpack/methods/lars/lars.cpp @@ -538,3 +538,11 @@ void LARS::CholeskyDelete(const size_t colToKill) matUtriCholFactor.shed_row(n); } } + +arma::mat LARS::ComputeError(const arma::mat& matX, + const arma::rowvec& y) +{ + arma::mat cost = (y-matX)*(y-matX); + + return cost; +} \ No newline at end of file diff --git a/src/mlpack/methods/lars/lars.hpp b/src/mlpack/methods/lars/lars.hpp index 2d9e336609..3c23899ba0 100644 --- a/src/mlpack/methods/lars/lars.hpp +++ b/src/mlpack/methods/lars/lars.hpp @@ -328,6 +328,18 @@ class LARS arma::mat& G); void CholeskyDelete(const size_t colToKill); + /** + * Find cost error while predicting the value using LARS. + * + * @param data Column-major input data (or row-major input data if rowMajor = + * true). + * @param responses A vector of targets. + * @param beta Vector to store the solution (the coefficients) in. + * @param transposeData Set to false if the data is row-major. + * @return The cost error. + */ + arma::mat ComputeError(const arma::mat& matX, + const arma::rowvec& y); }; } // namespace regression From f50d1097f4db2f30ebce7f8a8d86003a448f0dfc Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Sat, 4 Jan 2020 01:08:39 +0530 Subject: [PATCH 154/265] Some changes for computeerror --- src/mlpack/methods/lars/lars.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/lars/lars.cpp b/src/mlpack/methods/lars/lars.cpp index dcc4ecdc26..8eb08fdaef 100644 --- a/src/mlpack/methods/lars/lars.cpp +++ b/src/mlpack/methods/lars/lars.cpp @@ -540,9 +540,8 @@ void LARS::CholeskyDelete(const size_t colToKill) } arma::mat LARS::ComputeError(const arma::mat& matX, - const arma::rowvec& y) + const arma::rowvec& y) { - arma::mat cost = (y-matX)*(y-matX); - + arma::mat cost = arma::inv(arma::trans(matX)*matX)*arma::trans(matX)*y; return cost; } \ No newline at end of file From 3167387086209c584fb6fcd001a8ec17b1eeb711 Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Mon, 13 Jan 2020 02:00:59 +0530 Subject: [PATCH 155/265] Changing method of calculating cost error --- src/mlpack/methods/lars/lars.cpp | 16 +++++++++++++--- src/mlpack/methods/lars/lars.hpp | 28 ++++++++++++++++------------ src/mlpack/tests/lars_test.cpp | 21 +++++++++++++++++++++ 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/mlpack/methods/lars/lars.cpp b/src/mlpack/methods/lars/lars.cpp index 8eb08fdaef..4e3ba0aee2 100644 --- a/src/mlpack/methods/lars/lars.cpp +++ b/src/mlpack/methods/lars/lars.cpp @@ -539,9 +539,19 @@ void LARS::CholeskyDelete(const size_t colToKill) } } -arma::mat LARS::ComputeError(const arma::mat& matX, - const arma::rowvec& y) +double LARS::ComputeError(const arma::mat& matX, + const arma::rowvec& y, + const bool rowMajor) { - arma::mat cost = arma::inv(arma::trans(matX)*matX)*arma::trans(matX)*y; + double cost = 0.0; + arma::rowvec u; + if (rowMajor) + u = trans(matX * betaPath.back()); + else + u = betaPath.back().t() * matX; + for (size_t i = 0; i < y.size(); i++) + { + cost = cost + std::fabs(y[i]-u[i])*std::fabs(y[i]-u[i]); + } return cost; } \ No newline at end of file diff --git a/src/mlpack/methods/lars/lars.hpp b/src/mlpack/methods/lars/lars.hpp index 3c23899ba0..08b3dcd395 100644 --- a/src/mlpack/methods/lars/lars.hpp +++ b/src/mlpack/methods/lars/lars.hpp @@ -243,6 +243,21 @@ class LARS */ template void serialize(Archive& ar, const unsigned int /* version */); + /** + * Compute cost error in the given data matrix using the + * currently-trained LARS model. Only ||y-betaX||2 is used to calculate + * cost error. + * + * @param data Column-major input data (or row-major input data if rowMajor = + * true). + * @param responses A vector of targets. + * @param rowMajor Should be true if the data points matrix is row-major and + * false otherwise. + * @return The cost error. + */ + double ComputeError(const arma::mat& matX, + const arma::rowvec& y, + const bool rowMajor = false); private: //! Gram matrix. @@ -328,18 +343,7 @@ class LARS arma::mat& G); void CholeskyDelete(const size_t colToKill); - /** - * Find cost error while predicting the value using LARS. - * - * @param data Column-major input data (or row-major input data if rowMajor = - * true). - * @param responses A vector of targets. - * @param beta Vector to store the solution (the coefficients) in. - * @param transposeData Set to false if the data is row-major. - * @return The cost error. - */ - arma::mat ComputeError(const arma::mat& matX, - const arma::rowvec& y); + }; } // namespace regression diff --git a/src/mlpack/tests/lars_test.cpp b/src/mlpack/tests/lars_test.cpp index fd908a8ad6..6f6ccfac5d 100644 --- a/src/mlpack/tests/lars_test.cpp +++ b/src/mlpack/tests/lars_test.cpp @@ -397,4 +397,25 @@ BOOST_AUTO_TEST_CASE(LARSTrainReturnCorrelation) BOOST_REQUIRE_EQUAL(std::isfinite(maxCorr), true); } +BOOST_AUTO_TEST_CASE(LARSTestComputeError) +{ + arma::mat X; + arma::mat Y; + + data::Load("lars_dependent_x.csv", X); + data::Load("lars_dependent_y.csv", Y); + + arma::rowvec y = Y.row(0); + + double lambda1 = 0.1; + + LARS lars1(true, lambda1, 0.0); + arma::vec betaOpt1; + lars1.Train(X, y, betaOpt1); + double cost = lars1.ComputeError(X, y); + + BOOST_REQUIRE_EQUAL(std::isfinite(cost), true); + +} + BOOST_AUTO_TEST_SUITE_END(); From 57e604054153cc3a823f3970c4c0c0a040178a30 Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Mon, 13 Jan 2020 02:14:24 +0530 Subject: [PATCH 156/265] Removing styling mistakes --- src/mlpack/methods/lars/lars.cpp | 5 ++++- src/mlpack/methods/lars/lars.hpp | 2 +- src/mlpack/tests/lars_test.cpp | 1 - 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/lars/lars.cpp b/src/mlpack/methods/lars/lars.cpp index 4e3ba0aee2..284646265a 100644 --- a/src/mlpack/methods/lars/lars.cpp +++ b/src/mlpack/methods/lars/lars.cpp @@ -545,13 +545,16 @@ double LARS::ComputeError(const arma::mat& matX, { double cost = 0.0; arma::rowvec u; + if (rowMajor) u = trans(matX * betaPath.back()); + else u = betaPath.back().t() * matX; + for (size_t i = 0; i < y.size(); i++) { cost = cost + std::fabs(y[i]-u[i])*std::fabs(y[i]-u[i]); } return cost; -} \ No newline at end of file +} diff --git a/src/mlpack/methods/lars/lars.hpp b/src/mlpack/methods/lars/lars.hpp index 08b3dcd395..9f1bac255d 100644 --- a/src/mlpack/methods/lars/lars.hpp +++ b/src/mlpack/methods/lars/lars.hpp @@ -243,6 +243,7 @@ class LARS */ template void serialize(Archive& ar, const unsigned int /* version */); + /** * Compute cost error in the given data matrix using the * currently-trained LARS model. Only ||y-betaX||2 is used to calculate @@ -343,7 +344,6 @@ class LARS arma::mat& G); void CholeskyDelete(const size_t colToKill); - }; } // namespace regression diff --git a/src/mlpack/tests/lars_test.cpp b/src/mlpack/tests/lars_test.cpp index 6f6ccfac5d..274447bfab 100644 --- a/src/mlpack/tests/lars_test.cpp +++ b/src/mlpack/tests/lars_test.cpp @@ -415,7 +415,6 @@ BOOST_AUTO_TEST_CASE(LARSTestComputeError) double cost = lars1.ComputeError(X, y); BOOST_REQUIRE_EQUAL(std::isfinite(cost), true); - } BOOST_AUTO_TEST_SUITE_END(); From dec056dedddd06e21999b2ca7639ff046ef270df Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Mon, 13 Jan 2020 02:22:42 +0530 Subject: [PATCH 157/265] Removing styling mistakes --- src/mlpack/methods/lars/lars.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/lars/lars.cpp b/src/mlpack/methods/lars/lars.cpp index 284646265a..1e60f6d70b 100644 --- a/src/mlpack/methods/lars/lars.cpp +++ b/src/mlpack/methods/lars/lars.cpp @@ -545,13 +545,13 @@ double LARS::ComputeError(const arma::mat& matX, { double cost = 0.0; arma::rowvec u; - + if (rowMajor) u = trans(matX * betaPath.back()); - + else u = betaPath.back().t() * matX; - + for (size_t i = 0; i < y.size(); i++) { cost = cost + std::fabs(y[i]-u[i])*std::fabs(y[i]-u[i]); From 50ac26a89a0838263642d5744d2138b48a889970 Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Mon, 20 Jan 2020 12:54:39 +0530 Subject: [PATCH 158/265] Adding changes in train() --- src/mlpack/methods/lars/lars.cpp | 2 +- src/mlpack/methods/lars/lars.hpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mlpack/methods/lars/lars.cpp b/src/mlpack/methods/lars/lars.cpp index 1e60f6d70b..dfc986190e 100644 --- a/src/mlpack/methods/lars/lars.cpp +++ b/src/mlpack/methods/lars/lars.cpp @@ -361,7 +361,7 @@ double LARS::Train(const arma::mat& matX, beta = betaPath.back(); Timer::Stop("lars_regression"); - return maxCorr; + return ComputeError(matX, y, transposeData); } double LARS::Train(const arma::mat& data, diff --git a/src/mlpack/methods/lars/lars.hpp b/src/mlpack/methods/lars/lars.hpp index 9f1bac255d..c6bb396487 100644 --- a/src/mlpack/methods/lars/lars.hpp +++ b/src/mlpack/methods/lars/lars.hpp @@ -183,7 +183,7 @@ class LARS * @param responses A vector of targets. * @param beta Vector to store the solution (the coefficients) in. * @param transposeData Set to false if the data is row-major. - * @return The final absolute maximum correlation. + * @return The minimum cost error. */ double Train(const arma::mat& data, const arma::rowvec& responses, @@ -202,7 +202,7 @@ class LARS * @param responses A vector of targets. * @param transposeData Should be true if the input data is column-major and * false otherwise. - * @return The final absolute maximum correlation. + * @return The minimum cost error. */ double Train(const arma::mat& data, const arma::rowvec& responses, @@ -254,7 +254,7 @@ class LARS * @param responses A vector of targets. * @param rowMajor Should be true if the data points matrix is row-major and * false otherwise. - * @return The cost error. + * @return The minimum cost error. */ double ComputeError(const arma::mat& matX, const arma::rowvec& y, From ca4a898f19b12c6439cc2104d5892b2bcc216efd Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Mon, 20 Jan 2020 23:39:43 +0530 Subject: [PATCH 159/265] Adding changes in documentation --- .ci/windows-steps.yaml | 4 ++++ src/mlpack/methods/lars/lars.cpp | 2 +- src/mlpack/methods/lars/lars.hpp | 8 ++++---- src/mlpack/tests/lars_test.cpp | 21 ++++++++++++--------- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/.ci/windows-steps.yaml b/.ci/windows-steps.yaml index 26cd2c0ff4..dab3484697 100644 --- a/.ci/windows-steps.yaml +++ b/.ci/windows-steps.yaml @@ -28,7 +28,11 @@ steps: # Configure armadillo - bash: | git clone --depth 1 https://github.com/mlpack/jenkins-conf.git conf +<<<<<<< HEAD +======= + +>>>>>>> Adding changes in documentation curl -O http://masterblaster.mlpack.org:5005/armadillo-8.400.0.tar.gz -o armadillo-8.400.0.tar.gz tar -xzvf armadillo-8.400.0.tar.gz diff --git a/src/mlpack/methods/lars/lars.cpp b/src/mlpack/methods/lars/lars.cpp index dfc986190e..3059103a93 100644 --- a/src/mlpack/methods/lars/lars.cpp +++ b/src/mlpack/methods/lars/lars.cpp @@ -361,7 +361,7 @@ double LARS::Train(const arma::mat& matX, beta = betaPath.back(); Timer::Stop("lars_regression"); - return ComputeError(matX, y, transposeData); + return ComputeError(matX, y, !transposeData); } double LARS::Train(const arma::mat& data, diff --git a/src/mlpack/methods/lars/lars.hpp b/src/mlpack/methods/lars/lars.hpp index c6bb396487..9d7aa24ada 100644 --- a/src/mlpack/methods/lars/lars.hpp +++ b/src/mlpack/methods/lars/lars.hpp @@ -183,7 +183,7 @@ class LARS * @param responses A vector of targets. * @param beta Vector to store the solution (the coefficients) in. * @param transposeData Set to false if the data is row-major. - * @return The minimum cost error. + * @return minimum cost error(||y-beta*X||2 is used to calculate error). */ double Train(const arma::mat& data, const arma::rowvec& responses, @@ -202,7 +202,7 @@ class LARS * @param responses A vector of targets. * @param transposeData Should be true if the input data is column-major and * false otherwise. - * @return The minimum cost error. + * @return minimum cost error(||y-beta*X||2 is used to calculate error). */ double Train(const arma::mat& data, const arma::rowvec& responses, @@ -245,8 +245,8 @@ class LARS void serialize(Archive& ar, const unsigned int /* version */); /** - * Compute cost error in the given data matrix using the - * currently-trained LARS model. Only ||y-betaX||2 is used to calculate + * Compute cost error of the given data matrix using the + * currently-trained LARS model. Only ||y-beta*X||2 is used to calculate * cost error. * * @param data Column-major input data (or row-major input data if rowMajor = diff --git a/src/mlpack/tests/lars_test.cpp b/src/mlpack/tests/lars_test.cpp index 274447bfab..cc89b6d7db 100644 --- a/src/mlpack/tests/lars_test.cpp +++ b/src/mlpack/tests/lars_test.cpp @@ -353,7 +353,7 @@ BOOST_AUTO_TEST_CASE(TrainingConstructorWithNonDefaultsTest) } /** - * Test that LARS::Train() returns finite correlation value. + * Test that LARS::Train() returns finite error value. */ BOOST_AUTO_TEST_CASE(LARSTrainReturnCorrelation) { @@ -371,32 +371,35 @@ BOOST_AUTO_TEST_CASE(LARSTrainReturnCorrelation) // Test with Cholesky decomposition and with lasso. LARS lars1(true, lambda1, 0.0); arma::vec betaOpt1; - double maxCorr = lars1.Train(X, y, betaOpt1); + double error = lars1.Train(X, y, betaOpt1); - BOOST_REQUIRE_EQUAL(std::isfinite(maxCorr), true); + BOOST_REQUIRE_EQUAL(std::isfinite(error), true); // Test without Cholesky decomposition and with lasso. LARS lars2(false, lambda1, 0.0); arma::vec betaOpt2; - maxCorr = lars2.Train(X, y, betaOpt2); + error = lars2.Train(X, y, betaOpt2); - BOOST_REQUIRE_EQUAL(std::isfinite(maxCorr), true); + BOOST_REQUIRE_EQUAL(std::isfinite(error), true); // Test with Cholesky decomposition and with elasticnet. LARS lars3(true, lambda1, lambda2); arma::vec betaOpt3; - maxCorr = lars3.Train(X, y, betaOpt3); + error = lars3.Train(X, y, betaOpt3); - BOOST_REQUIRE_EQUAL(std::isfinite(maxCorr), true); + BOOST_REQUIRE_EQUAL(std::isfinite(error), true); // Test without Cholesky decomposition and with elasticnet. LARS lars4(false, lambda1, lambda2); arma::vec betaOpt4; - maxCorr = lars4.Train(X, y, betaOpt4); + error = lars4.Train(X, y, betaOpt4); - BOOST_REQUIRE_EQUAL(std::isfinite(maxCorr), true); + BOOST_REQUIRE_EQUAL(std::isfinite(error), true); } +/** + * Test that LARS::ComputeError() returns finite error value. + */ BOOST_AUTO_TEST_CASE(LARSTestComputeError) { arma::mat X; From 31ca2df59e2d1245e426ff7c06c296cfed7027fb Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Sat, 25 Jan 2020 03:36:33 +0530 Subject: [PATCH 160/265] Adding changes in test and using arma::accu --- src/mlpack/methods/lars/lars.cpp | 5 +---- src/mlpack/tests/lars_test.cpp | 8 +++----- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/mlpack/methods/lars/lars.cpp b/src/mlpack/methods/lars/lars.cpp index 3059103a93..dd7f7a24f6 100644 --- a/src/mlpack/methods/lars/lars.cpp +++ b/src/mlpack/methods/lars/lars.cpp @@ -552,9 +552,6 @@ double LARS::ComputeError(const arma::mat& matX, else u = betaPath.back().t() * matX; - for (size_t i = 0; i < y.size(); i++) - { - cost = cost + std::fabs(y[i]-u[i])*std::fabs(y[i]-u[i]); - } + cost = arma::accu(arma::abs(y-u)); return cost; } diff --git a/src/mlpack/tests/lars_test.cpp b/src/mlpack/tests/lars_test.cpp index cc89b6d7db..6f0eef498a 100644 --- a/src/mlpack/tests/lars_test.cpp +++ b/src/mlpack/tests/lars_test.cpp @@ -398,7 +398,7 @@ BOOST_AUTO_TEST_CASE(LARSTrainReturnCorrelation) } /** - * Test that LARS::ComputeError() returns finite error value. + * Test that LARS::ComputeError() returns error value less than 1. */ BOOST_AUTO_TEST_CASE(LARSTestComputeError) { @@ -410,14 +410,12 @@ BOOST_AUTO_TEST_CASE(LARSTestComputeError) arma::rowvec y = Y.row(0); - double lambda1 = 0.1; - - LARS lars1(true, lambda1, 0.0); + LARS lars1(true, 0.1, 0.0); arma::vec betaOpt1; lars1.Train(X, y, betaOpt1); double cost = lars1.ComputeError(X, y); - BOOST_REQUIRE_EQUAL(std::isfinite(cost), true); + BOOST_REQUIRE_EQUAL(cost <= 1 ,true); } BOOST_AUTO_TEST_SUITE_END(); From 551d00a4902353002b17dbe3ded612ca5c92b59d Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Sat, 25 Jan 2020 03:39:57 +0530 Subject: [PATCH 161/265] Styling fix --- src/mlpack/tests/lars_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/tests/lars_test.cpp b/src/mlpack/tests/lars_test.cpp index 6f0eef498a..12098e04af 100644 --- a/src/mlpack/tests/lars_test.cpp +++ b/src/mlpack/tests/lars_test.cpp @@ -415,7 +415,7 @@ BOOST_AUTO_TEST_CASE(LARSTestComputeError) lars1.Train(X, y, betaOpt1); double cost = lars1.ComputeError(X, y); - BOOST_REQUIRE_EQUAL(cost <= 1 ,true); + BOOST_REQUIRE_EQUAL(cost <= 1, true); } BOOST_AUTO_TEST_SUITE_END(); From a46fc082b5b9a47e875082602e20e8b9771343ff Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Sun, 26 Jan 2020 13:13:26 +0530 Subject: [PATCH 162/265] Adding changes in implementation and improving test --- src/mlpack/methods/lars/lars.cpp | 2 +- src/mlpack/tests/lars_test.cpp | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/lars/lars.cpp b/src/mlpack/methods/lars/lars.cpp index dd7f7a24f6..1ee634fc44 100644 --- a/src/mlpack/methods/lars/lars.cpp +++ b/src/mlpack/methods/lars/lars.cpp @@ -552,6 +552,6 @@ double LARS::ComputeError(const arma::mat& matX, else u = betaPath.back().t() * matX; - cost = arma::accu(arma::abs(y-u)); + cost = arma::accu((y - u) % (y - u)); return cost; } diff --git a/src/mlpack/tests/lars_test.cpp b/src/mlpack/tests/lars_test.cpp index 12098e04af..ea82f2701c 100644 --- a/src/mlpack/tests/lars_test.cpp +++ b/src/mlpack/tests/lars_test.cpp @@ -398,7 +398,8 @@ BOOST_AUTO_TEST_CASE(LARSTrainReturnCorrelation) } /** - * Test that LARS::ComputeError() returns error value less than 1. + * Test that LARS::ComputeError() returns error value less than 1 + * and greater than 0. */ BOOST_AUTO_TEST_CASE(LARSTestComputeError) { @@ -416,6 +417,7 @@ BOOST_AUTO_TEST_CASE(LARSTestComputeError) double cost = lars1.ComputeError(X, y); BOOST_REQUIRE_EQUAL(cost <= 1, true); + BOOST_REQUIRE_EQUAL(cost >= 0, true); } BOOST_AUTO_TEST_SUITE_END(); From 61b280f9c5de1fcae623ceccc7799c22e3598e84 Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Thu, 30 Jan 2020 01:14:28 +0530 Subject: [PATCH 163/265] Using arma::pow --- src/mlpack/methods/lars/lars.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/mlpack/methods/lars/lars.cpp b/src/mlpack/methods/lars/lars.cpp index 1ee634fc44..848f9ebb67 100644 --- a/src/mlpack/methods/lars/lars.cpp +++ b/src/mlpack/methods/lars/lars.cpp @@ -543,15 +543,13 @@ double LARS::ComputeError(const arma::mat& matX, const arma::rowvec& y, const bool rowMajor) { - double cost = 0.0; - arma::rowvec u; - if (rowMajor) - u = trans(matX * betaPath.back()); + { + return arma::accu(arma::pow(y - trans(matX * betaPath.back()), 2.0)); + } else - u = betaPath.back().t() * matX; - - cost = arma::accu((y - u) % (y - u)); - return cost; + { + return arma::accu(arma::pow(y - betaPath.back().t() * matX, 2.0)); + } } From 8012078ea14687e35c867b370117bd572d85d29b Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Wed, 11 Mar 2020 01:17:15 +0530 Subject: [PATCH 164/265] Adding log in history.md --- HISTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 3fb7cce8fa..d2f17b12f7 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -25,6 +25,9 @@ * Add functions to access parameters of `Convolution` and `AtrousConvolution` layers (#1985). + * Add Compute Error function in lars regression and changing Train function to + return computed error (#2139). + * Add Julia bindings (#1949). Build settings can be controlled with the `BUILD_JULIA_BINDINGS=(ON/OFF)` and `JULIA_EXECUTABLE=/path/to/julia` CMake parameters. From 0f167ba693c24b3aeafdae805fda3f95d0bc9256 Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Thu, 12 Mar 2020 22:53:51 +0530 Subject: [PATCH 165/265] Resolving conflicts --- .ci/windows-steps.yaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.ci/windows-steps.yaml b/.ci/windows-steps.yaml index dab3484697..26cd2c0ff4 100644 --- a/.ci/windows-steps.yaml +++ b/.ci/windows-steps.yaml @@ -28,11 +28,7 @@ steps: # Configure armadillo - bash: | git clone --depth 1 https://github.com/mlpack/jenkins-conf.git conf -<<<<<<< HEAD -======= - ->>>>>>> Adding changes in documentation curl -O http://masterblaster.mlpack.org:5005/armadillo-8.400.0.tar.gz -o armadillo-8.400.0.tar.gz tar -xzvf armadillo-8.400.0.tar.gz From 94f7ff3a3be6dca8047739e4b31c6921c6633ba1 Mon Sep 17 00:00:00 2001 From: himanshupathak21061998 Date: Tue, 17 Mar 2020 22:13:09 +0530 Subject: [PATCH 166/265] Adding check for train --- src/mlpack/tests/lars_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mlpack/tests/lars_test.cpp b/src/mlpack/tests/lars_test.cpp index ea82f2701c..6b7edc267e 100644 --- a/src/mlpack/tests/lars_test.cpp +++ b/src/mlpack/tests/lars_test.cpp @@ -413,11 +413,12 @@ BOOST_AUTO_TEST_CASE(LARSTestComputeError) LARS lars1(true, 0.1, 0.0); arma::vec betaOpt1; - lars1.Train(X, y, betaOpt1); + double train1 = lars1.Train(X, y, betaOpt1); double cost = lars1.ComputeError(X, y); BOOST_REQUIRE_EQUAL(cost <= 1, true); BOOST_REQUIRE_EQUAL(cost >= 0, true); + BOOST_REQUIRE_EQUAL(cost == train1, true); } BOOST_AUTO_TEST_SUITE_END(); From 556be0928163d6a0efa0c4f134d2dc55b56e3cf3 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Tue, 17 Mar 2020 21:31:28 -0400 Subject: [PATCH 167/265] Move all STB-related code into .cpp files. --- HISTORY.md | 3 + src/mlpack/core/data/CMakeLists.txt | 2 + src/mlpack/core/data/image_info.hpp | 18 --- src/mlpack/core/data/load.hpp | 14 +-- src/mlpack/core/data/load_image.cpp | 125 +++++++++++++++++++++ src/mlpack/core/data/load_image_impl.hpp | 114 ++++++------------- src/mlpack/core/data/save.hpp | 16 +-- src/mlpack/core/data/save_image.cpp | 135 +++++++++++++++++++++++ src/mlpack/core/data/save_impl.hpp | 134 +++++----------------- src/mlpack/tests/CMakeLists.txt | 10 +- src/mlpack/tests/image_load_test.cpp | 11 +- 11 files changed, 350 insertions(+), 232 deletions(-) create mode 100644 src/mlpack/core/data/load_image.cpp create mode 100644 src/mlpack/core/data/save_image.cpp diff --git a/HISTORY.md b/HISTORY.md index 3fb7cce8fa..0d279b2b91 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -52,6 +52,9 @@ * Change neural network types to avoid unnecessary use of rvalue references (#2259). + * Refactor STB support so HAS_STB macro is not needed when compiling against + mlpack (#????). + ### mlpack 3.2.2 ###### 2019-11-26 * Add `valid` and `same` padding option in `Convolution` and `Atrous diff --git a/src/mlpack/core/data/CMakeLists.txt b/src/mlpack/core/data/CMakeLists.txt index d21c27da75..0ef68d4a94 100644 --- a/src/mlpack/core/data/CMakeLists.txt +++ b/src/mlpack/core/data/CMakeLists.txt @@ -11,6 +11,7 @@ set(SOURCES load_csv.cpp load.hpp load_image_impl.hpp + load_image.cpp load_model_impl.hpp load_vec_impl.hpp load_impl.hpp @@ -21,6 +22,7 @@ set(SOURCES normalize_labels_impl.hpp save.hpp save_impl.hpp + save_image.cpp serialization_template_version.hpp split_data.hpp imputer.hpp diff --git a/src/mlpack/core/data/image_info.hpp b/src/mlpack/core/data/image_info.hpp index e7f444447c..9693127500 100644 --- a/src/mlpack/core/data/image_info.hpp +++ b/src/mlpack/core/data/image_info.hpp @@ -13,28 +13,12 @@ #ifndef MLPACK_CORE_DATA_IMAGE_INFO_HPP #define MLPACK_CORE_DATA_IMAGE_INFO_HPP - #include - #include "extension.hpp" -#ifdef HAS_STB // Compile this only if stb is present. - -#define STB_IMAGE_STATIC -#define STB_IMAGE_IMPLEMENTATION -#include - -#define STB_IMAGE_WRITE_STATIC -#define STB_IMAGE_WRITE_IMPLEMENTATION -#include - -#endif - namespace mlpack { namespace data { -#ifdef HAS_STB // Compile this only if stb is present. - /** * Checks if the given image filename is supported. * @@ -44,8 +28,6 @@ namespace data { inline bool ImageFormatSupported(const std::string& fileName, const bool save = false); -#endif - /** * Implements meta-data of images required by data::Load and * data::Save for loading and saving images into arma::Mat. diff --git a/src/mlpack/core/data/load.hpp b/src/mlpack/core/data/load.hpp index 7974227409..256163f72b 100644 --- a/src/mlpack/core/data/load.hpp +++ b/src/mlpack/core/data/load.hpp @@ -291,7 +291,6 @@ bool Load(const std::string& filename, /** * Image load/save interfaces. */ -#ifdef HAS_STB /** * Load the image file into the given matrix. @@ -300,15 +299,13 @@ bool Load(const std::string& filename, * @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). - * @param transpose If true, transpose the matrix after loading. * @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, - const bool transpose = true); + const bool fatal = false); /** * Load the image file into the given matrix. @@ -324,10 +321,13 @@ template bool Load(const std::vector& files, arma::Mat& matrix, ImageInfo& info, - const bool fatal = false, - const bool transpose = true); + const bool fatal = false); -#endif // HAS_STB. +// Implementation found in load_image.cpp. +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.cpp b/src/mlpack/core/data/load_image.cpp new file mode 100644 index 0000000000..2796e9a1f4 --- /dev/null +++ b/src/mlpack/core/data/load_image.cpp @@ -0,0 +1,125 @@ +/** + * @file load_image.cpp + * @author Mehul Kumar Nirala + * + * Implementation of image loading functionality via STB. + */ +#include "load.hpp" +#include "image_info.hpp" + +#ifdef HAS_STB + +#define STB_IMAGE_STATIC +#define STB_IMAGE_IMPLEMENTATION +#include + +#define STB_IMAGE_WRITE_STATIC +#define STB_IMAGE_WRITE_IMPLEMENTATION +#include + +namespace mlpack { +namespace data { + +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; +} + +} // namespace data +} // namespace mlpack + +#else + +namespace mlpack { +namespace data { + +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 diff --git a/src/mlpack/core/data/load_image_impl.hpp b/src/mlpack/core/data/load_image_impl.hpp index 23e45be40c..b5be00861b 100644 --- a/src/mlpack/core/data/load_image_impl.hpp +++ b/src/mlpack/core/data/load_image_impl.hpp @@ -19,68 +19,28 @@ namespace mlpack { namespace data { -#ifdef HAS_STB // Compile this only if stb is present. - // Image loading API. template bool Load(const std::string& filename, arma::Mat& matrix, ImageInfo& info, - const bool /* fatal */, - const bool transpose) + const bool fatal) { Timer::Start("loading_image"); - unsigned char* image; - if (!ImageFormatSupported(filename)) + // STB loads into unsigned char matrices, so we may have to convert once + // loaded. + arma::Mat tempMatrix; + const bool result = LoadImage(filename, tempMatrix, info, fatal); + + // If fatal is true, then the program will have already thrown an exception. + if (!result) { - std::ostringstream oss; - oss << "File type " << Extension(filename) << " not supported.\n"; - oss << "Currently it supports "; - for (auto extension : loadFileTypes) - oss << " " << extension; - oss << std::endl; - throw std::runtime_error(oss.str()); + Timer::Stop("loading_image"); return false; } - stbi_set_flip_vertically_on_load(transpose); - - // 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 (tempWidth <= 0 || tempHeight <= 0) - { - std::ostringstream oss; - oss << "Image '" << filename << "' not found." << std::endl; - free(image); - throw std::runtime_error(oss.str()); - - 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); + matrix = arma::conv_to>::from(tempMatrix); Timer::Stop("loading_image"); return true; } @@ -90,58 +50,46 @@ template bool Load(const std::vector& files, arma::Mat& matrix, ImageInfo& info, - const bool fatal, - const bool transpose) + const bool fatal) { if (files.size() == 0) { std::ostringstream oss; - oss << "Files vector is empty." << std::endl; + oss << "Load(): vector of image files is empty." << std::endl; + + if (fatal) + Log::Fatal << oss.str(); + else + Log::Warn << oss.str(); - throw std::runtime_error(oss.str()); return false; } arma::Mat img; - bool status = Load(files[0], img, info, fatal, transpose); + bool status = LoadImage(files[0], img, info, fatal); + + if (!status) + return false; // Decide matrix dimension using the image height and width. - matrix.set_size(info.Width() * info.Height() * info.Channels(), files.size()); - matrix.col(0) = img; + arma::Mat tmpMatrix( + info.Width() * info.Height() * info.Channels(), files.size()); + tmpMatrix.col(0) = img; for (size_t i = 1; i < files.size() ; i++) { - arma::Mat colImg(matrix.colptr(i), matrix.n_rows, 1, + arma::Mat colImg(tmpMatrix.colptr(i), tmpMatrix.n_rows, 1, false, true); - status &= Load(files[i], colImg, info, fatal, transpose); + status = LoadImage(files[i], colImg, info, fatal); + + if (!status) + return false; } - return status; -} -#else // No STB. -template -bool Load(const std::string& filename, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal = false, - const bool transpose = true) -{ - throw std::runtime_error("Load(): HAS_STB is not defined, " - "so STB is not available and images cannot be loaded!"); + matrix = arma::conv_to>::from(tmpMatrix); + return true; } -template -bool Load(const std::vector& files, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal = false, - const bool transpose = true) -{ - throw std::runtime_error("Load(): HAS_STB is not defined, " - "so STB is not available and images cannot be loaded!"); -} -#endif // HAS_STB. - } // namespace data } // namespace mlpack diff --git a/src/mlpack/core/data/save.hpp b/src/mlpack/core/data/save.hpp index 121265ac81..a8cdaf4e75 100644 --- a/src/mlpack/core/data/save.hpp +++ b/src/mlpack/core/data/save.hpp @@ -91,8 +91,6 @@ bool Save(const std::string& filename, const bool fatal = false, format f = format::autodetect); -#ifdef HAS_STB - /** * Save the image file from the given matrix. * @@ -107,8 +105,7 @@ template bool Save(const std::string& filename, arma::Mat& matrix, ImageInfo& info, - const bool fatal = false, - const bool transpose = true); + const bool fatal = false); /** * Save the image file from the given matrix. @@ -124,10 +121,15 @@ template bool Save(const std::vector& files, arma::Mat& matrix, ImageInfo& info, - const bool fatal = false, - const bool transpose = true); + const bool fatal = false); -#endif // HAS_STB. +/** + * Helper function to save files. Implementation in save_image.cpp. + */ +bool SaveImage(const std::string& filename, + arma::Mat& image, + ImageInfo& info, + const bool fatal); } // namespace data } // namespace mlpack diff --git a/src/mlpack/core/data/save_image.cpp b/src/mlpack/core/data/save_image.cpp new file mode 100644 index 0000000000..c1802073e4 --- /dev/null +++ b/src/mlpack/core/data/save_image.cpp @@ -0,0 +1,135 @@ +/** + * @file save_image.cpp + * @author Mehul Kumar Nirala + * + * Implementation of image saving functionality via STB. + */ +#include "save.hpp" + +#ifdef HAS_STB + +#define STB_IMAGE_STATIC +#define STB_IMAGE_IMPLEMENTATION +#include + +#define STB_IMAGE_WRITE_STATIC +#define STB_IMAGE_WRITE_IMPLEMENTATION +#include + +namespace mlpack { +namespace data { + +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; + } + + 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 { + +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 diff --git a/src/mlpack/core/data/save_impl.hpp b/src/mlpack/core/data/save_impl.hpp index 18a61530a5..e19f8928bf 100644 --- a/src/mlpack/core/data/save_impl.hpp +++ b/src/mlpack/core/data/save_impl.hpp @@ -283,86 +283,25 @@ bool Save(const std::string& filename, } } -#ifdef HAS_STB -// Image saving API. +/** + * 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, - const bool transpose) + const bool fatal) { - Timer::Start("saving_image"); - // We transpose by default. So, un-transpose if necessary. - if (!transpose) - matrix = arma::trans(matrix); + arma::Mat tmpMatrix = + arma::conv_to>::from(matrix); - int tempWidth, tempHeight, tempChannels, tempQuality; - - tempWidth = info.Width(); - tempHeight = info.Height(); - tempChannels = info.Channels(); - tempQuality = info.Quality(); - - if (!ImageFormatSupported(filename, true)) - { - std::ostringstream oss; - oss << "File type " << Extension(filename) << " not supported.\n"; - oss << "Currently it supports "; - for (auto extension : saveFileTypes) - oss << ", " << extension; - oss << std::endl; - throw std::runtime_error(oss.str()); - return false; - } - if (matrix.n_cols > 1) - { - std::cout << "Input Matrix contains more than 1 image." << std::endl; - std::cout << "Only the firstimage will be saved!" << std::endl; - } - stbi_flip_vertically_on_write(transpose); - - bool status = false; - try - { - unsigned char* image = matrix.memptr(); - - if ("png" == Extension(filename)) - { - status = stbi_write_png(filename.c_str(), tempWidth, tempHeight, - tempChannels, image, tempWidth * tempChannels); - } - else if ("bmp" == Extension(filename)) - { - status = stbi_write_bmp(filename.c_str(), tempWidth, tempHeight, - tempChannels, image); - } - else if ("tga" == Extension(filename)) - { - status = stbi_write_tga(filename.c_str(), tempWidth, tempHeight, - tempChannels, image); - } - else if ("hdr" == Extension(filename)) - { - status = stbi_write_hdr(filename.c_str(), tempWidth, tempHeight, - tempChannels, reinterpret_cast(image)); - } - else if ("jpg" == Extension(filename)) - { - status = stbi_write_jpg(filename.c_str(), tempWidth, tempHeight, - tempChannels, image, tempQuality); - } - } - catch (std::exception& e) - { - Timer::Stop("saving_image"); - if (fatal) - Log::Fatal << e.what() << std::endl; - Log::Warn << e.what() << std::endl; - return false; - } - Timer::Stop("saving_image"); - return status; + // Call out to .cpp implementation. + return SaveImage(filename, tmpMatrix, info, fatal); } // Image saving API for multiple files. @@ -370,23 +309,26 @@ template bool Save(const std::vector& files, arma::Mat& matrix, ImageInfo& info, - const bool fatal, - const bool transpose) + const bool fatal) { if (files.size() == 0) { - std::ostringstream oss; - oss << "Files vector is empty." << std::endl; + 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; + } - throw std::runtime_error(oss.str()); return false; } - // We transpose by default. So, un-transpose if necessary. - if (!transpose) - matrix = arma::trans(matrix); arma::Mat img; - bool status = Save(files[0], img, info, fatal, transpose); + bool status = Save(files[0], img, info, fatal); // Decide matrix dimension using the image height and width. matrix.set_size(info.Width() * info.Height() * info.Channels(), files.size()); @@ -396,33 +338,11 @@ bool Save(const std::vector& files, { arma::Mat colImg(matrix.colptr(i), matrix.n_rows, 1, false, true); - status &= Save(files[i], colImg, info, fatal, transpose); + status &= Save(files[i], colImg, info, fatal); } + return status; } -#else -template -bool Save(const std::string& filename, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal = false, - const bool transpose = true) -{ - throw std::runtime_error("Save(): HAS_STB is not defined, " - "so STB is not available and images cannot be saved!"); -} - -template -bool Save(const std::vector& files, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal = false, - const bool transpose = true) -{ - throw std::runtime_error("Save(): HAS_STB is not defined, " - "so STB is not available and images cannot be saved!"); -} -#endif // HAS_STB. } // namespace data } // namespace mlpack diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index a1a865a1cc..32bc77b5d3 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -186,11 +186,11 @@ add_custom_command(TARGET mlpack_test ) add_custom_command(TARGET mlpack_test POST_BUILD - COMMAND ${CMAKE_COMMAND} -E tar xjpf mnist_first250_training_4s_and_9s.tar.bz2 - COMMAND ${CMAKE_COMMAND} -E tar xjpf digits_train.tar.bz2 - COMMAND ${CMAKE_COMMAND} -E tar xjpf digits_test.tar.bz2 - COMMAND ${CMAKE_COMMAND} -E tar xjpf digits_train_label.tar.bz2 - COMMAND ${CMAKE_COMMAND} -E tar xjpf digits_test_label.tar.bz2 + COMMAND ${CMAKE_COMMAND} -E tar xjf mnist_first250_training_4s_and_9s.tar.bz2 + COMMAND ${CMAKE_COMMAND} -E tar xjf digits_train.tar.bz2 + COMMAND ${CMAKE_COMMAND} -E tar xjf digits_test.tar.bz2 + COMMAND ${CMAKE_COMMAND} -E tar xjf digits_train_label.tar.bz2 + COMMAND ${CMAKE_COMMAND} -E tar xjf digits_test_label.tar.bz2 WORKING_DIRECTORY ${PROJECT_BINARY_DIR} ) diff --git a/src/mlpack/tests/image_load_test.cpp b/src/mlpack/tests/image_load_test.cpp index 0e2bbb0459..e7c90acb1a 100644 --- a/src/mlpack/tests/image_load_test.cpp +++ b/src/mlpack/tests/image_load_test.cpp @@ -29,8 +29,10 @@ BOOST_AUTO_TEST_CASE(LoadInvalidExtensionFile) { arma::Mat matrix; data::ImageInfo info; + Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(data::Load("invalidExtendion.p4ng", matrix, info, - false, true), std::runtime_error); + true), std::runtime_error); + Log::Fatal.ignoreInput = false; } /** @@ -40,8 +42,7 @@ BOOST_AUTO_TEST_CASE(LoadImageAPITest) { arma::Mat matrix; data::ImageInfo info; - BOOST_REQUIRE(data::Load("test_image.png", matrix, info, false, - true) == true); + BOOST_REQUIRE(data::Load("test_image.png", matrix, info, false) == true); BOOST_REQUIRE_EQUAL(matrix.n_rows, 50 * 50 * 3); // width * height * channels. BOOST_REQUIRE_EQUAL(matrix.n_cols, 1); } @@ -56,10 +57,10 @@ BOOST_AUTO_TEST_CASE(SaveImageAPITest) arma::Mat im1; size_t dimension = info.Width() * info.Height() * info.Channels(); im1 = arma::randi>(dimension, 1); - BOOST_REQUIRE(data::Save("APITest.bmp", im1, info, false, true) == true); + BOOST_REQUIRE(data::Save("APITest.bmp", im1, info, false) == true); arma::Mat im2; - BOOST_REQUIRE(data::Load("APITest.bmp", im2, info, false, true) == true); + BOOST_REQUIRE(data::Load("APITest.bmp", im2, info, false) == true); BOOST_REQUIRE_EQUAL(im1.n_cols, im2.n_cols); BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows); From a967547d28cb860387ac582092c8214bcb542abe Mon Sep 17 00:00:00 2001 From: Lakshya Ojha <57477999+ojhalakshya@users.noreply.github.com> Date: Wed, 18 Mar 2020 10:48:45 +0530 Subject: [PATCH 168/265] Adding Hinge embedding loss function (#2229) * Adding hard shrink function (#2186) * completed hardshrink_function.hpp * resetting master * complete hardsrhink_function.hpp * activation_functions_test.cpp * cmake changes * base layer changes * style correction * starting implementing hard shrink as layer * new changes * changes in tests * inv changes * test changes * test changes * deleting prev function * test style changes * more style changes * comment changes * minor changes * comment corrections hard shrink * minor changes * dummy commit * Style fix for parameter lambda * hardshrink.hpp to hardshrink_impl.hpp fn function shift * style fix Co-authored-by: Marcus Edel * completed hardshrink_function.hpp * resetting master * complete hardsrhink_function.hpp * activation_functions_test.cpp * cmake changes * base layer changes * style correction * starting implementing hard shrink as layer * new changes * changes in tests * inv changes * test changes * test changes * deleting prev function * test style changes * more style changes * comment changes * minor changes * comment corrections hard shrink * minor changes * dummy commit * Style fix for parameter lambda * hardshrink.hpp to hardshrink_impl.hpp fn function shift * style fix Co-authored-by: Marcus Edel dummy commit Redirected link on NUMfocus logo test bugs fixing rebasing branch * rvalue refactor corrections edited copyright.txt changes in History.md bug fix * bug fixes minor change * dummy commit * style fix --- COPYRIGHT.txt | 1 + HISTORY.md | 6 ++ .../methods/ann/loss_functions/CMakeLists.txt | 2 + .../loss_functions/hinge_embedding_loss.hpp | 87 +++++++++++++++++++ .../hinge_embedding_loss_impl.hpp | 60 +++++++++++++ src/mlpack/tests/loss_functions_test.cpp | 40 +++++++++ 6 files changed, 196 insertions(+) create mode 100644 src/mlpack/methods/ann/loss_functions/hinge_embedding_loss.hpp create mode 100644 src/mlpack/methods/ann/loss_functions/hinge_embedding_loss_impl.hpp diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt index 11c82cda4b..ce5c944e0d 100644 --- a/COPYRIGHT.txt +++ b/COPYRIGHT.txt @@ -129,6 +129,7 @@ Copyright: Copyright 2020, Manoranjan Kumar Bharti ( Nakul Bharti ) Copyright 2020, Saraansh Tandon Copyright 2020, Gaurav Singh + Copyright 2020, Lakshya Ojha License: BSD-3-clause All rights reserved. diff --git a/HISTORY.md b/HISTORY.md index b45285d8cf..c401f0fc68 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -54,6 +54,12 @@ * Bump minimum Boost version to 1.58 (#2305). + * Add Hard Shrink Activation Function (#2186). + + * Add Soft Shrink Activation Function (#2174). + + * Add Hinge Embedding Loss Function (#2229). + ### mlpack 3.2.2 ###### 2019-11-26 * Add `valid` and `same` padding option in `Convolution` and `Atrous diff --git a/src/mlpack/methods/ann/loss_functions/CMakeLists.txt b/src/mlpack/methods/ann/loss_functions/CMakeLists.txt index a1bdcb8fd8..f41f43ad43 100644 --- a/src/mlpack/methods/ann/loss_functions/CMakeLists.txt +++ b/src/mlpack/methods/ann/loss_functions/CMakeLists.txt @@ -23,6 +23,8 @@ set(SOURCES reconstruction_loss_impl.hpp sigmoid_cross_entropy_error.hpp sigmoid_cross_entropy_error_impl.hpp + hinge_embedding_loss.hpp + hinge_embedding_loss_impl.hpp ) # Add directory name to sources. diff --git a/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss.hpp b/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss.hpp new file mode 100644 index 0000000000..bcb9b17d6d --- /dev/null +++ b/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss.hpp @@ -0,0 +1,87 @@ +/** + * @file hinge_embedding_loss.hpp + * @author Lakshya Ojha + * + * Definition of the Hinge Embedding Loss Function. + * The Hinge Embedding loss function is often used to improve performance + * in semi-supervised learning or to learn nonlinear embeddings. + * + * 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_METHODS_ANN_LOSS_FUNCTION_HINGE_EMBEDDING_LOSS_HPP +#define MLPACK_METHODS_ANN_LOSS_FUNCTION_HINGE_EMBEDDING_LOSS_HPP + +#include + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +/** + * The Hinge Embedding loss function is often used to compute the loss + * between y_true and y_pred. + * + * @tparam InputDataType Type of the input data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + * @tparam OutputDataType Type of the output data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + */ +template < + typename InputDataType = arma::mat, + typename OutputDataType = arma::mat +> +class HingeEmbeddingLoss +{ + public: + /** + * Create the Hinge Embedding object. + */ + HingeEmbeddingLoss(); + + /** + * Computes the Hinge Embedding loss function. + * + * @param input Input data used for evaluating the specified function. + * @param target Target data to compare with. + */ + template + double Forward(const InputType& input, const TargetType& target); + + /** + * Ordinary feed backward pass of a neural network. + * + * @param input The propagated input activation. + * @param target The target vector. + * @param output The calculated error. + */ + template + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); + + //! Get the output parameter. + OutputDataType& OutputParameter() const { return outputParameter; } + //! Modify the output parameter. + OutputDataType& OutputParameter() { return outputParameter; } + + /** + * Serialize the loss function. + */ + template + void serialize(Archive& ar, const unsigned int /* version */); + + private: + //! Locally-stored output parameter object. + OutputDataType outputParameter; +}; // class HingeEmbeddingLoss + +} // namespace ann +} // namespace mlpack + +// include implementation +#include "hinge_embedding_loss_impl.hpp" + +#endif diff --git a/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss_impl.hpp new file mode 100644 index 0000000000..b0e71632cb --- /dev/null +++ b/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss_impl.hpp @@ -0,0 +1,60 @@ +/** + * @file hinge_embedding_loss_impl.hpp + * @author Lakshya Ojha + * + * Implementation of the Hinge Embedding loss function. + * + * 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_METHODS_ANN_LOSS_FUNCTION_HINGE_EMBEDDING_LOSS_IMPL_HPP +#define MLPACK_METHODS_ANN_LOSS_FUNCTION_HINGE_EMBEDDING_LOSS_IMPL_HPP + +// In case it hasn't yet been included. +#include "hinge_embedding_loss.hpp" + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +template +HingeEmbeddingLoss::HingeEmbeddingLoss() +{ + // Nothing to do here. +} + +template +template +double HingeEmbeddingLoss::Forward( + const InputType& input, const TargetType& target) +{ + TargetType temp = target - (target == 0); + return (arma::accu(arma::max(1-input % temp, 0.))) / target.n_elem; +} + +template +template +void HingeEmbeddingLoss::Backward( + const InputType& input, + const TargetType& target, + OutputType& output) +{ + TargetType temp = target - (target == 0); + output = (input < 1 / temp) % -temp; +} + +template +template +void HingeEmbeddingLoss::serialize( + Archive& /* ar */, + const unsigned int /* version */) +{ + // Nothing to do here. +} + +} // namespace ann +} // namespace mlpack + +#endif diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index ce902f4e43..6df0b15fc4 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -510,4 +511,43 @@ BOOST_AUTO_TEST_CASE(LogCoshLossTest) BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); } + +/** + * Simple test for the Hinge Embedding loss function. + */ +BOOST_AUTO_TEST_CASE(HingeEmbeddingLossTest) +{ + arma::mat input, target, output; + double loss; + HingeEmbeddingLoss<> module; + + // Test the Forward function. Loss should be 0 if input = target. + input = arma::ones(10, 1); + target = arma::ones(10, 1); + loss = module.Forward(input, target); + BOOST_REQUIRE_EQUAL(loss, 0); + + // Test the Backward function for input = target. + module.Backward(input, target, output); + for (double el : output) + { + // For input = target we should get 0.0 everywhere. + BOOST_REQUIRE_CLOSE(el, 0.0, 1e-5); + } + + BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); + BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); + + // Test the Forward function. Loss should be 0.84. + input = arma::mat("0.1 0.8 0.6 0.0 0.5"); + target = arma::mat("0 1.0 1.0 0 0"); + loss = module.Forward(input, target); + BOOST_REQUIRE_CLOSE(loss, 0.84, 1e-3); + + // Test the Backward function. + module.Backward(input, target, output); + BOOST_REQUIRE_CLOSE(arma::accu(output), -2, 1e-3); + BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); + BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); +} BOOST_AUTO_TEST_SUITE_END(); From 1919522431da83c8cd1b9a298741adb4bbface34 Mon Sep 17 00:00:00 2001 From: Mrityunjay Tripathi Date: Wed, 18 Mar 2020 11:12:13 +0530 Subject: [PATCH 169/265] style fix --- src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp index 356da2633c..e638a1eb22 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp @@ -56,10 +56,7 @@ void HuberLoss::Backward( const double absError = std::abs(target[i] - input[i]); output[i] = absError > delta ? - delta * (target[i] - input[i]) / absError : input[i] - target[i]; - if (mean) - { - output[i] /= output.n_elem; - } + if (mean) output[i] /= output.n_elem; } } From a555f47d41756142cdd3b5ca20d8b49d8f8ff01a Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Wed, 18 Mar 2020 13:25:59 +0200 Subject: [PATCH 170/265] Changed variable names int the test file --- src/mlpack/tests/loss_functions_test.cpp | 26 ++++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index f4e007507c..8402212e2b 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -585,37 +585,37 @@ BOOST_AUTO_TEST_CASE(HingeEmbeddingLossTest) */ BOOST_AUTO_TEST_CASE(MarginRankingLossTest) { - arma::mat x1, x2, y, output; + arma::mat input1, input2, target, output; MarginRankingLoss<> module; // Test the Forward function on a user generator input and compare it against // the manually calculated result. - x1 = arma::mat("1 2 5 7 -1 -3"); - x2 = arma::mat("-1 3 -4 11 3 -3"); - y = arma::mat("1 -1 -1 1 -1 1"); - double error = module.Forward(x1, x2, y); + input1 = arma::mat("1 2 5 7 -1 -3"); + input2 = arma::mat("-1 3 -4 11 3 -3"); + target = arma::mat("1 -1 -1 1 -1 1"); + double error = module.Forward(input1, input2, target); // Computed using torch.nn.functional.margin_ranking_loss() BOOST_REQUIRE_CLOSE(error, 2.66667, 1e-3); // Test the Backward function. - module.Backward(x1, x2, y, output); + module.Backward(input1, input2, target, output); CheckMatrices(output, arma::mat("-0.000000 0.166667 -1.500000 0.666667 " "0.000000 -0.000000"), 1e-3); - BOOST_REQUIRE_EQUAL(output.n_rows, y.n_rows); - BOOST_REQUIRE_EQUAL(output.n_cols, y.n_cols); + BOOST_REQUIRE_EQUAL(output.n_rows, target.n_rows); + BOOST_REQUIRE_EQUAL(output.n_cols, target.n_cols); // Test the error function on another input. - x1 = arma::mat("0.4287 -1.6208 -1.5006 -0.4473 1.5208 -4.5184 9.3574 " + input1 = arma::mat("0.4287 -1.6208 -1.5006 -0.4473 1.5208 -4.5184 9.3574 " "-4.8090 4.3455 5.2070"); - x2 = arma::mat("-4.5288 -9.2766 -0.5882 -5.6643 -6.0175 8.8506 3.4759 " + input2 = arma::mat("-4.5288 -9.2766 -0.5882 -5.6643 -6.0175 8.8506 3.4759 " "-9.4886 2.2755 8.4951"); - y = arma::mat("1 1 -1 1 -1 1 1 1 -1 1"); - error = module.Forward(x1, x2, y); + target = arma::mat("1 1 -1 1 -1 1 1 1 -1 1"); + error = module.Forward(input1, input2, target); BOOST_REQUIRE_CLOSE(error, 3.03530, 1e-3); // Test the Backward function on the second input. - module.Backward(x1, x2, y, output); + module.Backward(input1, input2, target, output); CheckMatrices(output, arma::mat("0.000000 0.000000 0.091240 0.000000 " "-0.753830 1.336900 0.000000 0.000000 -0.207000 0.328810"), 1e-6); From cdbdbe70c63915f0299d2e73c854127e82ec9978 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Wed, 18 Mar 2020 13:42:39 +0200 Subject: [PATCH 171/265] Rebuild From 237828ff619aff7144b260ff1101ad91f8acc8c4 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Wed, 18 Mar 2020 09:18:44 -0400 Subject: [PATCH 172/265] Update tutorial to remove transpose parameter. --- doc/guide/formats.hpp | 56 ++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/doc/guide/formats.hpp b/doc/guide/formats.hpp index acf8a00c20..e7f216aae3 100644 --- a/doc/guide/formats.hpp +++ b/doc/guide/formats.hpp @@ -330,39 +330,46 @@ mlpack's image saving/loading functionality is based on [stb/](https://github.co Image utilities supports loading and saving of images. -It supports filetypes "jpg", "png", "tga","bmp", "psd", "gif", "hdr", "pic", "pnm" for loading and "jpg", "png", "tga", "bmp", "hdr" for saving. +It supports filetypes "jpg", "png", "tga", "bmp", "psd", "gif", "hdr", "pic", +"pnm" for loading and "jpg", "png", "tga", "bmp", "hdr" for saving. -The datatype associated is unsigned char to support RGB values in the range 1-255. To feed data into the network typecast of `arma::Mat` may be required. Images are stored in matrix as (width * height * channels, NumberOfImages). Therefore imageMatrix.col(0) would be the first image if images are loaded in imageMatrix. +The datatype associated is unsigned char to support RGB values in the range +1-255. To feed data into the network typecast of `arma::Mat` may be required. +Images are stored in the matrix as (width * height * channels, NumberOfImages). +Therefore @c imageMatrix.col(0) would be the first image if images are loaded in +@c imageMatrix. @section imageinfo_api_imagetut Accessing Metadata of Images: ImageInfo ImageInfo class contains the metadata of the images. @code ImageInfo(const size_t width, - const size_t height, - const size_t channels); + const size_t height, + const size_t channels, + const size_t quality = 90); @endcode -Other public memebers include: - - flipVertical Flip the image vertical upon loading. - - quality Compression of the image if saved as jpg (0-100). + +The @c quality member denotes the compression of the image if it is saved as +`jpg`; it takes values from 0 to 100. @section load_api_imagetut Loading Images in C++ - Standalone loading of images. + @code - template - bool Load(const std::string& filename, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal, - const bool transpose); +template +bool Load(const std::string& filename, + arma::Mat& matrix, + ImageInfo& info, + const bool fatal); @endcode -Loading a test image. It also fills up the ImageInfo class object. +The example below loads a test image. It also fills up the ImageInfo class +object. + @code data::ImageInfo info; -data::Load("test_image.png", matrix, info, false, true); +data::Load("test_image.png", matrix, info, false); @endcode ImageInfo requires height, width, number of channels of the image. @@ -377,18 +384,17 @@ More than one image can be loaded into the same matrix. Loading multiple images: @code - template - bool Load(const std::vector& files, - arma::Mat& matrix, - ImageInfo& info, - const bool fatal, - const bool transpose); +template +bool Load(const std::vector& files, + arma::Mat& matrix, + ImageInfo& info, + const bool fatal); @endcode @code - data::ImageInfo info; - std::vector> files{"test_image1.bmp","test_image2.bmp"}; - data::load(files, matrix, info, false, true); +data::ImageInfo info; +std::vector> files{"test_image1.bmp","test_image2.bmp"}; +data::Load(files, matrix, info, false); @endcode @section save_api_imagetut Saving Images in C++ From 6f7f916067eb5df15f7969fad0ebfe7b77d66d59 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Wed, 18 Mar 2020 10:42:31 -0400 Subject: [PATCH 173/265] Re-add default value. --- src/mlpack/core/data/save.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/core/data/save.hpp b/src/mlpack/core/data/save.hpp index a8cdaf4e75..0fb3fe8438 100644 --- a/src/mlpack/core/data/save.hpp +++ b/src/mlpack/core/data/save.hpp @@ -129,7 +129,7 @@ bool Save(const std::vector& files, bool SaveImage(const std::string& filename, arma::Mat& image, ImageInfo& info, - const bool fatal); + const bool fatal = false); } // namespace data } // namespace mlpack From 888631202ae66e9b94424e6b0bd3ff26a2cfa008 Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Wed, 18 Mar 2020 23:52:56 +0530 Subject: [PATCH 174/265] Set HAS_STB --- CMakeLists.txt | 1 + src/mlpack/methods/preprocess/CMakeLists.txt | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dc6d0beb8..817a947b6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -328,6 +328,7 @@ if (NOT STB_IMAGE_FOUND) install(FILES "${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") install(FILES "${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image_write.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") add_definitions(-DHAS_STB) + set(HAS_STB 1) set(STB_AVAILABLE "1") else () message(WARNING diff --git a/src/mlpack/methods/preprocess/CMakeLists.txt b/src/mlpack/methods/preprocess/CMakeLists.txt index 48d0378614..4faccbf6d6 100644 --- a/src/mlpack/methods/preprocess/CMakeLists.txt +++ b/src/mlpack/methods/preprocess/CMakeLists.txt @@ -42,7 +42,7 @@ add_cli_executable(preprocess_scale) add_python_binding(preprocess_scale) add_markdown_docs(preprocess_scale "cli;python" "preprocessing") -if (NOT HAS_STB) +if (HAS_STB) add_cli_executable(image_converter) add_python_binding(limage_converter) add_markdown_docs(image_converter "cli;python" "preprocessing") From 19f5766f12c4f24dbec1573a7902b172badb58a5 Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Thu, 19 Mar 2020 00:05:24 +0530 Subject: [PATCH 175/265] HAS_STB DOESN'T WORK --- src/mlpack/methods/preprocess/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/preprocess/CMakeLists.txt b/src/mlpack/methods/preprocess/CMakeLists.txt index 4faccbf6d6..48d0378614 100644 --- a/src/mlpack/methods/preprocess/CMakeLists.txt +++ b/src/mlpack/methods/preprocess/CMakeLists.txt @@ -42,7 +42,7 @@ add_cli_executable(preprocess_scale) add_python_binding(preprocess_scale) add_markdown_docs(preprocess_scale "cli;python" "preprocessing") -if (HAS_STB) +if (NOT HAS_STB) add_cli_executable(image_converter) add_python_binding(limage_converter) add_markdown_docs(image_converter "cli;python" "preprocessing") From 2242743a5f8c70634d4305f46df04c415feabeab Mon Sep 17 00:00:00 2001 From: adithya-tp Date: Thu, 19 Mar 2020 11:32:58 +0400 Subject: [PATCH 176/265] Minor code clean-up --- doc/tutorials/ann/ann.txt | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/doc/tutorials/ann/ann.txt b/doc/tutorials/ann/ann.txt index 43188d422d..82011e0795 100644 --- a/doc/tutorials/ann/ann.txt +++ b/doc/tutorials/ann/ann.txt @@ -256,11 +256,7 @@ int main() Compute the error between predictions and testLabels, now that we have the desired predictions. */ - size_t correct = 0; - for (size_t i = 0; i < testData.n_cols; i++) - { - correct = arma::accu(prediction == testLabels); - } + size_t correct = arma::accu(prediction == testLabels); double classificationError = 1 - double(correct) / testData.n_cols; // Print out the classification error for the testing dataset. From 0524c11b40b0cb458035aa85fd6aa28c9f21d6ad Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Fri, 20 Mar 2020 22:06:37 +0530 Subject: [PATCH 177/265] Removed tranpose option --- CMakeLists.txt | 1 - src/mlpack/core/data/load.hpp | 9 ++-- src/mlpack/core/data/load_image.cpp | 8 +-- src/mlpack/core/data/load_image_impl.hpp | 12 ++--- src/mlpack/core/data/save.hpp | 9 ++-- src/mlpack/core/data/save_image.cpp | 8 +-- src/mlpack/core/data/save_impl.hpp | 10 ++-- src/mlpack/methods/preprocess/CMakeLists.txt | 2 +- .../preprocess/image_converter_main.cpp | 15 ++---- src/mlpack/tests/image_load_test.cpp | 52 ++----------------- .../tests/main_tests/image_converter_test.cpp | 25 --------- 11 files changed, 29 insertions(+), 122 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 817a947b6e..4dc6d0beb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -328,7 +328,6 @@ if (NOT STB_IMAGE_FOUND) install(FILES "${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") install(FILES "${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image_write.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") add_definitions(-DHAS_STB) - set(HAS_STB 1) set(STB_AVAILABLE "1") else () message(WARNING diff --git a/src/mlpack/core/data/load.hpp b/src/mlpack/core/data/load.hpp index 586ddfcf16..256163f72b 100644 --- a/src/mlpack/core/data/load.hpp +++ b/src/mlpack/core/data/load.hpp @@ -305,8 +305,7 @@ template bool Load(const std::string& filename, arma::Mat& matrix, ImageInfo& info, - const bool fatal = false, - const bool transpose = true); + const bool fatal = false); /** * Load the image file into the given matrix. @@ -322,15 +321,13 @@ template bool Load(const std::vector& files, arma::Mat& matrix, ImageInfo& info, - const bool fatal = false, - const bool transpose = true); + 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, - const bool transpose = true); + const bool fatal = false); } // namespace data } // namespace mlpack diff --git a/src/mlpack/core/data/load_image.cpp b/src/mlpack/core/data/load_image.cpp index 53922628d5..2796e9a1f4 100644 --- a/src/mlpack/core/data/load_image.cpp +++ b/src/mlpack/core/data/load_image.cpp @@ -23,8 +23,7 @@ namespace data { bool LoadImage(const std::string& filename, arma::Mat& matrix, ImageInfo& info, - const bool fatal, - const bool transpose) + const bool fatal) { unsigned char* image; @@ -49,8 +48,6 @@ bool LoadImage(const std::string& filename, return false; } - stbi_set_flip_vertically_on_load(transpose); - // Temporary variables needed as stb_image.h supports int parameters. int tempWidth, tempHeight, tempChannels; @@ -106,8 +103,7 @@ namespace data { bool LoadImage(const std::string& /* filename */, arma::Mat& /* matrix */, ImageInfo& /* info */, - const bool fatal, - const bool transpose) + const bool fatal) { if (fatal) { diff --git a/src/mlpack/core/data/load_image_impl.hpp b/src/mlpack/core/data/load_image_impl.hpp index 43540504e4..b5be00861b 100644 --- a/src/mlpack/core/data/load_image_impl.hpp +++ b/src/mlpack/core/data/load_image_impl.hpp @@ -24,15 +24,14 @@ template bool Load(const std::string& filename, arma::Mat& matrix, ImageInfo& info, - const bool fatal, - const bool transpose) + const bool fatal) { Timer::Start("loading_image"); // STB loads into unsigned char matrices, so we may have to convert once // loaded. arma::Mat tempMatrix; - const bool result = LoadImage(filename, tempMatrix, info, fatal, transpose); + const bool result = LoadImage(filename, tempMatrix, info, fatal); // If fatal is true, then the program will have already thrown an exception. if (!result) @@ -51,8 +50,7 @@ template bool Load(const std::vector& files, arma::Mat& matrix, ImageInfo& info, - const bool fatal, - const bool transpose) + const bool fatal) { if (files.size() == 0) { @@ -68,7 +66,7 @@ bool Load(const std::vector& files, } arma::Mat img; - bool status = LoadImage(files[0], img, info, fatal, transpose); + bool status = LoadImage(files[0], img, info, fatal); if (!status) return false; @@ -82,7 +80,7 @@ bool Load(const std::vector& files, { arma::Mat colImg(tmpMatrix.colptr(i), tmpMatrix.n_rows, 1, false, true); - status = LoadImage(files[i], colImg, info, fatal, transpose); + status = LoadImage(files[i], colImg, info, fatal); if (!status) return false; diff --git a/src/mlpack/core/data/save.hpp b/src/mlpack/core/data/save.hpp index de1860d0ac..0fb3fe8438 100644 --- a/src/mlpack/core/data/save.hpp +++ b/src/mlpack/core/data/save.hpp @@ -105,8 +105,7 @@ template bool Save(const std::string& filename, arma::Mat& matrix, ImageInfo& info, - const bool fatal = false, - const bool transpose = true); + const bool fatal = false); /** * Save the image file from the given matrix. @@ -122,8 +121,7 @@ template bool Save(const std::vector& files, arma::Mat& matrix, ImageInfo& info, - const bool fatal = false, - const bool transpose = true); + const bool fatal = false); /** * Helper function to save files. Implementation in save_image.cpp. @@ -131,8 +129,7 @@ bool Save(const std::vector& files, bool SaveImage(const std::string& filename, arma::Mat& image, ImageInfo& info, - const bool fatal = false, - const bool transpose = true); + 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.cpp index b97cda57e8..c1802073e4 100644 --- a/src/mlpack/core/data/save_image.cpp +++ b/src/mlpack/core/data/save_image.cpp @@ -22,8 +22,7 @@ namespace data { bool SaveImage(const std::string& filename, arma::Mat& image, ImageInfo& info, - const bool fatal, - const bool transpose) + const bool fatal) { // Check to see if the file type is supported. if (!ImageFormatSupported(filename, true)) @@ -55,8 +54,6 @@ bool SaveImage(const std::string& filename, Log::Warn << "Only the first image will be saved!" << std::endl; } - stbi_flip_vertically_on_write(transpose); - bool status = false; unsigned char* imageMem = image.memptr(); @@ -116,8 +113,7 @@ namespace data { bool SaveImage(const std::string& /* filename */, arma::Mat& /* image */, ImageInfo& /* info */, - const bool fatal, - const bool transpose) + const bool fatal) { if (fatal) { diff --git a/src/mlpack/core/data/save_impl.hpp b/src/mlpack/core/data/save_impl.hpp index 8750ac063d..516e0aaf93 100644 --- a/src/mlpack/core/data/save_impl.hpp +++ b/src/mlpack/core/data/save_impl.hpp @@ -295,14 +295,13 @@ template bool Save(const std::string& filename, arma::Mat& matrix, ImageInfo& info, - const bool fatal, - const bool transpose) + const bool fatal) { arma::Mat tmpMatrix = arma::conv_to>::from(matrix); // Call out to .cpp implementation. - return SaveImage(filename, tmpMatrix, info, fatal, transpose); + return SaveImage(filename, tmpMatrix, info, fatal); } // Image saving API for multiple files. @@ -310,8 +309,7 @@ template bool Save(const std::vector& files, arma::Mat& matrix, ImageInfo& info, - const bool fatal, - const bool transpose) + const bool fatal) { if (files.size() == 0) { @@ -336,7 +334,7 @@ bool Save(const std::vector& files, { arma::Mat colImg(matrix.colptr(i), matrix.n_rows, 1, false, true); - status &= Save(files[i], colImg, info, fatal, transpose); + status &= Save(files[i], colImg, info, fatal); } return status; diff --git a/src/mlpack/methods/preprocess/CMakeLists.txt b/src/mlpack/methods/preprocess/CMakeLists.txt index 48d0378614..2f9f4b0725 100644 --- a/src/mlpack/methods/preprocess/CMakeLists.txt +++ b/src/mlpack/methods/preprocess/CMakeLists.txt @@ -42,7 +42,7 @@ add_cli_executable(preprocess_scale) add_python_binding(preprocess_scale) add_markdown_docs(preprocess_scale "cli;python" "preprocessing") -if (NOT HAS_STB) +if (STB_AVAILABLE) add_cli_executable(image_converter) add_python_binding(limage_converter) add_markdown_docs(image_converter "cli;python" "preprocessing") diff --git a/src/mlpack/methods/preprocess/image_converter_main.cpp b/src/mlpack/methods/preprocess/image_converter_main.cpp index f8e26755b1..ac90eace66 100644 --- a/src/mlpack/methods/preprocess/image_converter_main.cpp +++ b/src/mlpack/methods/preprocess/image_converter_main.cpp @@ -33,7 +33,7 @@ PROGRAM_INFO("Image Converter", PRINT_PARAM_STRING("channel") + " of the images that needs to be loaded. " "\n" "There are other options too, that can be specified such as " + - PRINT_PARAM_STRING("quality") + " and " + PRINT_PARAM_STRING("transpose") + PRINT_PARAM_STRING("quality") + ".\n\n" + "You can also provide a dataset and save them as images using " + PRINT_PARAM_STRING("dataset") + " and " + PRINT_PARAM_STRING("save") + @@ -45,12 +45,7 @@ PROGRAM_INFO("Image Converter", " An example to save an image is :" + "\n\n" + PRINT_CALL("image_converter", "input", "X", "height", 256, "width", 256, - "channel", 3, "dataset", "Y", "save", true) + - "\n\n" + - " An example to load an image and also flipping it while loading is :" - + "\n\n" + - PRINT_CALL("image_converter", "input", "X", "height", 256, "width", 256, - "channel", 3, "output", "Y", "transpose", true), + "channel", 3, "dataset", "Y", "save", true), SEE_ALSO("@preprocess_binarize", "#preprocess_binarize"), SEE_ALSO("@preprocess_describe", "#preprocess_describe"), SEE_ALSO("@preprocess_imputer", "#preprocess_imputer")); @@ -67,7 +62,6 @@ PARAM_MATRIX_OUT("output", "Matrix to save images data to.", "o"); PARAM_INT_IN("quality", "Compression of the image if saved as jpg (0-100).", "q", 90); -PARAM_FLAG("transpose", "Loaded dataset to be transposed", "t"); PARAM_INT_IN("height", "Height of the images", "H", 256); PARAM_FLAG("save", "Save a dataset as images", "s"); @@ -119,7 +113,7 @@ static void mlpackMain() arma::mat out; if (!CLI::HasParam("save")) { - Load(fileNames, out, *info, true, !CLI::HasParam("transpose")); + Load(fileNames, out, *info, true); if (CLI::HasParam("output")) CLI::GetParam("output") = std::move(out); } @@ -130,8 +124,7 @@ static void mlpackMain() throw std::runtime_error("Please provide a input matrix to save " "images from"); } - Save(fileNames, CLI::GetParam ("dataset"), *info, true, - !CLI::HasParam("transpose")); + Save(fileNames, CLI::GetParam ("dataset"), *info, true); } if (CLI::HasParam("output_model")) CLI::GetParam ("output_model") = info; diff --git a/src/mlpack/tests/image_load_test.cpp b/src/mlpack/tests/image_load_test.cpp index 213c01e46e..396cb536da 100644 --- a/src/mlpack/tests/image_load_test.cpp +++ b/src/mlpack/tests/image_load_test.cpp @@ -69,27 +69,7 @@ BOOST_AUTO_TEST_CASE(SaveImageAPITest) BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows); for (size_t i = 0; i < im1.n_elem; ++i) BOOST_REQUIRE_EQUAL(im1[i], im2[i]); -} - -/** - * Test if the image is saved correctly using API for transpose. - */ -BOOST_AUTO_TEST_CASE(SaveImageTransposeAPITest) -{ - data::ImageInfo info(5, 5, 3, 90); - - arma::Mat im1; - size_t dimension = info.Width() * info.Height() * info.Channels(); - im1 = arma::randi>(dimension, 1); - BOOST_REQUIRE(data::Save("APITest.bmp", im1, info, false, false) == true); - - arma::Mat im2; - BOOST_REQUIRE(data::Load("APITest.bmp", im2, info, false, false) == true); - - BOOST_REQUIRE_EQUAL(im1.n_cols, im2.n_cols); - BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows); - for (size_t i = 0; i < im1.n_elem; ++i) - BOOST_REQUIRE_EQUAL(im1[i], im2[i]); + remove("APITest.bmp"); } /** @@ -101,34 +81,11 @@ BOOST_AUTO_TEST_CASE(LoadVectorImageAPITest) arma::Mat matrix; data::ImageInfo info; std::vector files = {"test_image.png", "test_image.png"}; - BOOST_REQUIRE(data::Load(files, matrix, info, false, - true) == true); + BOOST_REQUIRE(data::Load(files, matrix, info, false) == true); BOOST_REQUIRE_EQUAL(matrix.n_rows, 50 * 50 * 3); // width * height * channels. BOOST_REQUIRE_EQUAL(matrix.n_cols, 2); } -/** - * Test if the image is saved correctly using vector saving API for transpose. - */ -BOOST_AUTO_TEST_CASE(SaveImageVectorAPITest) -{ - data::ImageInfo info(5, 5, 3); - - arma::Mat im1; - size_t dimension = info.Width() * info.Height() * info.Channels(); - im1 = arma::randi>(dimension, 2); - std::vector files = {"APITest1.bmp", "APITest2.bmp"}; - BOOST_REQUIRE(data::Save(files, im1, info, false, false) == true); - - arma::Mat im2; - BOOST_REQUIRE(data::Load(files, im2, info, false, false) == true); - - BOOST_REQUIRE_EQUAL(im1.n_cols, im2.n_cols); - BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows); - for (size_t i = 0; i < im1.n_elem; ++i) - BOOST_REQUIRE_EQUAL(im1[i], im2[i]); -} - /** * Test if the image is saved correctly using API for arma mat. */ @@ -140,15 +97,16 @@ BOOST_AUTO_TEST_CASE(SaveImageMatAPITest) size_t dimension = info.Width() * info.Height() * info.Channels(); im1 = arma::randi>(dimension, 1); arma::mat input = arma::conv_to::from(im1); - BOOST_REQUIRE(Save("APITest.bmp", input, info, false, false) == true); + BOOST_REQUIRE(Save("APITest.bmp", input, info, false) == true); arma::mat output; - BOOST_REQUIRE(Load("APITest.bmp", output, info, false, false) == true); + BOOST_REQUIRE(Load("APITest.bmp", output, info, false) == true); BOOST_REQUIRE_EQUAL(input.n_cols, output.n_cols); BOOST_REQUIRE_EQUAL(input.n_rows, output.n_rows); for (size_t i = 0; i < input.n_elem; ++i) BOOST_REQUIRE_CLOSE(input[i], output[i], 1e-5); + remove("APITest.bmp"); } /** diff --git a/src/mlpack/tests/main_tests/image_converter_test.cpp b/src/mlpack/tests/main_tests/image_converter_test.cpp index bd34831c72..dac1924ad6 100644 --- a/src/mlpack/tests/main_tests/image_converter_test.cpp +++ b/src/mlpack/tests/main_tests/image_converter_test.cpp @@ -111,31 +111,6 @@ BOOST_AUTO_TEST_CASE(SavedModelTest) CheckMatrices(randomOutput, savedOutput); } -/** - * Check transpose option give two different output. - */ -BOOST_AUTO_TEST_CASE(TransposeTest) -{ - SetInputParam>("input", {"test_image.png", "test_image.png"}); - SetInputParam("height", 50); - SetInputParam("width", 50); - SetInputParam("channel", 3); - - mlpackMain(); - arma::mat normalOutput = CLI::GetParam("output"); - - SetInputParam>("input", {"test_image.png", "test_image.png"}); - SetInputParam("input_model", - CLI::GetParam("output_model")); - SetInputParam("transpose", true); - mlpackMain(); - arma::mat transposeOutput = CLI::GetParam("output"); - - CheckMatricesNotEqual(normalOutput, transposeOutput); - BOOST_REQUIRE_EQUAL(normalOutput.n_rows, transposeOutput.n_rows); - BOOST_REQUIRE_EQUAL(normalOutput.n_cols, transposeOutput.n_cols); -} - /** * Check whether binding throws error if height, width or channel are not * specified. From 147d4d6db39763b5555519e9d282d26e6068d023 Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Sun, 22 Mar 2020 17:03:00 +0530 Subject: [PATCH 178/265] Implemented R2 Score --- src/mlpack/core/cv/metrics/CMakeLists.txt | 2 + src/mlpack/core/cv/metrics/r2_score.hpp | 71 ++++++++++++++++++++ src/mlpack/core/cv/metrics/r2_score_impl.hpp | 60 +++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 src/mlpack/core/cv/metrics/r2_score.hpp create mode 100644 src/mlpack/core/cv/metrics/r2_score_impl.hpp diff --git a/src/mlpack/core/cv/metrics/CMakeLists.txt b/src/mlpack/core/cv/metrics/CMakeLists.txt index 4cf1027874..b9edacaf9a 100644 --- a/src/mlpack/core/cv/metrics/CMakeLists.txt +++ b/src/mlpack/core/cv/metrics/CMakeLists.txt @@ -13,6 +13,8 @@ set(SOURCES precision_impl.hpp recall.hpp recall_impl.hpp + r2_score.hpp + r2_score_impl.hpp ) # Add directory name to sources. diff --git a/src/mlpack/core/cv/metrics/r2_score.hpp b/src/mlpack/core/cv/metrics/r2_score.hpp new file mode 100644 index 0000000000..a1ff04bf0f --- /dev/null +++ b/src/mlpack/core/cv/metrics/r2_score.hpp @@ -0,0 +1,71 @@ +/** + * @file r2_score.hpp + * @author Bisakh Mondal + * + * The R^2 (Coefficient of determination) regression metric. + * + * 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_CV_METRICS_R2SCORE_HPP +#define MLPACK_CORE_CV_METRICS_R2SCORE_HPP + +#include + +namespace mlpack { +namespace cv { + +/** + * The R2Score is a metric of performance for regression algorithms + * that represents the proportion of variance (of y) that has been + * explained by the independent variables in the model. It provides + * an indication of goodness of fit and therefore a measure of how + * well unseen samples are likely to be predicted by the model, + * through the proportion of explained variance. + * As R2Score is dataset dependent it can have wide range of values, + * best possible score is @f$R^2 =1.0@f$, and it can be negative too for an + * arbitraryly worse model. For a model which predicts exactly the expected + * value of y, disregarding the input features, gets a R2Score equals to 0.0. + * If a model predicts @f$ \hat{y}_i $@f of the @f$ i $@f-th sample for a true + * @f$ y_i $@f for total n samples, the R2Score is calculated by + * @f{eqnarray*}{ + * R^{2} \left( y, \hat{y} \right) &=& 1-\frac{\sum_{i=1}^{n} + * \left( y_i - \hat{y_i} \right)^2 } + * {\sum_{i=1}^{n} \left( y_i - \bar{y}\right)^2}\\ + * @f} + * where @f$ \bar{y} = frac{1}{y}\sum_{i=1}^{n} y_i $@f. + * For example, a model having R2Score = 0.85, explains 85 \% variability of + * the response data around its mean. + */ +class R2Score +{ + public: + /** + * Run prediction and calculate the R squared error. + * + * @param model A regression model. + * @param data Column-major data containing test items. + * @param responses Ground truth (correct) target values for the test items, + * should be either a row vector or a column-major matrix. + */ + template + static double Evaluate(MLAlgorithm& model, + const DataType& data, + const ResponsesType& responses); + + /** + * Information for hyper-parameter tuning code. It indicates that we want + * to maximize the measurement. + */ + static const bool NeedsMinimization = false; +}; + +} // namespace cv +} // namespace mlpack + +// Include implementation. +#include "r2_score_impl.hpp" + +#endif diff --git a/src/mlpack/core/cv/metrics/r2_score_impl.hpp b/src/mlpack/core/cv/metrics/r2_score_impl.hpp new file mode 100644 index 0000000000..b047709b12 --- /dev/null +++ b/src/mlpack/core/cv/metrics/r2_score_impl.hpp @@ -0,0 +1,60 @@ +/** + * @file r2_score_impl.hpp + * @author Bisakh Mondal + * + * The implementation of the class R2Score. + * + * 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_CV_METRICS_R2SCORE_IMPL_HPP +#define MLPACK_CORE_CV_METRICS_R2SCORE_IMPL_HPP + +namespace mlpack { +namespace cv { + +template +double R2Score::Evaluate(MLAlgorithm& model, + const DataType& data, + const ResponsesType& responses) +{ + if (data.n_cols != responses.n_cols) + { + std::ostringstream oss; + oss << "R2Score::Evaluate(): number of points (" << data.n_cols << ") " + << "does not match number of responses (" << responses.n_cols << ")!" + << std::endl; + throw std::invalid_argument(oss.str()); + } + + ResponsesType predictedResponses; + // Taking Predicted Output from the model. + model.Predict(data, predictedResponses); + // Mean value of response. + double mean_responses = arma::mean(responses); + + // Calculate the numerator i.e. residual sum of squares. + double ss_res = arma::accu(arma::square(responses, predictedResponses)); + + // Calculate the denominator i.e.total sum of squares. + double ss_tot = arma::accu(arma::square(responses, mean_responses)); + + // Handling undefined R2Score when both denominator and numerator is 0.0 + if (ss_res == 0.0) + { + if (ss_tot !=0.0) + return 1.0; + else + return DBL_MIN; + + } + + return 1 - ss_res/ss_tot; +} + +} // namespace cv +} // namespace mlpack + +#endif From ab261514f744c2243fddabc103dbef084610754e Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Sun, 22 Mar 2020 17:34:09 +0530 Subject: [PATCH 179/265] R2Score tests added --- src/mlpack/tests/cv_test.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/mlpack/tests/cv_test.cpp b/src/mlpack/tests/cv_test.cpp index b4182ac1fb..fe7f0aa8d2 100644 --- a/src/mlpack/tests/cv_test.cpp +++ b/src/mlpack/tests/cv_test.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -164,6 +165,29 @@ BOOST_AUTO_TEST_CASE(MSETest) BOOST_REQUIRE_CLOSE(MSE::Evaluate(lr, data, responses), expectedMSE, 1e-5); } +/** + * Test the R squared metric (R2Score). + */ +BOOST_AUTO_TEST_CASE(R2ScoreTest) +{ + // Making two points that define the linear function f(x) = x - 1 + arma::mat trainingData("0 1"); + arma::rowvec trainingResponses("-1 0"); + + LinearRegression lr(trainingData, trainingResponses); + + // Making three responses that differ from the correct ones by 0, 1, and 2 + // respectively. Original Responses mean (1 + 2 + 3) / 3 = 2 + arma::mat data("2 3 4"); + arma::rowvec responses("1 3 5");// Mean_responses= (1+ 3 + 5) /3 = 3 + + double ss_reg = (0 + 1 * 1 + 2 * 2); + double ss_tot = ((1 - 2) * (1- 2) + (2 -2) * (2 - 2) + (3 -2) * (3 - 2)); + double expectedR2 = 1 - ss_reg / ss_tot; + + BOOST_REQUIRE_CLOSE(R2Score::Evaluate(lr, data, responses), expectedR2, 1e-5); +} + /** * Test the mean squared error with matrix responses. */ From c0d6259b4a9ee574cf3719da477f39ff26d5d90c Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Sun, 22 Mar 2020 20:08:12 +0530 Subject: [PATCH 180/265] Typo fixed --- src/mlpack/core/cv/metrics/r2_score_impl.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mlpack/core/cv/metrics/r2_score_impl.hpp b/src/mlpack/core/cv/metrics/r2_score_impl.hpp index b047709b12..6fecfac378 100644 --- a/src/mlpack/core/cv/metrics/r2_score_impl.hpp +++ b/src/mlpack/core/cv/metrics/r2_score_impl.hpp @@ -36,10 +36,10 @@ double R2Score::Evaluate(MLAlgorithm& model, double mean_responses = arma::mean(responses); // Calculate the numerator i.e. residual sum of squares. - double ss_res = arma::accu(arma::square(responses, predictedResponses)); + double ss_res = arma::accu(arma::square(responses - predictedResponses)); // Calculate the denominator i.e.total sum of squares. - double ss_tot = arma::accu(arma::square(responses, mean_responses)); + double ss_tot = arma::accu(arma::square(responses - mean_responses)); // Handling undefined R2Score when both denominator and numerator is 0.0 if (ss_res == 0.0) @@ -48,7 +48,6 @@ double R2Score::Evaluate(MLAlgorithm& model, return 1.0; else return DBL_MIN; - } return 1 - ss_res/ss_tot; From 56982ac742cdf18de065921e43eff883c0935120 Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Sun, 22 Mar 2020 20:12:41 +0530 Subject: [PATCH 181/265] Tests slighltly modified --- src/mlpack/tests/cv_test.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/mlpack/tests/cv_test.cpp b/src/mlpack/tests/cv_test.cpp index fe7f0aa8d2..8886f68c4b 100644 --- a/src/mlpack/tests/cv_test.cpp +++ b/src/mlpack/tests/cv_test.cpp @@ -176,13 +176,17 @@ BOOST_AUTO_TEST_CASE(R2ScoreTest) LinearRegression lr(trainingData, trainingResponses); - // Making three responses that differ from the correct ones by 0, 1, and 2 - // respectively. Original Responses mean (1 + 2 + 3) / 3 = 2 - arma::mat data("2 3 4"); - arma::rowvec responses("1 3 5");// Mean_responses= (1+ 3 + 5) /3 = 3 - - double ss_reg = (0 + 1 * 1 + 2 * 2); - double ss_tot = ((1 - 2) * (1- 2) + (2 -2) * (2 - 2) + (3 -2) * (3 - 2)); + // Making five responses that are the output of regression function f(x) + // with some responses having a slight deviation of 0.005 + // Mean Responses = (1 + 2 + 3 + 6 + 8)/5 = 4 + arma::mat data("2 3 4 7 9"); + arma::rowvec responses("1 2.005 3 6.005 8.005"); + + double ss_reg = (0 * 0 + 0.005 * 0.005 + 0 * 0 + + 0.005 * 0.005 + 0.005 *0.005); + double ss_tot = ((1 - 4) * (1- 4) + (2.005 - 4) * + (2.005 - 4) + (3 - 4) * (3 - 4) + (6.005 - 4) * + (6.005 - 4) + (8.005 - 4) * (8.005 -4)); double expectedR2 = 1 - ss_reg / ss_tot; BOOST_REQUIRE_CLOSE(R2Score::Evaluate(lr, data, responses), expectedR2, 1e-5); From 0f361925ad548f074a3eaebacc457d1831b64162 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 15:38:33 -0400 Subject: [PATCH 182/265] Delete any old pointers created before serialization. --- src/mlpack/core/arma_extend/Cube_extra_meat.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/mlpack/core/arma_extend/Cube_extra_meat.hpp b/src/mlpack/core/arma_extend/Cube_extra_meat.hpp index a2ebd94cb4..3f33bb59d8 100644 --- a/src/mlpack/core/arma_extend/Cube_extra_meat.hpp +++ b/src/mlpack/core/arma_extend/Cube_extra_meat.hpp @@ -19,6 +19,9 @@ void Cube::serialize(Archive& ar, const unsigned int /* version */) // mem_state will always be 0 on load, so we don't need to save it. if (Archive::is_loading::value) { + // Clean any mat pointers. + delete_mat(); + // Don't free if local memory is being used. if (mem_state == 0 && mem != NULL && old_n_elem > arma_config::mat_prealloc) { From e551c887ebba38935daece06d3d47b22ecb4780e Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 15:39:17 -0400 Subject: [PATCH 183/265] No need to allocate memory for the internally-held PCA object. --- .../data/scaler_methods/zca_whitening.hpp | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/mlpack/core/data/scaler_methods/zca_whitening.hpp b/src/mlpack/core/data/scaler_methods/zca_whitening.hpp index 0c78fb3495..80638de954 100644 --- a/src/mlpack/core/data/scaler_methods/zca_whitening.hpp +++ b/src/mlpack/core/data/scaler_methods/zca_whitening.hpp @@ -52,10 +52,7 @@ class ZCAWhitening * * @param eps Regularization parameter. */ - ZCAWhitening(double eps = 0.00005) - { - pca = new data::PCAWhitening(eps); - } + ZCAWhitening(double eps = 0.00005) : pca(eps) { } /** * Function to fit features, to find out the min max and scale. @@ -65,7 +62,7 @@ class ZCAWhitening template void Fit(const MatType& input) { - pca->Fit(input); + pca.Fit(input); } /** @@ -77,8 +74,8 @@ class ZCAWhitening template void Transform(const MatType& input, MatType& output) { - pca->Transform(input, output); - output = pca->EigenVectors() * output; + pca.Transform(input, output); + output = pca.EigenVectors() * output; } /** @@ -90,19 +87,19 @@ class ZCAWhitening template void InverseTransform(const MatType& input, MatType& output) { - output = inv(pca->EigenVectors()) * arma::diagmat(arma::sqrt( - pca->EigenValues())) * inv(pca->EigenVectors().t()) * input; - output = (output.each_col() + pca->ItemMean()); + output = inv(pca.EigenVectors()) * arma::diagmat(arma::sqrt( + pca.EigenValues())) * inv(pca.EigenVectors().t()) * input; + output = (output.each_col() + pca.ItemMean()); } //! Get the mean row vector. - const arma::vec& ItemMean() const { return pca->ItemMean(); } + const arma::vec& ItemMean() const { return pca.ItemMean(); } //! Get the eigenvalues vector. - const arma::vec& EigenValues() const { return pca->EigenValues(); } + const arma::vec& EigenValues() const { return pca.EigenValues(); } //! Get the eigenvector. - const arma::mat& EigenVectors() const { return pca->EigenVectors(); } + const arma::mat& EigenVectors() const { return pca.EigenVectors(); } //! Get the regularization parameter. - double Epsilon() const { return pca->Epsilon(); } + double Epsilon() const { return pca.Epsilon(); } template void serialize(Archive& ar, const unsigned int /* version */) @@ -112,7 +109,7 @@ class ZCAWhitening private: // A pointer to PcaWhitening Class. - PCAWhitening* pca; + PCAWhitening pca; }; // class ZCAWhitening } // namespace data From b4e37680b2e937b926256e650fcc00c234b6f4e4 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 15:39:37 -0400 Subject: [PATCH 184/265] Clean copy constructor and fix memory loss in serialization. --- src/mlpack/core/metrics/ip_metric_impl.hpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/mlpack/core/metrics/ip_metric_impl.hpp b/src/mlpack/core/metrics/ip_metric_impl.hpp index 777315a576..4c9091749c 100644 --- a/src/mlpack/core/metrics/ip_metric_impl.hpp +++ b/src/mlpack/core/metrics/ip_metric_impl.hpp @@ -49,7 +49,7 @@ IPMetric::~IPMetric() template IPMetric::IPMetric(const IPMetric& other) : - kernel(other.kernel), + kernel(!other.kernelOwner ? other.kernel : new KernelType(*other.kernel)), kernelOwner(other.kernelOwner) { // Nothing to do. @@ -90,7 +90,11 @@ void IPMetric::serialize(Archive& ar, // If we're loading, we need to allocate space for the kernel, and we will own // the kernel. if (Archive::is_loading::value) + { + if (kernelOwner) + delete kernel; kernelOwner = true; + } ar & BOOST_SERIALIZATION_NVP(kernel); } From 31c29505df5f797e800dde073dafbf8cd9759d0d Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 15:42:25 -0400 Subject: [PATCH 185/265] Fix various memory handling issues. Delete things that were allocated, be clear about ownership. --- .../core/tree/hollow_ball_bound_impl.hpp | 3 ++ src/mlpack/methods/ann/brnn.hpp | 6 ++-- src/mlpack/methods/ann/brnn_impl.hpp | 36 ++++++++++++++----- .../hoeffding_trees/hoeffding_tree.hpp | 5 ++- .../hoeffding_trees/hoeffding_tree_impl.hpp | 28 +++++++++++---- .../hoeffding_trees/hoeffding_tree_model.cpp | 6 ++++ .../hoeffding_trees/hoeffding_tree_model.hpp | 12 ------- 7 files changed, 66 insertions(+), 30 deletions(-) diff --git a/src/mlpack/core/tree/hollow_ball_bound_impl.hpp b/src/mlpack/core/tree/hollow_ball_bound_impl.hpp index e6d9737ee6..88e3e082ea 100644 --- a/src/mlpack/core/tree/hollow_ball_bound_impl.hpp +++ b/src/mlpack/core/tree/hollow_ball_bound_impl.hpp @@ -80,6 +80,9 @@ template HollowBallBound& HollowBallBound:: operator=(const HollowBallBound& other) { + if (ownsMetric) + delete metric; + radii = other.radii; center = other.center; hollowCenter = other.hollowCenter; diff --git a/src/mlpack/methods/ann/brnn.hpp b/src/mlpack/methods/ann/brnn.hpp index 57789336a9..7e042de6fd 100644 --- a/src/mlpack/methods/ann/brnn.hpp +++ b/src/mlpack/methods/ann/brnn.hpp @@ -73,10 +73,12 @@ class BRNN BRNN(const size_t rho, const bool single = false, OutputLayerType outputLayer = OutputLayerType(), - MergeLayerType mergeLayer = MergeLayerType(), - MergeOutputType mergeOutput = MergeOutputType(), + MergeLayerType* mergeLayer = new MergeLayerType(), + MergeOutputType* mergeOutput = new MergeOutputType(), InitializationRuleType initializeRule = InitializationRuleType()); + ~BRNN(); + /** * Check if the optimizer has MaxIterations() parameter, if it does * then check if it's value is less than the number of datapoints diff --git a/src/mlpack/methods/ann/brnn_impl.hpp b/src/mlpack/methods/ann/brnn_impl.hpp index 697bad7dfa..69e9020768 100644 --- a/src/mlpack/methods/ann/brnn_impl.hpp +++ b/src/mlpack/methods/ann/brnn_impl.hpp @@ -39,13 +39,13 @@ BRNN +BRNN::~BRNN() +{ + // Remove mergeLayer from the forward and backward RNNs so it doesn't get + // deleted. This assumes that mergeLayer is the last layer! + forwardRNN.network.pop_back(); + backwardRNN.network.pop_back(); + + // Clean up layers that we allocated. + boost::apply_visitor(DeleteVisitor(), mergeLayer); + boost::apply_visitor(DeleteVisitor(), mergeOutput); +} + template @@ -230,7 +246,7 @@ void BRNN( forwardRNN.network.back()), mergeLayer); boost::apply_visitor(AddVisitor( @@ -707,6 +725,8 @@ void BRNN& numericSplitIn = NumericSplitType(0), std::unordered_map>* - dimensionMappings = NULL); + dimensionMappings = NULL, + const bool copyDatasetInfo = true); /** * Construct a Hoeffding tree with no data and no information. Be sure to diff --git a/src/mlpack/methods/hoeffding_trees/hoeffding_tree_impl.hpp b/src/mlpack/methods/hoeffding_trees/hoeffding_tree_impl.hpp index c0f647fe4c..e1769e55b1 100644 --- a/src/mlpack/methods/hoeffding_trees/hoeffding_tree_impl.hpp +++ b/src/mlpack/methods/hoeffding_trees/hoeffding_tree_impl.hpp @@ -96,7 +96,8 @@ HoeffdingTree< categoricalSplitIn, const NumericSplitType& numericSplitIn, std::unordered_map>* - dimensionMappingsIn) : + dimensionMappingsIn, + const bool copyDatasetInfo) : dimensionMappings((dimensionMappingsIn != NULL) ? dimensionMappingsIn : new std::unordered_map>()), ownsMappings(dimensionMappingsIn == NULL), @@ -105,8 +106,9 @@ HoeffdingTree< maxSamples((maxSamples == 0) ? size_t(-1) : maxSamples), checkInterval(checkInterval), minSamples(minSamples), - datasetInfo(new data::DatasetInfo(datasetInfo)), - ownsInfo(true), + datasetInfo(copyDatasetInfo ? new data::DatasetInfo(datasetInfo) : + &datasetInfo), + ownsInfo(copyDatasetInfo), successProbability(successProbability), splitDimension(size_t(-1)), majorityClass(0), @@ -208,7 +210,18 @@ HoeffdingTree:: { // Copy each of the children. for (size_t i = 0; i < other.children.size(); ++i) + { children.push_back(new HoeffdingTree(*other.children[i])); + + // Delete copied datasetInfo and dimension mappings. + delete children[i]->datasetInfo; + children[i]->datasetInfo = this->datasetInfo; + children[i]->ownsInfo = false; + + delete children[i]->dimensionMappings; + children[i]->dimensionMappings = this->dimensionMappings; + children[i]->ownsMappings = false; + } } template(0, numClasses), - numericSplits[0], dimensionMappings)); + numericSplits[0], dimensionMappings, false)); } else if (numericSplits.size() == 0) { @@ -752,14 +765,14 @@ void HoeffdingTree< children.push_back(new HoeffdingTree(*datasetInfo, numClasses, successProbability, maxSamples, checkInterval, minSamples, categoricalSplits[0], NumericSplitType(numClasses), - dimensionMappings)); + dimensionMappings, false)); } else { // Pass both splits that we already have. children.push_back(new HoeffdingTree(*datasetInfo, numClasses, successProbability, maxSamples, checkInterval, minSamples, - categoricalSplits[0], numericSplits[0], dimensionMappings)); + categoricalSplits[0], numericSplits[0], dimensionMappings, false)); } children[i]->MajorityClass() = childMajorities[i]; @@ -874,7 +887,8 @@ void HoeffdingTree< { // The child doesn't actually own its own DatasetInfo. We do. The same // applies for the dimension mappings. - children[i]->ownsInfo = false; + if (children[i]->datasetInfo == datasetInfo) + children[i]->ownsInfo = false; children[i]->ownsMappings = false; } diff --git a/src/mlpack/methods/hoeffding_trees/hoeffding_tree_model.cpp b/src/mlpack/methods/hoeffding_trees/hoeffding_tree_model.cpp index 5a27cffa3b..55635f50f1 100644 --- a/src/mlpack/methods/hoeffding_trees/hoeffding_tree_model.cpp +++ b/src/mlpack/methods/hoeffding_trees/hoeffding_tree_model.cpp @@ -135,6 +135,12 @@ void HoeffdingTreeModel::BuildModel( const size_t bins, const size_t observationsBeforeBinning) { + // Clean memory, if needed. + delete giniHoeffdingTree; + delete giniBinaryTree; + delete infoHoeffdingTree; + delete infoBinaryTree; + // Depending on the type, create the tree. switch (type) { diff --git a/src/mlpack/methods/hoeffding_trees/hoeffding_tree_model.hpp b/src/mlpack/methods/hoeffding_trees/hoeffding_tree_model.hpp index 7ab8754f3b..bdf12e3d6b 100644 --- a/src/mlpack/methods/hoeffding_trees/hoeffding_tree_model.hpp +++ b/src/mlpack/methods/hoeffding_trees/hoeffding_tree_model.hpp @@ -188,30 +188,18 @@ class HoeffdingTreeModel data::DatasetInfo info; if (type == GINI_HOEFFDING) { - // Create fake tree to load into if needed. - if (Archive::is_loading::value) - giniHoeffdingTree = new GiniHoeffdingTreeType(info, 1, 1); ar & BOOST_SERIALIZATION_NVP(giniHoeffdingTree); } else if (type == GINI_BINARY) { - // Create fake tree to load into if needed. - if (Archive::is_loading::value) - giniBinaryTree = new GiniBinaryTreeType(info, 1, 1); ar & BOOST_SERIALIZATION_NVP(giniBinaryTree); } else if (type == INFO_HOEFFDING) { - // Create fake tree to load into if needed. - if (Archive::is_loading::value) - infoHoeffdingTree = new InfoHoeffdingTreeType(info, 1, 1); ar & BOOST_SERIALIZATION_NVP(infoHoeffdingTree); } else if (type == INFO_BINARY) { - // Create fake tree to load into if needed. - if (Archive::is_loading::value) - infoBinaryTree = new InfoBinaryTreeType(info, 1, 1); ar & BOOST_SERIALIZATION_NVP(infoBinaryTree); } } From e3b77096d982365812acaea08bed81734bda0575 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 15:42:52 -0400 Subject: [PATCH 186/265] Fix unused parameter warning. --- src/mlpack/methods/ann/ffn_impl.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/ffn_impl.hpp b/src/mlpack/methods/ann/ffn_impl.hpp index 68f3ddd653..d2f71fcdef 100644 --- a/src/mlpack/methods/ann/ffn_impl.hpp +++ b/src/mlpack/methods/ann/ffn_impl.hpp @@ -96,7 +96,8 @@ typename std::enable_if< !HasMaxIterations ::value, void>::type FFN:: -WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const +WarnMessageMaxIterations(OptimizerType& /* optimizer */, size_t /* samples */) + const { return; } From bde18b80ada1e0d5774f17c39cb72477a1a97618 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 15:49:39 -0400 Subject: [PATCH 187/265] Handle memory leaks when exceptions are thrown. --- src/mlpack/methods/cf/cf_main.cpp | 22 ++++++++++--- src/mlpack/methods/fastmks/fastmks_main.cpp | 32 +++++++++++++++++-- src/mlpack/methods/hmm/hmm_train_main.cpp | 18 ++++++++--- src/mlpack/methods/kde/kde_impl.hpp | 11 ++++++- .../preprocess/preprocess_scale_main.cpp | 19 +++++++++-- 5 files changed, 87 insertions(+), 15 deletions(-) diff --git a/src/mlpack/methods/cf/cf_main.cpp b/src/mlpack/methods/cf/cf_main.cpp index 49faf450fc..d609bd51e9 100644 --- a/src/mlpack/methods/cf/cf_main.cpp +++ b/src/mlpack/methods/cf/cf_main.cpp @@ -223,14 +223,14 @@ void ComputeRecommendations(CFModel* cf, const size_t numRecs, arma::Mat& recommendations) { - // Verifying the Interpolation algorithms + // Verify the Interpolation algorithms. RequireParamInSet("interpolation", { "average", "regression", "similarity" }, true, "unknown interpolation algorithm"); - // Taking Interpolation Alternatives + // Taking Interpolation Alternatives const string interpolationAlgorithm = CLI::GetParam("interpolation"); - // Determining the Interpolation Algorithm + // Determining the Interpolation Algorithm if (interpolationAlgorithm == "average") { ComputeRecommendations @@ -382,6 +382,11 @@ void PerformAction(arma::mat& dataset, const double minResidue) { const size_t neighborhood = (size_t) CLI::GetParam("neighborhood"); + + // Make sure the normalization strategy is valid. + RequireParamInSet("normalization", { "overall_mean", "item_mean", + "user_mean", "z_score", "none" }, true, "unknown normalization type"); + CFModel* c = new CFModel(); const string normalizationType = CLI::GetParam("normalization"); @@ -390,7 +395,16 @@ void PerformAction(arma::mat& dataset, maxIterations, minResidue, CLI::HasParam("iteration_only_termination"), normalizationType); - PerformAction(c); + try + { + PerformAction(c); + } + catch (std::exception& e) + { + // Clean the memory before throwing completely. + delete c; + throw; + } } void AssembleFactorizerType(const std::string& algorithm, diff --git a/src/mlpack/methods/fastmks/fastmks_main.cpp b/src/mlpack/methods/fastmks/fastmks_main.cpp index 31a575c1b2..b9a96118aa 100644 --- a/src/mlpack/methods/fastmks/fastmks_main.cpp +++ b/src/mlpack/methods/fastmks/fastmks_main.cpp @@ -128,6 +128,12 @@ static void mlpackMain() "number of maximum kernels must be greater than 0"); } + if (CLI::HasParam("base")) + { + RequireParamValue("base", [](double x) { return x > 1.0; }, true, + "base must be greater than or equal to 1!"); + } + // Naive mode overrides single mode. ReportIgnoredParam({{ "naive", true }}, "single"); @@ -223,12 +229,32 @@ static void mlpackMain() Log::Info << "Loaded query data (" << queryData.n_rows << " x " << queryData.n_cols << ")." << endl; - model->Search(queryData, (size_t) CLI::GetParam("k"), indices, - kernels, base); + try + { + model->Search(queryData, (size_t) CLI::GetParam("k"), indices, + kernels, base); + } + catch (std::invalid_argument& e) + { + // Delete the memory, if needed. + if (CLI::HasParam("reference")) + delete model; + throw; + } } else { - model->Search((size_t) CLI::GetParam("k"), indices, kernels); + try + { + model->Search((size_t) CLI::GetParam("k"), indices, kernels); + } + catch (std::invalid_argument& e) + { + // Delete the memory, if needed. + if (CLI::HasParam("reference")) + delete model; + throw e; + } } // Save output. diff --git a/src/mlpack/methods/hmm/hmm_train_main.cpp b/src/mlpack/methods/hmm/hmm_train_main.cpp index 5769a0f21e..e748d7d7e9 100644 --- a/src/mlpack/methods/hmm/hmm_train_main.cpp +++ b/src/mlpack/methods/hmm/hmm_train_main.cpp @@ -515,16 +515,26 @@ static void mlpackMain() if (CLI::HasParam("input_model")) { hmm = CLI::GetParam("input_model"); + + hmm->PerformAction>(&trainSeq); } else { // We need to initialize the model. hmm = new HMMModel(typeId); - hmm->PerformAction>(&trainSeq); - } - // Train the model. - hmm->PerformAction>(&trainSeq); + // Catch any exceptions so that we can clean the model if needed. + try + { + hmm->PerformAction>(&trainSeq); + hmm->PerformAction>(&trainSeq); + } + catch (std::exception& e) + { + delete hmm; + throw; + } + } // If necessary, save the output. CLI::GetParam("output_model") = hmm; diff --git a/src/mlpack/methods/kde/kde_impl.hpp b/src/mlpack/methods/kde/kde_impl.hpp index 07a0ccfa0d..78f7c4fb4f 100644 --- a/src/mlpack/methods/kde/kde_impl.hpp +++ b/src/mlpack/methods/kde/kde_impl.hpp @@ -336,7 +336,16 @@ Evaluate(MatType querySet, arma::vec& estimations) std::vector oldFromNewQueries; Tree* queryTree = BuildTree(std::move(querySet), oldFromNewQueries); Timer::Stop("building_query_tree"); - this->Evaluate(queryTree, oldFromNewQueries, estimations); + try + { + this->Evaluate(queryTree, oldFromNewQueries, estimations); + } + catch (std::exception& e) + { + // Make sure we delete the query tree. + delete queryTree; + throw; + } delete queryTree; } else if (mode == SINGLE_TREE_MODE) diff --git a/src/mlpack/methods/preprocess/preprocess_scale_main.cpp b/src/mlpack/methods/preprocess/preprocess_scale_main.cpp index bcbdc46f08..a63acbe641 100644 --- a/src/mlpack/methods/preprocess/preprocess_scale_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_scale_main.cpp @@ -131,6 +131,7 @@ static void mlpackMain() { m = new ScalingModel(CLI::GetParam("min_value"), CLI::GetParam("max_value"), CLI::GetParam("epsilon")); + if (scalerMethod == "standard_scaler") { m->ScalerType() = ScalingModel::ScalerTypes::STANDARD_SCALER; @@ -155,8 +156,20 @@ static void mlpackMain() { m->ScalerType() = ScalingModel::ScalerTypes::PCA_WHITENING; } - m->Fit(input); + + // Fit() can throw an exception on invalid inputs, so we have to catch that + // and clean the memory in that situation. + try + { + m->Fit(input); + } + catch (std::exception& e) + { + delete m; + throw; + } } + if (!CLI::HasParam("inverse_scaling")) { m->Transform(input, output); @@ -165,8 +178,8 @@ static void mlpackMain() { if (!CLI::HasParam("input_model")) { - delete(m); - throw std::runtime_error("Please provide a saved model"); + delete m; + throw std::runtime_error("Please provide a saved model."); } m->InverseTransform(input, output); } From 0bfbe786e68910541263b6457e5f3fdf83626341 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 15:52:27 -0400 Subject: [PATCH 188/265] Handle memory properly in the case of failures. --- src/mlpack/methods/gmm/gmm_train_main.cpp | 13 ++++--- .../methods/linear_svm/linear_svm_main.cpp | 12 +++++++ .../local_coordinate_coding_main.cpp | 24 +++++++++---- src/mlpack/methods/lsh/lsh_main.cpp | 7 ++-- .../methods/neighbor_search/kfn_main.cpp | 34 +++++++++++++++---- .../methods/neighbor_search/knn_main.cpp | 33 ++++++++++++++---- .../range_search/range_search_main.cpp | 4 +-- 7 files changed, 101 insertions(+), 26 deletions(-) diff --git a/src/mlpack/methods/gmm/gmm_train_main.cpp b/src/mlpack/methods/gmm/gmm_train_main.cpp index bfe29ae562..712c1e957b 100644 --- a/src/mlpack/methods/gmm/gmm_train_main.cpp +++ b/src/mlpack/methods/gmm/gmm_train_main.cpp @@ -188,10 +188,6 @@ static void mlpackMain() << " model (given with " << PRINT_PARAM_STRING("input_model") << " has dimensionality " << gmm->Dimensionality() << "!" << endl; } - else - { - gmm = new GMM(size_t(gaussians), dataPoints.n_rows); - } // Gather parameters for EMFit object. const size_t maxIterations = (size_t) CLI::GetParam("max_iterations"); @@ -212,6 +208,11 @@ static void mlpackMain() return x > 0.0 && x <= 1.0; }, true, "percentage to sample must be " "be greater than 0.0 and less than or equal to 1.0"); + // Initialize the GMM if needed. (We didn't do this earlier, because + // RequireParamValue() would leak the memory if the check failed.) + if (!CLI::HasParam("input_model")) + gmm = new GMM(size_t(gaussians), dataPoints.n_rows); + const int samplings = CLI::GetParam("samplings"); const double percentage = CLI::GetParam("percentage"); @@ -274,6 +275,10 @@ static void mlpackMain() } else { + // Initialize the GMM if needed. + if (!CLI::HasParam("input_model")) + gmm = new GMM(size_t(gaussians), dataPoints.n_rows); + // Depending on the value of forcePositive and diagonalCovariance, we have // to use different types. if (diagonalCovariance) diff --git a/src/mlpack/methods/linear_svm/linear_svm_main.cpp b/src/mlpack/methods/linear_svm/linear_svm_main.cpp index 13385cb74c..ed210a5a62 100644 --- a/src/mlpack/methods/linear_svm/linear_svm_main.cpp +++ b/src/mlpack/methods/linear_svm/linear_svm_main.cpp @@ -312,6 +312,13 @@ static void mlpackMain() model->svm.NumClasses() = numClasses; model->svm.FitIntercept() = intercept; + if (numClasses <= 1) + { + if (!CLI::HasParam("input_model")) + delete model; + throw std::invalid_argument("Given input data has only 1 class!"); + } + if (optimizerType == "lbfgs") { ens::L_BFGS lbfgsOpt; @@ -369,6 +376,9 @@ static void mlpackMain() // Checking the dimensionality of the test data. if (testSet.n_rows != trainingDimensionality) { + // Clean memory if needed. + if (!CLI::HasParam("input_model")) + delete model; Log::Fatal << "Test data dimensionality (" << testSet.n_rows << ") must " << "be the same as the dimensionality of the training data (" << trainingDimensionality << ")!" << endl; @@ -398,6 +408,8 @@ static void mlpackMain() if (testSet.n_cols != testLabels.n_elem) { + if (!CLI::HasParam("input_model")) + delete model; Log::Fatal << "Test data given with " << PRINT_PARAM_STRING("test") << " has " << testSet.n_cols << " points, but labels in " << PRINT_PARAM_STRING("test_labels") << " have " diff --git a/src/mlpack/methods/local_coordinate_coding/local_coordinate_coding_main.cpp b/src/mlpack/methods/local_coordinate_coding/local_coordinate_coding_main.cpp index d7a057a97f..1aaf645768 100644 --- a/src/mlpack/methods/local_coordinate_coding/local_coordinate_coding_main.cpp +++ b/src/mlpack/methods/local_coordinate_coding/local_coordinate_coding_main.cpp @@ -134,8 +134,6 @@ static void mlpackMain() LocalCoordinateCoding* lcc; if (CLI::HasParam("input_model")) lcc = CLI::GetParam("input_model"); - else - lcc = new LocalCoordinateCoding(0, 0.0); if (CLI::HasParam("training")) { @@ -160,6 +158,8 @@ static void mlpackMain() RequireParamValue("tolerance", [](double x) { return x > 0; }, 1, "Tolerance should be a positive real number"); + lcc = new LocalCoordinateCoding(0, 0.0); + lcc->Lambda() = CLI::GetParam("lambda"); lcc->Atoms() = (size_t) CLI::GetParam("atoms"); lcc->MaxIterations() = (size_t) CLI::GetParam("max_iterations"); @@ -181,14 +181,21 @@ static void mlpackMain() // Validate the size of the initial dictionary. if (lcc->Dictionary().n_cols != lcc->Atoms()) { - Log::Fatal << "The initial dictionary has " << lcc->Dictionary().n_cols + const size_t dictionarySize = lcc->Dictionary().n_cols; + const size_t atoms = lcc->Atoms(); + if (!CLI::HasParam("input_model")) + delete lcc; + Log::Fatal << "The initial dictionary has " << dictionarySize << " atoms, but the number of atoms was specified to be " - << lcc->Atoms() << "!" << endl; + << atoms << "!" << endl; } if (lcc->Dictionary().n_rows != matX.n_rows) { - Log::Fatal << "The initial dictionary has " << lcc->Dictionary().n_rows + const size_t dictionaryDimension = lcc->Dictionary().n_rows; + if (!CLI::HasParam("input_model")) + delete lcc; + Log::Fatal << "The initial dictionary has " << dictionaryDimension << " dimensions, but the data has " << matX.n_rows << " dimensions!" << endl; } @@ -209,10 +216,15 @@ static void mlpackMain() mat matY = std::move(CLI::GetParam("test")); if (matY.n_rows != lcc->Dictionary().n_rows) + { + const size_t dictionaryDimension = lcc->Dictionary().n_rows; + if (!CLI::HasParam("input_model")) + delete lcc; Log::Fatal << "Model was trained with a dimensionality of " - << lcc->Dictionary().n_rows << ", but data in test file " + << dictionaryDimension << ", but data in test file " << CLI::GetPrintableParam("test") << " has a dimensionality of " << matY.n_rows << "!" << endl; + } // Normalize each point if the user asked for it. if (CLI::HasParam("normalize")) diff --git a/src/mlpack/methods/lsh/lsh_main.cpp b/src/mlpack/methods/lsh/lsh_main.cpp index c082e10aa1..179d92ad00 100644 --- a/src/mlpack/methods/lsh/lsh_main.cpp +++ b/src/mlpack/methods/lsh/lsh_main.cpp @@ -220,8 +220,11 @@ static void mlpackMain() if (trueNeighbors.n_rows != neighbors.n_rows || trueNeighbors.n_cols != neighbors.n_cols) { - Log::Fatal << "The true neighbors file must have the same number of " - << "values as the set of neighbors being queried!" << endl; + // Delete the model if needed. + if (CLI::HasParam("reference")) + delete allkann; + Log::Fatal << "The true neighbors file must have the same number of " + << "values as the set of neighbors being queried!" << endl; } Log::Info << "Using true neighbor indices from '" diff --git a/src/mlpack/methods/neighbor_search/kfn_main.cpp b/src/mlpack/methods/neighbor_search/kfn_main.cpp index d046655888..26485bbe77 100644 --- a/src/mlpack/methods/neighbor_search/kfn_main.cpp +++ b/src/mlpack/methods/neighbor_search/kfn_main.cpp @@ -188,8 +188,6 @@ static void mlpackMain() if (CLI::HasParam("reference")) { - kfn = new KFNModel(); - // Get all the parameters. RequireParamInSet("tree_type", { "kd", "cover", "r", "r-star", "ball", "x", "hilbert-r", "r-plus", "r-plus-plus", "vp", "rp", "max-rp", @@ -197,6 +195,8 @@ static void mlpackMain() const string treeType = CLI::GetParam("tree_type"); const bool randomBasis = CLI::HasParam("random_basis"); + kfn = new KFNModel(); + KFNModel::TreeTypes tree = KFNModel::KD_TREE; if (treeType == "kd") tree = KFNModel::KD_TREE; @@ -274,8 +274,12 @@ static void mlpackMain() << queryData.n_rows << "x" << queryData.n_cols << ")." << endl; if (queryData.n_rows != kfn->Dataset().n_rows) { + // Clean memory if needed. + const size_t dimensions = kfn->Dataset().n_rows; + if (CLI::HasParam("reference")) + delete kfn; Log::Fatal << "Query has invalid dimensions (" << queryData.n_rows << - "); should be " << kfn->Dataset().n_rows << "!" << endl; + "); should be " << dimensions << "!" << endl; } } @@ -284,18 +288,26 @@ static void mlpackMain() // we only test the upper bound. if (k > kfn->Dataset().n_cols) { + // Clean memory if needed. + const size_t referencePoints = kfn->Dataset().n_cols; + if (CLI::HasParam("reference")) + delete kfn; Log::Fatal << "Invalid k: " << k << "; must be greater than 0 and less " << "than or equal to the number of reference points (" - << kfn->Dataset().n_cols << ")." << endl; + << referencePoints << ")." << endl; } // Sanity check on k value: must not be equal to the number of reference // points when query data has not been provided. if (!CLI::HasParam("query") && k == kfn->Dataset().n_cols) { + // Clean memory if needed. + const size_t referencePoints = kfn->Dataset().n_cols; + if (CLI::HasParam("reference")) + delete kfn; Log::Fatal << "Invalid k: " << k << "; must be less than the number of " - << "reference points (" << kfn->Dataset().n_cols << ") " - << "if query data has not been provided." << endl; + << "reference points (" << referencePoints << ") if query data has " + << "not been provided." << endl; } // Now run the search. @@ -321,8 +333,13 @@ static void mlpackMain() if (trueDistances.n_rows != distances.n_rows || trueDistances.n_cols != distances.n_cols) + { + // Clean memory if needed. + if (CLI::HasParam("reference")) + delete kfn; Log::Fatal << "The true distances file must have the same number of " << "values than the set of distances being queried!" << endl; + } Log::Info << "Effective error: " << KFN::EffectiveError(distances, trueDistances) << endl; @@ -341,8 +358,13 @@ static void mlpackMain() if (trueNeighbors.n_rows != neighbors.n_rows || trueNeighbors.n_cols != neighbors.n_cols) + { + // Clean memory if needed. + if (CLI::HasParam("reference")) + delete kfn; Log::Fatal << "The true neighbors file must have the same number of " << "values than the set of neighbors being queried!" << endl; + } Log::Info << "Recall: " << KFN::Recall(neighbors, trueNeighbors) << endl; } diff --git a/src/mlpack/methods/neighbor_search/knn_main.cpp b/src/mlpack/methods/neighbor_search/knn_main.cpp index ece34d861b..01918ec1b0 100644 --- a/src/mlpack/methods/neighbor_search/knn_main.cpp +++ b/src/mlpack/methods/neighbor_search/knn_main.cpp @@ -200,8 +200,6 @@ static void mlpackMain() if (CLI::HasParam("reference")) { - knn = new KNNModel(); - // Get all the parameters. const string treeType = CLI::GetParam("tree_type"); const bool randomBasis = CLI::HasParam("random_basis"); @@ -210,6 +208,9 @@ static void mlpackMain() RequireParamInSet("tree_type", { "kd", "cover", "r", "r-star", "ball", "x", "hilbert-r", "r-plus", "r-plus-plus", "spill", "vp", "rp", "max-rp", "ub", "oct" }, true, "unknown tree type"); + + knn = new KNNModel(); + if (treeType == "kd") tree = KNNModel::KD_TREE; else if (treeType == "cover") @@ -292,8 +293,12 @@ static void mlpackMain() << queryData.n_rows << "x" << queryData.n_cols << ")." << endl; if (queryData.n_rows != knn->Dataset().n_rows) { + // Clean memory if needed before crashing. + const size_t dimensions = knn->Dataset().n_rows; + if (CLI::HasParam("reference")) + delete knn; Log::Fatal << "Query has invalid dimensions(" << queryData.n_rows << - "); should be " << knn->Dataset().n_rows << "!" << endl; + "); should be " << dimensions << "!" << endl; } } @@ -302,18 +307,26 @@ static void mlpackMain() // we only test the upper bound. if (k > knn->Dataset().n_cols) { + // Clean memory if needed before crashing. + const size_t referencePoints = knn->Dataset().n_cols; + if (CLI::HasParam("reference")) + delete knn; Log::Fatal << "Invalid k: " << k << "; must be greater than 0 and less " << "than or equal to the number of reference points (" - << knn->Dataset().n_cols << ")." << endl; + << referencePoints << ")." << endl; } // Sanity check on k value: must not be equal to the number of reference // points when query data has not been provided. if (!CLI::HasParam("query") && k == knn->Dataset().n_cols) { + // Clean memory if needed before crashing. + const size_t referencePoints = knn->Dataset().n_cols; + if (CLI::HasParam("reference")) + delete knn; Log::Fatal << "Invalid k: " << k << "; must be less than the number of " - << "reference points (" << knn->Dataset().n_cols << ") " - << "if query data has not been provided." << endl; + << "reference points (" << referencePoints << ") if query data has " + << "not been provided." << endl; } // Now run the search. @@ -339,8 +352,12 @@ static void mlpackMain() if (trueDistances.n_rows != distances.n_rows || trueDistances.n_cols != distances.n_cols) + { + if (CLI::HasParam("reference")) + delete knn; Log::Fatal << "The true distances file must have the same number of " << "values than the set of distances being queried!" << endl; + } Log::Info << "Effective error: " << KNN::EffectiveError(distances, trueDistances) << endl; @@ -359,8 +376,12 @@ static void mlpackMain() if (trueNeighbors.n_rows != neighbors.n_rows || trueNeighbors.n_cols != neighbors.n_cols) + { + if (CLI::HasParam("reference")) + delete knn; Log::Fatal << "The true neighbors file must have the same number of " << "values than the set of neighbors being queried!" << endl; + } Log::Info << "Recall: " << KNN::Recall(neighbors, trueNeighbors) << endl; } diff --git a/src/mlpack/methods/range_search/range_search_main.cpp b/src/mlpack/methods/range_search/range_search_main.cpp index 346e133c05..26f2ec2396 100644 --- a/src/mlpack/methods/range_search/range_search_main.cpp +++ b/src/mlpack/methods/range_search/range_search_main.cpp @@ -155,8 +155,6 @@ static void mlpackMain() const bool singleMode = CLI::HasParam("single_mode"); if (CLI::HasParam("reference")) { - rs = new RSModel(); - // Get all the parameters. const string treeType = CLI::GetParam("tree_type"); RequireParamInSet("tree_type", { "kd", "cover", "r", "r-star", @@ -164,6 +162,8 @@ static void mlpackMain() "ub", "oct" }, true, "unknown tree type"); const bool randomBasis = CLI::HasParam("random_basis"); + rs = new RSModel(); + RSModel::TreeTypes tree = RSModel::KD_TREE; if (treeType == "kd") tree = RSModel::KD_TREE; From 9e70b542e67ded8f5c9eee219cbb0e26d35a3da9 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 15:52:58 -0400 Subject: [PATCH 189/265] Make sure output model is set even when not requested (important for non-CLI bindings). --- src/mlpack/methods/kde/kde_main.cpp | 3 +-- src/mlpack/methods/preprocess/preprocess_scale_main.cpp | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/mlpack/methods/kde/kde_main.cpp b/src/mlpack/methods/kde/kde_main.cpp index 32f4afedc5..87909d180b 100644 --- a/src/mlpack/methods/kde/kde_main.cpp +++ b/src/mlpack/methods/kde/kde_main.cpp @@ -314,6 +314,5 @@ static void mlpackMain() CLI::GetParam("predictions") = std::move(estimations); // Save model. - if (CLI::HasParam("output_model")) - CLI::GetParam("output_model") = kde; + CLI::GetParam("output_model") = kde; } diff --git a/src/mlpack/methods/preprocess/preprocess_scale_main.cpp b/src/mlpack/methods/preprocess/preprocess_scale_main.cpp index a63acbe641..44d26b3a4b 100644 --- a/src/mlpack/methods/preprocess/preprocess_scale_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_scale_main.cpp @@ -188,6 +188,6 @@ static void mlpackMain() if (CLI::HasParam("output")) CLI::GetParam("output") = std::move(output); Timer::Stop("feature_scaling"); - if (CLI::HasParam("output_model")) - CLI::GetParam("output_model") = m; + + CLI::GetParam("output_model") = m; } From 68fd4c30b494670390877a54a1639e9675315260 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 15:54:11 -0400 Subject: [PATCH 190/265] Allow adding layer and manually specifying ownership of internal layers. --- src/mlpack/methods/ann/layer/add_merge.hpp | 14 ++++++++++++-- src/mlpack/methods/ann/layer/add_merge_impl.hpp | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/mlpack/methods/ann/layer/add_merge.hpp b/src/mlpack/methods/ann/layer/add_merge.hpp index cd92cdaec3..b47d1193e3 100644 --- a/src/mlpack/methods/ann/layer/add_merge.hpp +++ b/src/mlpack/methods/ann/layer/add_merge.hpp @@ -50,6 +50,15 @@ class AddMerge */ AddMerge(const bool model = false, const bool run = true); + /** + * Create the AddMerge object using the specified parameters. + * + * @param model Expose all the network modules. + * @param run Call the Forward/Backward method before the output is merged. + * @param ownsLayers Delete the layers when this is deallocated. + */ + AddMerge(const bool model, const bool run, const bool ownsLayers); + //! Destructor to release allocated memory. ~AddMerge(); @@ -184,8 +193,9 @@ class AddMerge //! before merging the output. bool run; - //! We need this to know whether we should delete the layer in the destructor. - bool ownsLayer; + //! We need this to know whether we should delete the internally-held layers + //! in the destructor. + bool ownsLayers; //! Locally-stored network modules. std::vector > network; diff --git a/src/mlpack/methods/ann/layer/add_merge_impl.hpp b/src/mlpack/methods/ann/layer/add_merge_impl.hpp index a4985577f8..239e4575fa 100644 --- a/src/mlpack/methods/ann/layer/add_merge_impl.hpp +++ b/src/mlpack/methods/ann/layer/add_merge_impl.hpp @@ -27,7 +27,16 @@ template AddMerge::AddMerge( const bool model, const bool run) : - model(model), run(run), ownsLayer(!model) + model(model), run(run), ownsLayers(!model) +{ + // Nothing to do here. +} + +template +AddMerge::AddMerge( + const bool model, const bool run, const bool ownsLayers) : + model(model), run(run), ownsLayers(ownsLayers) { // Nothing to do here. } @@ -36,7 +45,7 @@ template AddMerge::~AddMerge() { - if (ownsLayer) + if (!model && ownsLayers) { std::for_each(network.begin(), network.end(), boost::apply_visitor(deleteVisitor)); @@ -150,7 +159,7 @@ void AddMerge::serialize( ar & BOOST_SERIALIZATION_NVP(network); ar & BOOST_SERIALIZATION_NVP(model); ar & BOOST_SERIALIZATION_NVP(run); - ar & BOOST_SERIALIZATION_NVP(ownsLayer); + ar & BOOST_SERIALIZATION_NVP(ownsLayers); } } // namespace ann From 4ad421a9b47302d0fddbd9219ab62b85bd123d0e Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 15:54:50 -0400 Subject: [PATCH 191/265] Fix memory handling---only clear if model=false. Otherwise DeleteVisitor will do it. --- src/mlpack/methods/ann/layer/concat_impl.hpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/ann/layer/concat_impl.hpp b/src/mlpack/methods/ann/layer/concat_impl.hpp index ff69d7e0e8..0aeee24fa2 100644 --- a/src/mlpack/methods/ann/layer/concat_impl.hpp +++ b/src/mlpack/methods/ann/layer/concat_impl.hpp @@ -90,9 +90,12 @@ template Concat::~Concat() { - // Clear memory. - std::for_each(network.begin(), network.end(), - boost::apply_visitor(deleteVisitor)); + if (!model) + { + // Clear memory. + std::for_each(network.begin(), network.end(), + boost::apply_visitor(deleteVisitor)); + } } template Date: Sun, 22 Mar 2020 15:55:38 -0400 Subject: [PATCH 192/265] Delete internal layers of models when Model() exists. --- .../methods/ann/visitor/delete_visitor.hpp | 12 +++++++++-- .../ann/visitor/delete_visitor_impl.hpp | 20 ++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/ann/visitor/delete_visitor.hpp b/src/mlpack/methods/ann/visitor/delete_visitor.hpp index 6dbcad293d..5ffd96d775 100644 --- a/src/mlpack/methods/ann/visitor/delete_visitor.hpp +++ b/src/mlpack/methods/ann/visitor/delete_visitor.hpp @@ -27,9 +27,17 @@ namespace ann { class DeleteVisitor : public boost::static_visitor { public: - //! Execute the destructor. + //! Execute the destructor if the layer does not hold layers internally. template - void operator()(LayerType* layer) const; + typename std::enable_if< + !HasModelCheck::value, void>::type + operator()(LayerType* layer) const; + + //! Execute the destructor if the layer does hold layers internally. + template + typename std::enable_if< + HasModelCheck::value, void>::type + operator()(LayerType* layer) const; void operator()(MoreTypes layer) const; }; diff --git a/src/mlpack/methods/ann/visitor/delete_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/delete_visitor_impl.hpp index dbf6e2a7c2..43e01f764c 100644 --- a/src/mlpack/methods/ann/visitor/delete_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/delete_visitor_impl.hpp @@ -20,12 +20,30 @@ namespace ann { //! DeleteVisitor visitor class. template -inline void DeleteVisitor::operator()(LayerType* layer) const +inline typename std::enable_if< + !HasModelCheck::value, void>::type +DeleteVisitor::operator()(LayerType* layer) const { if (layer) delete layer; } +template +inline typename std::enable_if< + HasModelCheck::value, void>::type +DeleteVisitor::operator()(LayerType* layer) const +{ + if (layer) + { + for (size_t i = 0; i < layer->Model().size(); ++i) + { + boost::apply_visitor(DeleteVisitor(), layer->Model()[i]); + } + + delete layer; + } +} + inline void DeleteVisitor::operator()(MoreTypes layer) const { layer.apply_visitor(*this); From 29422c6066d81e3200bbf1961124183340e6f77e Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 16:40:02 -0400 Subject: [PATCH 193/265] No need for inputTemp; use the input given in Gradient(). This solves #2146 in a better way than #2234. --- src/mlpack/methods/ann/layer/atrous_convolution.hpp | 3 --- .../methods/ann/layer/atrous_convolution_impl.hpp | 12 +++++++----- src/mlpack/methods/ann/layer/convolution.hpp | 3 --- src/mlpack/methods/ann/layer/convolution_impl.hpp | 12 +++++++----- .../methods/ann/layer/transposed_convolution.hpp | 3 --- .../ann/layer/transposed_convolution_impl.hpp | 12 +++++++----- 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/mlpack/methods/ann/layer/atrous_convolution.hpp b/src/mlpack/methods/ann/layer/atrous_convolution.hpp index 34d16cabb4..617ba4125f 100644 --- a/src/mlpack/methods/ann/layer/atrous_convolution.hpp +++ b/src/mlpack/methods/ann/layer/atrous_convolution.hpp @@ -365,9 +365,6 @@ class AtrousConvolution //! Locally-stored transformed output parameter. arma::cube outputTemp; - //! Locally-stored transformed input parameter. - arma::cube inputTemp; - //! Locally-stored transformed padded input parameter. arma::cube inputPaddedTemp; diff --git a/src/mlpack/methods/ann/layer/atrous_convolution_impl.hpp b/src/mlpack/methods/ann/layer/atrous_convolution_impl.hpp index 33d854b5b3..02bdfbfe28 100644 --- a/src/mlpack/methods/ann/layer/atrous_convolution_impl.hpp +++ b/src/mlpack/methods/ann/layer/atrous_convolution_impl.hpp @@ -187,7 +187,7 @@ void AtrousConvolution< >::Forward(const arma::Mat& input, arma::Mat& output) { batchSize = input.n_cols; - inputTemp = arma::cube(const_cast&>(input).memptr(), + arma::cube inputTemp(const_cast&>(input).memptr(), inputWidth, inputHeight, inSize * batchSize, false, false); if (padding.PadWLeft() != 0 || padding.PadWRight() != 0 || @@ -271,9 +271,9 @@ void AtrousConvolution< arma::cube mappedError(((arma::Mat&) gy).memptr(), outputWidth, outputHeight, outSize * batchSize, false, false); - g.set_size(inputTemp.n_rows * inputTemp.n_cols * inSize, batchSize); - gTemp = arma::Cube(g.memptr(), inputTemp.n_rows, - inputTemp.n_cols, inputTemp.n_slices, false, false); + g.set_size(inputWidth * inputHeight * inSize, batchSize); + gTemp = arma::Cube(g.memptr(), inputWidth, inputHeight, + inSize * batchSize, false, false); gTemp.zeros(); for (size_t outMap = 0, outMapIdx = 0, batchCount = 0; outMap < @@ -325,12 +325,14 @@ void AtrousConvolution< InputDataType, OutputDataType >::Gradient( - const arma::Mat& /* input */, + const arma::Mat& input, const arma::Mat& error, arma::Mat& gradient) { arma::cube mappedError(((arma::Mat&) error).memptr(), outputWidth, outputHeight, outSize * batchSize, false, false); + arma::cube inputTemp(const_cast&>(input).memptr(), + inputWidth, inputHeight, inSize * batchSize, false, false); gradient.set_size(weights.n_elem, 1); gradientTemp = arma::Cube(gradient.memptr(), weight.n_rows, diff --git a/src/mlpack/methods/ann/layer/convolution.hpp b/src/mlpack/methods/ann/layer/convolution.hpp index 9e16594657..bad950d2d7 100644 --- a/src/mlpack/methods/ann/layer/convolution.hpp +++ b/src/mlpack/methods/ann/layer/convolution.hpp @@ -362,9 +362,6 @@ class Convolution //! Locally-stored transformed output parameter. arma::cube outputTemp; - //! Locally-stored transformed input parameter. - arma::cube inputTemp; - //! Locally-stored transformed padded input parameter. arma::cube inputPaddedTemp; diff --git a/src/mlpack/methods/ann/layer/convolution_impl.hpp b/src/mlpack/methods/ann/layer/convolution_impl.hpp index ebabb31380..d9c5886a10 100644 --- a/src/mlpack/methods/ann/layer/convolution_impl.hpp +++ b/src/mlpack/methods/ann/layer/convolution_impl.hpp @@ -178,7 +178,7 @@ void Convolution< >::Forward(const arma::Mat& input, arma::Mat& output) { batchSize = input.n_cols; - inputTemp = arma::cube(const_cast&>(input).memptr(), + arma::cube inputTemp(const_cast&>(input).memptr(), inputWidth, inputHeight, inSize * batchSize, false, false); if (padWLeft != 0 || padWRight != 0 || padHTop != 0 || padHBottom != 0) @@ -258,9 +258,9 @@ void Convolution< arma::cube mappedError(((arma::Mat&) gy).memptr(), outputWidth, outputHeight, outSize * batchSize, false, false); - g.set_size(inputTemp.n_rows * inputTemp.n_cols * inSize, batchSize); - gTemp = arma::Cube(g.memptr(), inputTemp.n_rows, - inputTemp.n_cols, inputTemp.n_slices, false, false); + g.set_size(inputWidth * inputHeight * inSize, batchSize); + gTemp = arma::Cube(g.memptr(), inputWidth, inputHeight, + inSize * batchSize, false, false); gTemp.zeros(); for (size_t outMap = 0, outMapIdx = 0, batchCount = 0; outMap < @@ -308,12 +308,14 @@ void Convolution< InputDataType, OutputDataType >::Gradient( - const arma::Mat& /* input */, + const arma::Mat& input, const arma::Mat& error, arma::Mat& gradient) { arma::cube mappedError(((arma::Mat&) error).memptr(), outputWidth, outputHeight, outSize * batchSize, false, false); + arma::cube inputTemp(((arma::Mat&) input).memptr(), inputWidth, + inputHeight, inSize * batchSize, false, false); gradient.set_size(weights.n_elem, 1); gradientTemp = arma::Cube(gradient.memptr(), weight.n_rows, diff --git a/src/mlpack/methods/ann/layer/transposed_convolution.hpp b/src/mlpack/methods/ann/layer/transposed_convolution.hpp index eda7364e1d..ef54fb9d24 100644 --- a/src/mlpack/methods/ann/layer/transposed_convolution.hpp +++ b/src/mlpack/methods/ann/layer/transposed_convolution.hpp @@ -429,9 +429,6 @@ class TransposedConvolution //! Locally-stored transformed output parameter. arma::cube outputTemp; - //! Locally-stored transformed input parameter. - arma::cube inputTemp; - //! Locally-stored transformed padded input parameter. arma::cube inputPaddedTemp; diff --git a/src/mlpack/methods/ann/layer/transposed_convolution_impl.hpp b/src/mlpack/methods/ann/layer/transposed_convolution_impl.hpp index 4f9e84d3a5..aac15f7595 100644 --- a/src/mlpack/methods/ann/layer/transposed_convolution_impl.hpp +++ b/src/mlpack/methods/ann/layer/transposed_convolution_impl.hpp @@ -210,7 +210,7 @@ void TransposedConvolution< >::Forward(const arma::Mat& input, arma::Mat& output) { batchSize = input.n_cols; - inputTemp = arma::cube(const_cast&>(input).memptr(), + arma::cube inputTemp(const_cast&>(input).memptr(), inputWidth, inputHeight, inSize * batchSize, false, false); if (strideWidth > 1 || strideHeight > 1) @@ -330,9 +330,9 @@ void TransposedConvolution< mappedErrorPadded.slice(i)); } } - g.set_size(inputTemp.n_rows * inputTemp.n_cols * inSize, batchSize); - gTemp = arma::Cube(g.memptr(), inputTemp.n_rows, - inputTemp.n_cols, inputTemp.n_slices, false, false); + g.set_size(inputWidth * inputHeight * inSize, batchSize); + gTemp = arma::Cube(g.memptr(), inputWidth, inputHeight, inSize * + batchSize, false, false); gTemp.zeros(); @@ -381,12 +381,14 @@ void TransposedConvolution< InputDataType, OutputDataType >::Gradient( - const arma::Mat& /* input */, + const arma::Mat& input, const arma::Mat& error, arma::Mat& gradient) { arma::Cube mappedError(((arma::Mat&) error).memptr(), outputWidth, outputHeight, outSize * batchSize, false, false); + arma::cube inputTemp(const_cast&>(input).memptr(), + inputWidth, inputHeight, inSize * batchSize, false, false); gradient.set_size(weights.n_elem, 1); gradientTemp = arma::Cube(gradient.memptr(), weight.n_rows, From ca34426680851319983760dd1926fb00358bf48f Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 16:41:56 -0400 Subject: [PATCH 194/265] Don't free---DeleteVisitor will. --- src/mlpack/methods/ann/layer/dropconnect.hpp | 12 +++++------- src/mlpack/methods/ann/layer/dropconnect_impl.hpp | 6 ------ 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/mlpack/methods/ann/layer/dropconnect.hpp b/src/mlpack/methods/ann/layer/dropconnect.hpp index 3d73896eab..c1e5c51a68 100644 --- a/src/mlpack/methods/ann/layer/dropconnect.hpp +++ b/src/mlpack/methods/ann/layer/dropconnect.hpp @@ -78,14 +78,12 @@ class DropConnect const size_t outSize, const double ratio = 0.5); - ~DropConnect(); - /** - * Ordinary feed forward pass of the DropConnect layer. - * - * @param input Input data used for evaluating the specified function. - * @param output Resulting output activation. - */ + * Ordinary feed forward pass of the DropConnect layer. + * + * @param input Input data used for evaluating the specified function. + * @param output Resulting output activation. + */ template void Forward(const arma::Mat& input, arma::Mat& output); diff --git a/src/mlpack/methods/ann/layer/dropconnect_impl.hpp b/src/mlpack/methods/ann/layer/dropconnect_impl.hpp index 85b56ff51d..194b4d7fda 100644 --- a/src/mlpack/methods/ann/layer/dropconnect_impl.hpp +++ b/src/mlpack/methods/ann/layer/dropconnect_impl.hpp @@ -48,12 +48,6 @@ DropConnect::DropConnect( network.push_back(baseLayer); } -template -DropConnect::~DropConnect() -{ - boost::apply_visitor(DeleteVisitor(), baseLayer); -} - template template void DropConnect::Forward( From b892f2cfab896dd4f04aa2dd3472f2b5d5646067 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 16:42:38 -0400 Subject: [PATCH 195/265] Don't delete on free---DeleteVisitor() in FFN will do that. --- src/mlpack/methods/ann/layer/gru.hpp | 5 ----- src/mlpack/methods/ann/layer/gru_impl.hpp | 11 ----------- 2 files changed, 16 deletions(-) diff --git a/src/mlpack/methods/ann/layer/gru.hpp b/src/mlpack/methods/ann/layer/gru.hpp index ef82ce0e82..52dd2730c3 100644 --- a/src/mlpack/methods/ann/layer/gru.hpp +++ b/src/mlpack/methods/ann/layer/gru.hpp @@ -72,11 +72,6 @@ class GRU const size_t outSize, const size_t rho = std::numeric_limits::max()); - /** - * Delete the GRU and the layers it holds. - */ - ~GRU(); - /** * Ordinary feed forward pass of a neural network, evaluating the function * f(x) by propagating the activity forward through f. diff --git a/src/mlpack/methods/ann/layer/gru_impl.hpp b/src/mlpack/methods/ann/layer/gru_impl.hpp index b2e7cee39d..b5eb914468 100644 --- a/src/mlpack/methods/ann/layer/gru_impl.hpp +++ b/src/mlpack/methods/ann/layer/gru_impl.hpp @@ -76,17 +76,6 @@ GRU::GRU( gradIterator = outParameter.end(); } -template -GRU::~GRU() -{ - boost::apply_visitor(deleteVisitor, input2GateModule); - boost::apply_visitor(deleteVisitor, output2GateModule); - boost::apply_visitor(deleteVisitor, outputHidden2GateModule); - boost::apply_visitor(deleteVisitor, inputGateModule); - boost::apply_visitor(deleteVisitor, forgetGateModule); - boost::apply_visitor(deleteVisitor, hiddenStateModule); -} - template template void GRU::Forward( From e66f0dc7bd8748293dca9c6787cd421760a1b532 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 16:43:11 -0400 Subject: [PATCH 196/265] Be clear about which layers are owned by Highway. --- src/mlpack/methods/ann/layer/highway.hpp | 20 ++++++++++++------- src/mlpack/methods/ann/layer/highway_impl.hpp | 19 +++--------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/mlpack/methods/ann/layer/highway.hpp b/src/mlpack/methods/ann/layer/highway.hpp index 4c238cefc1..fb8e1f9717 100644 --- a/src/mlpack/methods/ann/layer/highway.hpp +++ b/src/mlpack/methods/ann/layer/highway.hpp @@ -74,11 +74,6 @@ class Highway //! Destroy the Highway object. ~Highway(); - /** - * Destroy all the modules added to the Highway object. - */ - void DeleteModules(); - /** * Reset the layer parameter. */ @@ -126,14 +121,22 @@ class Highway * @param args The layer parameter. */ template - void Add(Args... args) { network.push_back(new LayerType(args...)); } + void Add(Args... args) + { + network.push_back(new LayerType(args...)); + networkOwnerships.push_back(true); + } /** * Add a new module to the model. * * @param layer The Layer to be added to the model. */ - void Add(LayerTypes layer) { network.push_back(layer); } + void Add(LayerTypes layer) + { + network.push_back(layer); + networkOwnerships.push_back(false); + } //! Return the modules of the model. std::vector >& Model() @@ -190,6 +193,9 @@ class Highway //! Locally-stored network modules. std::vector > network; + //! The list of network modules we are responsible for. + std::vector networkOwnerships; + //! Locally-stored empty list of modules. std::vector > empty; diff --git a/src/mlpack/methods/ann/layer/highway_impl.hpp b/src/mlpack/methods/ann/layer/highway_impl.hpp index 80b4959a36..e0b344b608 100644 --- a/src/mlpack/methods/ann/layer/highway_impl.hpp +++ b/src/mlpack/methods/ann/layer/highway_impl.hpp @@ -57,23 +57,10 @@ Highway::~Highway() { if (!model) { - for (LayerTypes& layer : network) + for (size_t i = 0; i < network.size(); ++i) { - boost::apply_visitor(deleteVisitor, layer); - } - } -} - -template -void Highway< - InputDataType, OutputDataType, CustomLayers...>::DeleteModules() -{ - if (model) - { - for (LayerTypes& layer : network) - { - boost::apply_visitor(deleteVisitor, layer); + if (networkOwnerships[i]) + boost::apply_visitor(deleteVisitor, network[i]); } } } From 4326b7d86977e8de9f0b6b4945848ed063e6d597 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 16:48:29 -0400 Subject: [PATCH 197/265] Be clear about which layers are owned by Recurrent. Add `ownsLayers` members to AddMerge and Sequential to avoid double-frees or memory leaks. --- src/mlpack/methods/ann/layer/add_merge.hpp | 18 ++++++++ .../methods/ann/layer/add_merge_impl.hpp | 10 ++++- src/mlpack/methods/ann/layer/recurrent.hpp | 3 -- .../methods/ann/layer/recurrent_impl.hpp | 25 +++-------- src/mlpack/methods/ann/layer/sequential.hpp | 36 +++++++++++++--- .../methods/ann/layer/sequential_impl.hpp | 42 ++++++++++--------- 6 files changed, 87 insertions(+), 47 deletions(-) diff --git a/src/mlpack/methods/ann/layer/add_merge.hpp b/src/mlpack/methods/ann/layer/add_merge.hpp index b47d1193e3..aafea251c2 100644 --- a/src/mlpack/methods/ann/layer/add_merge.hpp +++ b/src/mlpack/methods/ann/layer/add_merge.hpp @@ -231,6 +231,24 @@ class AddMerge } // namespace ann } // namespace mlpack +//! Set the serialization version of the AddMerge class. +namespace boost { +namespace serialization { + +template< + typename InputDataType, + typename OutputDataType, + typename... CustomLayers +> +struct version> +{ + BOOST_STATIC_CONSTANT(int, value = 1); +} + +} // namespace serialization +} // namespace boost + // Include implementation. #include "add_merge_impl.hpp" diff --git a/src/mlpack/methods/ann/layer/add_merge_impl.hpp b/src/mlpack/methods/ann/layer/add_merge_impl.hpp index 239e4575fa..7ac8c10f5b 100644 --- a/src/mlpack/methods/ann/layer/add_merge_impl.hpp +++ b/src/mlpack/methods/ann/layer/add_merge_impl.hpp @@ -159,7 +159,15 @@ void AddMerge::serialize( ar & BOOST_SERIALIZATION_NVP(network); ar & BOOST_SERIALIZATION_NVP(model); ar & BOOST_SERIALIZATION_NVP(run); - ar & BOOST_SERIALIZATION_NVP(ownsLayers); + + if (version >= 1) + { + ar & BOOST_SERIALIZATION_NVP(ownsLayers); + } + else if (Archive::is_loading::value) + { + ownsLayers = !model; + } } } // namespace ann diff --git a/src/mlpack/methods/ann/layer/recurrent.hpp b/src/mlpack/methods/ann/layer/recurrent.hpp index 97d6187e2e..da677cad5a 100644 --- a/src/mlpack/methods/ann/layer/recurrent.hpp +++ b/src/mlpack/methods/ann/layer/recurrent.hpp @@ -50,9 +50,6 @@ class Recurrent */ Recurrent(); - //! Destructor to release allocated memory. - ~Recurrent(); - //! Copy constructor. Recurrent(const Recurrent&); diff --git a/src/mlpack/methods/ann/layer/recurrent_impl.hpp b/src/mlpack/methods/ann/layer/recurrent_impl.hpp index b79ba06fed..e526baef35 100644 --- a/src/mlpack/methods/ann/layer/recurrent_impl.hpp +++ b/src/mlpack/methods/ann/layer/recurrent_impl.hpp @@ -37,19 +37,6 @@ Recurrent::Recurrent() : // Nothing to do. } -template -Recurrent::~Recurrent() -{ - if (ownsLayer) - { - boost::apply_visitor(DeleteVisitor(), recurrentModule); - boost::apply_visitor(DeleteVisitor(), initialModule); - boost::apply_visitor(DeleteVisitor(), startModule); - network.clear(); - } -} - template template< @@ -76,8 +63,8 @@ Recurrent::Recurrent( ownsLayer(true) { initialModule = new Sequential<>(); - mergeModule = new AddMerge<>(false, false); - recurrentModule = new Sequential<>(false); + mergeModule = new AddMerge<>(false, false, false); + recurrentModule = new Sequential<>(false, false); boost::apply_visitor(AddVisitor(inputModule), initialModule); @@ -116,8 +103,8 @@ Recurrent::Recurrent( feedbackModule = boost::apply_visitor(copyVisitor, network.feedbackModule); transferModule = boost::apply_visitor(copyVisitor, network.transferModule); initialModule = new Sequential<>(); - mergeModule = new AddMerge<>(false, false); - recurrentModule = new Sequential<>(false); + mergeModule = new AddMerge<>(false, false, false); + recurrentModule = new Sequential<>(false, false); boost::apply_visitor(AddVisitor(inputModule), initialModule); @@ -292,8 +279,8 @@ void Recurrent::serialize( if (Archive::is_loading::value) { initialModule = new Sequential<>(); - mergeModule = new AddMerge<>(false, false); - recurrentModule = new Sequential<>(false); + mergeModule = new AddMerge<>(false, false, false); + recurrentModule = new Sequential<>(false, false); boost::apply_visitor(AddVisitor(inputModule), initialModule); diff --git a/src/mlpack/methods/ann/layer/sequential.hpp b/src/mlpack/methods/ann/layer/sequential.hpp index be62af8d82..fcf6a32ef3 100644 --- a/src/mlpack/methods/ann/layer/sequential.hpp +++ b/src/mlpack/methods/ann/layer/sequential.hpp @@ -78,6 +78,15 @@ class Sequential */ Sequential(const bool model = true); + /** + * Create the Sequential object using the specified parameters. + * + * @param model Expose all the network modules. + * @param ownsLayers If true, then this module will delete its layers when + * deallocated. + */ + Sequential(const bool model, const bool ownsLayers); + //! Destroy the Sequential object. ~Sequential(); @@ -132,11 +141,6 @@ class Sequential */ void Add(LayerTypes layer) { network.push_back(layer); } - /* - * Destroy all the modules added to the Sequential object. - */ - void DeleteModules(); - //! Return the model modules. std::vector >& Model() { @@ -227,6 +231,9 @@ class Sequential //! The input height. size_t height; + + //! Whether we are responsible for deleting the layers held in this module. + bool ownsLayers; }; // class Sequential /* @@ -243,6 +250,25 @@ using Residual = Sequential< } // namespace ann } // namespace mlpack +//! Set the serialization version of the Sequential class. +namespace boost { +namespace serialization { + +template < + typename InputDataType, + typename OutputDataType, + bool Residual, + typename... CustomLayers +> +struct version> +{ + BOOST_STATIC_CONSTANT(int, value = 1); +} + +} // namespace serialization +} // namespace boost + // Include implementation. #include "sequential_impl.hpp" diff --git a/src/mlpack/methods/ann/layer/sequential_impl.hpp b/src/mlpack/methods/ann/layer/sequential_impl.hpp index 89647277be..6b26f86209 100644 --- a/src/mlpack/methods/ann/layer/sequential_impl.hpp +++ b/src/mlpack/methods/ann/layer/sequential_impl.hpp @@ -27,9 +27,18 @@ namespace ann /** Artificial Neural Network. */ { template -Sequential< - InputDataType, OutputDataType, Residual, CustomLayers...>::Sequential( - const bool model) : model(model), reset(false), width(0), height(0) +Sequential:: +Sequential(const bool model) : + model(model), reset(false), width(0), height(0), ownsLayers(!model) +{ + // Nothing to do here. +} + +template +Sequential:: +Sequential(const bool model, const bool ownsLayers) : + model(model), reset(false), width(0), height(0), ownsLayers(ownsLayers) { // Nothing to do here. } @@ -39,7 +48,7 @@ template ::~Sequential() { - if (!model) + if (!model && ownsLayers) { for (LayerTypes& layer : network) boost::apply_visitor(deleteVisitor, layer); @@ -175,26 +184,12 @@ Gradient(const arma::Mat& input, boost::apply_visitor(deltaVisitor, network[1])), network.front()); } -template -void Sequential< - InputDataType, OutputDataType, Residual, CustomLayers...>::DeleteModules() -{ - if (model == true) - { - for (LayerTypes& layer : network) - { - boost::apply_visitor(deleteVisitor, layer); - } - } -} - template template void Sequential< InputDataType, OutputDataType, Residual, CustomLayers...>::serialize( - Archive& ar, const unsigned int /* version */) + Archive& ar, const unsigned int version) { // If loading, delete the old layers. if (Archive::is_loading::value) @@ -207,6 +202,15 @@ void Sequential< ar & BOOST_SERIALIZATION_NVP(model); ar & BOOST_SERIALIZATION_NVP(network); + + if (version >= 1) + { + ar & BOOST_SERIALIZATION_NVP(ownsLayers); + } + else if (Archive::is_loading::value) + { + ownsLayers = !model; + } } } // namespace ann From 8d5b338cf19774b5af4febb96e05a58910f0d3d3 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 16:49:39 -0400 Subject: [PATCH 198/265] Fix minor bugs in ANN tests for memory handling. --- src/mlpack/tests/ann_layer_test.cpp | 9 ++++++--- src/mlpack/tests/ann_visitor_test.cpp | 2 ++ src/mlpack/tests/feedforward_network_test.cpp | 2 +- src/mlpack/tests/recurrent_network_test.cpp | 4 ++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/mlpack/tests/ann_layer_test.cpp b/src/mlpack/tests/ann_layer_test.cpp index 301205dca1..c064d61d9a 100644 --- a/src/mlpack/tests/ann_layer_test.cpp +++ b/src/mlpack/tests/ann_layer_test.cpp @@ -1148,7 +1148,9 @@ BOOST_AUTO_TEST_CASE(GradientGRULayerTest) */ BOOST_AUTO_TEST_CASE(ForwardGRULayerTest) { - GRU<> gru(3, 3, 5); + // This will make it easier to clean memory later. + GRU<>* gruAlloc = new GRU<>(3, 3, 5); + GRU<>& gru = *gruAlloc; // Initialize the weights to all ones. NetworkInitialization @@ -1194,6 +1196,9 @@ BOOST_AUTO_TEST_CASE(ForwardGRULayerTest) expectedOutput = z_t % expectedOutput + (arma::ones(3, 1) - z_t) % o_t; BOOST_REQUIRE_LE(arma::as_scalar(arma::trans(output) * expectedOutput), 1e-2); + + LayerTypes<> layer(gruAlloc); + boost::apply_visitor(DeleteVisitor(), layer); } /** @@ -2633,7 +2638,6 @@ BOOST_AUTO_TEST_CASE(GradientHighwayLayerTest) ~GradientFunction() { - highway->DeleteModules(); delete model; } @@ -2685,7 +2689,6 @@ BOOST_AUTO_TEST_CASE(GradientSequentialLayerTest) ~GradientFunction() { - sequential->DeleteModules(); delete model; } diff --git a/src/mlpack/tests/ann_visitor_test.cpp b/src/mlpack/tests/ann_visitor_test.cpp index ef45919bfb..e79dc4b5b2 100644 --- a/src/mlpack/tests/ann_visitor_test.cpp +++ b/src/mlpack/tests/ann_visitor_test.cpp @@ -51,6 +51,8 @@ BOOST_AUTO_TEST_CASE(BiasSetVisitorTest) boost::apply_visitor(ForwardVisitor(input, output), linear); BOOST_REQUIRE_EQUAL(arma::accu(output), 55); + + boost::apply_visitor(DeleteVisitor(), linear); } BOOST_AUTO_TEST_SUITE_END(); diff --git a/src/mlpack/tests/feedforward_network_test.cpp b/src/mlpack/tests/feedforward_network_test.cpp index 4773325ee0..c7c8ef6fdb 100644 --- a/src/mlpack/tests/feedforward_network_test.cpp +++ b/src/mlpack/tests/feedforward_network_test.cpp @@ -300,7 +300,7 @@ BOOST_AUTO_TEST_CASE(HighwayNetworkTest) Highway<>* highway = new Highway<>(10, true); highway->Add >(10, 10); highway->Add >(); - model.Add(highway); + model.Add(highway); // This takes ownership of the memory. model.Add >(10, 2); model.Add >(); TestNetwork<>(model, dataset, labels, dataset, labels, 10, 0.2); diff --git a/src/mlpack/tests/recurrent_network_test.cpp b/src/mlpack/tests/recurrent_network_test.cpp index ddf328ecc0..9d178f12e0 100644 --- a/src/mlpack/tests/recurrent_network_test.cpp +++ b/src/mlpack/tests/recurrent_network_test.cpp @@ -114,7 +114,7 @@ BOOST_AUTO_TEST_CASE(SequenceClassificationBRNNTest) BOOST_TEST_CHECKPOINT("Training over"); arma::cube prediction; model.Predict(input, prediction); - BOOST_TEST_CHECKPOINT("Predicion over"); + BOOST_TEST_CHECKPOINT("Prediction over"); size_t error = 0; for (size_t i = 0; i < prediction.n_cols; ++i) @@ -825,7 +825,7 @@ void DistractedSequenceRecallTestNetwork( inputTemp = arma::cube(trainInput.at(0, j).memptr(), inputSize, 1, trainInput.at(0, j).n_elem / inputSize, false, true); labelsTemp = arma::cube(trainLabels.at(0, j).memptr(), outputSize, 1, - trainInput.at(0, j).n_elem / outputSize, false, true); + trainLabels.at(0, j).n_elem / outputSize, false, true); model.Train(inputTemp, labelsTemp, opt); } From 58209d7c9b7f2b3a6eba1787dfcc2556546f3d17 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 16:50:53 -0400 Subject: [PATCH 199/265] Fix missed deallocations in main tests. --- src/mlpack/tests/main_tests/fastmks_test.cpp | 5 ++++- .../tests/main_tests/gmm_train_test.cpp | 21 ++++++++++++++++--- src/mlpack/tests/main_tests/kde_test.cpp | 11 ++++++---- src/mlpack/tests/main_tests/knn_test.cpp | 3 ++- .../tests/main_tests/linear_svm_test.cpp | 4 ++-- .../main_tests/preprocess_scale_test.cpp | 14 ++++++++++--- .../tests/main_tests/range_search_test.cpp | 20 ++++++++++++++++-- 7 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/mlpack/tests/main_tests/fastmks_test.cpp b/src/mlpack/tests/main_tests/fastmks_test.cpp index 6546e93e86..1d81551038 100644 --- a/src/mlpack/tests/main_tests/fastmks_test.cpp +++ b/src/mlpack/tests/main_tests/fastmks_test.cpp @@ -397,7 +397,7 @@ BOOST_AUTO_TEST_CASE(FastMKSBaseTest) SetInputParam("base", 0.0); // Invalid. Log::Fatal.ignoreInput = true; - BOOST_REQUIRE_THROW(mlpackMain(), std::invalid_argument); + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } @@ -464,6 +464,9 @@ BOOST_AUTO_TEST_CASE(FastMKSKernelTest) CLI::GetSingleton().Parameters()["reference"].wasPassed = false; CLI::GetSingleton().Parameters()["query"].wasPassed = false; CLI::GetSingleton().Parameters()["kernel"].wasPassed = false; + + if (i != nofkerneltypes - 1) + bindings::tests::CleanMemory(); } } diff --git a/src/mlpack/tests/main_tests/gmm_train_test.cpp b/src/mlpack/tests/main_tests/gmm_train_test.cpp index 25ab918eb3..c0b8026aed 100644 --- a/src/mlpack/tests/main_tests/gmm_train_test.cpp +++ b/src/mlpack/tests/main_tests/gmm_train_test.cpp @@ -249,6 +249,8 @@ BOOST_AUTO_TEST_CASE(GmmTrainNoiseTest) GMM* gmm1 = CLI::GetParam("output_model"); BOOST_REQUIRE(CheckDifferent(gmm, gmm1)); + + delete gmm; } // Ensure that Trials affects the final result. @@ -273,7 +275,7 @@ BOOST_AUTO_TEST_CASE(GmmTrainTrialsTest) mlpackMain(); - GMM* gmm = std::move(CLI::GetParam("output_model")); + GMM* gmm = CLI::GetParam("output_model"); ResetGmmTrainSetting(); @@ -290,10 +292,12 @@ BOOST_AUTO_TEST_CASE(GmmTrainTrialsTest) GMM* gmm1 = CLI::GetParam("output_model"); success = CheckDifferent(gmm, gmm1); + + delete gmm; + if (success) break; - delete gmm; bindings::tests::CleanMemory(); } @@ -332,6 +336,8 @@ BOOST_AUTO_TEST_CASE(GmmTrainDiffMaxIterationsTest) GMM* gmm1 = CLI::GetParam("output_model"); BOOST_REQUIRE(CheckDifferent(gmm, gmm1)); + + delete gmm; } // Ensure that the maximum number of k-means iterations affects the result. @@ -375,10 +381,13 @@ BOOST_AUTO_TEST_CASE(GmmTrainDiffKmeansMaxIterationsTest) ResetGmmTrainSetting(); success = CheckDifferent(gmm, gmm1); + + delete gmm; + delete gmm1; + if (success) break; - delete gmm; bindings::tests::CleanMemory(); } @@ -419,6 +428,8 @@ BOOST_AUTO_TEST_CASE(GmmTrainPercentageTest) GMM* gmm1 = CLI::GetParam("output_model"); BOOST_REQUIRE(CheckDifferent(gmm, gmm1)); + + delete gmm; } // Ensure that Sampling affects the final result when refined_start is true. @@ -455,6 +466,8 @@ BOOST_AUTO_TEST_CASE(GmmTrainSamplingsTest) GMM* gmm1 = CLI::GetParam("output_model"); BOOST_REQUIRE(CheckDifferent(gmm, gmm1)); + + delete gmm; } // Ensure that tolerance affects the final result. @@ -487,6 +500,8 @@ BOOST_AUTO_TEST_CASE(GmmTrainToleranceTest) GMM* gmm1 = CLI::GetParam("output_model"); BOOST_REQUIRE(CheckDifferent(gmm, gmm1)); + + delete gmm; } // Ensure that saved model can be used again. diff --git a/src/mlpack/tests/main_tests/kde_test.cpp b/src/mlpack/tests/main_tests/kde_test.cpp index b8666cc165..69f38c60cf 100644 --- a/src/mlpack/tests/main_tests/kde_test.cpp +++ b/src/mlpack/tests/main_tests/kde_test.cpp @@ -30,14 +30,15 @@ struct KDETestFixture public: KDETestFixture() { - // Cache in the options for this program. - CLI::RestoreSettings(testName); + // Cache in the options for this program. + CLI::RestoreSettings(testName); } ~KDETestFixture() { - // Clear the settings. - CLI::ClearSettings(); + // Clear the settings. + bindings::tests::CleanMemory(); + CLI::ClearSettings(); } }; @@ -545,6 +546,8 @@ BOOST_AUTO_TEST_CASE(KDEMainMonteCarloFlag) mlpackMain(); estimations1 = std::move(CLI::GetParam("predictions")); + delete CLI::GetParam("output_model"); + // Compute estimations 2. SetInputParam("reference", reference); SetInputParam("query", query); diff --git a/src/mlpack/tests/main_tests/knn_test.cpp b/src/mlpack/tests/main_tests/knn_test.cpp index 18f91af45c..02b8c2e75d 100644 --- a/src/mlpack/tests/main_tests/knn_test.cpp +++ b/src/mlpack/tests/main_tests/knn_test.cpp @@ -683,6 +683,7 @@ BOOST_AUTO_TEST_CASE(KNNDifferentLeafSizes) BOOST_CHECK_EQUAL(output_model->LeafSize(), (int) 1); BOOST_CHECK_EQUAL(CLI::GetParam("output_model")->LeafSize(), (int) 10); - } + delete output_model; +} BOOST_AUTO_TEST_SUITE_END(); diff --git a/src/mlpack/tests/main_tests/linear_svm_test.cpp b/src/mlpack/tests/main_tests/linear_svm_test.cpp index 057a33d1e5..c0d1c5f136 100644 --- a/src/mlpack/tests/main_tests/linear_svm_test.cpp +++ b/src/mlpack/tests/main_tests/linear_svm_test.cpp @@ -414,7 +414,7 @@ BOOST_AUTO_TEST_CASE(LinearSVMNonNegativeEpochsTest) } /** - * Ensuring that number classes must not be zero. + * Ensuring that number classes must not be one. */ BOOST_AUTO_TEST_CASE(LinearSVMZeroNumberOfClassesTest) { @@ -429,7 +429,7 @@ BOOST_AUTO_TEST_CASE(LinearSVMZeroNumberOfClassesTest) SetInputParam("training", std::move(trainData)); SetInputParam("labels", std::move(trainLabels)); - // Number of classes for optimizer is zero. + // Number of classes for optimizer is only one. // It should throw a invalid_argument error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::invalid_argument); diff --git a/src/mlpack/tests/main_tests/preprocess_scale_test.cpp b/src/mlpack/tests/main_tests/preprocess_scale_test.cpp index debf848883..934542584b 100644 --- a/src/mlpack/tests/main_tests/preprocess_scale_test.cpp +++ b/src/mlpack/tests/main_tests/preprocess_scale_test.cpp @@ -57,16 +57,18 @@ BOOST_AUTO_TEST_CASE(TwoScalerTest) SetInputParam("scaler_method", method); mlpackMain(); - arma::mat max_abs_scaler_output = CLI::GetParam("output"); + arma::mat maxAbsScalerOutput = CLI::GetParam("output"); + + bindings::tests::CleanMemory(); method = "standard_scaler"; SetInputParam("input", dataset); SetInputParam("scaler_method", std::move(method)); mlpackMain(); - arma::mat standard_scaler_output = CLI::GetParam("output"); + arma::mat standardScalerOutput = CLI::GetParam("output"); - CheckMatricesNotEqual(standard_scaler_output, max_abs_scaler_output); + CheckMatricesNotEqual(standardScalerOutput, maxAbsScalerOutput); } /** @@ -83,6 +85,8 @@ BOOST_AUTO_TEST_CASE(TwoOptionTest) mlpackMain(); arma::mat output = CLI::GetParam("output"); + bindings::tests::CleanMemory(); + SetInputParam("input", dataset); SetInputParam("scaler_method", std::move(method)); SetInputParam("min_value", 2); @@ -107,6 +111,8 @@ BOOST_AUTO_TEST_CASE(UnrelatedOptionTest) mlpackMain(); arma::mat scaled = CLI::GetParam("output"); + bindings::tests::CleanMemory(); + SetInputParam("input", dataset); SetInputParam("scaler_method", std::move(method)); SetInputParam("min_value", 2); @@ -177,6 +183,8 @@ BOOST_AUTO_TEST_CASE(EpsilonTest) mlpackMain(); arma::mat scaled = CLI::GetParam("output"); + bindings::tests::CleanMemory(); + SetInputParam("scaler_method", std::move(method)); SetInputParam("input", dataset); SetInputParam("epsilon", 1.0); diff --git a/src/mlpack/tests/main_tests/range_search_test.cpp b/src/mlpack/tests/main_tests/range_search_test.cpp index 5673e1ef09..e28b14a4a3 100644 --- a/src/mlpack/tests/main_tests/range_search_test.cpp +++ b/src/mlpack/tests/main_tests/range_search_test.cpp @@ -278,7 +278,7 @@ BOOST_AUTO_TEST_CASE(ModelCheck) neighbors = ReadData(neighborsFile); distances = ReadData(distanceFile); - RSModel* outputModel = move(CLI::GetParam("output_model")); + RSModel* outputModel = CLI::GetParam("output_model"); CLI::GetSingleton().Parameters()["reference"].wasPassed = false; SetInputParam("input_model", outputModel); @@ -293,7 +293,7 @@ BOOST_AUTO_TEST_CASE(ModelCheck) CheckMatrices(distances, distancetemp); BOOST_REQUIRE_EQUAL(ModelToString(outputModel), - ModelToString(CLI::GetParam("output_model"))); + ModelToString(CLI::GetParam("output_model"))); remove(neighborsFile.c_str()); remove(distanceFile.c_str()); @@ -351,8 +351,13 @@ BOOST_AUTO_TEST_CASE(LeafValueTesting) BOOST_REQUIRE_NE(ModelToString(outputModel1), ModelToString(CLI::GetParam("output_model"))); + + if (i != leafSizes.size() - 1) + delete CLI::GetParam("output_model"); } + delete outputModel1; + remove(neighborsFile.c_str()); remove(distanceFile.c_str()); } @@ -419,8 +424,13 @@ BOOST_AUTO_TEST_CASE(TreeTypeTesting) CheckMatrices(distances, distancestemp); BOOST_REQUIRE_NE(ModelToString(outputModel1), ModelToString(CLI::GetParam("output_model"))); + + if (i != trees.size() - 1) + delete CLI::GetParam("output_model"); } + delete outputModel1; + remove(neighborsFile.c_str()); remove(distanceFile.c_str()); } @@ -463,6 +473,8 @@ BOOST_AUTO_TEST_CASE(RandomBasisTesting) BOOST_REQUIRE_NE(ModelToString(outputModel), ModelToString(CLI::GetParam("output_model"))); + delete outputModel; + remove(neighborsFile.c_str()); remove(distanceFile.c_str()); } @@ -515,6 +527,8 @@ BOOST_AUTO_TEST_CASE(NaiveModeTest) BOOST_REQUIRE_NE(ModelToString(outputModel), ModelToString(CLI::GetParam("output_model"))); + delete outputModel; + remove(neighborsFile.c_str()); remove(distanceFile.c_str()); } @@ -566,6 +580,8 @@ BOOST_AUTO_TEST_CASE(SingleModeTest) BOOST_REQUIRE_NE(ModelToString(outputModel), ModelToString(CLI::GetParam("output_model"))); + delete outputModel; + remove(neighborsFile.c_str()); remove(distanceFile.c_str()); } From f172fcf90390bc41b66e60b79efa0ac94874d781 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 16:51:09 -0400 Subject: [PATCH 200/265] Fix memory leak in tree test. --- src/mlpack/tests/tree_test.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/mlpack/tests/tree_test.cpp b/src/mlpack/tests/tree_test.cpp index 22deb31a58..af8eaeb081 100644 --- a/src/mlpack/tests/tree_test.cpp +++ b/src/mlpack/tests/tree_test.cpp @@ -2129,12 +2129,15 @@ BOOST_AUTO_TEST_CASE(BinarySpaceTreeCopyConstructor) TreeType b(data); b.Begin() = 10; b.Count() = 50; + b.Left() = new TreeType(data); b.Left()->Begin() = 10; b.Left()->Count() = 30; + b.Left()->Parent() = &b; b.Right() = new TreeType(data); b.Right()->Begin() = 40; b.Right()->Count() = 20; + b.Right()->Parent() = &b; // Copy the tree. TreeType c(b); @@ -2159,6 +2162,11 @@ BOOST_AUTO_TEST_CASE(BinarySpaceTreeCopyConstructor) BOOST_REQUIRE_EQUAL(b.Right()->Left(), c.Right()->Left()); BOOST_REQUIRE_EQUAL(b.Right()->Right(), (TreeType*) NULL); BOOST_REQUIRE_EQUAL(b.Right()->Right(), c.Right()->Right()); + + // Clean memory (we built the tree by hand, so this is what we have to do + // since the destructor won't free the children's datasets). + delete &b.Left()->Dataset(); + delete &b.Right()->Dataset(); } //! Count the number of leaves under this node. From 7dabe28e3192081c6aefa122680502ad5f66e77d Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sun, 22 Mar 2020 21:51:10 -0400 Subject: [PATCH 201/265] Fix minor syntax errors (should have checked!). --- src/mlpack/methods/ann/layer/add_merge.hpp | 2 +- src/mlpack/methods/ann/layer/add_merge_impl.hpp | 2 +- src/mlpack/methods/ann/layer/sequential.hpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/ann/layer/add_merge.hpp b/src/mlpack/methods/ann/layer/add_merge.hpp index aafea251c2..7b47988a79 100644 --- a/src/mlpack/methods/ann/layer/add_merge.hpp +++ b/src/mlpack/methods/ann/layer/add_merge.hpp @@ -244,7 +244,7 @@ struct version> { BOOST_STATIC_CONSTANT(int, value = 1); -} +}; } // namespace serialization } // namespace boost diff --git a/src/mlpack/methods/ann/layer/add_merge_impl.hpp b/src/mlpack/methods/ann/layer/add_merge_impl.hpp index 7ac8c10f5b..bf47437172 100644 --- a/src/mlpack/methods/ann/layer/add_merge_impl.hpp +++ b/src/mlpack/methods/ann/layer/add_merge_impl.hpp @@ -150,7 +150,7 @@ template template void AddMerge::serialize( - Archive& ar, const unsigned int /* version */) + Archive& ar, const unsigned int version) { // Be sure to clear other layers before loading. if (Archive::is_loading::value) diff --git a/src/mlpack/methods/ann/layer/sequential.hpp b/src/mlpack/methods/ann/layer/sequential.hpp index fcf6a32ef3..3b84fa9bcd 100644 --- a/src/mlpack/methods/ann/layer/sequential.hpp +++ b/src/mlpack/methods/ann/layer/sequential.hpp @@ -264,7 +264,7 @@ struct version> { BOOST_STATIC_CONSTANT(int, value = 1); -} +}; } // namespace serialization } // namespace boost From edca4cd82b1965299fa4811d69fb5350c3203889 Mon Sep 17 00:00:00 2001 From: Bisakh Mondal <41498427+bisakhmondal@users.noreply.github.com> Date: Mon, 23 Mar 2020 09:55:18 +0530 Subject: [PATCH 202/265] Apply suggestions from code review Co-Authored-By: favre49 <40389657+favre49@users.noreply.github.com> --- src/mlpack/core/cv/metrics/r2_score.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/core/cv/metrics/r2_score.hpp b/src/mlpack/core/cv/metrics/r2_score.hpp index a1ff04bf0f..7ffb608760 100644 --- a/src/mlpack/core/cv/metrics/r2_score.hpp +++ b/src/mlpack/core/cv/metrics/r2_score.hpp @@ -24,11 +24,11 @@ namespace cv { * an indication of goodness of fit and therefore a measure of how * well unseen samples are likely to be predicted by the model, * through the proportion of explained variance. - * As R2Score is dataset dependent it can have wide range of values, + * As R2Score is dataset dependent it can have wide range of values, * best possible score is @f$R^2 =1.0@f$, and it can be negative too for an * arbitraryly worse model. For a model which predicts exactly the expected * value of y, disregarding the input features, gets a R2Score equals to 0.0. - * If a model predicts @f$ \hat{y}_i $@f of the @f$ i $@f-th sample for a true + * If a model predicts @f$ \hat{y}_i $@f of the @f$ i $@f-th sample for a true * @f$ y_i $@f for total n samples, the R2Score is calculated by * @f{eqnarray*}{ * R^{2} \left( y, \hat{y} \right) &=& 1-\frac{\sum_{i=1}^{n} From ee7206576e8968db900612264dd8d5a289eeb72e Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Mon, 23 Mar 2020 11:27:39 +0530 Subject: [PATCH 203/265] style fixes --- src/mlpack/core/cv/metrics/r2_score.hpp | 13 ++++++++----- src/mlpack/core/cv/metrics/r2_score_impl.hpp | 11 ++++++----- src/mlpack/tests/cv_test.cpp | 11 ++++++----- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/mlpack/core/cv/metrics/r2_score.hpp b/src/mlpack/core/cv/metrics/r2_score.hpp index 7ffb608760..0654899498 100644 --- a/src/mlpack/core/cv/metrics/r2_score.hpp +++ b/src/mlpack/core/cv/metrics/r2_score.hpp @@ -25,15 +25,18 @@ namespace cv { * well unseen samples are likely to be predicted by the model, * through the proportion of explained variance. * As R2Score is dataset dependent it can have wide range of values, - * best possible score is @f$R^2 =1.0@f$, and it can be negative too for an - * arbitraryly worse model. For a model which predicts exactly the expected - * value of y, disregarding the input features, gets a R2Score equals to 0.0. + * best possible score is @f$R^2 =1.0@f$.arbitraryly worse model. Values + * of R2 outside the range 0 to 1 can occur when the model fits the data + * worse than a horizontal hyperplane. This would occur when the wrong model + * was chosen, or nonsensical constraints were applied by mistake. For a model + * which predicts exactly the expected value of y, disregarding the input + * features, gets a R2Score equals to 0.0. * If a model predicts @f$ \hat{y}_i $@f of the @f$ i $@f-th sample for a true * @f$ y_i $@f for total n samples, the R2Score is calculated by * @f{eqnarray*}{ * R^{2} \left( y, \hat{y} \right) &=& 1-\frac{\sum_{i=1}^{n} - * \left( y_i - \hat{y_i} \right)^2 } - * {\sum_{i=1}^{n} \left( y_i - \bar{y}\right)^2}\\ + * \left( y_i - \hat{y_i} \right)^2 } + * {\sum_{i=1}^{n} \left( y_i - \bar{y}\right)^2}\\ * @f} * where @f$ \bar{y} = frac{1}{y}\sum_{i=1}^{n} y_i $@f. * For example, a model having R2Score = 0.85, explains 85 \% variability of diff --git a/src/mlpack/core/cv/metrics/r2_score_impl.hpp b/src/mlpack/core/cv/metrics/r2_score_impl.hpp index 6fecfac378..578c73d840 100644 --- a/src/mlpack/core/cv/metrics/r2_score_impl.hpp +++ b/src/mlpack/core/cv/metrics/r2_score_impl.hpp @@ -16,9 +16,10 @@ namespace mlpack { namespace cv { template -double R2Score::Evaluate(MLAlgorithm& model, - const DataType& data, - const ResponsesType& responses) +double R2Score::Evaluate( + MLAlgorithm& model, + const DataType& data, + const ResponsesType& responses) { if (data.n_cols != responses.n_cols) { @@ -44,13 +45,13 @@ double R2Score::Evaluate(MLAlgorithm& model, // Handling undefined R2Score when both denominator and numerator is 0.0 if (ss_res == 0.0) { - if (ss_tot !=0.0) + if (ss_tot != 0.0) return 1.0; else return DBL_MIN; } - return 1 - ss_res/ss_tot; + return 1 - ss_res / ss_tot; } } // namespace cv diff --git a/src/mlpack/tests/cv_test.cpp b/src/mlpack/tests/cv_test.cpp index 8886f68c4b..b93ea13e3b 100644 --- a/src/mlpack/tests/cv_test.cpp +++ b/src/mlpack/tests/cv_test.cpp @@ -181,12 +181,13 @@ BOOST_AUTO_TEST_CASE(R2ScoreTest) // Mean Responses = (1 + 2 + 3 + 6 + 8)/5 = 4 arma::mat data("2 3 4 7 9"); arma::rowvec responses("1 2.005 3 6.005 8.005"); - - double ss_reg = (0 * 0 + 0.005 * 0.005 + 0 * 0 - + 0.005 * 0.005 + 0.005 *0.005); + + double ss_reg = (0 * 0 + 0.005 * 0.005 + 0 * 0 + + 0.005 * 0.005 + 0.005 *0.005); double ss_tot = ((1 - 4) * (1- 4) + (2.005 - 4) * - (2.005 - 4) + (3 - 4) * (3 - 4) + (6.005 - 4) * - (6.005 - 4) + (8.005 - 4) * (8.005 -4)); + (2.005 - 4) + (3 - 4) * (3 - 4) + (6.005 - 4) * + (6.005 - 4) + (8.005 - 4) * (8.005 -4)); + double expectedR2 = 1 - ss_reg / ss_tot; BOOST_REQUIRE_CLOSE(R2Score::Evaluate(lr, data, responses), expectedR2, 1e-5); From c42b9f7670a0e4ba5902ce93fa4625e011ca99ec Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 23 Mar 2020 08:25:33 -0400 Subject: [PATCH 204/265] Fix comment, thanks @saksham189! --- src/mlpack/methods/ann/brnn_impl.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/ann/brnn_impl.hpp b/src/mlpack/methods/ann/brnn_impl.hpp index 69e9020768..f7ceef7677 100644 --- a/src/mlpack/methods/ann/brnn_impl.hpp +++ b/src/mlpack/methods/ann/brnn_impl.hpp @@ -66,8 +66,9 @@ template::~BRNN() { - // Remove mergeLayer from the forward and backward RNNs so it doesn't get - // deleted. This assumes that mergeLayer is the last layer! + // Remove the last layers from the forward and backward RNNs, as they are held + // in mergeLayer. So, when we use DeleteVisitor with mergeLayer, those two + // layers will be properly (and not doubly) freed. forwardRNN.network.pop_back(); backwardRNN.network.pop_back(); From b1e51468f0a95c9cb82a06cb2104eab74c4c2937 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 23 Mar 2020 08:27:01 -0400 Subject: [PATCH 205/265] Remove unnecessary braces. --- src/mlpack/methods/ann/layer/add_merge_impl.hpp | 4 ---- src/mlpack/methods/ann/layer/sequential_impl.hpp | 4 ---- src/mlpack/methods/ann/visitor/delete_visitor_impl.hpp | 2 -- .../methods/hoeffding_trees/hoeffding_tree_model.hpp | 8 -------- 4 files changed, 18 deletions(-) diff --git a/src/mlpack/methods/ann/layer/add_merge_impl.hpp b/src/mlpack/methods/ann/layer/add_merge_impl.hpp index bf47437172..d3cd789d6a 100644 --- a/src/mlpack/methods/ann/layer/add_merge_impl.hpp +++ b/src/mlpack/methods/ann/layer/add_merge_impl.hpp @@ -161,13 +161,9 @@ void AddMerge::serialize( ar & BOOST_SERIALIZATION_NVP(run); if (version >= 1) - { ar & BOOST_SERIALIZATION_NVP(ownsLayers); - } else if (Archive::is_loading::value) - { ownsLayers = !model; - } } } // namespace ann diff --git a/src/mlpack/methods/ann/layer/sequential_impl.hpp b/src/mlpack/methods/ann/layer/sequential_impl.hpp index 6b26f86209..994d377b4c 100644 --- a/src/mlpack/methods/ann/layer/sequential_impl.hpp +++ b/src/mlpack/methods/ann/layer/sequential_impl.hpp @@ -204,13 +204,9 @@ void Sequential< ar & BOOST_SERIALIZATION_NVP(network); if (version >= 1) - { ar & BOOST_SERIALIZATION_NVP(ownsLayers); - } else if (Archive::is_loading::value) - { ownsLayers = !model; - } } } // namespace ann diff --git a/src/mlpack/methods/ann/visitor/delete_visitor_impl.hpp b/src/mlpack/methods/ann/visitor/delete_visitor_impl.hpp index 43e01f764c..e2e82d5880 100644 --- a/src/mlpack/methods/ann/visitor/delete_visitor_impl.hpp +++ b/src/mlpack/methods/ann/visitor/delete_visitor_impl.hpp @@ -36,9 +36,7 @@ DeleteVisitor::operator()(LayerType* layer) const if (layer) { for (size_t i = 0; i < layer->Model().size(); ++i) - { boost::apply_visitor(DeleteVisitor(), layer->Model()[i]); - } delete layer; } diff --git a/src/mlpack/methods/hoeffding_trees/hoeffding_tree_model.hpp b/src/mlpack/methods/hoeffding_trees/hoeffding_tree_model.hpp index bdf12e3d6b..83ef9edf76 100644 --- a/src/mlpack/methods/hoeffding_trees/hoeffding_tree_model.hpp +++ b/src/mlpack/methods/hoeffding_trees/hoeffding_tree_model.hpp @@ -187,21 +187,13 @@ class HoeffdingTreeModel // Fake dataset info may be needed to create fake trees. data::DatasetInfo info; if (type == GINI_HOEFFDING) - { ar & BOOST_SERIALIZATION_NVP(giniHoeffdingTree); - } else if (type == GINI_BINARY) - { ar & BOOST_SERIALIZATION_NVP(giniBinaryTree); - } else if (type == INFO_HOEFFDING) - { ar & BOOST_SERIALIZATION_NVP(infoHoeffdingTree); - } else if (type == INFO_BINARY) - { ar & BOOST_SERIALIZATION_NVP(infoBinaryTree); - } } private: From c8795e766b8177a9080067a3fe609b388c9aeb17 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 23 Mar 2020 10:50:57 -0400 Subject: [PATCH 206/265] Force the use of the old HDF5 API as a build workaround. --- src/mlpack/core/arma_extend/arma_extend.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/mlpack/core/arma_extend/arma_extend.hpp b/src/mlpack/core/arma_extend/arma_extend.hpp index e323b993ea..9539d1a534 100644 --- a/src/mlpack/core/arma_extend/arma_extend.hpp +++ b/src/mlpack/core/arma_extend/arma_extend.hpp @@ -36,6 +36,13 @@ #endif #endif +// Force definition of old HDF5 API. Thanks to Mike Roberts for helping find +// this workaround. +#if !defined(H5_USE_110_API) + #undef H5_USE_18_API + #define H5_USE_18_API +#endif + // Include everything we'll need for serialize(). #include #include From 2e5110a20daa41a24f16928a25bc790e367dfcbc Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Tue, 24 Mar 2020 09:03:52 +0530 Subject: [PATCH 207/265] Update src/mlpack/core/cv/metrics/r2_score.hpp Co-Authored-By: favre49 <40389657+favre49@users.noreply.github.com> --- src/mlpack/core/cv/metrics/r2_score.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/core/cv/metrics/r2_score.hpp b/src/mlpack/core/cv/metrics/r2_score.hpp index 0654899498..8a08aa6f74 100644 --- a/src/mlpack/core/cv/metrics/r2_score.hpp +++ b/src/mlpack/core/cv/metrics/r2_score.hpp @@ -25,7 +25,7 @@ namespace cv { * well unseen samples are likely to be predicted by the model, * through the proportion of explained variance. * As R2Score is dataset dependent it can have wide range of values, - * best possible score is @f$R^2 =1.0@f$.arbitraryly worse model. Values + * best possible score is @f$R^2 =1.0@f$. Values * of R2 outside the range 0 to 1 can occur when the model fits the data * worse than a horizontal hyperplane. This would occur when the wrong model * was chosen, or nonsensical constraints were applied by mistake. For a model From 0086654e92aa9ff3955167079a413e5fdb881efa Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Tue, 24 Mar 2020 09:32:47 +0530 Subject: [PATCH 208/265] R2Score tests hardcoded --- src/mlpack/core/cv/metrics/r2_score.hpp | 10 +++++----- src/mlpack/core/cv/metrics/r2_score_impl.hpp | 7 +++---- src/mlpack/tests/cv_test.cpp | 8 +------- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/mlpack/core/cv/metrics/r2_score.hpp b/src/mlpack/core/cv/metrics/r2_score.hpp index 8a08aa6f74..b96745937e 100644 --- a/src/mlpack/core/cv/metrics/r2_score.hpp +++ b/src/mlpack/core/cv/metrics/r2_score.hpp @@ -25,11 +25,11 @@ namespace cv { * well unseen samples are likely to be predicted by the model, * through the proportion of explained variance. * As R2Score is dataset dependent it can have wide range of values, - * best possible score is @f$R^2 =1.0@f$. Values - * of R2 outside the range 0 to 1 can occur when the model fits the data - * worse than a horizontal hyperplane. This would occur when the wrong model - * was chosen, or nonsensical constraints were applied by mistake. For a model - * which predicts exactly the expected value of y, disregarding the input + * best possible score is @f$R^2 =1.0@f$. Values of R2 outside the range + * 0 to 1 can occur when the model fits the data worse than a horizontal + * hyperplane. This would occur when the wrong model was chosen, or + * nonsensical constraints were applied by mistake. For a model which + * predicts exactly the expected value of y, disregarding the input * features, gets a R2Score equals to 0.0. * If a model predicts @f$ \hat{y}_i $@f of the @f$ i $@f-th sample for a true * @f$ y_i $@f for total n samples, the R2Score is calculated by diff --git a/src/mlpack/core/cv/metrics/r2_score_impl.hpp b/src/mlpack/core/cv/metrics/r2_score_impl.hpp index 578c73d840..0bc398fd2d 100644 --- a/src/mlpack/core/cv/metrics/r2_score_impl.hpp +++ b/src/mlpack/core/cv/metrics/r2_score_impl.hpp @@ -16,10 +16,9 @@ namespace mlpack { namespace cv { template -double R2Score::Evaluate( - MLAlgorithm& model, - const DataType& data, - const ResponsesType& responses) +double R2Score::Evaluate(MLAlgorithm& model, + const DataType& data, + const ResponsesType& responses) { if (data.n_cols != responses.n_cols) { diff --git a/src/mlpack/tests/cv_test.cpp b/src/mlpack/tests/cv_test.cpp index b93ea13e3b..f896a209d3 100644 --- a/src/mlpack/tests/cv_test.cpp +++ b/src/mlpack/tests/cv_test.cpp @@ -182,13 +182,7 @@ BOOST_AUTO_TEST_CASE(R2ScoreTest) arma::mat data("2 3 4 7 9"); arma::rowvec responses("1 2.005 3 6.005 8.005"); - double ss_reg = (0 * 0 + 0.005 * 0.005 + 0 * 0 + - 0.005 * 0.005 + 0.005 *0.005); - double ss_tot = ((1 - 4) * (1- 4) + (2.005 - 4) * - (2.005 - 4) + (3 - 4) * (3 - 4) + (6.005 - 4) * - (6.005 - 4) + (8.005 - 4) * (8.005 -4)); - - double expectedR2 = 1 - ss_reg / ss_tot; + double expectedR2 = 0.99999779; BOOST_REQUIRE_CLOSE(R2Score::Evaluate(lr, data, responses), expectedR2, 1e-5); } From 8841e1cb29b0385ac919e8730e718f88dae41d58 Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Tue, 24 Mar 2020 09:41:21 +0530 Subject: [PATCH 209/265] full stop added --- src/mlpack/tests/cv_test.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mlpack/tests/cv_test.cpp b/src/mlpack/tests/cv_test.cpp index f896a209d3..a1240fe69f 100644 --- a/src/mlpack/tests/cv_test.cpp +++ b/src/mlpack/tests/cv_test.cpp @@ -170,15 +170,15 @@ BOOST_AUTO_TEST_CASE(MSETest) */ BOOST_AUTO_TEST_CASE(R2ScoreTest) { - // Making two points that define the linear function f(x) = x - 1 + // Making two points that define the linear function f(x) = x - 1. arma::mat trainingData("0 1"); arma::rowvec trainingResponses("-1 0"); LinearRegression lr(trainingData, trainingResponses); // Making five responses that are the output of regression function f(x) - // with some responses having a slight deviation of 0.005 - // Mean Responses = (1 + 2 + 3 + 6 + 8)/5 = 4 + // with some responses having a slight deviation of 0.005. + // Mean Responses = (1 + 2 + 3 + 6 + 8)/5 = 4. arma::mat data("2 3 4 7 9"); arma::rowvec responses("1 2.005 3 6.005 8.005"); From 0cf6af509690c9bb3b01236a60f92a50a6f29e8a Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Tue, 24 Mar 2020 11:29:59 +0530 Subject: [PATCH 210/265] Serialization Fix --- src/mlpack/methods/ann/layer/concat_performance_impl.hpp | 4 ++-- src/mlpack/methods/ann/layer/reinforce_normal.hpp | 2 +- src/mlpack/methods/ann/layer/reinforce_normal_impl.hpp | 4 ++-- src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp | 5 +++-- src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp | 4 ++-- src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp | 4 ++-- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/mlpack/methods/ann/layer/concat_performance_impl.hpp b/src/mlpack/methods/ann/layer/concat_performance_impl.hpp index 7cf55dc41b..96894bc404 100644 --- a/src/mlpack/methods/ann/layer/concat_performance_impl.hpp +++ b/src/mlpack/methods/ann/layer/concat_performance_impl.hpp @@ -102,9 +102,9 @@ void ConcatPerformance< OutputLayerType, InputDataType, OutputDataType ->::serialize(Archive& /* ar */, const unsigned int /* version */) +>::serialize(Archive& ar, const unsigned int /* version */) { - // Nothing to do here. + ar & BOOST_SERIALIZATION_NVP(inSize); } } // namespace ann diff --git a/src/mlpack/methods/ann/layer/reinforce_normal.hpp b/src/mlpack/methods/ann/layer/reinforce_normal.hpp index e9be3578a2..c26bc8f495 100644 --- a/src/mlpack/methods/ann/layer/reinforce_normal.hpp +++ b/src/mlpack/methods/ann/layer/reinforce_normal.hpp @@ -87,7 +87,7 @@ class ReinforceNormal * Serialize the layer */ template - void serialize(Archive& /* ar */, const unsigned int /* version */); + void serialize(Archive& ar, const unsigned int /* version */); private: //! Standard deviation used during the forward and backward pass. diff --git a/src/mlpack/methods/ann/layer/reinforce_normal_impl.hpp b/src/mlpack/methods/ann/layer/reinforce_normal_impl.hpp index b526694dc9..a0a2b03d4f 100644 --- a/src/mlpack/methods/ann/layer/reinforce_normal_impl.hpp +++ b/src/mlpack/methods/ann/layer/reinforce_normal_impl.hpp @@ -63,9 +63,9 @@ void ReinforceNormal::Backward( template template void ReinforceNormal::serialize( - Archive& /* ar */, const unsigned int /* version */) + Archive& ar, const unsigned int /* version */) { - // Nothing to do here. + ar & BOOST_SERIALIZATION_NVP(stdev); } } // namespace ann diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp index e638a1eb22..25da492bcf 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp @@ -63,10 +63,11 @@ void HuberLoss::Backward( template template void HuberLoss::serialize( - Archive& /* ar */, + Archive& ar, const unsigned int /* version */) { - // Nothing to do here. + ar & BOOST_SERIALIZATION_NVP(delta); + ar & BOOST_SERIALIZATION_NVP(mean); } } // namespace ann diff --git a/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp b/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp index 4bd6a143dc..dc72dc1645 100644 --- a/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp @@ -62,10 +62,10 @@ void KLDivergence::Backward( template template void KLDivergence::serialize( - Archive& /* ar */, + Archive& ar, const unsigned int /* version */) { - // Nothing to do here. + ar & BOOST_SERIALIZATION_NVP(takeMean); } } // namespace ann diff --git a/src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp index a74aeb2e59..5de18340e6 100644 --- a/src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp @@ -47,10 +47,10 @@ void LogCoshLoss::Backward( template template void LogCoshLoss::serialize( - Archive& /* ar */, + Archive& ar, const unsigned int /* version */) { - // Nothing to do here. + ar & BOOST_SERIALIZATION_NVP(a); } } // namespace ann From 30577238cc40e0d6015292ae3b96f40beb6b8561 Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Tue, 24 Mar 2020 21:59:57 +0530 Subject: [PATCH 211/265] Apply suggestions from code review Co-Authored-By: favre49 <40389657+favre49@users.noreply.github.com> --- src/mlpack/core/cv/metrics/r2_score.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/core/cv/metrics/r2_score.hpp b/src/mlpack/core/cv/metrics/r2_score.hpp index b96745937e..26b4fb25d9 100644 --- a/src/mlpack/core/cv/metrics/r2_score.hpp +++ b/src/mlpack/core/cv/metrics/r2_score.hpp @@ -24,11 +24,11 @@ namespace cv { * an indication of goodness of fit and therefore a measure of how * well unseen samples are likely to be predicted by the model, * through the proportion of explained variance. - * As R2Score is dataset dependent it can have wide range of values, + * As R2Score is dataset dependent it can have wide range of values. The * best possible score is @f$R^2 =1.0@f$. Values of R2 outside the range * 0 to 1 can occur when the model fits the data worse than a horizontal * hyperplane. This would occur when the wrong model was chosen, or - * nonsensical constraints were applied by mistake. For a model which + * nonsensical constraints were applied by mistake. A model which * predicts exactly the expected value of y, disregarding the input * features, gets a R2Score equals to 0.0. * If a model predicts @f$ \hat{y}_i $@f of the @f$ i $@f-th sample for a true From af930b79358635a79ff484105e4e1b47b239db32 Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Tue, 24 Mar 2020 22:12:30 +0530 Subject: [PATCH 212/265] Update HISTORY.md --- HISTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 9dba6a517d..01e5493f38 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,7 @@ ### mlpack ?.?.? ###### ????-??-?? + * Added `R2Score` regression metric (#2323). + * Added `mean squared logarithmic error` loss function for neural networks (#2210). From 5bbed7ea78a5f050751ba5df59bcb3990fb9df51 Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Tue, 17 Mar 2020 23:25:01 +0530 Subject: [PATCH 213/265] Row major to Column Major --- .../bag_of_words_encoding_policy.hpp | 6 +- .../dictionary_encoding_policy.hpp | 6 +- .../tf_idf_encoding_policy.hpp | 8 ++- src/mlpack/tests/string_encoding_test.cpp | 61 ++++++++++--------- 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index b6f164db36..8c7bc83036 100644 --- a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -49,7 +49,7 @@ class BagOfWordsEncodingPolicy const size_t /* maxNumTokens */, const size_t dictionarySize) { - output.zeros(datasetSize, dictionarySize); + output.zeros(dictionarySize, datasetSize); } /** @@ -76,6 +76,7 @@ class BagOfWordsEncodingPolicy /** * The function performs the bag of words encoding algorithm i.e. it writes * the encoded token to the output. + * Returns the encodings in column-major format. * * @tparam MatType The output matrix type. * @@ -91,13 +92,14 @@ class BagOfWordsEncodingPolicy const size_t /* col */) { // The labels are assigned sequentially starting from one. - output(row, value - 1) = 1; + output(value - 1, row) = 1; } /** * The function performs the bag of words encoding algorithm i.e. it writes * the encoded token to the output. * Overload function to accepted vector> as output type. + * Returns the encodings in row-major format. * * @param output Output matrix to store the encoded results. * @param value The encoded token. diff --git a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp index e3de62e526..e907df5c1d 100644 --- a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp @@ -47,12 +47,13 @@ class DictionaryEncodingPolicy const size_t maxNumTokens, const size_t /* dictionarySize */) { - output.zeros(datasetSize, maxNumTokens); + output.zeros(maxNumTokens, datasetSize); } /** * The function performs the dictionary encoding algorithm i.e. it writes * the encoded token to the ouput. + * Returns the encodings in column-major format. * * @tparam MatType The output matrix type. * @@ -67,13 +68,14 @@ class DictionaryEncodingPolicy const size_t row, const size_t col) { - output(row, col) = value; + output(col, row) = value; } /** * The function performs the dictionary encoding algorithm i.e. it writes * the encoded token to the ouput. This is an overload function which saves * the result into the given vector to avoid padding. + * Returns the encodings in row-major format. * * @tparam OutputType Type of the output vector. * diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index ecba9f1a8d..eba42ee0ca 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -92,7 +92,7 @@ class TfIdfEncodingPolicy const size_t /* maxNumTokens */, const size_t dictionarySize) { - output.zeros(datasetSize, dictionarySize); + output.zeros(dictionarySize, datasetSize); } /** @@ -119,6 +119,7 @@ class TfIdfEncodingPolicy /** * The function performs the TfIdf encoding algorithm i.e. it writes * the encoded token to the output. + * Returns the encodings in column-major format. * * @tparam MatType The output matrix type. * @@ -139,9 +140,9 @@ class TfIdfEncodingPolicy const typename MatType::elem_type idf = InverseDocumentFrequency( - output.n_rows, numContainingStrings[value]); + output.n_cols, numContainingStrings[value]); - output(row, value - 1) = tf * idf; + output(value - 1, row) = tf * idf; } /** @@ -149,6 +150,7 @@ class TfIdfEncodingPolicy * the encoded token to the output. * Overloaded function to accept vector> as the output * type. + * Returns the encodings in row-major format. * * @tparam OutputType Type of the output vector. * diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 8e4b8107b9..c8f3821c4e 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -56,6 +56,17 @@ static vector stringEncodingUtf8Input = { "\xE2\x93\x82\xE2\x93\x81\xE2\x93\x85\xE2\x92\xB6\xE2\x92\xB8\xE2\x93\x80" }; +/** + * Function used to compare two vectors. + */ +template +void checkVector(vector> A, vector> B) +{ + for (size_t i = 0; i < A.size(); i++) + for (size_t j = 0; j < A[i].size(); j++) + BOOST_REQUIRE_CLOSE(A[i][j], B[i][j], 1e-12); +} + /** * Test the dictionary encoding algorithm. */ @@ -90,7 +101,7 @@ BOOST_AUTO_TEST_CASE(DictionaryEncodingTest) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; - CheckMatrices(output, expected); + CheckMatrices(output, expected.t()); } /** @@ -124,7 +135,7 @@ BOOST_AUTO_TEST_CASE(UnicodeDictionaryEncodingTest) { 5, 2, 3, 5, 4 } }; - CheckMatrices(output, expected); + CheckMatrices(output, expected.t()); } /** @@ -244,7 +255,7 @@ BOOST_AUTO_TEST_CASE(DictionaryEncodingIndividualCharactersTest) { 2, 4, 3, 2, 4, 3, 5 }, { 1, 2, 4, 0, 0, 0, 0 } }; - CheckMatrices(output, target); + CheckMatrices(output, target.t()); } /** @@ -551,7 +562,7 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; - CheckMatrices(output, expected); + CheckMatrices(output, expected.t()); } /** @@ -613,7 +624,7 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) { 1, 1, 0, 1, 0 } }; - CheckMatrices(output, target); + CheckMatrices(output, target.t()); } /** @@ -686,7 +697,7 @@ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995 } }; - CheckMatrices(output, expected, 1e-12); + CheckMatrices(output, expected.t(), 1e-12); } /** @@ -735,9 +746,7 @@ BOOST_AUTO_TEST_CASE(VectorRawCountSmoothIdfEncodingTest) 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995 } }; - for (size_t i = 0; i < expected.size(); i++) - for (size_t j = 0; j < expected[i].size(); j++) - BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); + checkVector(output, expected); } /** @@ -762,7 +771,7 @@ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } }; - CheckMatrices(output, target, 1e-12); + CheckMatrices(output, target.t(), 1e-12); } /** @@ -787,9 +796,7 @@ BOOST_AUTO_TEST_CASE(VectorRawCountSmoothIdfEncodingIndividualCharactersTest) { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } }; - for (size_t i = 0; i < expected.size(); i++) - for (size_t j = 0; j < expected[i].size(); j++) - BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); + checkVector(output, expected); } /** @@ -837,7 +844,7 @@ BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811 } }; - CheckMatrices(output, expected, 1e-12); + CheckMatrices(output, expected.t(), 1e-12); } /** @@ -886,9 +893,7 @@ BOOST_AUTO_TEST_CASE(VectorTfIdfRawCountEncodingTest) 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811 } }; - for (size_t i = 0; i < expected.size(); i++) - for (size_t j = 0; j < expected[i].size(); j++) - BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); + checkVector(output, expected); } /** @@ -914,7 +919,7 @@ BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } }; - CheckMatrices(output, target, 1e-12); + CheckMatrices(output, target.t(), 1e-12); } /** @@ -941,9 +946,7 @@ BOOST_AUTO_TEST_CASE(VectorRawcountEncodingIndividualCharactersTest) { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } }; - for (size_t i = 0; i < expected.size(); i++) - for (size_t j = 0; j < expected[i].size(); j++) - BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); + checkVector(output, expected); } /** @@ -969,7 +972,7 @@ BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } }; - CheckMatrices(output, target, 1e-12); + CheckMatrices(output, target.t(), 1e-12); } /** @@ -995,9 +998,7 @@ BOOST_AUTO_TEST_CASE(VectorBnarySmoothIdfEncodingIndividualCharactersTest) { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } }; - for (size_t i = 0; i < expected.size(); i++) - for (size_t j = 0; j < expected[i].size(); j++) - BOOST_REQUIRE_CLOSE(expected[i][j], output[i][j], 1e-12); + checkVector(output, expected); } /** @@ -1023,7 +1024,7 @@ BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) { 0, 1, 1.4054651081081644, 1.4054651081081644, 2.0986122886681100 }, { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } }; - CheckMatrices(output, target, 1e-12); + CheckMatrices(output, target.t(), 1e-12); } /** @@ -1050,7 +1051,7 @@ BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) 1.6931471805599454 }, { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } }; - CheckMatrices(output, target, 1e-12); + CheckMatrices(output, target.t(), 1e-12); } /** @@ -1077,7 +1078,7 @@ BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) 2.0986122886681100 }, { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } }; - CheckMatrices(output, target, 1e-12); + CheckMatrices(output, target.t(), 1e-12); } /** @@ -1104,7 +1105,7 @@ BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) 0.2418781686514208 }, { 0.4292273574839269, 0.3333333333333333, 0, 0.4292273574839269, 0 } }; - CheckMatrices(output, target, 1e-12); + CheckMatrices(output, target.t(), 1e-12); } /** @@ -1131,7 +1132,7 @@ BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) 0.2998017555240157 }, { 0.4684883693693881, 0.3333333333333333, 0, 0.4684883693693881, 0 } }; - CheckMatrices(output, target, 1e-12); + CheckMatrices(output, target.t(), 1e-12); } /** From 10b26d26277982a719b58512c7233552d4dd5c4f Mon Sep 17 00:00:00 2001 From: Mikhail Lozhnikov Date: Wed, 25 Mar 2020 02:26:20 +0300 Subject: [PATCH 214/265] Added detailed description for the string encoding output format. Described the column-major case and the row-major case. --- src/mlpack/core/data/string_encoding.hpp | 21 ++++- .../bag_of_words_encoding_policy.hpp | 54 ++++++----- .../dictionary_encoding_policy.hpp | 37 ++++---- .../tf_idf_encoding_policy.hpp | 90 ++++++++++--------- src/mlpack/tests/string_encoding_test.cpp | 30 ++++--- 5 files changed, 134 insertions(+), 98 deletions(-) diff --git a/src/mlpack/core/data/string_encoding.hpp b/src/mlpack/core/data/string_encoding.hpp index ac9d67c366..db5896576c 100644 --- a/src/mlpack/core/data/string_encoding.hpp +++ b/src/mlpack/core/data/string_encoding.hpp @@ -24,7 +24,8 @@ namespace data { /** * The class translates a set of strings into numbers using various encoding - * algorithms. + * algorithms. The encoder writes data either in the column-major order or + * in the row-major order depending on the output data type. * * @tparam EncodingPolicyType Type of the encoding algorithm itself. * @tparam DictionaryType Type of the dictionary. @@ -90,7 +91,13 @@ class StringEncoding void Clear(); /** - * Encode the given text and write the result to the given output. + * Encode the given text and write the result to the given output. The encoder + * writes data in the column-major order or in the row-major order depending + * on the output data type. + * + * If the output type is either arma::mat or arma::sp_mat then the function + * writes it in the column-major order. If the output type is 2D std::vector + * then the function writes it in the row major order. * * @tparam OutputType Type of the output container. The function supports * the following types: arma::mat, arma::sp_mat, @@ -132,7 +139,12 @@ class StringEncoding private: /** * A helper function to encode the given text and write the result to - * the given output. + * the given output. The encoder writes data in the column-major order or + * in the row-major order depending on the output data type. + * + * If the output type is either arma::mat or arma::sp_mat then the function + * writes it in the column-major order. If the output type is 2D std::vector + * then the function writes it in the row major order. * * @tparam OutputType Type of the output container. The function supports * the following types: arma::mat, arma::sp_mat, @@ -162,7 +174,8 @@ class StringEncoding /** * A helper function to encode the given text and write the result to * the given output. This is an optimized overload for policies that support - * the one pass encoding algorithm. + * the one pass encoding algorithm. The encoder writes data in the row-major + * order. * * @tparam TokenizerType Type of the tokenizer. * @tparam PolicyType The type of the encoding policy. It has to be diff --git a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index 8c7bc83036..e3be0f5faa 100644 --- a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -27,13 +27,16 @@ namespace data { * of tokens. If an item of the dataset has the i-th token, then the i-th * coordinate of the corresponding vector is equal to 1, otherwise it's equal to * zero. The order in which the tokens are labeled is defined by the dictionary - * used by the StringEncoding class. + * used by the StringEncoding class. The encoder writes data either in the + * column-major order or in the row-major order depending on the output data + * type. */ class BagOfWordsEncodingPolicy { public: /** - * The function initializes the output matrix. + * The function initializes the output matrix. The encoder writes data + * in the column-major order. * * @tparam MatType The output matrix type. * @@ -53,8 +56,10 @@ class BagOfWordsEncodingPolicy } /** - * The function initializes the output matrix. - * Overloaded function to store result in vector> + * The function initializes the output matrix. The encoder writes data + * in the row-major order. + * + * Overloaded function to store result in vector>. * * @tparam OutputType Type of the output vector. * @@ -73,59 +78,60 @@ class BagOfWordsEncodingPolicy output.resize(datasetSize, std::vector(dictionarySize, 0)); } - /** + /** * The function performs the bag of words encoding algorithm i.e. it writes - * the encoded token to the output. - * Returns the encodings in column-major format. + * the encoded token to the output. The encoder writes data in the + * column-major order. * * @tparam MatType The output matrix type. * * @param output Output matrix to store the encoded results (sp_mat or mat). * @param value The encoded token. - * @param row The row number at which the encoding is performed. - * @param col The token index in the row. + * @param line The line number at which the encoding is performed. + * @param index The token index in the line. */ template static void Encode(MatType& output, const size_t value, - const size_t row, - const size_t /* col */) + const size_t line, + const size_t /* index */) { // The labels are assigned sequentially starting from one. - output(value - 1, row) = 1; + output(value - 1, line) = 1; } - /** + /** * The function performs the bag of words encoding algorithm i.e. it writes - * the encoded token to the output. + * the encoded token to the output. The encoder writes data in the + * row-major order. + * * Overload function to accepted vector> as output type. - * Returns the encodings in row-major format. * * @param output Output matrix to store the encoded results. * @param value The encoded token. - * @param row The row number at which the encoding is performed. - * @param col The row token number at which the encoding is performed. + * @param line The line number at which the encoding is performed. + * @param index The line token number at which the encoding is performed. * @tparam OutputType The type of output vector. */ template static void Encode(std::vector>& output, const size_t value, - const size_t row, - const size_t /* col */) + const size_t line, + const size_t /* index */) { // The labels are assigned sequentially starting from one. - output[row][value - 1] = 1; + output[line][value - 1] = 1; } /** * The function is not used by the bag of words encoding policy. * - * @param row The row number at which the encoding is performed. - * @param col The token sequence number in the row. + * @param line The line number at which the encoding is performed. + * @param index The token sequence number in the line. * @param value The encoded token. */ - static void PreprocessToken(size_t /* row */, - size_t /* col */, + static void PreprocessToken(size_t /* line */, + size_t /* index */, size_t /* value */) { } diff --git a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp index e907df5c1d..4b26860abd 100644 --- a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp @@ -25,13 +25,16 @@ namespace data { * The encoder assigns a positive integer number to each unique token and treats * the dataset as categorical. The numbers are assigned sequentially starting * from one. The order in which the tokens are labeled is defined by - * the dictionary used by the StringEncoding class. + * the dictionary used by the StringEncoding class. The encoder writes data + * either in the column-major order or in the row-major order depending on + * the output data type. */ class DictionaryEncodingPolicy { public: /** - * The function initializes the output matrix. + * The function initializes the output matrix. The encoder writes data + * in the column-major order. * * @tparam MatType The output matrix type. * @@ -50,32 +53,32 @@ class DictionaryEncodingPolicy output.zeros(maxNumTokens, datasetSize); } - /** + /** * The function performs the dictionary encoding algorithm i.e. it writes - * the encoded token to the ouput. - * Returns the encodings in column-major format. + * the encoded token to the ouput. The encoder writes data in the + * column-major order. * * @tparam MatType The output matrix type. * * @param output Output matrix to store the encoded results (sp_mat or mat). * @param value The encoded token. - * @param row The row number at which the encoding is performed. - * @param col The token index in the row. + * @param line The line number at which the encoding is performed. + * @param index The token index in the line. */ template static void Encode(MatType& output, const size_t value, - const size_t row, - const size_t col) + const size_t line, + const size_t index) { - output(col, row) = value; + output(index, line) = value; } - /** + /** * The function performs the dictionary encoding algorithm i.e. it writes * the encoded token to the ouput. This is an overload function which saves - * the result into the given vector to avoid padding. - * Returns the encodings in row-major format. + * the result into the given vector to avoid padding. The encoder writes data + * in the row-major order. * * @tparam OutputType Type of the output vector. * @@ -91,12 +94,12 @@ class DictionaryEncodingPolicy /** * The function is not used by the dictionary encoding policy. * - * @param row The row number at which the encoding is performed. - * @param col The token sequence number in the row. + * @param line The line number at which the encoding is performed. + * @param index The token sequence number in the line. * @param value The encoded token. */ - static void PreprocessToken(const size_t /* row */, - const size_t /* col */, + static void PreprocessToken(const size_t /* line */, + const size_t /* index */, const size_t /* value */) { } diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index eba42ee0ca..2ad8b15e9f 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -28,7 +28,8 @@ namespace data { * multiplied by inverse document frequency (idf). * The encoder assigns the corresponding tf-idf value to each token. The order * in which the tokens are labeled is defined by the dictionary used by the - * StringEncoding class. + * StringEncoding class. The encoder writes data either in the column-major + * order or in the row-major order depending on the output data type. */ class TfIdfEncodingPolicy { @@ -76,7 +77,8 @@ class TfIdfEncodingPolicy { } /** - * The function initializes the output matrix. + * The function initializes the output matrix. The encoder writes data + * in the row-major order. * * @tparam MatType The output matrix type. * @@ -96,7 +98,9 @@ class TfIdfEncodingPolicy } /** - * The function initializes the output matrix. + * The function initializes the output matrix. The encoder writes data + * in the row-major order. + * * Overloaded function to store result in vector>. * * @tparam OutputType Type of the output vector. @@ -118,86 +122,87 @@ class TfIdfEncodingPolicy /** * The function performs the TfIdf encoding algorithm i.e. it writes - * the encoded token to the output. - * Returns the encodings in column-major format. + * the encoded token to the output. The encoder writes data in the + * column-major order. * * @tparam MatType The output matrix type. * * @param output Output matrix to store the encoded results (sp_mat or mat). * @param value The encoded token. - * @param row The row number at which the encoding is performed. - * @param col The token index in the row. + * @param line The line number at which the encoding is performed. + * @param index The token index in the line. */ template void Encode(MatType& output, const size_t value, - const size_t row, - const size_t /* col */) + const size_t line, + const size_t /* index */) { const typename MatType::elem_type tf = TermFrequency( - tokensFrequences[row][value], rowsSizes[row]); + tokensFrequences[line][value], linesSizes[line]); const typename MatType::elem_type idf = InverseDocumentFrequency( output.n_cols, numContainingStrings[value]); - output(value - 1, row) = tf * idf; + output(value - 1, line) = tf * idf; } /** * The function performs the TfIdf encoding algorithm i.e. it writes - * the encoded token to the output. + * the encoded token to the output. The encoder writes data in the + * row-major order. + * * Overloaded function to accept vector> as the output * type. - * Returns the encodings in row-major format. * * @tparam OutputType Type of the output vector. * * @param output Output matrix to store the encoded results. * @param value The encoded token. - * @param row The row number at which the encoding is performed. - * @param col The token index in the row. + * @param line The line number at which the encoding is performed. + * @param index The token index in the line. */ template void Encode(std::vector >& output, const size_t value, - const size_t row, - const size_t /* col */) + const size_t line, + const size_t /* index */) { const OutputType tf = TermFrequency( - tokensFrequences[row][value], rowsSizes[row]); + tokensFrequences[line][value], linesSizes[line]); const OutputType idf = InverseDocumentFrequency( output.size(), numContainingStrings[value]); - output[row][value - 1] = tf * idf; + output[line][value - 1] = tf * idf; } /* * The function calculates the necessary statistics for the purpose * of the tf-idf algorithm during the first pass through the dataset. * - * @param row The row number at which the encoding is performed. - * @param col The token sequence number in the row. + * @param line The line number at which the encoding is performed. + * @param index The token sequence number in the line. * @param value The encoded token. */ - void PreprocessToken(const size_t row, - const size_t /* col */, + void PreprocessToken(const size_t line, + const size_t /* index */, const size_t value) { - if (row >= tokensFrequences.size()) + if (line >= tokensFrequences.size()) { - rowsSizes.resize(row + 1); - tokensFrequences.resize(row + 1); + linesSizes.resize(line + 1); + tokensFrequences.resize(line + 1); } - tokensFrequences[row][value]++; + tokensFrequences[line][value]++; - if (tokensFrequences[row][value] == 1) + if (tokensFrequences[line][value] == 1) numContainingStrings[value]++; - rowsSizes[row]++; + linesSizes[line]++; } //! Return token frequencies. @@ -221,10 +226,10 @@ class TfIdfEncodingPolicy return numContainingStrings; } - //! Return the rows sizes. - const std::vector& RowsSizes() const { return rowsSizes; } - //! Modify the rows sizes. - std::vector& RowsSizes() { return rowsSizes; } + //! Return the lines sizes. + const std::vector& LinesSizes() const { return linesSizes; } + //! Modify the lines sizes. + std::vector& LinesSizes() { return linesSizes; } //! Return the term frequency type. TfTypes TfType() const { return tfType; } @@ -252,8 +257,9 @@ class TfIdfEncodingPolicy * * @tparam ValueType Type of the returned value. * - * @param numOccurrences The number of the given token occurrences in the row. - * @param numTokens The total number of tokens in the row. + * @param numOccurrences The number of the given token occurrences in + * the line. + * @param numTokens The total number of tokens in the line. */ template ValueType TermFrequency(const size_t numOccurrences, @@ -280,36 +286,36 @@ class TfIdfEncodingPolicy * * @tparam ValueType Type of the returned value. * - * @param totalNumRows The total number of strings in the input dataset. + * @param totalNumLines The total number of strings in the input dataset. * @param numOccurrences The number of strings in the input dataset * which contain the current token. */ template - ValueType InverseDocumentFrequency(const size_t totalNumRows, + ValueType InverseDocumentFrequency(const size_t totalNumLines, const size_t numOccurrences) { if (smoothIdf) { - return std::log(static_cast(totalNumRows + 1) / + return std::log(static_cast(totalNumLines + 1) / (1 + numOccurrences)) + 1.0; } else { - return std::log(static_cast(totalNumRows) / + return std::log(static_cast(totalNumLines) / numOccurrences) + 1.0; } } private: - //! Used to store the total number of tokens for each row. + //! Used to store the total number of tokens for each line. std::vector> tokensFrequences; /** * Used to store the number of strings which contain a token depending * on the given token. */ std::unordered_map numContainingStrings; - //! Used to store the number of tokens in each row. - std::vector rowsSizes; + //! Used to store the number of tokens in each line. + std::vector linesSizes; //! Type of the term frequency scheme. TfTypes tfType; //! Indicates whether the idf scheme is smooth or not. diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index c8f3821c4e..baec566bcb 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -57,14 +57,22 @@ static vector stringEncodingUtf8Input = { }; /** - * Function used to compare two vectors. + * Check the values of two 2D vectors. */ -template -void checkVector(vector> A, vector> B) +template +void CheckVectors(const vector>& a, + const vector>& b, + const ValueType tolerance = 1e-5) { - for (size_t i = 0; i < A.size(); i++) - for (size_t j = 0; j < A[i].size(); j++) - BOOST_REQUIRE_CLOSE(A[i][j], B[i][j], 1e-12); + BOOST_REQUIRE_EQUAL(a.size(), b.size()); + + for (size_t i = 0; i < a.size(); i++) + { + BOOST_REQUIRE_EQUAL(a[i].size(), b[i].size()); + + for (size_t j = 0; j < a[i].size(); j++) + BOOST_REQUIRE_CLOSE(a[i][j], b[i][j], tolerance); + } } /** @@ -746,7 +754,7 @@ BOOST_AUTO_TEST_CASE(VectorRawCountSmoothIdfEncodingTest) 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995 } }; - checkVector(output, expected); + CheckVectors(output, expected, 1e-12); } /** @@ -796,7 +804,7 @@ BOOST_AUTO_TEST_CASE(VectorRawCountSmoothIdfEncodingIndividualCharactersTest) { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } }; - checkVector(output, expected); + CheckVectors(output, expected, 1e-12); } /** @@ -893,7 +901,7 @@ BOOST_AUTO_TEST_CASE(VectorTfIdfRawCountEncodingTest) 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811 } }; - checkVector(output, expected); + CheckVectors(output, expected, 1e-12); } /** @@ -946,7 +954,7 @@ BOOST_AUTO_TEST_CASE(VectorRawcountEncodingIndividualCharactersTest) { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } }; - checkVector(output, expected); + CheckVectors(output, expected, 1e-12); } /** @@ -998,7 +1006,7 @@ BOOST_AUTO_TEST_CASE(VectorBnarySmoothIdfEncodingIndividualCharactersTest) { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } }; - checkVector(output, expected); + CheckVectors(output, expected, 1e-12); } /** From cc7a370b31bff9370e2c3bfab8c8111a4dca68ef Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Wed, 25 Mar 2020 10:26:11 +0530 Subject: [PATCH 215/265] updates following review suggestions --- HISTORY.md | 2 +- src/mlpack/core/cv/metrics/r2_score.hpp | 12 +++++++----- src/mlpack/core/cv/metrics/r2_score_impl.hpp | 18 ++++++++---------- src/mlpack/tests/cv_test.cpp | 2 +- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 01e5493f38..81729e3292 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,6 @@ ### mlpack ?.?.? ###### ????-??-?? - * Added `R2Score` regression metric (#2323). + * Added `R2 Score` regression metric (#2323). * Added `mean squared logarithmic error` loss function for neural networks (#2210). diff --git a/src/mlpack/core/cv/metrics/r2_score.hpp b/src/mlpack/core/cv/metrics/r2_score.hpp index 26b4fb25d9..6fcac955aa 100644 --- a/src/mlpack/core/cv/metrics/r2_score.hpp +++ b/src/mlpack/core/cv/metrics/r2_score.hpp @@ -18,26 +18,27 @@ namespace mlpack { namespace cv { /** - * The R2Score is a metric of performance for regression algorithms - * that represents the proportion of variance (of y) that has been + * The R2 Score is a metric of performance for regression algorithms + * that represents the proportion of variance (here y) that has been * explained by the independent variables in the model. It provides * an indication of goodness of fit and therefore a measure of how * well unseen samples are likely to be predicted by the model, * through the proportion of explained variance. - * As R2Score is dataset dependent it can have wide range of values. The + * As R2 Score is dataset dependent it can have wide range of values. The * best possible score is @f$R^2 =1.0@f$. Values of R2 outside the range * 0 to 1 can occur when the model fits the data worse than a horizontal * hyperplane. This would occur when the wrong model was chosen, or * nonsensical constraints were applied by mistake. A model which * predicts exactly the expected value of y, disregarding the input - * features, gets a R2Score equals to 0.0. + * features, gets a R2 Score equals to 0.0. * If a model predicts @f$ \hat{y}_i $@f of the @f$ i $@f-th sample for a true - * @f$ y_i $@f for total n samples, the R2Score is calculated by + * @f$ y_i $@f for total n samples, the R2 Score is calculated by * @f{eqnarray*}{ * R^{2} \left( y, \hat{y} \right) &=& 1-\frac{\sum_{i=1}^{n} * \left( y_i - \hat{y_i} \right)^2 } * {\sum_{i=1}^{n} \left( y_i - \bar{y}\right)^2}\\ * @f} + * * where @f$ \bar{y} = frac{1}{y}\sum_{i=1}^{n} y_i $@f. * For example, a model having R2Score = 0.85, explains 85 \% variability of * the response data around its mean. @@ -52,6 +53,7 @@ class R2Score * @param data Column-major data containing test items. * @param responses Ground truth (correct) target values for the test items, * should be either a row vector or a column-major matrix. + * @return calculated R2 Score. */ template static double Evaluate(MLAlgorithm& model, diff --git a/src/mlpack/core/cv/metrics/r2_score_impl.hpp b/src/mlpack/core/cv/metrics/r2_score_impl.hpp index 0bc398fd2d..1dd6df2278 100644 --- a/src/mlpack/core/cv/metrics/r2_score_impl.hpp +++ b/src/mlpack/core/cv/metrics/r2_score_impl.hpp @@ -33,24 +33,22 @@ double R2Score::Evaluate(MLAlgorithm& model, // Taking Predicted Output from the model. model.Predict(data, predictedResponses); // Mean value of response. - double mean_responses = arma::mean(responses); + double meanResponses = arma::mean(responses); // Calculate the numerator i.e. residual sum of squares. - double ss_res = arma::accu(arma::square(responses - predictedResponses)); + double residualSumSquared = arma::accu(arma::square(responses - + predictedResponses)); // Calculate the denominator i.e.total sum of squares. - double ss_tot = arma::accu(arma::square(responses - mean_responses)); + double totalSumSquared = arma::accu(arma::square(responses - meanResponses)); - // Handling undefined R2Score when both denominator and numerator is 0.0 - if (ss_res == 0.0) + // Handling undefined R2 Score when both denominator and numerator is 0.0. + if (residualSumSquared == 0.0) { - if (ss_tot != 0.0) - return 1.0; - else - return DBL_MIN; + return totalSumSquared ? 1.0 : DBL_MIN; } - return 1 - ss_res / ss_tot; + return 1 - residualSumSquared / totalSumSquared; } } // namespace cv diff --git a/src/mlpack/tests/cv_test.cpp b/src/mlpack/tests/cv_test.cpp index a1240fe69f..4210065887 100644 --- a/src/mlpack/tests/cv_test.cpp +++ b/src/mlpack/tests/cv_test.cpp @@ -166,7 +166,7 @@ BOOST_AUTO_TEST_CASE(MSETest) } /** - * Test the R squared metric (R2Score). + * Test the R squared metric (R2 Score). */ BOOST_AUTO_TEST_CASE(R2ScoreTest) { From 13d18960e1a2c3390004e271cba185c51cbe930f Mon Sep 17 00:00:00 2001 From: kartikdutt18 <39593019+kartikdutt18@users.noreply.github.com> Date: Fri, 27 Mar 2020 01:07:24 +0530 Subject: [PATCH 216/265] Attempt to Fix Azure pipeline (#2340) * Install config parser * Fix file * Removed pandas to start build * Update macos-steps.yaml --- .ci/macos-steps.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ci/macos-steps.yaml b/.ci/macos-steps.yaml index 7439d0b2b2..968abd018f 100644 --- a/.ci/macos-steps.yaml +++ b/.ci/macos-steps.yaml @@ -14,7 +14,7 @@ steps: set -e sudo xcode-select --switch /Applications/Xcode_10.1.app/Contents/Developer unset BOOST_ROOT - pip install cython numpy pandas zipp + pip install cython numpy pandas zipp configparser brew install openblas armadillo boost if [ "a$(julia.version)" != "a" ]; then From 75b5e235a6082a06ea6141652cd9707d9ceba8d8 Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Fri, 27 Mar 2020 01:58:19 +0530 Subject: [PATCH 217/265] added asynchronous learning tutorial --- .../reinforcement_learning.txt | 167 +++++++++++++++++- 1 file changed, 166 insertions(+), 1 deletion(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index 1c837ef070..df9a5ae02f 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -1,6 +1,7 @@ /*! @file rl.txt @author Sriram S K +@author Joel Joseph @brief Tutorial for how to use the Reinforcement Learning module in mlpack. @page rltutorial Reinforcement Learning Tutorial @@ -29,6 +30,7 @@ This tutorial is split into the following sections: - \ref environment_rltut - \ref agent_components_rltut - \ref q_learning_rltut + - \ref async_learning_rltut - \ref further_rltut @section environment_rltut Reinforcement Learning Environments @@ -231,9 +233,172 @@ to have converged when the average return reaches a predetermined value (i.e. > Conversely, if the average return does not go beyond that amount even after a thousand episodes, we can conclude that the agent will not converge and exit the training loop. +@section async_learning_rltut + +In 2016, Researchers at Deepmind and University of Montreal published their paper +"Asynchronous Methods for Deep Reinforcement Learning". In it they described asynchronous +variants of four standard reinforcement learning algorithms: + - One-Step SARSA + - One-Step Q-Learning + - N-Step Q-Learning + - Advantage Actor-Critic(A3C) + +Online RL algorithms and Deep Neural Networks make an unstable combination because of the +non-stationary and correlated nature of online updates. Although this is solved by Experience Replay, +it has several drawbacks: it uses more memory and computation per real interaction; and it requires +off-policy learning algorithms. + +Asynchronous methods, instead of experience replay, asynchronously executes multiple agents +in parallel, on multiple instances of the environment, which solves all the above problems. + +Here, we demonstrate Asynchronous Learning methods in mlpack through the training of an async +agent. Asynchronous learning involves training several agents simultaneously. Here, each of the +agents are referred to as "workers". Currently mlpack has One-Step Q-Learning worker, N-Step +Q-Learning worker and One-Step SARSA worker. + +Lets examine the sample code in chunks. + +Apart from the includes used for the q-learning example, two more have to be included: + +@code +#include +#include +@endcode + +Here we don't use experience replay. And instead of a single policy, we use three different +policies, each corresponding to its worker. Number of workers created, depends on the number of +policies given in the Aggregated Policy. The column vector contains the probability distribution +for each child policy. We should make sure its size is same as the number of policies and the sum +of its elements is equal to 1. + +@code +AggregatedPolicy> policy({GreedyPolicy(0.7, 5000, 0.1), + GreedyPolicy(0.7, 5000, 0.01), + GreedyPolicy(0.7, 5000, 0.5)}, + arma::colvec("0.4 0.3 0.3")); +@endcode + +Now, we will create the "OneStepQLearning" agent. We could have used "NStepQLearning" or "OneStepSarsa" +here according to our requirement. + +@code +OneStepQLearning< + CartPole, decltype(model), ens::AdamUpdate, decltype(policy)> + agent(std::move(config), std::move(model), std::move(policy)); +@endcode + +Here, unlike the Q-Learning example, instead of the entire while loop, we use the Train method of the Asynchronous +Learning class inside a for loop which runs for 100 training episodes. + +@code +for(int i=0;i<100;i++) +{ + agent.Train(measure); +} +@endcode + +What is "measure" here? It can be a lambda function which returns a boolean value(indicating the end of training) +and accepts the episode return(total reward of a deterministic test episode) as parameter. +So, lets create that. + +@code +arma::vec returns(20, arma::fill::zeros); +size_t position = 0; +size_t episode = 0; + +auto measure = [&returns, &position, &episode](double episodeReturn) +{ + if(episode > 10000) return true; + + returns[position++] = episodeReturn; + position = position % returns.n_elem; + episode++; + + std::cout << "Episode No.: " << episode + << "; Episode Return: " << episodeReturn + << "; Average Return: " << arma::mean(returns) << endl; +}; +@endcode + +This will train three different agents on three CPU threads asynchronously and use this data to update the +action value estimate. +Voila, thats all there is to it. + +Here is the full code, to try this right away: + +@code +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace mlpack; +using namespace mlpack::ann; +using namespace mlpack::rl; +int main() +{ + // Set up the network. + FFN, GaussianInitialization> model(MeanSquaredError<>(), + GaussianInitialization(0, 0.001)); + model.Add>(4, 128); + model.Add>(); + model.Add>(128, 128); + model.Add>(); + model.Add>(128, 2); + + AggregatedPolicy> policy({GreedyPolicy(0.7, 5000, 0.1), + GreedyPolicy(0.7, 5000, 0.01), + GreedyPolicy(0.7, 5000, 0.5)}, + arma::colvec("0.4 0.3 0.3")); + + TrainingConfig config; + config.StepSize() = 0.01; + config.Discount() = 0.9; + config.TargetNetworkSyncInterval() = 100; + config.ExplorationSteps() = 100; + config.DoubleQLearning() = false; + config.StepLimit() = 200; + + OneStepQLearning< + CartPole, decltype(model), ens::VanillaUpdate, decltype(policy)> + agent(std::move(config), std::move(model), std::move(policy)); + + arma::vec returns(20, arma::fill::zeros); + size_t position = 0; + size_t episode = 0; + + auto measure = [&returns, &position, &episode](double episodeReturn) + { + if(episode > 10000) return true; + + returns[position++] = episodeReturn; + position = position % returns.n_elem; + episode++; + + std::cout << "Episode No.: " << episode + << "; Episode Return: " << episodeReturn + << "; Average Return: " << arma::mean(returns) << endl; + }; + + for(int i=0;i<100;i++) + { + agent.Train(measure); + } +} +@endcode + +It will train for 100 episodes, which will take around 50 seconds. + @section further_rltut Further documentation For further documentation on the rl classes, consult the \ref mlpack::rl "complete API documentation". -*/ \ No newline at end of file +*/ From 7d7fc91e958cd88a3f393b730c4863d125832dc1 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Thu, 26 Mar 2020 22:11:50 -0400 Subject: [PATCH 218/265] Use HDF 1.10 API instead of 1.08. --- src/mlpack/core/arma_extend/arma_extend.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mlpack/core/arma_extend/arma_extend.hpp b/src/mlpack/core/arma_extend/arma_extend.hpp index 9539d1a534..ba47c1f190 100644 --- a/src/mlpack/core/arma_extend/arma_extend.hpp +++ b/src/mlpack/core/arma_extend/arma_extend.hpp @@ -39,8 +39,7 @@ // Force definition of old HDF5 API. Thanks to Mike Roberts for helping find // this workaround. #if !defined(H5_USE_110_API) - #undef H5_USE_18_API - #define H5_USE_18_API + #define H5_USE_110_API #endif // Include everything we'll need for serialize(). From dd29a5ef41e6d5a03aec4f078335a7a58ee8817c Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Fri, 27 Mar 2020 10:00:46 +0530 Subject: [PATCH 219/265] Update r2_score_impl.hpp Removing braces on 47-49 --- src/mlpack/core/cv/metrics/r2_score_impl.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/mlpack/core/cv/metrics/r2_score_impl.hpp b/src/mlpack/core/cv/metrics/r2_score_impl.hpp index 1dd6df2278..86c57e11fb 100644 --- a/src/mlpack/core/cv/metrics/r2_score_impl.hpp +++ b/src/mlpack/core/cv/metrics/r2_score_impl.hpp @@ -44,9 +44,7 @@ double R2Score::Evaluate(MLAlgorithm& model, // Handling undefined R2 Score when both denominator and numerator is 0.0. if (residualSumSquared == 0.0) - { return totalSumSquared ? 1.0 : DBL_MIN; - } return 1 - residualSumSquared / totalSumSquared; } From d367f0a4bfcc73c6c6f50f603db8c2a4abf9e25f Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Fri, 27 Mar 2020 11:35:40 +0530 Subject: [PATCH 220/265] Update doc/tutorials/reinforcement_learning/reinforcement_learning.txt Co-Authored-By: favre49 <40389657+favre49@users.noreply.github.com> --- doc/tutorials/reinforcement_learning/reinforcement_learning.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index df9a5ae02f..10cab01a55 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -256,7 +256,7 @@ agent. Asynchronous learning involves training several agents simultaneously. He agents are referred to as "workers". Currently mlpack has One-Step Q-Learning worker, N-Step Q-Learning worker and One-Step SARSA worker. -Lets examine the sample code in chunks. +Let's examine the sample code in chunks. Apart from the includes used for the q-learning example, two more have to be included: From 7f68d6eeaac07a320a508b39fe2b12464d3396cf Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Fri, 27 Mar 2020 11:36:05 +0530 Subject: [PATCH 221/265] Update doc/tutorials/reinforcement_learning/reinforcement_learning.txt Co-Authored-By: favre49 <40389657+favre49@users.noreply.github.com> --- doc/tutorials/reinforcement_learning/reinforcement_learning.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index 10cab01a55..783097fb8e 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -265,7 +265,7 @@ Apart from the includes used for the q-learning example, two more have to be inc #include @endcode -Here we don't use experience replay. And instead of a single policy, we use three different +Here we don't use experience replay, and instead of a single policy, we use three different policies, each corresponding to its worker. Number of workers created, depends on the number of policies given in the Aggregated Policy. The column vector contains the probability distribution for each child policy. We should make sure its size is same as the number of policies and the sum From ac291035480a78840af4b13f34ffbc90bd52bd68 Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Fri, 27 Mar 2020 11:37:02 +0530 Subject: [PATCH 222/265] Update doc/tutorials/reinforcement_learning/reinforcement_learning.txt Co-Authored-By: favre49 <40389657+favre49@users.noreply.github.com> --- doc/tutorials/reinforcement_learning/reinforcement_learning.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index 783097fb8e..50d17baad5 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -297,7 +297,7 @@ for(int i=0;i<100;i++) } @endcode -What is "measure" here? It can be a lambda function which returns a boolean value(indicating the end of training) +What is "measure" here? It is a lambda function which returns a boolean value (indicating the end of training) and accepts the episode return(total reward of a deterministic test episode) as parameter. So, lets create that. From 6dcdd941c7700bbfd43d17eebd667488816c3b5b Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Fri, 27 Mar 2020 11:37:12 +0530 Subject: [PATCH 223/265] Update doc/tutorials/reinforcement_learning/reinforcement_learning.txt Co-Authored-By: favre49 <40389657+favre49@users.noreply.github.com> --- doc/tutorials/reinforcement_learning/reinforcement_learning.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index 50d17baad5..7d450b3b69 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -299,7 +299,7 @@ for(int i=0;i<100;i++) What is "measure" here? It is a lambda function which returns a boolean value (indicating the end of training) and accepts the episode return(total reward of a deterministic test episode) as parameter. -So, lets create that. +So, let's create that. @code arma::vec returns(20, arma::fill::zeros); From 0e7c466d7c8ac3ef58604afe013ccb3da715cf14 Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Fri, 27 Mar 2020 11:37:21 +0530 Subject: [PATCH 224/265] Update doc/tutorials/reinforcement_learning/reinforcement_learning.txt Co-Authored-By: favre49 <40389657+favre49@users.noreply.github.com> --- doc/tutorials/reinforcement_learning/reinforcement_learning.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index 7d450b3b69..139112662b 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -314,7 +314,7 @@ auto measure = [&returns, &position, &episode](double episodeReturn) position = position % returns.n_elem; episode++; - std::cout << "Episode No.: " << episode + std::cout << "Episode No.: " << episode << "; Episode Return: " << episodeReturn << "; Average Return: " << arma::mean(returns) << endl; }; From ddff7d375bdfe51c748a95b7874127548fbaa511 Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Fri, 27 Mar 2020 11:38:08 +0530 Subject: [PATCH 225/265] Update doc/tutorials/reinforcement_learning/reinforcement_learning.txt Co-Authored-By: favre49 <40389657+favre49@users.noreply.github.com> --- doc/tutorials/reinforcement_learning/reinforcement_learning.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index 139112662b..8b817cc1e5 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -298,7 +298,7 @@ for(int i=0;i<100;i++) @endcode What is "measure" here? It is a lambda function which returns a boolean value (indicating the end of training) -and accepts the episode return(total reward of a deterministic test episode) as parameter. +and accepts the episode return (total reward of a deterministic test episode) as parameter. So, let's create that. @code From c388fac8ce3bc916ea46bafb531cb2b1bd7d3009 Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Fri, 27 Mar 2020 12:04:13 +0530 Subject: [PATCH 226/265] fixed the styling issues at lines 286,294 and 397 --- .../reinforcement_learning.txt | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index 8b817cc1e5..ed745c0ccd 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -282,16 +282,15 @@ Now, we will create the "OneStepQLearning" agent. We could have used "NStepQLear here according to our requirement. @code -OneStepQLearning< - CartPole, decltype(model), ens::AdamUpdate, decltype(policy)> +OneStepQLearning agent(std::move(config), std::move(model), std::move(policy)); @endcode Here, unlike the Q-Learning example, instead of the entire while loop, we use the Train method of the Asynchronous -Learning class inside a for loop which runs for 100 training episodes. +Learning class inside a for loop. 100 training episodes will take around 50 seconds. @code -for(int i=0;i<100;i++) +for (int i = 0; i < 100; i++) { agent.Train(measure); } @@ -315,8 +314,8 @@ auto measure = [&returns, &position, &episode](double episodeReturn) episode++; std::cout << "Episode No.: " << episode - << "; Episode Return: " << episodeReturn - << "; Average Return: " << arma::mean(returns) << endl; + << "; Episode Return: " << episodeReturn + << "; Average Return: " << arma::mean(returns) << endl; }; @endcode @@ -324,7 +323,7 @@ This will train three different agents on three CPU threads asynchronously and u action value estimate. Voila, thats all there is to it. -Here is the full code, to try this right away: +Here is the full code to try this right away: @code #include @@ -394,8 +393,6 @@ int main() } @endcode -It will train for 100 episodes, which will take around 50 seconds. - @section further_rltut Further documentation For further documentation on the rl classes, consult the \ref mlpack::rl From e7e2e86fd50b6bd601930594c345281496a0dc51 Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Fri, 27 Mar 2020 12:10:07 +0530 Subject: [PATCH 227/265] fixed styling issues of the test code --- .../reinforcement_learning.txt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index ed745c0ccd..dc8c066553 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -344,8 +344,7 @@ using namespace mlpack::rl; int main() { // Set up the network. - FFN, GaussianInitialization> model(MeanSquaredError<>(), - GaussianInitialization(0, 0.001)); + FFN, GaussianInitialization> model(MeanSquaredError<>(), GaussianInitialization(0, 0.001)); model.Add>(4, 128); model.Add>(); model.Add>(128, 128); @@ -365,9 +364,8 @@ int main() config.DoubleQLearning() = false; config.StepLimit() = 200; - OneStepQLearning< - CartPole, decltype(model), ens::VanillaUpdate, decltype(policy)> - agent(std::move(config), std::move(model), std::move(policy)); + OneStepQLearning + agent(std::move(config), std::move(model), std::move(policy)); arma::vec returns(20, arma::fill::zeros); size_t position = 0; @@ -382,11 +380,11 @@ int main() episode++; std::cout << "Episode No.: " << episode - << "; Episode Return: " << episodeReturn - << "; Average Return: " << arma::mean(returns) << endl; + << "; Episode Return: " << episodeReturn + << "; Average Return: " << arma::mean(returns) << endl; }; - for(int i=0;i<100;i++) + for (int i = 0; i < 100; i++) { agent.Train(measure); } From 82e52d762330d77e790d4d633653637d4c5d5406 Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Sat, 28 Mar 2020 11:22:23 +0530 Subject: [PATCH 228/265] Update COPYRIGHT.txt --- COPYRIGHT.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt index ce5c944e0d..db423bb3b8 100644 --- a/COPYRIGHT.txt +++ b/COPYRIGHT.txt @@ -130,6 +130,7 @@ Copyright: Copyright 2020, Saraansh Tandon Copyright 2020, Gaurav Singh Copyright 2020, Lakshya Ojha + Copyright 2020, Bisakh Mondal License: BSD-3-clause All rights reserved. From 90898dbb960ea42aa37f7344b4d599dd45abec53 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Sat, 28 Mar 2020 14:52:02 +0200 Subject: [PATCH 229/265] Started working on changing the input in margin ranking loss --- .../loss_functions/margin_ranking_loss.hpp | 19 +++++++------------ .../margin_ranking_loss_impl.hpp | 6 ++---- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index ec9c78b11f..ae21d38d50 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -43,35 +43,30 @@ class MarginRankingLoss * a value of 1 in the label means the first input should be ranked higher * and a value of -1 means the second input should be ranked higher. * - * @param input1 First input data used for evaluating the specified function. - * @param input2 Second input data used for evaluating the specified function. + * @param input Concatenation of the two inputs for evaluating the specified + * function. * @param target The label vector which contains -1 or 1 values. */ template < - typename FirstInputType, - typename SecondInputType, + typename InputType, typename TargetType > - double Forward(const FirstInputType& input1, - const SecondInputType& input2, + double Forward(const InputType& input, const TargetType& target); /** * Ordinary feed backward pass of a neural network. * - * @param input1 The propagated first input activation. - * @param input2 The propagated second input activation. + * @param input The propagated concatenated input activation. * @param target The label vector which contains -1 or 1 values. * @param output The calculated error. */ template < - typename FirstInputType, - typename SecondInputType, + typename InputType, typename TargetType, typename OutputType > - void Backward(const FirstInputType& input1, - const SecondInputType& input2, + void Backward(const InputType& input1, const TargetType& target, OutputType& output); diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp index 5e908dfede..fc06881c36 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp @@ -27,13 +27,11 @@ MarginRankingLoss::MarginRankingLoss( template template < - typename FirstInputType, - typename SecondInputType, + typename InputType, typename TargetType > double MarginRankingLoss::Forward( - const FirstInputType& input1, - const SecondInputType& input2, + const InputType& input, const TargetType& target) { return arma::accu(arma::max(arma::zeros(size(target)), From c6c2f9e85e6831aec0c9b9fe1f4334a8957f7622 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Sat, 28 Mar 2020 18:37:47 +0200 Subject: [PATCH 230/265] Merged the two inputs into a single one for the margin ranking loss --- .../ann/loss_functions/margin_ranking_loss_impl.hpp | 12 ++++++++---- src/mlpack/tests/loss_functions_test.cpp | 12 +++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp index fc06881c36..9920b07085 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp @@ -34,23 +34,27 @@ double MarginRankingLoss::Forward( const InputType& input, const TargetType& target) { + int input_rows = input.n_rows; + const InputType& input1 = input.rows(0, input_rows / 2 - 1); + const InputType& input2 = input.rows(input_rows / 2, input_rows - 1); return arma::accu(arma::max(arma::zeros(size(target)), -target % (input1 - input2) + margin)) / target.n_cols; } template template < - typename FirstInputType, - typename SecondInputType, + typename InputType, typename TargetType, typename OutputType > void MarginRankingLoss::Backward( - const FirstInputType& input1, - const SecondInputType& input2, + const InputType& input, const TargetType& target, OutputType& output) { + int input_rows = input.n_rows; + const InputType& input1 = input.rows(0, input_rows / 2 - 1); + const InputType& input2 = input.rows(input_rows / 2, input_rows - 1); output = -target % (input1 - input2) + margin; output.elem(arma::find(output >= 0)).ones(); output.elem(arma::find(output < 0)).zeros(); diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index 8402212e2b..7deeb6b6e7 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -585,20 +585,21 @@ BOOST_AUTO_TEST_CASE(HingeEmbeddingLossTest) */ BOOST_AUTO_TEST_CASE(MarginRankingLossTest) { - arma::mat input1, input2, target, output; + arma::mat input, input1, input2, target, output; MarginRankingLoss<> module; // Test the Forward function on a user generator input and compare it against // the manually calculated result. input1 = arma::mat("1 2 5 7 -1 -3"); input2 = arma::mat("-1 3 -4 11 3 -3"); + input = arma::join_cols(input1, input2); target = arma::mat("1 -1 -1 1 -1 1"); - double error = module.Forward(input1, input2, target); + double error = module.Forward(input, target); // Computed using torch.nn.functional.margin_ranking_loss() BOOST_REQUIRE_CLOSE(error, 2.66667, 1e-3); // Test the Backward function. - module.Backward(input1, input2, target, output); + module.Backward(input, target, output); CheckMatrices(output, arma::mat("-0.000000 0.166667 -1.500000 0.666667 " "0.000000 -0.000000"), 1e-3); @@ -610,12 +611,13 @@ BOOST_AUTO_TEST_CASE(MarginRankingLossTest) "-4.8090 4.3455 5.2070"); input2 = arma::mat("-4.5288 -9.2766 -0.5882 -5.6643 -6.0175 8.8506 3.4759 " "-9.4886 2.2755 8.4951"); + input = arma::join_cols(input1, input2); target = arma::mat("1 1 -1 1 -1 1 1 1 -1 1"); - error = module.Forward(input1, input2, target); + error = module.Forward(input, target); BOOST_REQUIRE_CLOSE(error, 3.03530, 1e-3); // Test the Backward function on the second input. - module.Backward(input1, input2, target, output); + module.Backward(input, target, output); CheckMatrices(output, arma::mat("0.000000 0.000000 0.091240 0.000000 " "-0.753830 1.336900 0.000000 0.000000 -0.207000 0.328810"), 1e-6); From 5c842717d33c38ae0f2c5a4a4ace749bfbfdc5d7 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Sat, 28 Mar 2020 22:17:20 +0200 Subject: [PATCH 231/265] Solved type in input variable name --- src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index ae21d38d50..2360b6b0fb 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -66,7 +66,7 @@ class MarginRankingLoss typename TargetType, typename OutputType > - void Backward(const InputType& input1, + void Backward(const InputType& input, const TargetType& target, OutputType& output); From dcc9fcb6c843b5876d51958ba7a3208d0ea3126a Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Sun, 29 Mar 2020 18:38:59 +0300 Subject: [PATCH 232/265] Rebuild From a41b0de91e29f86447d7ab4cc0fe5fbcfd834fde Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Mon, 30 Mar 2020 19:23:59 +0530 Subject: [PATCH 233/265] Remove saving of parameteres --- src/mlpack/bindings/python/setup.py.in | 9 +-- .../preprocess/image_converter_main.cpp | 64 ++++++++----------- src/mlpack/tests/image_load_test.cpp | 12 +++- .../tests/main_tests/image_converter_test.cpp | 55 ++++++++-------- 4 files changed, 63 insertions(+), 77 deletions(-) diff --git a/src/mlpack/bindings/python/setup.py.in b/src/mlpack/bindings/python/setup.py.in index cad63e3420..cbb3839127 100644 --- a/src/mlpack/bindings/python/setup.py.in +++ b/src/mlpack/bindings/python/setup.py.in @@ -30,11 +30,6 @@ if not '${OpenMP_CXX_FLAGS}': else: extra_link_args=['${OpenMP_CXX_FLAGS}'] -if '${STB_AVAILABLE}': - extra_args = ['-DHAS_STB'] -else : - extra_args = [] - # Get list of library dirs. # Get list of library dirs. Note that for the list of directories, any # directories with a (valid) space in the name will be given to us as '\ '; so, @@ -61,11 +56,11 @@ else: cxx_flags = '${CMAKE_CXX_FLAGS}'.strip() cxx_flags = re.sub(' +', ' ', cxx_flags) if cxx_flags: - extra_args = extra_args + ['-DBINDING_TYPE=BINDING_TYPE_PYX', + extra_args = ['-DBINDING_TYPE=BINDING_TYPE_PYX', '-std=c++11', '${OpenMP_CXX_FLAGS}'] + cxx_flags.split(' ') else: - extra_args = extra_args + ['-DBINDING_TYPE=BINDING_TYPE_PYX', + extra_args = ['-DBINDING_TYPE=BINDING_TYPE_PYX', '-std=c++11', '${OpenMP_CXX_FLAGS}'] diff --git a/src/mlpack/methods/preprocess/image_converter_main.cpp b/src/mlpack/methods/preprocess/image_converter_main.cpp index ac90eace66..0565c6d433 100644 --- a/src/mlpack/methods/preprocess/image_converter_main.cpp +++ b/src/mlpack/methods/preprocess/image_converter_main.cpp @@ -28,9 +28,11 @@ PROGRAM_INFO("Image Converter", "unpack an image dataset into individual files.", // Long description. "This utility takes a image or an array of images and loads them to a" - " matrix. You can specify the height " + PRINT_PARAM_STRING("height") + - " width " + PRINT_PARAM_STRING("width") + " and channel " + - PRINT_PARAM_STRING("channel") + " of the images that needs to be loaded. " + " matrix. You can optionally specify the height " + + PRINT_PARAM_STRING("height") + " width " + PRINT_PARAM_STRING("width") + + " and channel " + PRINT_PARAM_STRING("channel") + " of the images that" + "needs to be loaded; otherwise, these parameters will be automatically" + "detected from the image." "\n" "There are other options too, that can be specified such as " + PRINT_PARAM_STRING("quality") @@ -54,32 +56,32 @@ PROGRAM_INFO("Image Converter", PARAM_VECTOR_IN_REQ(string, "input", "Image filenames which have to " "be loaded/saved.", "i"); -PARAM_INT_IN("width", "Width of the image", "W", 256); -PARAM_INT_IN("channel", "Number of channel", "C", 3); +PARAM_INT_IN("width", "Width of the image.", "w", 256); +PARAM_INT_IN("channel", "Number of channel.", "c", 3); -PARAM_MATRIX_OUT("output", "Matrix to save images data to.", "o"); +PARAM_MATRIX_OUT("output", "Matrix to save images data to, Only" + "needed if you are specifing save option.", "o"); PARAM_INT_IN("quality", "Compression of the image if saved as jpg (0-100).", "q", 90); - -PARAM_INT_IN("height", "Height of the images", "H", 256); -PARAM_FLAG("save", "Save a dataset as images", "s"); +PARAM_INT_IN("height", "Height of the images.", "H", 256); +PARAM_FLAG("save", "Save a dataset as images.", "s"); PARAM_MATRIX_IN("dataset", "Input matrix to save as images.", "I"); -// Loading/saving of a Image Info model. -PARAM_MODEL_IN(ImageInfo, "input_model", "Input Image Info model.", "m"); -PARAM_MODEL_OUT(ImageInfo, "output_model", "Output Image Info model.", "M"); - static void mlpackMain() { - // Parse command line options. - data::ImageInfo* info; - Timer::Start("Loading/Saving Image"); - if (CLI::HasParam("input_model")) + // Parse command line options. + const vector fileNames = CLI::GetParam >("input"); + arma::mat out; + + if (!CLI::HasParam("save")) { - info = CLI::GetParam("input_model"); + data::ImageInfo info; + Load(fileNames, out, info, true); + if (CLI::HasParam("output")) + CLI::GetParam("output") = std::move(out); } else { @@ -102,31 +104,17 @@ static void mlpackMain() RequireParamValue("quality", [](int x) { return x >= 0;}, true, "quality must be positive"); - const size_t& height = CLI::GetParam("height"); - const size_t& width = CLI::GetParam("width"); - const size_t& channel = CLI::GetParam("channel"); - const size_t& quality = CLI::GetParam("quality"); - info = new data::ImageInfo(width, height, channel, quality); - } - const vector fileNames = - CLI::GetParam >("input"); - arma::mat out; - if (!CLI::HasParam("save")) - { - Load(fileNames, out, *info, true); - if (CLI::HasParam("output")) - CLI::GetParam("output") = std::move(out); - } - if (CLI::HasParam("save")) - { + const size_t height = CLI::GetParam("height"); + const size_t width = CLI::GetParam("width"); + const size_t channel = CLI::GetParam("channel"); + const size_t quality = CLI::GetParam("quality"); + data::ImageInfo info(width, height, channel, quality); if (!CLI::HasParam("dataset")) { throw std::runtime_error("Please provide a input matrix to save " "images from"); } - Save(fileNames, CLI::GetParam ("dataset"), *info, true); + Save(fileNames, CLI::GetParam("dataset"), info, true); } - if (CLI::HasParam("output_model")) - CLI::GetParam ("output_model") = info; } diff --git a/src/mlpack/tests/image_load_test.cpp b/src/mlpack/tests/image_load_test.cpp index 396cb536da..7d2108264c 100644 --- a/src/mlpack/tests/image_load_test.cpp +++ b/src/mlpack/tests/image_load_test.cpp @@ -46,7 +46,11 @@ BOOST_AUTO_TEST_CASE(LoadImageAPITest) arma::Mat matrix; data::ImageInfo info; BOOST_REQUIRE(data::Load("test_image.png", matrix, info, false) == true); - BOOST_REQUIRE_EQUAL(matrix.n_rows, 50 * 50 * 3); // width * height * channels. + // width * height * channels. + BOOST_REQUIRE_EQUAL(matrix.n_rows, 50 * 50 * 3); + BOOST_REQUIRE_EQUAL(info.Height(), 50); + BOOST_REQUIRE_EQUAL(info.Width(), 50); + BOOST_REQUIRE_EQUAL(info.Channels(), 3); BOOST_REQUIRE_EQUAL(matrix.n_cols, 1); } @@ -82,7 +86,11 @@ BOOST_AUTO_TEST_CASE(LoadVectorImageAPITest) data::ImageInfo info; std::vector files = {"test_image.png", "test_image.png"}; BOOST_REQUIRE(data::Load(files, matrix, info, false) == true); - BOOST_REQUIRE_EQUAL(matrix.n_rows, 50 * 50 * 3); // width * height * channels. + // width * height * channels. + BOOST_REQUIRE_EQUAL(matrix.n_rows, 50 * 50 * 3); + BOOST_REQUIRE_EQUAL(info.Height(), 50); + BOOST_REQUIRE_EQUAL(info.Width(), 50); + BOOST_REQUIRE_EQUAL(info.Channels(), 3); BOOST_REQUIRE_EQUAL(matrix.n_cols, 2); } diff --git a/src/mlpack/tests/main_tests/image_converter_test.cpp b/src/mlpack/tests/main_tests/image_converter_test.cpp index dac1924ad6..86397a888e 100644 --- a/src/mlpack/tests/main_tests/image_converter_test.cpp +++ b/src/mlpack/tests/main_tests/image_converter_test.cpp @@ -48,9 +48,6 @@ BOOST_FIXTURE_TEST_SUITE(ImageConverterMainTest, BOOST_AUTO_TEST_CASE(LoadImageTest) { SetInputParam>("input", {"test_image.png", "test_image.png"}); - SetInputParam("height", 50); - SetInputParam("width", 50); - SetInputParam("channel", 3); mlpackMain(); arma::mat output = CLI::GetParam("output"); @@ -89,37 +86,20 @@ BOOST_AUTO_TEST_CASE(SaveImageTest) BOOST_REQUIRE_CLOSE(testimage[i], output[i], 1e-5); } -/** - * Check Saved model is working. - */ -BOOST_AUTO_TEST_CASE(SavedModelTest) -{ - SetInputParam>("input", {"test_image.png", "test_image.png"}); - SetInputParam("height", 50); - SetInputParam("width", 50); - SetInputParam("channel", 3); - - mlpackMain(); - arma::mat randomOutput = CLI::GetParam("output"); - - SetInputParam>("input", {"test_image.png", "test_image.png"}); - SetInputParam("input_model", - CLI::GetParam("output_model")); - - mlpackMain(); - arma::mat savedOutput = CLI::GetParam("output"); - CheckMatrices(randomOutput, savedOutput); -} - /** * Check whether binding throws error if height, width or channel are not * specified. */ BOOST_AUTO_TEST_CASE(IncompleteTest) { - SetInputParam>("input", {"test_image.png", "test_image.png"}); + arma::mat testimage = arma::conv_to::from( + arma::randi>((5 * 5 * 3), 2)); + SetInputParam>("input", {"test_image777.png", + "test_image999.png"}); + SetInputParam("save", true); SetInputParam("height", 50); SetInputParam("width", 50); + SetInputParam("dataset", testimage); Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); @@ -131,7 +111,13 @@ BOOST_AUTO_TEST_CASE(IncompleteTest) */ BOOST_AUTO_TEST_CASE(InvalidInputTest) { - SetInputParam>("input", {"test_image.png", "test_image.png"}); + arma::mat testimage = arma::conv_to::from( + arma::randi>((5 * 5 * 3), 2)); + SetInputParam>("input", {"test_image777.png", + "test_image999.png"}); + SetInputParam("save", true); + SetInputParam("dataset", testimage); + SetInputParam("height", -50); SetInputParam("width", 50); SetInputParam("channel", 3); @@ -146,7 +132,12 @@ BOOST_AUTO_TEST_CASE(InvalidInputTest) */ BOOST_AUTO_TEST_CASE(InvalidWidthTest) { - SetInputParam>("input", {"test_image.png", "test_image.png"}); + arma::mat testimage = arma::conv_to::from( + arma::randi>((5 * 5 * 3), 2)); + SetInputParam>("input", {"test_image777.png", + "test_image999.png"}); + SetInputParam("save", true); + SetInputParam("dataset", testimage); SetInputParam("height", 50); SetInputParam("width", -50); SetInputParam("channel", 3); @@ -161,7 +152,12 @@ BOOST_AUTO_TEST_CASE(InvalidWidthTest) */ BOOST_AUTO_TEST_CASE(InvalidChannelTest) { - SetInputParam>("input", {"test_image.png", "test_image.png"}); + arma::mat testimage = arma::conv_to::from( + arma::randi>((5 * 5 * 3), 2)); + SetInputParam>("input", {"test_image777.png", + "test_image999.png"}); + SetInputParam("save", true); + SetInputParam("dataset", testimage); SetInputParam("height", 50); SetInputParam("width", 50); SetInputParam("channel", -1); @@ -186,5 +182,4 @@ BOOST_AUTO_TEST_CASE(EmptyInputTest) Log::Fatal.ignoreInput = false; } - BOOST_AUTO_TEST_SUITE_END(); From b3df3884dfe226a1c27062cecc34471d632b6720 Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Tue, 31 Mar 2020 13:50:50 +0530 Subject: [PATCH 234/265] Rewrote checks in binding and also added to History --- HISTORY.md | 3 ++ src/mlpack/bindings/python/setup.py.in | 1 - .../preprocess/image_converter_main.cpp | 37 +++++++++---------- .../tests/main_tests/image_converter_test.cpp | 12 +++--- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 639f180a9f..06067e1b4e 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -31,6 +31,9 @@ * CMake fix for finding STB include directory (#2145). + * Add bindings for Loading and Saving image (#2019); `mlpack_image_converter` + from the command-line, `mlpack_image_converter()` from Python. + * Add normalization support for CF binding (#2136). * Add Mish activation function (#2158). diff --git a/src/mlpack/bindings/python/setup.py.in b/src/mlpack/bindings/python/setup.py.in index cbb3839127..913063191c 100644 --- a/src/mlpack/bindings/python/setup.py.in +++ b/src/mlpack/bindings/python/setup.py.in @@ -30,7 +30,6 @@ if not '${OpenMP_CXX_FLAGS}': else: extra_link_args=['${OpenMP_CXX_FLAGS}'] -# Get list of library dirs. # Get list of library dirs. Note that for the list of directories, any # directories with a (valid) space in the name will be given to us as '\ '; so, # in order to split these right, we first convert all spaces to ';', then diff --git a/src/mlpack/methods/preprocess/image_converter_main.cpp b/src/mlpack/methods/preprocess/image_converter_main.cpp index 0565c6d433..af05853f7a 100644 --- a/src/mlpack/methods/preprocess/image_converter_main.cpp +++ b/src/mlpack/methods/preprocess/image_converter_main.cpp @@ -20,17 +20,17 @@ using namespace arma; using namespace std; using namespace mlpack::data; - PROGRAM_INFO("Image Converter", // Short description. "A utility to load an image or set of images into a single dataset that" "can then be used by other mlpack methods and utilities. This can also" - "unpack an image dataset into individual files.", + "unpack an image dataset into individual files, for instance after mlpack" + "methods have been used.", // Long description. "This utility takes a image or an array of images and loads them to a" " matrix. You can optionally specify the height " + PRINT_PARAM_STRING("height") + " width " + PRINT_PARAM_STRING("width") - + " and channel " + PRINT_PARAM_STRING("channel") + " of the images that" + + " and channel " + PRINT_PARAM_STRING("channels") + " of the images that" "needs to be loaded; otherwise, these parameters will be automatically" "detected from the image." "\n" @@ -42,12 +42,12 @@ PROGRAM_INFO("Image Converter", " as an parameter. An example to load an image : " + "\n\n" + PRINT_CALL("image_converter", "input", "X", "height", 256, "width", 256, - "channel", 3, "output", "Y") + + "channels", 3, "output", "Y") + "\n\n" + " An example to save an image is :" + "\n\n" + PRINT_CALL("image_converter", "input", "X", "height", 256, "width", 256, - "channel", 3, "dataset", "Y", "save", true), + "channels", 3, "dataset", "Y", "save", true), SEE_ALSO("@preprocess_binarize", "#preprocess_binarize"), SEE_ALSO("@preprocess_describe", "#preprocess_describe"), SEE_ALSO("@preprocess_imputer", "#preprocess_imputer")); @@ -56,16 +56,16 @@ PROGRAM_INFO("Image Converter", PARAM_VECTOR_IN_REQ(string, "input", "Image filenames which have to " "be loaded/saved.", "i"); -PARAM_INT_IN("width", "Width of the image.", "w", 256); -PARAM_INT_IN("channel", "Number of channel.", "c", 3); +PARAM_INT_IN("width", "Width of the image.", "w", 0); +PARAM_INT_IN("channels", "Number of channels in the image.", "c", 0); PARAM_MATRIX_OUT("output", "Matrix to save images data to, Only" - "needed if you are specifing save option.", "o"); + "needed if you are specifying 'save' option.", "o"); PARAM_INT_IN("quality", "Compression of the image if saved as jpg (0-100).", "q", 90); -PARAM_INT_IN("height", "Height of the images.", "H", 256); +PARAM_INT_IN("height", "Height of the images.", "H", 0); PARAM_FLAG("save", "Save a dataset as images.", "s"); PARAM_MATRIX_IN("dataset", "Input matrix to save as images.", "I"); @@ -78,6 +78,9 @@ static void mlpackMain() if (!CLI::HasParam("save")) { + ReportIgnoredParam("width", "Width of image is determined from file."); + ReportIgnoredParam("height", "Height of image is determined from file."); + ReportIgnoredParam("channels", "Number of channels determined from file."); data::ImageInfo info; Load(fileNames, out, info, true); if (CLI::HasParam("output")) @@ -85,12 +88,8 @@ static void mlpackMain() } else { - if (!CLI::HasParam("width") || !CLI::HasParam("height") || - !CLI::HasParam("channel")) - { - throw std::runtime_error("Please provide height, width and " - "number of channels of the images."); - } + RequireNoneOrAllPassed({ "save", "width", "height", "channels" }, true, + "image size information is needed when 'save' is specified!"); // Positive value for width. RequireParamValue("width", [](int x) { return x >= 0;}, true, "width must be positive"); @@ -98,17 +97,17 @@ static void mlpackMain() RequireParamValue("height", [](int x) { return x >= 0;}, true, "height must be positive"); // Positive value for channel. - RequireParamValue("channel", [](int x) { return x >= 0;}, true, - "channel must be positive"); + RequireParamValue("channels", [](int x) { return x >= 0;}, true, + "channels must be positive"); // Positive value for quality. RequireParamValue("quality", [](int x) { return x >= 0;}, true, "quality must be positive"); const size_t height = CLI::GetParam("height"); const size_t width = CLI::GetParam("width"); - const size_t channel = CLI::GetParam("channel"); + const size_t channels = CLI::GetParam("channels"); const size_t quality = CLI::GetParam("quality"); - data::ImageInfo info(width, height, channel, quality); + data::ImageInfo info(width, height, channels, quality); if (!CLI::HasParam("dataset")) { throw std::runtime_error("Please provide a input matrix to save " diff --git a/src/mlpack/tests/main_tests/image_converter_test.cpp b/src/mlpack/tests/main_tests/image_converter_test.cpp index 86397a888e..35e140c45e 100644 --- a/src/mlpack/tests/main_tests/image_converter_test.cpp +++ b/src/mlpack/tests/main_tests/image_converter_test.cpp @@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(SaveImageTest) "test_image999.png"}); SetInputParam("height", 5); SetInputParam("width", 5); - SetInputParam("channel", 3); + SetInputParam("channels", 3); SetInputParam("save", true); SetInputParam("dataset", testimage); mlpackMain(); @@ -76,7 +76,7 @@ BOOST_AUTO_TEST_CASE(SaveImageTest) "test_image999.png"}); SetInputParam("height", 5); SetInputParam("width", 5); - SetInputParam("channel", 3); + SetInputParam("channels", 3); mlpackMain(); arma::mat output = CLI::GetParam("output"); @@ -120,7 +120,7 @@ BOOST_AUTO_TEST_CASE(InvalidInputTest) SetInputParam("height", -50); SetInputParam("width", 50); - SetInputParam("channel", 3); + SetInputParam("channels", 3); Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); @@ -140,7 +140,7 @@ BOOST_AUTO_TEST_CASE(InvalidWidthTest) SetInputParam("dataset", testimage); SetInputParam("height", 50); SetInputParam("width", -50); - SetInputParam("channel", 3); + SetInputParam("channels", 3); Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); @@ -160,7 +160,7 @@ BOOST_AUTO_TEST_CASE(InvalidChannelTest) SetInputParam("dataset", testimage); SetInputParam("height", 50); SetInputParam("width", 50); - SetInputParam("channel", -1); + SetInputParam("channels", -1); Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); @@ -175,7 +175,7 @@ BOOST_AUTO_TEST_CASE(EmptyInputTest) SetInputParam>("input", {}); SetInputParam("height", 50); SetInputParam("width", 50); - SetInputParam("channel", 50); + SetInputParam("channels", 50); Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); From d3068f0fdc83e5ca0aa3b960672b36d7790532cf Mon Sep 17 00:00:00 2001 From: Andrei Mihalea Date: Tue, 31 Mar 2020 18:16:17 +0300 Subject: [PATCH 235/265] Update src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp Co-Authored-By: favre49 <40389657+favre49@users.noreply.github.com> --- src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index 2360b6b0fb..cdd8d8e5f1 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -39,7 +39,7 @@ class MarginRankingLoss /** * Computes the Margin Ranking Loss function. - * Measures the loss between two intputs and a label with -1 and 1 values. + * Measures the loss between two inputs and a label with -1 and 1 values. * a value of 1 in the label means the first input should be ranked higher * and a value of -1 means the second input should be ranked higher. * From 3208350409002c18846a7483c013df8ca6c63e22 Mon Sep 17 00:00:00 2001 From: Andrei Mihalea Date: Tue, 31 Mar 2020 18:16:31 +0300 Subject: [PATCH 236/265] Update src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp Co-Authored-By: favre49 <40389657+favre49@users.noreply.github.com> --- src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index cdd8d8e5f1..8eac9f4e12 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -40,7 +40,7 @@ class MarginRankingLoss /** * Computes the Margin Ranking Loss function. * Measures the loss between two inputs and a label with -1 and 1 values. - * a value of 1 in the label means the first input should be ranked higher + * A value of 1 in the label means the first input should be ranked higher * and a value of -1 means the second input should be ranked higher. * * @param input Concatenation of the two inputs for evaluating the specified From 13585beb0e5b7b2bdeeae2cb2376db8641ffe5a6 Mon Sep 17 00:00:00 2001 From: Andrei Mihalea Date: Tue, 31 Mar 2020 18:16:56 +0300 Subject: [PATCH 237/265] Update src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp Co-Authored-By: favre49 <40389657+favre49@users.noreply.github.com> --- src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index 8eac9f4e12..116bcf90fc 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -45,7 +45,7 @@ class MarginRankingLoss * * @param input Concatenation of the two inputs for evaluating the specified * function. - * @param target The label vector which contains -1 or 1 values. + * @param target The label vector which contains values of -1 or 1. */ template < typename InputType, From fd5e1b9eedada110d464938a8f3b3a900dc50e97 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Tue, 31 Mar 2020 18:50:13 +0300 Subject: [PATCH 238/265] Moved the description and renamed variables --- HISTORY.md | 2 ++ .../ann/loss_functions/margin_ranking_loss.hpp | 8 +++++--- .../ann/loss_functions/margin_ranking_loss_impl.hpp | 12 ++++++------ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 9dba6a517d..6d0d37a9d7 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -66,6 +66,8 @@ * Add Hinge Embedding Loss Function (#2229). + * Add Margin Ranking Loss Function (#2264). + ### mlpack 3.2.2 ###### 2019-11-26 * Add `valid` and `same` padding option in `Convolution` and `Atrous diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index 116bcf90fc..b5d64f0aae 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -18,6 +18,11 @@ namespace mlpack { namespace ann /** Artificial Neural Network. */ { /** + * Margin ranking loss measures the loss given inputs and a label vector with + * values of 1 or -1. If the label is 1 then the first input should be ranked + * higher than the second input at a distance larger than a margin, and vice- + * versa if the label is -1. + * * @tparam InputDataType Type of the input data (arma::colvec, arma::mat, * arma::sp_mat or arma::cube). * @tparam OutputDataType Type of the output data (arma::colvec, arma::mat, @@ -39,9 +44,6 @@ class MarginRankingLoss /** * Computes the Margin Ranking Loss function. - * Measures the loss between two inputs and a label with -1 and 1 values. - * A value of 1 in the label means the first input should be ranked higher - * and a value of -1 means the second input should be ranked higher. * * @param input Concatenation of the two inputs for evaluating the specified * function. diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp index 9920b07085..79e4d879e9 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp @@ -34,9 +34,9 @@ double MarginRankingLoss::Forward( const InputType& input, const TargetType& target) { - int input_rows = input.n_rows; - const InputType& input1 = input.rows(0, input_rows / 2 - 1); - const InputType& input2 = input.rows(input_rows / 2, input_rows - 1); + const int inputRows = input.n_rows; + const InputType& input1 = input.rows(0, inputRows / 2 - 1); + const InputType& input2 = input.rows(inputRows / 2, inputRows - 1); return arma::accu(arma::max(arma::zeros(size(target)), -target % (input1 - input2) + margin)) / target.n_cols; } @@ -52,9 +52,9 @@ void MarginRankingLoss::Backward( const TargetType& target, OutputType& output) { - int input_rows = input.n_rows; - const InputType& input1 = input.rows(0, input_rows / 2 - 1); - const InputType& input2 = input.rows(input_rows / 2, input_rows - 1); + const int inputRows = input.n_rows; + const InputType& input1 = input.rows(0, inputRows / 2 - 1); + const InputType& input2 = input.rows(inputRows / 2, inputRows - 1); output = -target % (input1 - input2) + margin; output.elem(arma::find(output >= 0)).ones(); output.elem(arma::find(output < 0)).zeros(); From b77d4b7d523fa1391e23222b23f7d57ba2ae2495 Mon Sep 17 00:00:00 2001 From: Gaurav Singh Date: Wed, 1 Apr 2020 01:28:55 +0530 Subject: [PATCH 239/265] Add CELU activation function for the issue #2181 (#2191) * add-celu * add-celu * celu activation function added. * Add CELU activation function * minor style changes * Minor style changes. * Removing deterministic parameter and correcting typos * Solving syntax issue. * HISTORY.md and documentation correction. * Reintroducing the deterministic parameter and minor style changes in other files. * Minor changes. * Minor changes in elu.hpp * Changes in layer_types.hpp. * Style changes in celu.hpp and celu_impl.hpp. * Minor changes in celu_impl.hpp. * Changes in activation_functions_test.cpp. * Removing r-values references from celu. * Adding deterministic method and check for alpha parameter to avoid division by zero error. * Minor style changes * Minor changes. * Solving merge conflicts. * Minor changes. * Adding the url to the paper and correcting identations in activation_functions_test.cpp. * Minor changes. * Update src/mlpack/methods/ann/layer/celu.hpp Co-Authored-By: Marcus Edel * Final changes. * Minor changes. * Initial commit. * Update src/mlpack/tests/activation_functions_test.cpp Co-Authored-By: Marcus Edel * Update src/mlpack/tests/activation_functions_test.cpp Co-Authored-By: Marcus Edel * Remove if from implementation. * Update src/mlpack/tests/activation_functions_test.cpp Co-Authored-By: Marcus Edel * Update src/mlpack/tests/activation_functions_test.cpp Co-Authored-By: Marcus Edel * Update src/mlpack/tests/activation_functions_test.cpp Co-Authored-By: Marcus Edel Co-authored-by: Marcus Edel --- HISTORY.md | 2 + src/mlpack/core/util/prefixedoutstream.hpp | 4 +- src/mlpack/methods/ann/layer/CMakeLists.txt | 2 + src/mlpack/methods/ann/layer/c_relu.hpp | 2 +- src/mlpack/methods/ann/layer/celu.hpp | 143 ++++++++++++++++++ src/mlpack/methods/ann/layer/celu_impl.hpp | 76 ++++++++++ src/mlpack/methods/ann/layer/concat.hpp | 2 +- src/mlpack/methods/ann/layer/elu.hpp | 2 +- src/mlpack/methods/ann/layer/elu_impl.hpp | 6 +- src/mlpack/methods/ann/layer/layer_types.hpp | 2 + .../tests/activation_functions_test.cpp | 82 ++++++++-- 11 files changed, 305 insertions(+), 18 deletions(-) create mode 100644 src/mlpack/methods/ann/layer/celu.hpp create mode 100644 src/mlpack/methods/ann/layer/celu_impl.hpp diff --git a/HISTORY.md b/HISTORY.md index 9dba6a517d..c93dcba0eb 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -49,6 +49,8 @@ * Add LiSHT activation function (#2182). * Add Valid and Same Padding for Transposed Convolution layer (#2163). + + * Add CELU activation function (#2191) * Add Log-Hyperbolic-Cosine Loss function (#2207) diff --git a/src/mlpack/core/util/prefixedoutstream.hpp b/src/mlpack/core/util/prefixedoutstream.hpp index 6078edde1b..138a79109d 100644 --- a/src/mlpack/core/util/prefixedoutstream.hpp +++ b/src/mlpack/core/util/prefixedoutstream.hpp @@ -124,7 +124,7 @@ class PrefixedOutStream private: /** * Conducts the base logic required in all the operator << overloads. Mostly - * just a good idea to reduce copy-pasta. + * just a good idea to reduce copy-paste. * * This overload is for non-Armadillo objects, which need special handling * during printing. @@ -138,7 +138,7 @@ class PrefixedOutStream /** * Conducts the base logic required in all the operator << overloads. Mostly - * just a good idea to reduce copy-pasta. + * just a good idea to reduce copy-paste. * * This overload is for Armadillo objects, which need special handling during * printing. diff --git a/src/mlpack/methods/ann/layer/CMakeLists.txt b/src/mlpack/methods/ann/layer/CMakeLists.txt index a101444157..2cc3e3fb98 100644 --- a/src/mlpack/methods/ann/layer/CMakeLists.txt +++ b/src/mlpack/methods/ann/layer/CMakeLists.txt @@ -96,6 +96,8 @@ set(SOURCES weight_norm_impl.hpp hardshrink.hpp hardshrink_impl.hpp + celu.hpp + celu_impl.hpp softshrink.hpp softshrink_impl.hpp ) diff --git a/src/mlpack/methods/ann/layer/c_relu.hpp b/src/mlpack/methods/ann/layer/c_relu.hpp index ea52cb999a..f41a3f77cf 100644 --- a/src/mlpack/methods/ann/layer/c_relu.hpp +++ b/src/mlpack/methods/ann/layer/c_relu.hpp @@ -58,7 +58,7 @@ class CReLU /** * Ordinary feed forward pass of a neural network, evaluating the function * f(x) by propagating the activity forward through f. - * Works only for 2D Tenosrs. + * Works only for 2D Tensors. * * @param input Input data used for evaluating the specified function. * @param output Resulting output activation. diff --git a/src/mlpack/methods/ann/layer/celu.hpp b/src/mlpack/methods/ann/layer/celu.hpp new file mode 100644 index 0000000000..a64d128489 --- /dev/null +++ b/src/mlpack/methods/ann/layer/celu.hpp @@ -0,0 +1,143 @@ +/** + * @file celu.hpp + * @author Gaurav Singh + * + * Definition of the CELU activation function as described by Jonathan T. Barron. + * + * For more information, read the following paper. + * + * @code + * @article{ + * author = {Jonathan T. Barron}, + * title = {Continuously Differentiable Exponential Linear Units}, + * year = {2017}, + * url = {https://arxiv.org/pdf/1704.07483} + * } + * @endcode + * + * 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_METHODS_ANN_LAYER_CELU_HPP +#define MLPACK_METHODS_ANN_LAYER_CELU_HPP + +#include + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +/** + * The CELU activation function, defined by + * + * @f{eqnarray*}{ + * f(x) &=& \left\{ + * \begin{array}{lr} + * x & : x \ge 0 \\ + * \alpha(e^(\frac{x}{\alpha}) - 1) & : x < 0 + * \end{array} + * \right. \\ + * f'(x) &=& \left\{ + * \begin{array}{lr} + * 1 & : x \ge 0 \\ + * (\frac{f(x)}{\alpha}) + 1 & : x < 0 + * \end{array} + * \right. + * @f} + * + * In the deterministic mode, there is no computation of the derivative. + * + * @tparam InputDataType Type of the input data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + * @tparam OutputDataType Type of the output data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + */ +template < + typename InputDataType = arma::mat, + typename OutputDataType = arma::mat +> +class CELU +{ + public: + /** + * Create the CELU object using the specified parameter. The non zero + * gradient for negative inputs can be adjusted by specifying the CELU + * hyperparameter alpha (alpha > 0). + * + * @param alpha Scale parameter for the negative factor (default = 1.0). + */ + CELU(const double alpha = 1.0); + + /** + * Ordinary feed forward pass of a neural network, evaluating the function + * f(x) by propagating the activity forward through f. + * + * @param input Input data used for evaluating the specified function. + * @param output Resulting output activation. + */ + template + void Forward(const InputType& input, OutputType& output); + + /** + * Ordinary feed backward pass of a neural network, calculating the function + * f(x) by propagating x backwards through f. Using the results from the feed + * forward pass. + * + * @param input The propagated input activation f(x). + * @param gy The backpropagated error. + * @param g The calculated gradient. + */ + template + void Backward(const DataType& input, const DataType& gy, DataType& g); + + //! Get the output parameter. + OutputDataType const& OutputParameter() const { return outputParameter; } + //! Modify the output parameter. + OutputDataType& OutputParameter() { return outputParameter; } + + //! Get the delta. + OutputDataType const& Delta() const { return delta; } + //! Modify the delta. + OutputDataType& Delta() { return delta; } + + //! Get the non zero gradient. + double const& Alpha() const { return alpha; } + //! Modify the non zero gradient. + double& Alpha() { return alpha; } + + //! Get the value of deterministic parameter. + bool Deterministic() const { return deterministic; } + //! Modify the value of deterministic parameter. + bool& Deterministic() { return deterministic; } + + /** + * Serialize the layer. + */ + template + void serialize(Archive& ar, const unsigned int /* version */); + + private: + //! Locally-stored delta object. + OutputDataType delta; + + //! Locally-stored output parameter object. + OutputDataType outputParameter; + + //! Locally stored first derivative of the activation function. + arma::mat derivative; + + //! CELU Hyperparameter (alpha > 0). + double alpha; + + //! If true the derivative computation is disabled, see notes above. + bool deterministic; +}; // class CELU + +} // namespace ann +} // namespace mlpack + +// Include implementation. +#include "celu_impl.hpp" + +#endif diff --git a/src/mlpack/methods/ann/layer/celu_impl.hpp b/src/mlpack/methods/ann/layer/celu_impl.hpp new file mode 100644 index 0000000000..bf92e5da1a --- /dev/null +++ b/src/mlpack/methods/ann/layer/celu_impl.hpp @@ -0,0 +1,76 @@ +/** + * @file celu_impl.hpp + * @author Gaurav Singh + * + * Implementation of the CELU activation function as described by Jonathan T. Barron. + * + * 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_METHODS_ANN_LAYER_CELU_IMPL_HPP +#define MLPACK_METHODS_ANN_LAYER_CELU_IMPL_HPP + +// In case it hasn't yet been included. +#include "celu.hpp" + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +template +CELU::CELU(const double alpha) : + alpha(alpha), + deterministic(false) +{ + if (alpha == 0) + { + Log::Fatal << "The value of alpha cannot be equal to 0, " + << "terminating the program." << std::endl; + } +} + +template +template +void CELU::Forward( + const InputType& input, OutputType& output) +{ + output = arma::ones(arma::size(input)); + for (size_t i = 0; i < input.n_elem; i++) + { + output(i) = (input(i) >= 0) ? input(i) : alpha * + (std::exp(input(i) / alpha) - 1); + } + + if (!deterministic) + { + derivative.set_size(arma::size(input)); + for (size_t i = 0; i < input.n_elem; i++) + { + derivative(i) = (input(i) >= 0) ? 1 : + (output(i) / alpha) + 1; + } + } +} + +template +template +void CELU::Backward( + const DataType& /* input */, const DataType& gy, DataType& g) +{ + g = gy % derivative; +} + +template +template +void CELU::serialize( + Archive& ar, + const unsigned int /* version */) +{ + ar & BOOST_SERIALIZATION_NVP(alpha); +} + +} // namespace ann +} // namespace mlpack + +#endif diff --git a/src/mlpack/methods/ann/layer/concat.hpp b/src/mlpack/methods/ann/layer/concat.hpp index 7716ee9207..51c2bf2971 100644 --- a/src/mlpack/methods/ann/layer/concat.hpp +++ b/src/mlpack/methods/ann/layer/concat.hpp @@ -3,7 +3,7 @@ * @author Marcus Edel * @author Mehul Kumar Nirala * - * Definition of the Concat class, which acts as a concatenation contain. + * Definition of the Concat class, which acts as a concatenation container. * * 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 diff --git a/src/mlpack/methods/ann/layer/elu.hpp b/src/mlpack/methods/ann/layer/elu.hpp index c837f6a955..200d873f81 100644 --- a/src/mlpack/methods/ann/layer/elu.hpp +++ b/src/mlpack/methods/ann/layer/elu.hpp @@ -3,7 +3,7 @@ * @author Vivek Pal * @author Dakshit Agrawal * - * Definition of the ELU activation function as descibed by Djork-Arne Clevert, + * Definition of the ELU activation function as described by Djork-Arne Clevert, * Thomas Unterthiner and Sepp Hochreiter. * * Definition of the SELU function as introduced by diff --git a/src/mlpack/methods/ann/layer/elu_impl.hpp b/src/mlpack/methods/ann/layer/elu_impl.hpp index a184b1d76c..cc2d189995 100644 --- a/src/mlpack/methods/ann/layer/elu_impl.hpp +++ b/src/mlpack/methods/ann/layer/elu_impl.hpp @@ -3,7 +3,7 @@ * @author Vivek Pal * @author Dakshit Agrawal * - * Implementation of the ELU activation function as descibed by Djork-Arne + * Implementation of the ELU activation function as described by Djork-Arne * Clevert, Thomas Unterthiner and Sepp Hochreiter. * * Implementation of the SELU function as introduced by Klambauer et. al. in @@ -51,7 +51,7 @@ template void ELU::Forward( const InputType& input, OutputType& output) { - output.set_size(arma::size(input)); + output = arma::ones(arma::size(input)); for (size_t i = 0; i < input.n_elem; i++) { if (input(i) < DBL_MAX) @@ -59,8 +59,6 @@ void ELU::Forward( output(i) = (input(i) > 0) ? lambda * input(i) : lambda * alpha * (std::exp(input(i)) - 1); } - else - output(i) = 1.0; } if (!deterministic) diff --git a/src/mlpack/methods/ann/layer/layer_types.hpp b/src/mlpack/methods/ann/layer/layer_types.hpp index 3682ed98cd..3c17021e45 100644 --- a/src/mlpack/methods/ann/layer/layer_types.hpp +++ b/src/mlpack/methods/ann/layer/layer_types.hpp @@ -42,6 +42,7 @@ #include #include #include +#include #include // Convolution modules. @@ -244,6 +245,7 @@ using LayerTypes = boost::variant< Padding*, PReLU*, WeightNorm*, + CELU*, MoreTypes, CustomLayers*... >; diff --git a/src/mlpack/tests/activation_functions_test.cpp b/src/mlpack/tests/activation_functions_test.cpp index 9913088ec5..78b32af158 100644 --- a/src/mlpack/tests/activation_functions_test.cpp +++ b/src/mlpack/tests/activation_functions_test.cpp @@ -219,7 +219,7 @@ void CheckLeakyReLUDerivativeCorrect(const arma::colvec input, * @param target Target data used to evaluate the ELU activation. */ void CheckELUActivationCorrect(const arma::colvec input, - const arma::colvec target) + const arma::colvec target) { // Initialize ELU object with alpha = 1.0. ELU<> lrf(1.0); @@ -241,7 +241,7 @@ void CheckELUActivationCorrect(const arma::colvec input, * @param target Target data used to evaluate the ELU activation. */ void CheckELUDerivativeCorrect(const arma::colvec input, - const arma::colvec target) + const arma::colvec target) { // Initialize ELU object with alpha = 1.0. ELU<> lrf(1.0); @@ -261,14 +261,14 @@ void CheckELUDerivativeCorrect(const arma::colvec input, /* * Implementation of the PReLU activation function test. The function - * is implemented as PReLU layer in the file perametric_relu.hpp + * is implemented as PReLU layer in the file parametric_relu.hpp. * * @param input Input data used for evaluating the PReLU activation * function. * @param target Target data used to evaluate the PReLU activation. */ void CheckPReLUActivationCorrect(const arma::colvec input, - const arma::colvec target) + const arma::colvec target) { PReLU<> prelu; @@ -284,14 +284,14 @@ void CheckPReLUActivationCorrect(const arma::colvec input, /* * Implementation of the PReLU activation function derivative test. * The function is implemented as PReLU layer in the file - * perametric_relu.hpp + * parametric_relu.hpp * * @param input Input data used for evaluating the PReLU activation * function. * @param target Target data used to evaluate the PReLU activation. */ void CheckPReLUDerivativeCorrect(const arma::colvec input, - const arma::colvec target) + const arma::colvec target) { PReLU<> prelu; @@ -317,7 +317,7 @@ void CheckPReLUDerivativeCorrect(const arma::colvec input, * @param target Target data used to evaluate the PReLU gradient. */ void CheckPReLUGradientCorrect(const arma::colvec input, - const arma::colvec target) + const arma::colvec target) { PReLU<> prelu; @@ -340,7 +340,7 @@ void CheckPReLUGradientCorrect(const arma::colvec input, * @param target Target data used to evaluate the Hard Shrink activation. */ void CheckHardShrinkActivationCorrect(const arma::colvec input, - const arma::colvec target) + const arma::colvec target) { HardShrink<> hardshrink; @@ -362,7 +362,7 @@ void CheckHardShrinkActivationCorrect(const arma::colvec input, * @param target Target data used to evaluate the HardShrink activation. */ void CheckHardShrinkDerivativeCorrect(const arma::colvec input, - const arma::colvec target) + const arma::colvec target) { HardShrink<> hardshrink; @@ -497,6 +497,54 @@ BOOST_AUTO_TEST_CASE(SELUFunctionDerivativeTest) selu.Lambda() * selu.Alpha() - arma::mean(activations))), 10e-4); } +/** + * Implementation of the CELU activation function test. The function is + * implemented as CELU layer in the file celu.hpp. + * + * @param input Input data used for evaluating the CELU activation function. + * @param target Target data used to evaluate the CELU activation. + */ +void CheckCELUActivationCorrect(const arma::colvec input, + const arma::colvec target) +{ + // Initialize CELU object with alpha = 1.0. + CELU<> lrf(1.0); + + // Test the activation function using the entire vector as input. + arma::colvec activations; + lrf.Forward(input, activations); + for (size_t i = 0; i < activations.n_elem; i++) + { + BOOST_REQUIRE_CLOSE(activations.at(i), target.at(i), 1e-3); + } +} + +/** + * Implementation of the CELU activation function derivative test. The function + * is implemented as CELU layer in the file celu.hpp. + * + * @param input Input data used for evaluating the CELU activation function. + * @param target Target data used to evaluate the CELU activation. + */ +void CheckCELUDerivativeCorrect(const arma::colvec input, + const arma::colvec target) +{ + // Initialize CELU object with alpha = 1.0. + CELU<> lrf(1.0); + + // Test the calculation of the derivatives using the entire vector as input. + arma::colvec derivatives, activations; + + // This error vector will be set to 1 to get the derivatives. + arma::colvec error = arma::ones(input.n_elem); + lrf.Forward(input, activations); + lrf.Backward(activations, error, derivatives); + for (size_t i = 0; i < derivatives.n_elem; i++) + { + BOOST_REQUIRE_CLOSE(derivatives.at(i), target.at(i), 1e-3); + } +} + /** * Basic test of the tanh function. */ @@ -828,4 +876,20 @@ BOOST_AUTO_TEST_CASE(SoftShrinkFunctionTest) desiredDerivatives); } +/** + * Basic test of the CELU activation function. + */ +BOOST_AUTO_TEST_CASE(CELUFunctionTest) +{ + const arma::colvec desiredActivations("-0.86466472 3.2 4.5 \ + -1 1 -0.63212056 2 0"); + + const arma::colvec desiredDerivatives("0.42119275 1 1 \ + 0.36787944 1 \ + 0.5314636 1 1"); + + CheckCELUActivationCorrect(activationData, desiredActivations); + CheckCELUDerivativeCorrect(desiredActivations, desiredDerivatives); +} + BOOST_AUTO_TEST_SUITE_END(); From 96be8af21de48ea4b5f6aab51bf9bfd90de367ae Mon Sep 17 00:00:00 2001 From: Mikhail Lozhnikov Date: Tue, 31 Mar 2020 23:42:27 +0300 Subject: [PATCH 240/265] Updated documentation for various string encoding tests. --- .../bag_of_words_encoding_policy.hpp | 1 + .../tf_idf_encoding_policy.hpp | 1 + src/mlpack/tests/string_encoding_test.cpp | 644 ++++++++++++------ 3 files changed, 450 insertions(+), 196 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index e3be0f5faa..44a8e72903 100644 --- a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -1,6 +1,7 @@ /** * @file bag_of_words_encoding_policy.hpp * @author Jeffin Sam + * @author Mikhail Lozhnikov * * Definition of the BagOfWordsEncodingPolicy class. * diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 2ad8b15e9f..4acfc03c56 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -1,6 +1,7 @@ /** * @file tf_idf_encoding_policy.hpp * @author Jeffin Sam + * @author Mikhail Lozhnikov * * Definition of the TfIdfEncodingPolicy class. * diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index baec566bcb..23d1dc59dd 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -522,6 +522,7 @@ BOOST_AUTO_TEST_CASE(CharExtractDictionaryEncodingSerialization) EncoderType encoder; CharExtract tokenizer; arma::mat output; + encoder.Encode(stringEncodingInput, output, tokenizer); EncoderType xmlEncoder, textEncoder, binaryEncoder; @@ -541,7 +542,7 @@ BOOST_AUTO_TEST_CASE(CharExtractDictionaryEncodingSerialization) } /** - * Test for Bag of Words encoding algorithm. + * Test the Bag of Words encoding algorithm. */ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) { @@ -552,29 +553,71 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) SplitByAnyOf tokenizer(" ,."); encoder.Encode(stringEncodingInput, output, tokenizer); + const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers. + // Checking that each token has a unique label. std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) { keysCount[keyValue.second]++; - // Every token should be mapped only once. + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } + +/* The expected values were obtained by the following Python script: + + from sklearn.feature_extraction.text import CountVectorizer + from collections import OrderedDict + import re + + string_encoding_input = [ + "mlpack is an intuitive, fast, and flexible C++ machine learning library " + "with bindings to other languages. ", + "It is meant to be a machine learning analog to LAPACK, and aims to " + "implement a wide array of machine learning methods and functions " + "as a \"swiss army knife\" for machine learning researchers.", + "In addition to its powerful C++ interface, mlpack also provides " + "command-line programs and Python bindings." + ] + + dictionary = OrderedDict() + + count = 0 + for line in string_encoding_input: + for word in re.split(' |,|\.', line): + if word and (not (word in dictionary)): + dictionary[word] = count + count += 1 + + def tokenizer(line): + return re.split(' |,|\.', line) + + vectorizer = CountVectorizer(strip_accents=False, lowercase=False, + preprocessor=None, tokenizer=tokenizer, stop_words=None, + vocabulary=dictionary, binary=True) + + X = vectorizer.fit_transform(string_encoding_input) + + for row in X.toarray(): + print("{ " + ", ".join(map(str, row)) + " },") +*/ + arma::mat expected = { - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; + CheckMatrices(output, expected.t()); } /** - * Bag of Words encoding algorithm output saved in a vector. + * Test the Bag of Words encoding algorithm. The output is saved into a vector. */ BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingTest) { @@ -589,29 +632,32 @@ BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingTest) const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers. + // Checking that each token has a unique label. std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) { keysCount[keyValue.second]++; - // Every token should be mapped only once. + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } + /* The expected values were obtained by the same script as in + BagOfWordsEncodingTest. */ vector> expected = { - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; BOOST_REQUIRE(output == expected); } /** - * Test Bag of Words encoding for characters. + * Test the Bag of Words algorithm for individual characters. */ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) { @@ -624,8 +670,8 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) arma::mat output; BagOfWordsEncoding encoder; - // Passing a empty string to encode characters. encoder.Encode(input, output, CharExtract()); + arma::mat target = { { 1, 1, 1, 0, 0 }, { 0, 1, 1, 1, 1 }, @@ -637,7 +683,7 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) /** * Test the Bag of Words encoding algorithm in case of individual - * character encoding, storing resulting in vector>. + * characters encoding. The output type is vector>. */ BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingIndividualCharactersTest) { @@ -650,7 +696,6 @@ BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingIndividualCharactersTest) vector> output; BagOfWordsEncoding encoder; - // Passing a empty string to encode characters. encoder.Encode(input, output, CharExtract()); vector> expected = { @@ -663,8 +708,9 @@ BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingIndividualCharactersTest) } /** - * Test the Tf-Idf Encoding using rawcount type and smoothidf as true, - * which is the default value used for algorithm. + * Test the Tf-Idf encoding algorithm with the raw count term frequency type + * and the smooth inverse document frequency type. These parameters are + * the default ones. */ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) { @@ -677,40 +723,96 @@ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingTest) encoder.Encode(stringEncodingInput, output, tokenizer); const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers. + // Checking that each token has a unique label. std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) { keysCount[keyValue.second]++; - // Every token should be mapped only once. + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } + + /* The expected values were obtained by the following Python script: + + from sklearn.feature_extraction.text import TfidfVectorizer + from collections import OrderedDict + import re + + string_encoding_input = [ + "mlpack is an intuitive, fast, and flexible C++ machine learning library " + "with bindings to other languages. ", + "It is meant to be a machine learning analog to LAPACK, and aims to " + "implement a wide array of machine learning methods and functions " + "as a \"swiss army knife\" for machine learning researchers.", + "In addition to its powerful C++ interface, mlpack also provides " + "command-line programs and Python bindings." + ] + + smooth_idf = True + tf_type = 'raw_count' + + dictionary = OrderedDict() + + count = 0 + for line in string_encoding_input: + for word in re.split(' |,|\.', line): + if word and (not (word in dictionary)): + dictionary[word] = count + count += 1 + + def tokenizer(line): + return re.split(' |,|\.', line) + + if tf_type == 'raw_count': + binary = False + sublinear_tf = False + elif tf_type == 'binary': + binary = True + sublinear_tf = False + elif tf_type == 'sublinear_tf': + binary = False + sublinear_tf = True + + vectorizer = TfidfVectorizer(strip_accents=False, lowercase=False, + preprocessor=None, tokenizer=tokenizer, stop_words=None, + vocabulary=dictionary, binary=binary, norm=None, smooth_idf=smooth_idf, + sublinear_tf=sublinear_tf) + + X = vectorizer.fit_transform(string_encoding_input) + + def format_result(value): + if value == int(value): + return str(int(value)) + else: + return "{0:.8f}".format(value) + + for row in X.toarray(): + print("{ " + ", ".join(map(format_result, row)) + " },") + */ arma::mat expected = { - { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, - 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, - 1.28768207245178, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0 }, - { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, - 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, - 1.28768207245178, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995 } + { 1.28768207, 1.28768207, 1.69314718, 1.69314718, 1.69314718, 1, 1.69314718, + 1.28768207, 1.28768207, 1.28768207, 1.69314718, 1.69314718, 1.28768207, 1, + 1.69314718, 1.69314718, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 1.28768207, 0, 0, 0, 2, 0, 0, 3.86304622, 3.86304622, 0, 0, 0, 3, 0, + 0, 1.69314718, 1.69314718, 1.69314718, 5.07944154, 1.69314718, 1.69314718, + 1.69314718, 1.69314718, 1.69314718, 1.69314718, 1.69314718, 1.69314718, + 1.69314718, 1.69314718, 1.69314718, 1.69314718, 1.69314718, 1.69314718, + 1.69314718, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.28768207, 0, 0, 0, 0, 1, 0, 1.28768207, 0, 0, 0, 0, 1.28768207, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.69314718, + 1.69314718, 1.69314718, 1.69314718, 1.69314718, 1.69314718, 1.69314718, + 1.69314718, 1.69314718, 1.69314718 } }; - CheckMatrices(output, expected.t(), 1e-12); + + CheckMatrices(output, expected.t(), 1e-6); } /** - * Test the TfIdf encoding algorithm, using rawcount as type of tf and smoothIdf - * as true, storing the result in a vector>. + * Test the Tf-Idf encoding algorithm with the raw count term frequency type + * and the smooth inverse document frequency type. These parameters are + * the default ones. The output type is vector>. */ BOOST_AUTO_TEST_CASE(VectorRawCountSmoothIdfEncodingTest) { @@ -725,41 +827,40 @@ BOOST_AUTO_TEST_CASE(VectorRawCountSmoothIdfEncodingTest) const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers. + // Checking that each token has a unique label. std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) { keysCount[keyValue.second]++; - // Every token should be mapped only once. + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } + /* The expected values were obtained by the same script as in + RawCountSmoothIdfEncodingTest. */ vector> expected = { - { 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1, 1.69314718055995, 1.28768207245178, - 1.28768207245178, 1.28768207245178, 1.69314718055995, 1.69314718055995, - 1.28768207245178, 1, 1.69314718055995, 1.69314718055995, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0 }, - { 0, 1.28768207245178, 0, 0, 0, 2, 0, 0, 3.86304621735534, - 3.86304621735534, 0, 0, 0, 3, 0, 0, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 5.07944154167984, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1.28768207245178, 0, 0, 0, 0, 1, 0, 1.28768207245178, 0, 0, 0, 0, - 1.28768207245178, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995, 1.69314718055995, - 1.69314718055995, 1.69314718055995, 1.69314718055995 } + { 1.28768207, 1.28768207, 1.69314718, 1.69314718, 1.69314718, 1, 1.69314718, + 1.28768207, 1.28768207, 1.28768207, 1.69314718, 1.69314718, 1.28768207, 1, + 1.69314718, 1.69314718, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 1.28768207, 0, 0, 0, 2, 0, 0, 3.86304622, 3.86304622, 0, 0, 0, 3, 0, + 0, 1.69314718, 1.69314718, 1.69314718, 5.07944154, 1.69314718, 1.69314718, + 1.69314718, 1.69314718, 1.69314718, 1.69314718, 1.69314718, 1.69314718, + 1.69314718, 1.69314718, 1.69314718, 1.69314718, 1.69314718, 1.69314718, + 1.69314718, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.28768207, 0, 0, 0, 0, 1, 0, 1.28768207, 0, 0, 0, 0, 1.28768207, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.69314718, + 1.69314718, 1.69314718, 1.69314718, 1.69314718, 1.69314718, 1.69314718, + 1.69314718, 1.69314718, 1.69314718 } }; - CheckVectors(output, expected, 1e-12); + CheckVectors(output, expected, 1e-6); } /** - * Test TFIDF encoding for characters, using rawcount as tf type and - * smoothidf as true. + * Test the Tf-Idf encoding algorithm for individual characters with the + * raw count term frequency type and the smooth inverse document frequency type. + * These parameters are the default ones. */ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) { @@ -772,19 +873,75 @@ BOOST_AUTO_TEST_CASE(RawCountSmoothIdfEncodingIndividualCharactersTest) arma::mat output; TfIdfEncoding encoder; - // Passing a empty string to encode characters. encoder.Encode(input, output, CharExtract()); + + /* The expected values were obtained by the following Python script: + + from sklearn.feature_extraction.text import TfidfVectorizer + from collections import OrderedDict + import re + + input_string = [ + "GACCA", + "ABCABCD", + "GAB" + ] + + smooth_idf = True + tf_type = 'raw_count' + + dictionary = OrderedDict() + + count = 0 + for line in input_string: + for word in list(line): + if word and (not (word in dictionary)): + dictionary[word] = count + count += 1 + + def tokenizer(line): + return list(line) + + if tf_type == 'raw_count': + binary = False + sublinear_tf = False + elif tf_type == 'binary': + binary = True + sublinear_tf = False + elif tf_type == 'sublinear_tf': + binary = False + sublinear_tf = True + + vectorizer = TfidfVectorizer(strip_accents=False, lowercase=False, + preprocessor=None, tokenizer=tokenizer, stop_words=None, + vocabulary=dictionary, binary=binary, norm=None, smooth_idf=smooth_idf, + sublinear_tf=sublinear_tf) + + X = vectorizer.fit_transform(input_string) + + def format_result(value): + if value == int(value): + return str(int(value)) + else: + return "{0:.14f}".format(value) + + for row in X.toarray(): + print("{ " + ", ".join(map(format_result, row)) + " },") + */ arma::mat target = { - { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, - { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, - { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + { 1.28768207245178, 2, 2.57536414490356, 0, 0 }, + { 0, 2, 2.57536414490356, 2.57536414490356, 1.69314718055995 }, + { 1.28768207245178, 1, 0, 1.28768207245178, 0 } }; + CheckMatrices(output, target.t(), 1e-12); } /** - * Test the Tf-Idf encoding algorithm to store result in vector - * in case of individual character encoding using default values. + * Test the Tf-Idf encoding algorithm for individual characters with the + * raw count term frequency type and the smooth inverse document frequency type. + * These parameters are the default ones. The output type is + * vector>. */ BOOST_AUTO_TEST_CASE(VectorRawCountSmoothIdfEncodingIndividualCharactersTest) { @@ -797,18 +954,22 @@ BOOST_AUTO_TEST_CASE(VectorRawCountSmoothIdfEncodingIndividualCharactersTest) vector> output; TfIdfEncoding encoder; - // Passing a empty string to encode characters. encoder.Encode(input, output, CharExtract()); + + /* The expected values were obtained by the same script as in + RawCountSmoothIdfEncodingIndividualCharactersTest. */ vector> expected = { - { 1.2876820724517808, 2, 2.5753641449035616, 0, 0 }, - { 0, 2, 2.5753641449035616, 2.5753641449035616, 1.6931471805599454 }, - { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + { 1.28768207245178, 2, 2.57536414490356, 0, 0 }, + { 0, 2, 2.57536414490356, 2.57536414490356, 1.69314718055995 }, + { 1.28768207245178, 1, 0, 1.28768207245178, 0 } }; + CheckVectors(output, expected, 1e-12); } /** - * Test the Tf-Idf Encoding using rawcount type and smoothidf as false. + * Test the Tf-Idf encoding algorithm with the raw count term frequency type + * and the non-smooth inverse document frequency type. */ BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) { @@ -816,49 +977,50 @@ BOOST_AUTO_TEST_CASE(TfIdfRawCountEncodingTest) arma::mat output; TfIdfEncoding encoder( - (TfIdfEncodingPolicy(TfIdfEncodingPolicy::TfTypes::RAW_COUNT, false))); + TfIdfEncodingPolicy(TfIdfEncodingPolicy::TfTypes::RAW_COUNT, false)); SplitByAnyOf tokenizer(" ,."); encoder.Encode(stringEncodingInput, output, tokenizer); const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers. + // Checking that each token has a unique label. std::unordered_map keysCount; + for (auto& keyValue : dictionary.Mapping()) { keysCount[keyValue.second]++; - // Every token should be mapped only once. + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } + /* The expected values were obtained by almost the same script as in + RawCountSmoothIdfEncodingTest. The only difference is smooth_idf equals + False. */ arma::mat expected = { - { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, - 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, - 1.40546510810816, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0 }, - { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, - 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, - 1.40546510810816, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811 } + { 1.40546511, 1.40546511, 2.09861229, 2.09861229, 2.09861229, 1, 2.09861229, + 1.40546511, 1.40546511, 1.40546511, 2.09861229, 2.09861229, 1.40546511, 1, + 2.09861229, 2.09861229, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 1.40546511, 0, 0, 0, 2, 0, 0, 4.21639532, 4.21639532, 0, 0, 0, 3, 0, 0, + 2.09861229, 2.09861229, 2.09861229, 6.29583687, 2.09861229, 2.09861229, + 2.09861229, 2.09861229, 2.09861229, 2.09861229, 2.09861229, 2.09861229, + 2.09861229, 2.09861229, 2.09861229, 2.09861229, 2.09861229, 2.09861229, + 2.09861229, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.40546511, 0, 0, 0, 0, 1, 0, 1.40546511, 0, 0, 0, 0, 1.40546511, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.09861229, + 2.09861229, 2.09861229, 2.09861229, 2.09861229, 2.09861229, 2.09861229, + 2.09861229, 2.09861229, 2.09861229 } }; - CheckMatrices(output, expected.t(), 1e-12); + + CheckMatrices(output, expected.t(), 1e-6); } /** - * Test the TfIdf encoding algorithm for output type as vector, with rawcount - * as type, but with smoothidf as false. - */ + * Test the Tf-Idf encoding algorithm with the raw count term frequency type + * and the non-smooth inverse document frequency type. The output type is + * vector>. + */ BOOST_AUTO_TEST_CASE(VectorTfIdfRawCountEncodingTest) { using DictionaryType = StringEncodingDictionary; @@ -872,43 +1034,42 @@ BOOST_AUTO_TEST_CASE(VectorTfIdfRawCountEncodingTest) const DictionaryType& dictionary = encoder.Dictionary(); - // Checking that everything is mapped to different numbers. + // Checking that each token has a unique label. std::unordered_map keysCount; for (auto& keyValue : dictionary.Mapping()) { keysCount[keyValue.second]++; - // Every token should be mapped only once. + BOOST_REQUIRE_EQUAL(keysCount[keyValue.second], 1); } + /* The expected values were obtained by almost the same script as in + RawCountSmoothIdfEncodingTest. The only difference is smooth_idf equals + False. */ vector> expected = { - { 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 1, 2.09861228866811, 1.40546510810816, - 1.40546510810816, 1.40546510810816, 2.09861228866811, 2.09861228866811, - 1.40546510810816, 1, 2.09861228866811, 2.09861228866811, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0 }, - { 0, 1.40546510810816, 0, 0, 0, 2, 0, 0, 4.21639532432449, - 4.21639532432449, 0, 0, 0, 3, 0, 0, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 6.29583686600433, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1.40546510810816, 0, 0, 0, 0, 1, 0, 1.40546510810816, 0, 0, 0, 0, - 1.40546510810816, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811, 2.09861228866811, - 2.09861228866811, 2.09861228866811, 2.09861228866811 } + { 1.40546511, 1.40546511, 2.09861229, 2.09861229, 2.09861229, 1, 2.09861229, + 1.40546511, 1.40546511, 1.40546511, 2.09861229, 2.09861229, 1.40546511, 1, + 2.09861229, 2.09861229, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 1.40546511, 0, 0, 0, 2, 0, 0, 4.21639532, 4.21639532, 0, 0, 0, 3, 0, 0, + 2.09861229, 2.09861229, 2.09861229, 6.29583687, 2.09861229, 2.09861229, + 2.09861229, 2.09861229, 2.09861229, 2.09861229, 2.09861229, 2.09861229, + 2.09861229, 2.09861229, 2.09861229, 2.09861229, 2.09861229, 2.09861229, + 2.09861229, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1.40546511, 0, 0, 0, 0, 1, 0, 1.40546511, 0, 0, 0, 0, 1.40546511, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.09861229, + 2.09861229, 2.09861229, 2.09861229, 2.09861229, 2.09861229, 2.09861229, + 2.09861229, 2.09861229, 2.09861229 } }; - CheckVectors(output, expected, 1e-12); + CheckVectors(output, expected, 1e-6); } /** - * Test TFIDF encoding for characters, using rawcount as - * tf type and smoothidf as false. + * Test the Tf-Idf encoding algorithm for individual characters with the + * raw count term frequency type and the non-smooth inverse document frequency + * type. */ -BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(RawCountTfIdfEncodingIndividualCharactersTest) { vector input = { "GACCA", @@ -917,25 +1078,29 @@ BOOST_AUTO_TEST_CASE(RawcountTfIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding - encoder(TfIdfEncodingPolicy::TfTypes::RAW_COUNT, false); + TfIdfEncoding encoder( + TfIdfEncodingPolicy::TfTypes::RAW_COUNT, false); - // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); + + /* The expected values were obtained by almost the same script as in + RawCountSmoothIdfEncodingIndividualCharactersTest. The only difference is + smooth_idf equals False. */ arma::mat target = { - { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, - { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, - { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } + { 1.40546510810816, 2, 2.81093021621633, 0, 0 }, + { 0, 2, 2.81093021621633, 2.81093021621633, 2.09861228866811 }, + { 1.40546510810816, 1, 0, 1.40546510810816, 0 } }; + CheckMatrices(output, target.t(), 1e-12); } /** - * Test the Tf Idf encoding algorithm to store result in vector - * in case of individual character encoding, using raw count as type, - * and smoothidf as false. + * Test the Tf-Idf encoding algorithm for individual characters with the + * raw count term frequency type and the non-smooth inverse document frequency + * type. The output type is vector>. */ -BOOST_AUTO_TEST_CASE(VectorRawcountEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(VectorRawCountTfIdfEncodingIndividualCharactersTest) { std::vector input = { "GACCA", @@ -944,22 +1109,26 @@ BOOST_AUTO_TEST_CASE(VectorRawcountEncodingIndividualCharactersTest) }; vector> output; - TfIdfEncoding - encoder(TfIdfEncodingPolicy::TfTypes::RAW_COUNT, false); + TfIdfEncoding encoder( + TfIdfEncodingPolicy::TfTypes::RAW_COUNT, false); - // Passing a empty string to encode characters. encoder.Encode(input, output, CharExtract()); + + /* The expected values were obtained by almost the same script as in + RawCountSmoothIdfEncodingIndividualCharactersTest. The only difference is + smooth_idf equals False. */ vector> expected = { - { 1.4054651081081644, 2, 2.8109302162163288, 0, 0 }, - { 0, 2, 2.8109302162163288, 2.8109302162163288, 2.0986122886681100 }, - { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } + { 1.40546510810816, 2, 2.81093021621633, 0, 0 }, + { 0, 2, 2.81093021621633, 2.81093021621633, 2.09861228866811 }, + { 1.40546510810816, 1, 0, 1.40546510810816, 0 } }; + CheckVectors(output, expected, 1e-12); } /** - * Test TFIDF encoding for characters, using binary weighting scheme - * for tf and smoothidf as true. + * Test the Tf-Idf encoding algorithm for individual characters with the + * binary term frequency type and the smooth inverse document frequency type. */ BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) { @@ -970,24 +1139,29 @@ BOOST_AUTO_TEST_CASE(BinarySmoothIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding - encoder(TfIdfEncodingPolicy::TfTypes::BINARY, true); + TfIdfEncoding encoder( + TfIdfEncodingPolicy::TfTypes::BINARY, true); - // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); + + /* The expected values were obtained by almost the same script as in + RawCountSmoothIdfEncodingIndividualCharactersTest. The only difference is + tf_type equals 'binary'. */ arma::mat target = { - { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, - { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, - { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + { 1.28768207245178, 1, 1.28768207245178, 0, 0 }, + { 0, 1, 1.28768207245178, 1.28768207245178, 1.69314718055995 }, + { 1.28768207245178, 1, 0, 1.28768207245178, 0 } }; + CheckMatrices(output, target.t(), 1e-12); } /** - * Test TFIDF encoding for characters to store results in vector, using binary - * weighting scheme for tf and smoothidf as true. + * Test the Tf-Idf encoding algorithm for individual characters with the + * binary term frequency type and the smooth inverse document frequency type. + * The output type is vector>. */ -BOOST_AUTO_TEST_CASE(VectorBnarySmoothIdfEncodingIndividualCharactersTest) +BOOST_AUTO_TEST_CASE(VectorBinarySmoothIdfEncodingIndividualCharactersTest) { std::vector input = { "GACCA", @@ -999,19 +1173,24 @@ BOOST_AUTO_TEST_CASE(VectorBnarySmoothIdfEncodingIndividualCharactersTest) TfIdfEncoding encoder(TfIdfEncodingPolicy::TfTypes::BINARY, true); - // Passing a empty string to encode characters. encoder.Encode(input, output, CharExtract()); + + /* The expected values were obtained by almost the same script as in + RawCountSmoothIdfEncodingIndividualCharactersTest. The only difference is + tf_type equals 'binary'. */ vector> expected = { - { 1.2876820724517808, 1, 1.2876820724517808, 0, 0 }, - { 0, 1, 1.2876820724517808, 1.2876820724517808, 1.6931471805599454 }, - { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + { 1.28768207245178, 1, 1.28768207245178, 0, 0 }, + { 0, 1, 1.28768207245178, 1.28768207245178, 1.69314718055995 }, + { 1.28768207245178, 1, 0, 1.28768207245178, 0 } }; + CheckVectors(output, expected, 1e-12); } /** - * Test TFIDF encoding for characters, using binary - * as weighting scheme and smoothidf as false. + * Test the Tf-Idf encoding algorithm for individual characters with the + * binary term frequency type and the non-smooth inverse document frequency + * type. */ BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) { @@ -1022,22 +1201,27 @@ BOOST_AUTO_TEST_CASE(BinaryTfIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding - encoder(TfIdfEncodingPolicy::TfTypes::BINARY, false); + TfIdfEncoding encoder( + TfIdfEncodingPolicy::TfTypes::BINARY, false); - // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); + + /* The expected values were obtained by almost the same script as in + RawCountSmoothIdfEncodingIndividualCharactersTest. The only difference is + tf_type equals 'binary' and smooth_idf equals False. */ arma::mat target = { - { 1.4054651081081644, 1, 1.4054651081081644, 0, 0 }, - { 0, 1, 1.4054651081081644, 1.4054651081081644, 2.0986122886681100 }, - { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } + { 1.40546510810816, 1, 1.40546510810816, 0, 0 }, + { 0, 1, 1.40546510810816, 1.40546510810816, 2.09861228866811 }, + { 1.40546510810816, 1, 0, 1.40546510810816, 0 } }; + CheckMatrices(output, target.t(), 1e-12); } /** - * Test TFIDF encoding for characters, using sublinear - * as weighting scheme and smoothidf as true. + * Test the Tf-Idf encoding algorithm for individual characters with the + * sublinear term frequency type and the smooth inverse document frequency + * type. */ BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) { @@ -1048,23 +1232,28 @@ BOOST_AUTO_TEST_CASE(SublinearSmoothIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding - encoder(TfIdfEncodingPolicy::TfTypes::SUBLINEAR_TF, true); + TfIdfEncoding encoder( + TfIdfEncodingPolicy::TfTypes::SUBLINEAR_TF, true); - // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); + + /* The expected values were obtained by almost the same script as in + RawCountSmoothIdfEncodingIndividualCharactersTest. The only difference is + tf_type equals 'sublinear_tf'. */ arma::mat target = { - { 1.2876820724517808, 1.6931471805599454, 2.1802352704293200, 0, 0 }, - { 0, 1.6931471805599454, 2.1802352704293200, 2.1802352704293200, - 1.6931471805599454 }, - { 1.2876820724517808, 1, 0, 1.2876820724517808, 0 } + { 1.28768207245178, 1.69314718055995, 2.18023527042932, 0, 0 }, + { 0, 1.69314718055995, 2.18023527042932, 2.18023527042932, + 1.69314718055995 }, + { 1.28768207245178, 1, 0, 1.28768207245178, 0 } }; + CheckMatrices(output, target.t(), 1e-12); } /** - * Test TFIDF encoding for characters, using sublinear - * as weighting scheme and smoothidf as false. + * Test the Tf-Idf encoding algorithm for individual characters with the + * sublinear term frequency type and the non-smooth inverse document frequency + * type. */ BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) { @@ -1078,20 +1267,25 @@ BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) TfIdfEncoding encoder(TfIdfEncodingPolicy::TfTypes::SUBLINEAR_TF, false); - // Passing a empty string to encode charactersrawcountsmoothidftrue. + /* The expected values were obtained by almost the same script as in + RawCountSmoothIdfEncodingIndividualCharactersTest. The only difference is + tf_type equals 'sublinear_tf' and smooth_idf equals False. */ encoder.Encode(input, output, CharExtract()); + arma::mat target = { - { 1.4054651081081644, 1.6931471805599454, 2.3796592851687173, 0, 0 }, - { 0, 1.6931471805599454, 2.3796592851687173, 2.3796592851687173, - 2.0986122886681100 }, - { 1.4054651081081644, 1, 0, 1.4054651081081644, 0 } + { 1.40546510810816, 1.69314718055995, 2.37965928516872, 0, 0 }, + { 0, 1.69314718055995, 2.37965928516872, 2.37965928516872, + 2.09861228866811 }, + { 1.40546510810816, 1, 0, 1.40546510810816, 0 } }; + CheckMatrices(output, target.t(), 1e-12); } /** - * Test TFIDF encoding for characters, using term - * Frequency as weighting scheme and smoothidf as true. + * Test the Tf-Idf encoding algorithm for individual characters with the + * standard term frequency type and the smooth inverse document frequency + * type. */ BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) { @@ -1102,23 +1296,77 @@ BOOST_AUTO_TEST_CASE(TermFrequencySmoothIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding - encoder(TfIdfEncodingPolicy::TfTypes::TERM_FREQUENCY, true); + TfIdfEncoding encoder( + TfIdfEncodingPolicy::TfTypes::TERM_FREQUENCY, true); - // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); + + /* The expected values were obtained by the following Python script: + + from sklearn.feature_extraction.text import CountVectorizer + from sklearn.feature_extraction.text import TfidfTransformer + from collections import OrderedDict + import numpy as np + import re + + input_string = [ + "GACCA", + "ABCABCD", + "GAB" + ] + + smooth_idf = True + + dictionary = OrderedDict() + + count = 0 + for line in input_string: + for word in list(line): + if word and (not (word in dictionary)): + dictionary[word] = count + count += 1 + + def tokenizer(line): + return list(line) + + vectorizer = CountVectorizer(strip_accents=False, lowercase=False, + preprocessor=None, tokenizer=tokenizer, stop_words=None, + vocabulary=dictionary, binary=False) + + count = vectorizer.fit_transform(input_string) + + lens = np.array(list(map(len, input_string))).reshape(len(input_string), 1) + + tf = count.toarray() / lens + + transformer = TfidfTransformer(norm=None, smooth_idf=smooth_idf, + sublinear_tf=False) + + X = transformer.fit_transform(tf) + + def format_result(value): + if value == int(value): + return str(int(value)) + else: + return "{0:.16}".format(value) + + for row in X.toarray(): + print("{ " + ", ".join(map(format_result, row)) + " },") + */ arma::mat target = { { 0.2575364144903562, 0.4, 0.5150728289807124, 0, 0 }, { 0, 0.2857142857142857, 0.3679091635576516, 0.3679091635576516, 0.2418781686514208 }, { 0.4292273574839269, 0.3333333333333333, 0, 0.4292273574839269, 0 } }; + CheckMatrices(output, target.t(), 1e-12); } /** - * Test TFIDF encoding for characters, using Term - * Frequency as weighting scheme and smoothidf as false. + * Test the Tf-Idf encoding algorithm for individual characters with the + * standard term frequency type and the non-smooth inverse document frequency + * type. */ BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) { @@ -1129,22 +1377,26 @@ BOOST_AUTO_TEST_CASE(TermFrequencyTfIdfEncodingIndividualCharactersTest) }; arma::mat output; - TfIdfEncoding - encoder(TfIdfEncodingPolicy::TfTypes::TERM_FREQUENCY, false); + TfIdfEncoding encoder( + TfIdfEncodingPolicy::TfTypes::TERM_FREQUENCY, false); - // Passing a empty string to encode charactersrawcountsmoothidftrue. encoder.Encode(input, output, CharExtract()); + + /* The expected values were obtained by almost the same script as in + TermFrequencySmoothIdfEncodingIndividualCharactersTest. The only difference + is smooth_idf equals False. */ arma::mat target = { { 0.2810930216216329, 0.4, 0.5621860432432658, 0, 0 }, { 0, 0.2857142857142857, 0.4015614594594755, 0.4015614594594755, 0.2998017555240157 }, { 0.4684883693693881, 0.3333333333333333, 0, 0.4684883693693881, 0 } }; + CheckMatrices(output, target.t(), 1e-12); } /** - * Serialization test for the TF-IDF encoding algorithm with + * Serialization test for the Tf-Idf encoding algorithm with * the SplitByAnyOf tokenizer. */ BOOST_AUTO_TEST_CASE(SplitByAnyOfTfIdfEncodingSerialization) From 2f23fe84bbceb9717540fc8ffb80d0c8e69ef086 Mon Sep 17 00:00:00 2001 From: Mrityunjay Tripathi Date: Wed, 25 Mar 2020 17:34:29 +0530 Subject: [PATCH 241/265] templating return type of loss functions --- HISTORY.md | 2 ++ .../ann/loss_functions/cross_entropy_error.hpp | 3 ++- .../loss_functions/cross_entropy_error_impl.hpp | 6 ++++-- .../methods/ann/loss_functions/dice_loss.hpp | 3 ++- .../ann/loss_functions/dice_loss_impl.hpp | 5 +++-- .../ann/loss_functions/earth_mover_distance.hpp | 3 ++- .../earth_mover_distance_impl.hpp | 6 ++++-- .../ann/loss_functions/hinge_embedding_loss.hpp | 3 ++- .../hinge_embedding_loss_impl.hpp | 6 ++++-- .../methods/ann/loss_functions/huber_loss.hpp | 3 ++- .../ann/loss_functions/huber_loss_impl.hpp | 17 +++++++++++------ .../ann/loss_functions/kl_divergence.hpp | 3 ++- .../ann/loss_functions/kl_divergence_impl.hpp | 5 +++-- .../ann/loss_functions/log_cosh_loss.hpp | 5 +++-- .../ann/loss_functions/log_cosh_loss_impl.hpp | 5 +++-- .../ann/loss_functions/mean_bias_error.hpp | 3 ++- .../ann/loss_functions/mean_bias_error_impl.hpp | 5 +++-- .../ann/loss_functions/mean_squared_error.hpp | 3 ++- .../loss_functions/mean_squared_error_impl.hpp | 6 ++++-- .../mean_squared_logarithmic_error.hpp | 3 ++- .../mean_squared_logarithmic_error_impl.hpp | 6 ++++-- .../loss_functions/negative_log_likelihood.hpp | 3 ++- .../negative_log_likelihood_impl.hpp | 9 ++++++--- .../ann/loss_functions/reconstruction_loss.hpp | 3 ++- .../loss_functions/reconstruction_loss_impl.hpp | 3 ++- .../sigmoid_cross_entropy_error.hpp | 4 ++-- .../sigmoid_cross_entropy_error_impl.hpp | 9 ++++++--- 27 files changed, 86 insertions(+), 46 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 9dba6a517d..849422955a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,7 @@ ### mlpack ?.?.? ###### ????-??-?? + * Templated return type of `Forward function` of loss functions (#2339). + * Added `mean squared logarithmic error` loss function for neural networks (#2210). diff --git a/src/mlpack/methods/ann/loss_functions/cross_entropy_error.hpp b/src/mlpack/methods/ann/loss_functions/cross_entropy_error.hpp index 3041ae3099..1811f99998 100644 --- a/src/mlpack/methods/ann/loss_functions/cross_entropy_error.hpp +++ b/src/mlpack/methods/ann/loss_functions/cross_entropy_error.hpp @@ -49,7 +49,8 @@ class CrossEntropyError * @param target The target vector. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. diff --git a/src/mlpack/methods/ann/loss_functions/cross_entropy_error_impl.hpp b/src/mlpack/methods/ann/loss_functions/cross_entropy_error_impl.hpp index 9c6d73e481..7d0d80357b 100644 --- a/src/mlpack/methods/ann/loss_functions/cross_entropy_error_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/cross_entropy_error_impl.hpp @@ -27,8 +27,10 @@ CrossEntropyError::CrossEntropyError( template template -double CrossEntropyError::Forward( - const InputType& input, const TargetType& target) +typename InputType::elem_type +CrossEntropyError::Forward( + const InputType& input, + const TargetType& target) { return -arma::accu(target % arma::log(input + eps) + (1. - target) % arma::log(1. - input + eps)); diff --git a/src/mlpack/methods/ann/loss_functions/dice_loss.hpp b/src/mlpack/methods/ann/loss_functions/dice_loss.hpp index a30618e424..42551388aa 100644 --- a/src/mlpack/methods/ann/loss_functions/dice_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/dice_loss.hpp @@ -62,7 +62,8 @@ class DiceLoss * @param target The target vector. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. diff --git a/src/mlpack/methods/ann/loss_functions/dice_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/dice_loss_impl.hpp index 708c1102f7..904d699c21 100644 --- a/src/mlpack/methods/ann/loss_functions/dice_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/dice_loss_impl.hpp @@ -27,8 +27,9 @@ DiceLoss::DiceLoss( template template -double DiceLoss::Forward( - const InputType& input, const TargetType& target) +typename InputType::elem_type DiceLoss::Forward( + const InputType& input, + const TargetType& target) { return 1 - ((2 * arma::accu(target % input) + smooth) / (arma::accu(target % target) + arma::accu( diff --git a/src/mlpack/methods/ann/loss_functions/earth_mover_distance.hpp b/src/mlpack/methods/ann/loss_functions/earth_mover_distance.hpp index 0683715bd4..71c0aa8e4b 100644 --- a/src/mlpack/methods/ann/loss_functions/earth_mover_distance.hpp +++ b/src/mlpack/methods/ann/loss_functions/earth_mover_distance.hpp @@ -45,7 +45,8 @@ class EarthMoverDistance * @param target The target vector. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. diff --git a/src/mlpack/methods/ann/loss_functions/earth_mover_distance_impl.hpp b/src/mlpack/methods/ann/loss_functions/earth_mover_distance_impl.hpp index 9ba7ac8f47..44d5e24c4e 100644 --- a/src/mlpack/methods/ann/loss_functions/earth_mover_distance_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/earth_mover_distance_impl.hpp @@ -26,8 +26,10 @@ EarthMoverDistance::EarthMoverDistance() template template -double EarthMoverDistance::Forward( - const InputType& input, const TargetType& target) +typename InputType::elem_type +EarthMoverDistance::Forward( + const InputType& input, + const TargetType& target) { return -arma::accu(target % input); } diff --git a/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss.hpp b/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss.hpp index bcb9b17d6d..3e3eac19ec 100644 --- a/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss.hpp @@ -48,7 +48,8 @@ class HingeEmbeddingLoss * @param target Target data to compare with. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. diff --git a/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss_impl.hpp index b0e71632cb..f3f420b3ca 100644 --- a/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/hinge_embedding_loss_impl.hpp @@ -27,8 +27,10 @@ HingeEmbeddingLoss::HingeEmbeddingLoss() template template -double HingeEmbeddingLoss::Forward( - const InputType& input, const TargetType& target) +typename InputType::elem_type +HingeEmbeddingLoss::Forward( + const InputType& input, + const TargetType& target) { TargetType temp = target - (target == 0); return (arma::accu(arma::max(1-input % temp, 0.))) / target.n_elem; diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp index 9a18cfea1b..60e8d96b10 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss.hpp @@ -52,7 +52,8 @@ class HuberLoss * @param target The target vector. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp index 25da492bcf..c9c5ee9145 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp @@ -30,13 +30,15 @@ HuberLoss::HuberLoss( template template -double HuberLoss::Forward( - const InputType& input, const TargetType& target) +typename InputType::elem_type +HuberLoss::Forward(const InputType& input, + const TargetType& target) { - double loss = 0; + typedef typename InputType::elem_type ElemType; + ElemType loss = 0; for (size_t i = 0; i < input.n_elem; ++i) { - const double absError = std::abs(target[i] - input[i]); + const ElemType absError = std::abs(target[i] - input[i]); loss += absError > delta ? delta * (absError - 0.5 * delta) : 0.5 * std::pow(absError, 2); } @@ -50,13 +52,16 @@ void HuberLoss::Backward( const TargetType& target, OutputType& output) { + typedef typename InputType::elem_type ElemType; + output.set_size(size(input)); for (size_t i = 0; i < output.n_elem; ++i) { - const double absError = std::abs(target[i] - input[i]); + const ElemType absError = std::abs(target[i] - input[i]); output[i] = absError > delta ? - delta * (target[i] - input[i]) / absError : input[i] - target[i]; - if (mean) output[i] /= output.n_elem; + if (mean) + output[i] /= output.n_elem; } } diff --git a/src/mlpack/methods/ann/loss_functions/kl_divergence.hpp b/src/mlpack/methods/ann/loss_functions/kl_divergence.hpp index ad39d16c62..4640dce43e 100644 --- a/src/mlpack/methods/ann/loss_functions/kl_divergence.hpp +++ b/src/mlpack/methods/ann/loss_functions/kl_divergence.hpp @@ -60,7 +60,8 @@ class KLDivergence * @param target Target data to compare with. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. diff --git a/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp b/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp index dc72dc1645..bc9b44ea09 100644 --- a/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp @@ -28,8 +28,9 @@ KLDivergence::KLDivergence(const bool takeMean) : template template -double KLDivergence::Forward( - const InputType& input, const TargetType& target) +typename InputType::elem_type +KLDivergence::Forward(const InputType& input, + const TargetType& target) { if (takeMean) { diff --git a/src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp b/src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp index d528157b11..2fc859c5c7 100644 --- a/src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/log_cosh_loss.hpp @@ -20,7 +20,7 @@ namespace ann /** Artificial Neural Network. */ { /** * The Log-Hyperbolic-Cosine loss function is often used to improve - * variational auto encoder. This function is the log of hyperbolic + * variational auto encoder. This function is the log of hyperbolic * cosine of difference between true values and predicted values. * * @tparam InputDataType Type of the input data (arma::colvec, arma::mat, @@ -55,7 +55,8 @@ class LogCoshLoss * @param target Target data to compare with. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. diff --git a/src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp index 5de18340e6..1fd13c922f 100644 --- a/src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/log_cosh_loss_impl.hpp @@ -28,8 +28,9 @@ LogCoshLoss::LogCoshLoss(const double a) : template template -double LogCoshLoss::Forward( - const InputType& input, const TargetType& target) +typename InputType::elem_type +LogCoshLoss::Forward(const InputType& input, + const TargetType& target) { return arma::accu(arma::log(arma::cosh(a * (target - input)))) / a; } diff --git a/src/mlpack/methods/ann/loss_functions/mean_bias_error.hpp b/src/mlpack/methods/ann/loss_functions/mean_bias_error.hpp index 40418980d3..a238ac50cb 100644 --- a/src/mlpack/methods/ann/loss_functions/mean_bias_error.hpp +++ b/src/mlpack/methods/ann/loss_functions/mean_bias_error.hpp @@ -45,7 +45,8 @@ class MeanBiasError * @param target The target vector. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. diff --git a/src/mlpack/methods/ann/loss_functions/mean_bias_error_impl.hpp b/src/mlpack/methods/ann/loss_functions/mean_bias_error_impl.hpp index 8488ae487a..9d014585f4 100644 --- a/src/mlpack/methods/ann/loss_functions/mean_bias_error_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/mean_bias_error_impl.hpp @@ -27,8 +27,9 @@ MeanBiasError::MeanBiasError() template template -double MeanBiasError::Forward( - const InputType& input, const TargetType& target) +typename InputType::elem_type +MeanBiasError::Forward(const InputType& input, + const TargetType& target) { return arma::accu(target - input) / target.n_cols; } diff --git a/src/mlpack/methods/ann/loss_functions/mean_squared_error.hpp b/src/mlpack/methods/ann/loss_functions/mean_squared_error.hpp index 6dc6642a0d..0315c10b5c 100644 --- a/src/mlpack/methods/ann/loss_functions/mean_squared_error.hpp +++ b/src/mlpack/methods/ann/loss_functions/mean_squared_error.hpp @@ -46,7 +46,8 @@ class MeanSquaredError * @param target The target vector. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. diff --git a/src/mlpack/methods/ann/loss_functions/mean_squared_error_impl.hpp b/src/mlpack/methods/ann/loss_functions/mean_squared_error_impl.hpp index d7203b2499..81cf2281cf 100644 --- a/src/mlpack/methods/ann/loss_functions/mean_squared_error_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/mean_squared_error_impl.hpp @@ -26,8 +26,10 @@ MeanSquaredError::MeanSquaredError() template template -double MeanSquaredError::Forward( - const InputType& input, const TargetType& target) +typename InputType::elem_type +MeanSquaredError::Forward( + const InputType& input, + const TargetType& target) { return arma::accu(arma::square(input - target)) / target.n_cols; } diff --git a/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error.hpp b/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error.hpp index 54b74d17d5..49b94d1d4f 100644 --- a/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error.hpp +++ b/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error.hpp @@ -45,7 +45,8 @@ class MeanSquaredLogarithmicError * @param target The target vector. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. diff --git a/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error_impl.hpp b/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error_impl.hpp index 4a2aa75d8a..92ead103a7 100644 --- a/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/mean_squared_logarithmic_error_impl.hpp @@ -27,8 +27,10 @@ MeanSquaredLogarithmicError template template -double MeanSquaredLogarithmicError::Forward( - const InputType& input, const TargetType& target) +typename InputType::elem_type +MeanSquaredLogarithmicError::Forward( + const InputType& input, + const TargetType& target) { return arma::accu(arma::square(arma::log(1. + target) - arma::log(1. + input))) / target.n_cols; diff --git a/src/mlpack/methods/ann/loss_functions/negative_log_likelihood.hpp b/src/mlpack/methods/ann/loss_functions/negative_log_likelihood.hpp index 200fa1c5f3..6c28321cb9 100644 --- a/src/mlpack/methods/ann/loss_functions/negative_log_likelihood.hpp +++ b/src/mlpack/methods/ann/loss_functions/negative_log_likelihood.hpp @@ -48,7 +48,8 @@ class NegativeLogLikelihood * between 1 and the number of classes. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. The negative log diff --git a/src/mlpack/methods/ann/loss_functions/negative_log_likelihood_impl.hpp b/src/mlpack/methods/ann/loss_functions/negative_log_likelihood_impl.hpp index d006b17912..1eb47280c5 100644 --- a/src/mlpack/methods/ann/loss_functions/negative_log_likelihood_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/negative_log_likelihood_impl.hpp @@ -26,10 +26,13 @@ NegativeLogLikelihood::NegativeLogLikelihood() template template -double NegativeLogLikelihood::Forward( - const InputType& input, const TargetType& target) +typename InputType::elem_type +NegativeLogLikelihood::Forward( + const InputType& input, + const TargetType& target) { - double output = 0; + typedef typename InputType::elem_type ElemType; + ElemType output = 0; for (size_t i = 0; i < input.n_cols; ++i) { size_t currentTarget = target(i) - 1; diff --git a/src/mlpack/methods/ann/loss_functions/reconstruction_loss.hpp b/src/mlpack/methods/ann/loss_functions/reconstruction_loss.hpp index d8c775efc0..7d7c8e7da6 100644 --- a/src/mlpack/methods/ann/loss_functions/reconstruction_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/reconstruction_loss.hpp @@ -49,7 +49,8 @@ class ReconstructionLoss * @param target The target matrix. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. diff --git a/src/mlpack/methods/ann/loss_functions/reconstruction_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/reconstruction_loss_impl.hpp index 02ea50f4a7..b47d5bfcdb 100644 --- a/src/mlpack/methods/ann/loss_functions/reconstruction_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/reconstruction_loss_impl.hpp @@ -30,7 +30,8 @@ ReconstructionLoss< template template -double ReconstructionLoss::Forward( +typename InputType::elem_type +ReconstructionLoss::Forward( const InputType& input, const TargetType& target) { dist = DistType(input); diff --git a/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp b/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp index fec584de8c..0d70d2d29d 100644 --- a/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp +++ b/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp @@ -64,8 +64,8 @@ class SigmoidCrossEntropyError * @param target The target vector. */ template - inline double Forward(const InputType& input, - const TargetType& target); + inline typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. * diff --git a/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error_impl.hpp b/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error_impl.hpp index 1ab976874a..e5cf69188c 100644 --- a/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error_impl.hpp @@ -29,10 +29,13 @@ SigmoidCrossEntropyError template template -inline double SigmoidCrossEntropyError::Forward( - const InputType& input, const TargetType& target) +inline typename InputType::elem_type +SigmoidCrossEntropyError::Forward( + const InputType& input, + const TargetType& target) { - double maximum = 0; + typedef typename InputType::elem_type ElemType; + ElemType maximum = 0; for (size_t i = 0; i < input.n_elem; ++i) { maximum += std::max(input[i], 0.0) + From 4ef37bd6958952f993e09c3f11a22c551121a421 Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Wed, 1 Apr 2020 12:22:28 +0530 Subject: [PATCH 242/265] probable typo that caused the CI build failed --- src/mlpack/methods/preprocess/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/preprocess/CMakeLists.txt b/src/mlpack/methods/preprocess/CMakeLists.txt index 2f9f4b0725..5b5a7bbdb6 100644 --- a/src/mlpack/methods/preprocess/CMakeLists.txt +++ b/src/mlpack/methods/preprocess/CMakeLists.txt @@ -44,6 +44,6 @@ add_markdown_docs(preprocess_scale "cli;python" "preprocessing") if (STB_AVAILABLE) add_cli_executable(image_converter) - add_python_binding(limage_converter) + add_python_binding(image_converter) add_markdown_docs(image_converter "cli;python" "preprocessing") endif () \ No newline at end of file From b7108a3a86f4466ac483b76b7ced0e4832bfb85c Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Wed, 1 Apr 2020 18:13:18 +0530 Subject: [PATCH 243/265] removed the 'example' code i will do another pull request at mlpack/examples to put it there. --- .../reinforcement_learning.txt | 68 +------------------ 1 file changed, 1 insertion(+), 67 deletions(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index dc8c066553..9fda6c258f 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -323,73 +323,7 @@ This will train three different agents on three CPU threads asynchronously and u action value estimate. Voila, thats all there is to it. -Here is the full code to try this right away: - -@code -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace mlpack; -using namespace mlpack::ann; -using namespace mlpack::rl; -int main() -{ - // Set up the network. - FFN, GaussianInitialization> model(MeanSquaredError<>(), GaussianInitialization(0, 0.001)); - model.Add>(4, 128); - model.Add>(); - model.Add>(128, 128); - model.Add>(); - model.Add>(128, 2); - - AggregatedPolicy> policy({GreedyPolicy(0.7, 5000, 0.1), - GreedyPolicy(0.7, 5000, 0.01), - GreedyPolicy(0.7, 5000, 0.5)}, - arma::colvec("0.4 0.3 0.3")); - - TrainingConfig config; - config.StepSize() = 0.01; - config.Discount() = 0.9; - config.TargetNetworkSyncInterval() = 100; - config.ExplorationSteps() = 100; - config.DoubleQLearning() = false; - config.StepLimit() = 200; - - OneStepQLearning - agent(std::move(config), std::move(model), std::move(policy)); - - arma::vec returns(20, arma::fill::zeros); - size_t position = 0; - size_t episode = 0; - - auto measure = [&returns, &position, &episode](double episodeReturn) - { - if(episode > 10000) return true; - - returns[position++] = episodeReturn; - position = position % returns.n_elem; - episode++; - - std::cout << "Episode No.: " << episode - << "; Episode Return: " << episodeReturn - << "; Average Return: " << arma::mean(returns) << endl; - }; - - for (int i = 0; i < 100; i++) - { - agent.Train(measure); - } -} -@endcode +If you want the example code to try this right away, see [mlpack/examples](https://github.com/mlpack/examples) @section further_rltut Further documentation From 26fedd0e62e44fafd5c88c9e245544771a6deee9 Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Wed, 1 Apr 2020 18:58:20 +0530 Subject: [PATCH 244/265] Docs update --- src/mlpack/core/data/save_image.cpp | 5 ++++ .../preprocess/image_converter_main.cpp | 23 ++++++++----------- src/mlpack/tests/image_load_test.cpp | 18 ++++++++++++++- .../tests/main_tests/image_converter_test.cpp | 2 +- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/mlpack/core/data/save_image.cpp b/src/mlpack/core/data/save_image.cpp index c1802073e4..944087e6d3 100644 --- a/src/mlpack/core/data/save_image.cpp +++ b/src/mlpack/core/data/save_image.cpp @@ -54,6 +54,11 @@ bool SaveImage(const std::string& filename, Log::Warn << "Only the first image will be saved!" << std::endl; } + if (info.Width() * info.Height() * info.Channels() != image.n_elem) + { + Log::Fatal << "The dimension given does not match the image dimesion. \n"; + } + bool status = false; unsigned char* imageMem = image.memptr(); diff --git a/src/mlpack/methods/preprocess/image_converter_main.cpp b/src/mlpack/methods/preprocess/image_converter_main.cpp index af05853f7a..8cc73359fc 100644 --- a/src/mlpack/methods/preprocess/image_converter_main.cpp +++ b/src/mlpack/methods/preprocess/image_converter_main.cpp @@ -23,16 +23,16 @@ using namespace mlpack::data; PROGRAM_INFO("Image Converter", // Short description. "A utility to load an image or set of images into a single dataset that" - "can then be used by other mlpack methods and utilities. This can also" - "unpack an image dataset into individual files, for instance after mlpack" - "methods have been used.", + " can then be used by other mlpack methods and utilities. This can also" + " unpack an image dataset into individual files, for instance after mlpack" + " methods have been used.", // Long description. - "This utility takes a image or an array of images and loads them to a" + "This utility takes an image or an array of images and loads them to a" " matrix. You can optionally specify the height " + PRINT_PARAM_STRING("height") + " width " + PRINT_PARAM_STRING("width") + " and channel " + PRINT_PARAM_STRING("channels") + " of the images that" - "needs to be loaded; otherwise, these parameters will be automatically" - "detected from the image." + " needs to be loaded; otherwise, these parameters will be automatically" + " detected from the image." "\n" "There are other options too, that can be specified such as " + PRINT_PARAM_STRING("quality") @@ -75,7 +75,7 @@ static void mlpackMain() // Parse command line options. const vector fileNames = CLI::GetParam >("input"); arma::mat out; - + if (!CLI::HasParam("save")) { ReportIgnoredParam("width", "Width of image is determined from file."); @@ -88,8 +88,8 @@ static void mlpackMain() } else { - RequireNoneOrAllPassed({ "save", "width", "height", "channels" }, true, - "image size information is needed when 'save' is specified!"); + RequireNoneOrAllPassed({ "save", "width", "height", "channels", "dataset" } + , true, "Image size information is needed when 'save' is specified!"); // Positive value for width. RequireParamValue("width", [](int x) { return x >= 0;}, true, "width must be positive"); @@ -108,11 +108,6 @@ static void mlpackMain() const size_t channels = CLI::GetParam("channels"); const size_t quality = CLI::GetParam("quality"); data::ImageInfo info(width, height, channels, quality); - if (!CLI::HasParam("dataset")) - { - throw std::runtime_error("Please provide a input matrix to save " - "images from"); - } Save(fileNames, CLI::GetParam("dataset"), info, true); } } diff --git a/src/mlpack/tests/image_load_test.cpp b/src/mlpack/tests/image_load_test.cpp index 7d2108264c..b5172dbeb0 100644 --- a/src/mlpack/tests/image_load_test.cpp +++ b/src/mlpack/tests/image_load_test.cpp @@ -12,7 +12,6 @@ #include #include -#include #include "test_tools.hpp" #include "serialization.hpp" @@ -76,6 +75,23 @@ BOOST_AUTO_TEST_CASE(SaveImageAPITest) remove("APITest.bmp"); } +/** + * Test if an image with a wrong dimesion throws an expected + * exception while saving. + */ +BOOST_AUTO_TEST_CASE(SaveImageWrongInfo) +{ + data::ImageInfo info(5, 5, 3, 90); + + arma::Mat im1; + size_t dimension = info.Width() * info.Height() * info.Channels(); + im1 = arma::randi>(24 * 25 * 7, 1); + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(data::Save("APITest.bmp", im1, info, false), + std::runtime_error); + Log::Fatal.ignoreInput = false; +} + /** * Test that the image is loaded correctly into the matrix using the API * for vectors. diff --git a/src/mlpack/tests/main_tests/image_converter_test.cpp b/src/mlpack/tests/main_tests/image_converter_test.cpp index 35e140c45e..cd77cbaf66 100644 --- a/src/mlpack/tests/main_tests/image_converter_test.cpp +++ b/src/mlpack/tests/main_tests/image_converter_test.cpp @@ -1,5 +1,5 @@ /** - * @file load_save_image_test.cpp + * @file image_converter_test.cpp * @author Jeffin Sam * * Test mlpackMain() of load_save_image_main.cpp. From 1f9429cc5184cabbb299e5a84a48e0d0a3564402 Mon Sep 17 00:00:00 2001 From: Joel Joseph <34275997+joeljosephjin@users.noreply.github.com> Date: Wed, 1 Apr 2020 19:45:01 +0530 Subject: [PATCH 245/265] reverted the last commit --- .../reinforcement_learning.txt | 68 ++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index 9fda6c258f..dc8c066553 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -323,7 +323,73 @@ This will train three different agents on three CPU threads asynchronously and u action value estimate. Voila, thats all there is to it. -If you want the example code to try this right away, see [mlpack/examples](https://github.com/mlpack/examples) +Here is the full code to try this right away: + +@code +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace mlpack; +using namespace mlpack::ann; +using namespace mlpack::rl; +int main() +{ + // Set up the network. + FFN, GaussianInitialization> model(MeanSquaredError<>(), GaussianInitialization(0, 0.001)); + model.Add>(4, 128); + model.Add>(); + model.Add>(128, 128); + model.Add>(); + model.Add>(128, 2); + + AggregatedPolicy> policy({GreedyPolicy(0.7, 5000, 0.1), + GreedyPolicy(0.7, 5000, 0.01), + GreedyPolicy(0.7, 5000, 0.5)}, + arma::colvec("0.4 0.3 0.3")); + + TrainingConfig config; + config.StepSize() = 0.01; + config.Discount() = 0.9; + config.TargetNetworkSyncInterval() = 100; + config.ExplorationSteps() = 100; + config.DoubleQLearning() = false; + config.StepLimit() = 200; + + OneStepQLearning + agent(std::move(config), std::move(model), std::move(policy)); + + arma::vec returns(20, arma::fill::zeros); + size_t position = 0; + size_t episode = 0; + + auto measure = [&returns, &position, &episode](double episodeReturn) + { + if(episode > 10000) return true; + + returns[position++] = episodeReturn; + position = position % returns.n_elem; + episode++; + + std::cout << "Episode No.: " << episode + << "; Episode Return: " << episodeReturn + << "; Average Return: " << arma::mean(returns) << endl; + }; + + for (int i = 0; i < 100; i++) + { + agent.Train(measure); + } +} +@endcode @section further_rltut Further documentation From b60c33bae4090b58b467c5d874653a07070e7444 Mon Sep 17 00:00:00 2001 From: favre49 <40389657+favre49@users.noreply.github.com> Date: Thu, 2 Apr 2020 10:34:09 +0530 Subject: [PATCH 246/265] Update doc/tutorials/reinforcement_learning/reinforcement_learning.txt --- doc/tutorials/reinforcement_learning/reinforcement_learning.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt index dc8c066553..a91dc27671 100644 --- a/doc/tutorials/reinforcement_learning/reinforcement_learning.txt +++ b/doc/tutorials/reinforcement_learning/reinforcement_learning.txt @@ -379,7 +379,7 @@ int main() position = position % returns.n_elem; episode++; - std::cout << "Episode No.: " << episode + std::cout << "Episode No.: " << episode << "; Episode Return: " << episodeReturn << "; Average Return: " << arma::mean(returns) << endl; }; From 3c2c3ce5f0717cd0426eafca0a4a82593bcb536c Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Thu, 2 Apr 2020 15:34:56 +0300 Subject: [PATCH 247/265] Changed return type accordingly to #2339 --- .../methods/ann/loss_functions/margin_ranking_loss.hpp | 9 +++------ .../ann/loss_functions/margin_ranking_loss_impl.hpp | 8 +++----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp index b5d64f0aae..e8798dd62b 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss.hpp @@ -49,12 +49,9 @@ class MarginRankingLoss * function. * @param target The label vector which contains values of -1 or 1. */ - template < - typename InputType, - typename TargetType - > - double Forward(const InputType& input, - const TargetType& target); + template + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. diff --git a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp index 79e4d879e9..59649cbb3a 100644 --- a/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/margin_ranking_loss_impl.hpp @@ -26,11 +26,9 @@ MarginRankingLoss::MarginRankingLoss( } template -template < - typename InputType, - typename TargetType -> -double MarginRankingLoss::Forward( +template +typename InputType::elem_type +MarginRankingLoss::Forward( const InputType& input, const TargetType& target) { From b371bd45aa5b23f92fb5fc6b1ad341f306e8e03c Mon Sep 17 00:00:00 2001 From: jeffin sam Date: Thu, 2 Apr 2020 18:51:52 +0530 Subject: [PATCH 248/265] Apply suggestions from code review Co-Authored-By: Ryan Curtin --- HISTORY.md | 4 ++-- src/mlpack/core/data/save_image.cpp | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 06067e1b4e..4f8443e9ae 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -31,8 +31,8 @@ * CMake fix for finding STB include directory (#2145). - * Add bindings for Loading and Saving image (#2019); `mlpack_image_converter` - from the command-line, `mlpack_image_converter()` from Python. + * Add bindings for loading and saving images (#2019); `mlpack_image_converter` + from the command-line, `mlpack.image_converter()` from Python. * Add normalization support for CF binding (#2136). diff --git a/src/mlpack/core/data/save_image.cpp b/src/mlpack/core/data/save_image.cpp index 944087e6d3..3235cb12b1 100644 --- a/src/mlpack/core/data/save_image.cpp +++ b/src/mlpack/core/data/save_image.cpp @@ -54,9 +54,10 @@ bool SaveImage(const std::string& filename, Log::Warn << "Only the first image will be saved!" << std::endl; } - if (info.Width() * info.Height() * info.Channels() != image.n_elem) + if (info.Width() * info.Height() * info.Channels() != image.n_elem) { - Log::Fatal << "The dimension given does not match the image dimesion. \n"; + Log::Fatal << "data::Save(): The given image dimensions do not match the " + << "dimensions of the matrix to be saved!" << std::endl; } bool status = false; From 7d26d2e5a563898fe7cacfa89e9233bd8435a1f3 Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Thu, 2 Apr 2020 23:16:51 +0530 Subject: [PATCH 249/265] Add julia binding --- src/mlpack/core/data/save_image.cpp | 2 +- src/mlpack/methods/preprocess/CMakeLists.txt | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/mlpack/core/data/save_image.cpp b/src/mlpack/core/data/save_image.cpp index 3235cb12b1..2c4654b711 100644 --- a/src/mlpack/core/data/save_image.cpp +++ b/src/mlpack/core/data/save_image.cpp @@ -57,7 +57,7 @@ bool SaveImage(const std::string& filename, 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; + << "dimensions of the matrix to be saved!" << std::endl; } bool status = false; diff --git a/src/mlpack/methods/preprocess/CMakeLists.txt b/src/mlpack/methods/preprocess/CMakeLists.txt index 5b5a7bbdb6..d1b1dd5816 100644 --- a/src/mlpack/methods/preprocess/CMakeLists.txt +++ b/src/mlpack/methods/preprocess/CMakeLists.txt @@ -40,10 +40,12 @@ add_markdown_docs(preprocess_imputer "cli" "preprocessing") add_cli_executable(preprocess_scale) add_python_binding(preprocess_scale) -add_markdown_docs(preprocess_scale "cli;python" "preprocessing") +add_julia_binding(preprocess_scale) +add_markdown_docs(preprocess_scale "cli;python;julia" "preprocessing") if (STB_AVAILABLE) add_cli_executable(image_converter) add_python_binding(image_converter) - add_markdown_docs(image_converter "cli;python" "preprocessing") + add_julia_binding(image_converter) + add_markdown_docs(image_converter "cli;python;julia" "preprocessing") endif () \ No newline at end of file From b84745ac5a3b80c56115362088b8109dd017faf5 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Thu, 2 Apr 2020 21:27:47 +0300 Subject: [PATCH 250/265] Rebuild From 0f31459896de2b6faad8f0aa5e535004c0e6837b Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Fri, 3 Apr 2020 01:29:21 +0300 Subject: [PATCH 251/265] Rebuild From 16d2fd3e945383775aff3145d745d110cdae5e49 Mon Sep 17 00:00:00 2001 From: Mikhail Lozhnikov Date: Fri, 3 Apr 2020 01:56:16 +0300 Subject: [PATCH 252/265] Various documentation fixes for string encoding algorithms. --- src/mlpack/core/data/string_encoding.hpp | 9 +++--- src/mlpack/core/data/string_encoding_impl.hpp | 4 +-- .../bag_of_words_encoding_policy.hpp | 20 +++++++------ .../dictionary_encoding_policy.hpp | 8 +++--- .../tf_idf_encoding_policy.hpp | 28 +++++++++---------- 5 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/mlpack/core/data/string_encoding.hpp b/src/mlpack/core/data/string_encoding.hpp index db5896576c..9ed18cba6e 100644 --- a/src/mlpack/core/data/string_encoding.hpp +++ b/src/mlpack/core/data/string_encoding.hpp @@ -101,7 +101,7 @@ class StringEncoding * * @tparam OutputType Type of the output container. The function supports * the following types: arma::mat, arma::sp_mat, - * std::vector>. + * std::vector>. * @tparam TokenizerType Type of the tokenizer. * * @param input Corpus of text to encode. @@ -148,7 +148,7 @@ class StringEncoding * * @tparam OutputType Type of the output container. The function supports * the following types: arma::mat, arma::sp_mat, - * std::vector>. + * std::vector>. * @tparam TokenizerType Type of the tokenizer. * @tparam PolicyType The type of the encoding policy. It has to be * equal to EncodingPolicyType. @@ -180,6 +180,7 @@ class StringEncoding * @tparam TokenizerType Type of the tokenizer. * @tparam PolicyType The type of the encoding policy. It has to be * equal to EncodingPolicyType. + * @tparam ElemType Type of the output values. * * @param input Corpus of text to encode. * @param output Output container to store the result. @@ -193,9 +194,9 @@ class StringEncoding * 2. IsTokenEmpty() that accepts a token and returns true if the given * token is empty. */ - template + template void EncodeHelper(const std::vector& input, - std::vector>& output, + std::vector>& output, const TokenizerType& tokenizer, PolicyType& policy, typename std::enable_if& input, } template -template +template void StringEncoding:: EncodeHelper(const std::vector& input, - std::vector>& output, + std::vector>& output, const TokenizerType& tokenizer, PolicyType& policy, typename std::enable_if>. + * Overloaded function to save the result in vector>. * - * @tparam OutputType Type of the output vector. + * @tparam ElemType Type of the output values. * * @param output Output matrix to store the encoded results. * @param datasetSize The number of strings in the input dataset. @@ -70,13 +70,13 @@ class BagOfWordsEncodingPolicy input dataset (not used). * @param dictionarySize The size of the dictionary. */ - template - static void InitMatrix(std::vector>& output, + template + static void InitMatrix(std::vector>& output, const size_t datasetSize, const size_t /* maxNumTokens */, const size_t dictionarySize) { - output.resize(datasetSize, std::vector(dictionarySize, 0)); + output.resize(datasetSize, std::vector(dictionarySize)); } /** @@ -106,16 +106,18 @@ class BagOfWordsEncodingPolicy * the encoded token to the output. The encoder writes data in the * row-major order. * - * Overload function to accepted vector> as output type. + * Overloaded function to accept vector> as the output + * type. + * + * @tparam ElemType Type of the output values. * * @param output Output matrix to store the encoded results. * @param value The encoded token. * @param line The line number at which the encoding is performed. * @param index The line token number at which the encoding is performed. - * @tparam OutputType The type of output vector. */ - template - static void Encode(std::vector>& output, + template + static void Encode(std::vector>& output, const size_t value, const size_t line, const size_t /* index */) diff --git a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp index 4b26860abd..d0b49775e0 100644 --- a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp @@ -80,13 +80,13 @@ class DictionaryEncodingPolicy * the result into the given vector to avoid padding. The encoder writes data * in the row-major order. * - * @tparam OutputType Type of the output vector. + * @tparam ElemType Type of the output values. * - * @param output Output vector to store the encoded results. + * @param output Output vector to store the encoded line. * @param value The encoded token. */ - template - static void Encode(std::vector& output, size_t value) + template + static void Encode(std::vector& output, size_t value) { output.push_back(value); } diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 4acfc03c56..141bb8ea1f 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -36,7 +36,7 @@ class TfIdfEncodingPolicy { public: /** - * Enum class used to identify the type of term frequency encoding. + * Enum class used to identify the type of the term frequency statistics. * * The present implementation supports the following types: * BINARY Term frequency equals 1 if the row contains the encoded @@ -58,8 +58,8 @@ class TfIdfEncodingPolicy }; /** - * A constructor for the class which is use to set the type of term frequency - * and also the value for smoothIdf. + * Construct this using the term frequency type and the inverse document + * frequency type. * * @param tfType Type of the term frequency statistics. * @param smoothIdf Used to indicate whether to use smooth idf or not. @@ -102,9 +102,9 @@ class TfIdfEncodingPolicy * The function initializes the output matrix. The encoder writes data * in the row-major order. * - * Overloaded function to store result in vector>. + * Overloaded function to save the result in vector>. * - * @tparam OutputType Type of the output vector. + * @tparam ElemType Type of the output values. * * @param output Output matrix to store the encoded results. * @param datasetSize The number of strings in the input dataset. @@ -112,13 +112,13 @@ class TfIdfEncodingPolicy input dataset (not used). * @param dictionarySize The size of the dictionary. */ - template - static void InitMatrix(std::vector>& output, + template + static void InitMatrix(std::vector>& output, const size_t datasetSize, const size_t /* maxNumTokens */, const size_t dictionarySize) { - output.resize(datasetSize, std::vector(dictionarySize, 0)); + output.resize(datasetSize, std::vector(dictionarySize)); } /** @@ -155,26 +155,26 @@ class TfIdfEncodingPolicy * the encoded token to the output. The encoder writes data in the * row-major order. * - * Overloaded function to accept vector> as the output + * Overloaded function to accept vector> as the output * type. * - * @tparam OutputType Type of the output vector. + * @tparam ElemType Type of the output values. * * @param output Output matrix to store the encoded results. * @param value The encoded token. * @param line The line number at which the encoding is performed. * @param index The token index in the line. */ - template - void Encode(std::vector >& output, + template + void Encode(std::vector>& output, const size_t value, const size_t line, const size_t /* index */) { - const OutputType tf = TermFrequency( + const ElemType tf = TermFrequency( tokensFrequences[line][value], linesSizes[line]); - const OutputType idf = InverseDocumentFrequency( + const ElemType idf = InverseDocumentFrequency( output.size(), numContainingStrings[value]); output[line][value - 1] = tf * idf; From 4bf8321ef16185ba905903f1e9f52eb52b5a3dc8 Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Fri, 14 Feb 2020 19:23:15 +0530 Subject: [PATCH 253/265] Add Cosine Embeddings --- .../methods/ann/loss_functions/CMakeLists.txt | 2 + .../loss_functions/cosine_embedding_loss.hpp | 145 ++++++++++++++++++ .../cosine_embedding_loss_impl.hpp | 88 +++++++++++ src/mlpack/tests/loss_functions_test.cpp | 26 ++++ 4 files changed, 261 insertions(+) create mode 100644 src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp create mode 100644 src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp diff --git a/src/mlpack/methods/ann/loss_functions/CMakeLists.txt b/src/mlpack/methods/ann/loss_functions/CMakeLists.txt index 8a2111091b..d807d6ecf9 100644 --- a/src/mlpack/methods/ann/loss_functions/CMakeLists.txt +++ b/src/mlpack/methods/ann/loss_functions/CMakeLists.txt @@ -3,6 +3,8 @@ set(SOURCES cross_entropy_error.hpp cross_entropy_error_impl.hpp + cosine_embedding_loss.hpp + cosine_embedding_loss_impl.hpp dice_loss.hpp dice_loss_impl.hpp earth_mover_distance.hpp diff --git a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp new file mode 100644 index 0000000000..926a74fe4a --- /dev/null +++ b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp @@ -0,0 +1,145 @@ +/** + * @file cosine_embedding_loss.hpp + * @author Kartik Dutt + * + * Definition of the Cosine Embedding loss function. + * + * 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_METHODS_ANN_LOSS_FUNCTION_COSINE_EMBEDDING_HPP +#define MLPACK_METHODS_ANN_LOSS_FUNCTION_COSINE_EMBEDDING_HPP + +#include + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +/** + * Cosine Embeddings Loss function is used for measuring whether two inputs are + * similar or dissimilar, using using the cosine distance, and is typically used + * for learning nonlinear embeddings or semi-supervised learning. + * + * @f{eqnarray*}{ + * f(x) = 1 - cos(x1, x2) , for y = 1 + * f(x) = max(0, cos(x1, x2) - margin) , for y = -1 + * @f} + * + * @tparam InputDataType Type of the input data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + * @tparam OutputDataType Type of the output data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + */ +template < + typename InputDataType = arma::mat, + typename OutputDataType = arma::mat +> +class CosineEmbeddingLoss +{ + public: + /** + * Create the CosineEmbeddingLoss object. + * + * @param margin Increases cosine distance in case of dissimilarity. + * Refer definition of cosine-embedding-loss above. + * @param takeMean Boolean variable to specify whether to take mean or not. + * Specifies reduction method i.e. sum or mean corresponding + * to 0 and 1 respectively. Default value = 0. + */ + CosineEmbeddingLoss(const double margin = 0.0, const bool takeMean = false); + + /** + * Ordinary feed forward pass of a neural network. + * + * @param x1 Input data used for evaluating the given function. + * @param x2 Input data used for evaluating the given function. + * @param y Input data used to determine whether to calculate + * cosine similarity or dissimilarity. It should only + * contain values equal to 1 or -1. + */ + template + double Forward(const InputType&& x1, const InputType x2, + const TargetType&& y); + + /** + * Ordinary feed backward pass of a neural network. The negative log + * likelihood layer expects that the input contains log-probabilities for + * each class. The layer also expects a class index, in the range between 1 + * and the number of classes, as target when calling the Forward function. + * + * @param input The propagated input activation. + * @param target The target vector, that contains the class index in the range + * between 1 and the number of classes. + * @param output The calculated error. + */ + template + void Backward(const InputType&& input, + const TargetType&& target, + OutputType&& output); + + //! Get the input parameter. + InputDataType& InputParameter() const { return inputParameter; } + //! Modify the input parameter. + InputDataType& InputParameter() { return inputParameter; } + + //! Get the output parameter. + OutputDataType& OutputParameter() const { return outputParameter; } + //! Modify the output parameter. + OutputDataType& OutputParameter() { return outputParameter; } + + //! Get the delta. + OutputDataType& Delta() const { return delta; } + //! Modify the delta. + OutputDataType& Delta() { return delta; } + + //! Get the value of takeMean. + bool TakeMean() const { return takeMean; } + //! Modify the value of takeMean. + bool& TakeMean() { return takeMean; } + + //! Get the value of margin. + bool Margin() const { return margin; } + //! Modify the value of takeMean. + bool& Margin() { return margin; } + + + /** + * Serialize the layer. + */ + template + void serialize(Archive& /* ar */, const unsigned int /* version */); + + private: + // Returns Cosine-Distance between two vectors. + template + double CosineDistance(InputType x1, InputType x2) + { + return 1.0 - arma::accu(x1 % x2) / + std::sqrt(arma::accu(arma::pow(x1, 2)) * arma::accu(arma::pow(x2, 2))); + } + + //! Locally-stored delta object. + OutputDataType delta; + + //! Locally-stored input parameter object. + InputDataType inputParameter; + + //! Locally-stored output parameter object. + OutputDataType outputParameter; + + //! Locally-stored value of margin parameter. + double margin; + + //! Locally-stored value of takeMean parameter. + bool takeMean; +}; // class CosineEmbeddingLoss + +} // namespace ann +} // namespace mlpack + +// Include implementation. +#include "cosine_embedding_loss_impl.hpp" + +#endif \ No newline at end of file diff --git a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp new file mode 100644 index 0000000000..e837397459 --- /dev/null +++ b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp @@ -0,0 +1,88 @@ +/** + * @file cosine_embedding_loss_impl.hpp + * @author Kartik Dutt + * + * Implementation of the Cosine Embedding loss function. + * + * 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_METHODS_ANN_LOSS_FUNCTION_COSINE_EMBEDDING_IMPL_HPP +#define MLPACK_METHODS_ANN_LOSS_FUNCTION_COSINE_EMBEDDING_IMPL_HPP + +// In case it hasn't yet been included. +#include "cosine_embedding_loss.hpp" + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +template +CosineEmbeddingLoss::CosineEmbeddingLoss( + const double margin, const bool takeMean): + margin(margin), takeMean(takeMean) +{ + // Nothing to do here. +} + +template +template +double CosineEmbeddingLoss::Forward( + const InputType &&x1, const InputType &&x2, const TargetType &&y) +{ + if (x1.n_rows != x2.n_rows || x1.n_cols != x2.n_cols || + x1.n_slices != x2.n_slices) + { + Log::Fatal << "Input Dimensions must be same." << std::endl; + } + + if(y.n_elem != x1.n_rows * x1.n_slices) + { + Log::Fatal << "Number of rows mismatch." << std::endl; + } + + arma::colvec inputTemp1 = arma::vectorise(x1); + arma::colvec inputTemp2 = arma::vectorise(x2); + double loss = 0.0; + const size_t cols = x1.n_cols; + const size_t batchSize = x1.n_rows * x1.n_slices; + + for(size_t i = 0; i < batchSize; i += cols) + { + if (y(i / cols) == 1) + { + loss += 1 - CosineDistance(inputTemp1(arma::span(i, i + cols - 1)), + inputTemp2(arma::span(i, i + cols -1))); + } + else if (y(i / cols) == -1) + { + loss += std::max(CosineDistance(inputTemp1(arma::span(i, i + cols - 1)), + inputTemp2(arma::span(i, i + cols -1))) - margin, 0); + } + else + { + Log::Fatal << "y should only contain 1 and -1." << std::endl; + } + } + + if(takeMean) + { + loss = (double)loss / y.n_elem; + } + return loss; +} + +template +template +void CosineEmbeddingLoss::serialize( + Archive& /* ar */, + const unsigned int /* version */) +{ + // Nothing to do here. +} + +} // namespace ann +} // namespace mlpack + +#endif \ No newline at end of file diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index e361c398d0..f65d72143a 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -25,8 +25,15 @@ #include #include #include +<<<<<<< HEAD #include +<<<<<<< HEAD #include +======= +======= +#include +>>>>>>> Add Cosine Embeddings +>>>>>>> Add Cosine Embeddings #include #include @@ -578,4 +585,23 @@ BOOST_AUTO_TEST_CASE(HingeEmbeddingLossTest) BOOST_REQUIRE_EQUAL(output.n_rows, input.n_rows); BOOST_REQUIRE_EQUAL(output.n_cols, input.n_cols); } + +/** + * Simple test for the Cosine Embedding loss function. + */ +BOOST_AUTO_TEST_CASE(CosineEmbeddingLossTest) +{ + arma::mat input1, input2, y, target, output; + double loss; + CosineEmbeddingLoss<> module; + + // Test the Forward function. Loss should be 0 if input1 = input2 and y = 1. + input1 = arma::ones(10, 1); + input2 = arma::ones(10, 1); + y = arma::ones(1); + loss = module.Forward(std::move(input1),std::move(input1), + std::move(target)); + BOOST_REQUIRE_CLOSE(loss, 0.0, 1e-4); +} + BOOST_AUTO_TEST_SUITE_END(); From 834f602118d64681e8694bdc6c655b416df86602 Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Sat, 15 Feb 2020 20:26:40 +0530 Subject: [PATCH 254/265] Completed Cosine Embeddings --- .../loss_functions/cosine_embedding_loss.hpp | 37 +++++---- .../cosine_embedding_loss_impl.hpp | 78 ++++++++++++++++--- src/mlpack/tests/loss_functions_test.cpp | 68 ++++++++++++++-- 3 files changed, 153 insertions(+), 30 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp index 926a74fe4a..dca30b8891 100644 --- a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp @@ -59,9 +59,13 @@ class CosineEmbeddingLoss * cosine similarity or dissimilarity. It should only * contain values equal to 1 or -1. */ - template - double Forward(const InputType&& x1, const InputType x2, - const TargetType&& y); + template< + typename FirstTensor, + typename SecondTensor, + typename ThirdTensor + > + double Forward(const FirstTensor&& x1, const SecondTensor&& x2, + const ThirdTensor&& y); /** * Ordinary feed backward pass of a neural network. The negative log @@ -69,15 +73,22 @@ class CosineEmbeddingLoss * each class. The layer also expects a class index, in the range between 1 * and the number of classes, as target when calling the Forward function. * - * @param input The propagated input activation. - * @param target The target vector, that contains the class index in the range + * @param x1 The propagated input activation. + * @param x2 The propagated input activation. + * @param y The target vector, that contains the class index in the range * between 1 and the number of classes. * @param output The calculated error. */ - template - void Backward(const InputType&& input, - const TargetType&& target, - OutputType&& output); + template< + typename FirstTensor, + typename SecondTensor, + typename ThirdTensor, + typename OutputTensor + > + void Backward(const FirstTensor&& x1, + const SecondTensor&& x2, + const ThirdTensor&& y, + const OutputTensor&& output); //! Get the input parameter. InputDataType& InputParameter() const { return inputParameter; } @@ -113,11 +124,11 @@ class CosineEmbeddingLoss private: // Returns Cosine-Distance between two vectors. - template - double CosineDistance(InputType x1, InputType x2) + template + double CosineDistance(const FirstTensor&& x1, + const SecondTensor&& x2) { - return 1.0 - arma::accu(x1 % x2) / - std::sqrt(arma::accu(arma::pow(x1, 2)) * arma::accu(arma::pow(x2, 2))); + return arma::dot(arma::normalise(x1), arma::normalise(x2)); } //! Locally-stored delta object. diff --git a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp index e837397459..a463742b49 100644 --- a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp @@ -26,18 +26,26 @@ CosineEmbeddingLoss::CosineEmbeddingLoss( // Nothing to do here. } -template -template +template +template< + typename FirstTensor, + typename SecondTensor, + typename ThirdTensor +> double CosineEmbeddingLoss::Forward( - const InputType &&x1, const InputType &&x2, const TargetType &&y) + const FirstTensor&& x1, + const SecondTensor&& x2, + const ThirdTensor&& y) { + const size_t cols = x1.n_cols; + const size_t batchSize = x1.n_elem / cols; if (x1.n_rows != x2.n_rows || x1.n_cols != x2.n_cols || - x1.n_slices != x2.n_slices) + x1.n_elem != x2.n_elem) { Log::Fatal << "Input Dimensions must be same." << std::endl; } - if(y.n_elem != x1.n_rows * x1.n_slices) + if (y.n_elem < batchSize) { Log::Fatal << "Number of rows mismatch." << std::endl; } @@ -45,20 +53,19 @@ double CosineEmbeddingLoss::Forward( arma::colvec inputTemp1 = arma::vectorise(x1); arma::colvec inputTemp2 = arma::vectorise(x2); double loss = 0.0; - const size_t cols = x1.n_cols; - const size_t batchSize = x1.n_rows * x1.n_slices; - for(size_t i = 0; i < batchSize; i += cols) + for(size_t i = 0; i < inputTemp1.n_elem; i+=cols) { if (y(i / cols) == 1) { loss += 1 - CosineDistance(inputTemp1(arma::span(i, i + cols - 1)), - inputTemp2(arma::span(i, i + cols -1))); + inputTemp2(arma::span(i, i + cols - 1))); } else if (y(i / cols) == -1) { - loss += std::max(CosineDistance(inputTemp1(arma::span(i, i + cols - 1)), - inputTemp2(arma::span(i, i + cols -1))) - margin, 0); + double currentLoss = CosineDistance(inputTemp1(arma::span(i, i + cols - 1)), + inputTemp2(arma::span(i, i + cols - 1))) - margin; + loss += currentLoss > 0 ? currentLoss : 0; } else { @@ -66,13 +73,60 @@ double CosineEmbeddingLoss::Forward( } } - if(takeMean) + if (takeMean) { loss = (double)loss / y.n_elem; } return loss; } +template +template< + typename FirstTensor, + typename SecondTensor, + typename ThirdTensor, + typename OutputTensor +> +void CosineEmbeddingLoss::Backward( + const FirstTensor&& x1, + const SecondTensor&& x2, + const ThirdTensor&& y, + const OutputTensor&& output) +{ + const size_t cols = x1.n_cols; + const size_t batchSize = x1.n_elem / cols; + if (x1.n_rows != x2.n_rows || x1.n_cols != x2.n_cols || + x1.n_elem != x2.n_elem) + { + Log::Fatal << "Input Dimensions must be same." << std::endl; + } + + if (y.n_elem < batchSize) + { + Log::Fatal << "Number of rows mismatch." << std::endl; + } + + arma::colvec inputTemp1 = arma::vectorise(x1); + arma::colvec inputTemp2 = arma::vectorise(x2); + arma::colvec outputTemp(inputTemp1.n_elem, 1); + + for(size_t i = 0; i < inputTemp1.n_elem; i+=cols) + { + if (y(i / cols) != 1 && y(i / cols) != -1) + { + Log::Fatal << "y should only contain 1 and -1." << std::endl; + } + + outputTemp(arma::span(i, i + cols -1)) = arma::sign(y(i / cols)) * + (arma::normalise(inputTemp2(arma::span(i, i + cols - 1))) - + arma::normalise(inputTemp1(arma::span(i, i + cols - 1))) * + CosineDistance(inputTemp1(arma::span(i, i + cols - 1)), + inputTemp2(arma::span(i, i + cols - 1)))) / + std::sqrt(arma::accu(arma::pow(inputTemp1(arma::span(i, i + cols - 1)), + 2))); + } +} + template template void CosineEmbeddingLoss::serialize( diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index f65d72143a..8a5819afe9 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -591,17 +591,75 @@ BOOST_AUTO_TEST_CASE(HingeEmbeddingLossTest) */ BOOST_AUTO_TEST_CASE(CosineEmbeddingLossTest) { - arma::mat input1, input2, y, target, output; + arma::mat input1, input2, y, output; double loss; CosineEmbeddingLoss<> module; // Test the Forward function. Loss should be 0 if input1 = input2 and y = 1. - input1 = arma::ones(10, 1); - input2 = arma::ones(10, 1); - y = arma::ones(1); + input1 = arma::mat(1, 10); + input2 = arma::mat(1, 10); + input1.ones(); + input2.ones(); + y = arma::mat(1, 1); + y.ones(); loss = module.Forward(std::move(input1),std::move(input1), - std::move(target)); + std::move(y)); BOOST_REQUIRE_CLOSE(loss, 0.0, 1e-4); + // Check for dissimilarity. + y.fill(-1); + loss = module.Forward(std::move(input1),std::move(input1), + std::move(y)); + BOOST_REQUIRE_CLOSE(loss, 1.0, 1e-4); + + // Test the Backward function. + module.Backward(std::move(input1), std::move(input1), std::move(y), + std::move(output)); + + input1 = arma::mat(3, 2); + input2 = arma::mat(3, 2); + input1.fill(1); + input1(4) = 2; + input2.fill(1); + input2(0) = 2; + input2(1) = 2; + input2(2) = 2; + y = arma::mat(3, 1); + y.fill(-1); + loss = module.Forward(std::move(input1),std::move(input2), + std::move(y)); + // Caclulated using torch.nn.CosineEmbeddingLoss(). + BOOST_REQUIRE_CLOSE(loss, 2.8973665961010275, 1e-3); + + // Check for correctness for cube. + CosineEmbeddingLoss<> module2(0.5); + + arma::cube input3(3, 2, 2); + arma::cube input4(3, 2, 2); + input3.fill(1); + input4.fill(1); + input3(0) = 2; + input3(1) = 2; + input3(4) = 2; + input3(6) = 2; + input3(8) = 2; + input3(10) = 2; + input4(2) = 2; + input4(9) = 2; + input4(11) = 2; + y = arma::mat(6, 1); + y.fill(1); + y(5) = -1; + y(2) = -1; + loss = module2.Forward(std::move(input3),std::move(input4), + std::move(y)); + // Caclulated using torch.nn.CosineEmbeddingLoss(). + BOOST_REQUIRE_CLOSE(loss, 1.0513167019494862, 1e-3); + + // Check Output for mean type of reduction. + CosineEmbeddingLoss<> module3(0.0, true); + loss = module3.Forward(std::move(input3),std::move(input4), + std::move(y)); + BOOST_REQUIRE_CLOSE(loss, 0.34188611699158106, 1e-3); } BOOST_AUTO_TEST_SUITE_END(); From 531358172356636c5e7531e390c28999da70c885 Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Tue, 18 Feb 2020 21:36:51 +0530 Subject: [PATCH 255/265] Added more tests (all input types), some style fix, fixed backward Remaining style fix Added more descriptive logs Spacing issue fixed in the new description Renamed Input Variables Change to BOOST_SMALL for precision error Style Fix' Rebased Small typo fix Design Acc. to ANN API Style Fix Style Fix --- .../loss_functions/cosine_embedding_loss.hpp | 94 ++++++------- .../cosine_embedding_loss_impl.hpp | 123 ++++++++---------- src/mlpack/tests/loss_functions_test.cpp | 69 +++++----- 3 files changed, 128 insertions(+), 158 deletions(-) diff --git a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp index dca30b8891..f792c19675 100644 --- a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp @@ -19,7 +19,7 @@ namespace ann /** Artificial Neural Network. */ { /** * Cosine Embeddings Loss function is used for measuring whether two inputs are - * similar or dissimilar, using using the cosine distance, and is typically used + * similar or dissimilar, using the cosine distance, and is typically used * for learning nonlinear embeddings or semi-supervised learning. * * @f{eqnarray*}{ @@ -41,96 +41,77 @@ class CosineEmbeddingLoss public: /** * Create the CosineEmbeddingLoss object. - * + * * @param margin Increases cosine distance in case of dissimilarity. * Refer definition of cosine-embedding-loss above. + * @param similarity Determines whether to use similarity or dissimilarity for + * comparision. * @param takeMean Boolean variable to specify whether to take mean or not. * Specifies reduction method i.e. sum or mean corresponding * to 0 and 1 respectively. Default value = 0. */ - CosineEmbeddingLoss(const double margin = 0.0, const bool takeMean = false); + CosineEmbeddingLoss(const double margin = 0.0, + const bool similarity = true, + const bool takeMean = false); /** * Ordinary feed forward pass of a neural network. * - * @param x1 Input data used for evaluating the given function. - * @param x2 Input data used for evaluating the given function. - * @param y Input data used to determine whether to calculate - * cosine similarity or dissimilarity. It should only - * contain values equal to 1 or -1. + * @param input Input data used for evaluating the specified function. + * @param target The target vector. */ - template< - typename FirstTensor, - typename SecondTensor, - typename ThirdTensor - > - double Forward(const FirstTensor&& x1, const SecondTensor&& x2, - const ThirdTensor&& y); + template + double Forward(const InputType& input, const TargetType& target); /** - * Ordinary feed backward pass of a neural network. The negative log - * likelihood layer expects that the input contains log-probabilities for - * each class. The layer also expects a class index, in the range between 1 - * and the number of classes, as target when calling the Forward function. + * Ordinary feed backward pass of a neural network. * - * @param x1 The propagated input activation. - * @param x2 The propagated input activation. - * @param y The target vector, that contains the class index in the range - * between 1 and the number of classes. + * @param input The propagated input activation. + * @param target The target vector. * @param output The calculated error. */ - template< - typename FirstTensor, - typename SecondTensor, - typename ThirdTensor, - typename OutputTensor - > - void Backward(const FirstTensor&& x1, - const SecondTensor&& x2, - const ThirdTensor&& y, - const OutputTensor&& output); + template + void Backward(const InputType& input, + const TargetType& target, + OutputType& output); //! Get the input parameter. - InputDataType& InputParameter() const { return inputParameter; } + InputDataType &InputParameter() const { return inputParameter; } //! Modify the input parameter. - InputDataType& InputParameter() { return inputParameter; } + InputDataType &InputParameter() { return inputParameter; } //! Get the output parameter. - OutputDataType& OutputParameter() const { return outputParameter; } + OutputDataType &OutputParameter() const { return outputParameter; } //! Modify the output parameter. - OutputDataType& OutputParameter() { return outputParameter; } + OutputDataType &OutputParameter() { return outputParameter; } //! Get the delta. - OutputDataType& Delta() const { return delta; } + OutputDataType &Delta() const { return delta; } //! Modify the delta. - OutputDataType& Delta() { return delta; } + OutputDataType &Delta() { return delta; } //! Get the value of takeMean. bool TakeMean() const { return takeMean; } //! Modify the value of takeMean. - bool& TakeMean() { return takeMean; } + bool &TakeMean() { return takeMean; } //! Get the value of margin. - bool Margin() const { return margin; } + double Margin() const { return margin; } //! Modify the value of takeMean. - bool& Margin() { return margin; } - + double &Margin() { return margin; } + + //! Get the value of similarity hyperparameter. + bool Similarity() const { return similarity; } + //! Modify the value of takeMean. + bool &Similarity() { return similarity; } /** * Serialize the layer. */ template - void serialize(Archive& /* ar */, const unsigned int /* version */); + void serialize(Archive& ar, const unsigned int /* version */); private: - // Returns Cosine-Distance between two vectors. - template - double CosineDistance(const FirstTensor&& x1, - const SecondTensor&& x2) - { - return arma::dot(arma::normalise(x1), arma::normalise(x2)); - } - //! Locally-stored delta object. OutputDataType delta; @@ -140,10 +121,13 @@ class CosineEmbeddingLoss //! Locally-stored output parameter object. OutputDataType outputParameter; - //! Locally-stored value of margin parameter. + //! Locally-stored value of similarity hyper-parameter. + bool similarity; + + //! Locally-stored value of margin hyper-parameter. double margin; - //! Locally-stored value of takeMean parameter. + //! Locally-stored value of takeMean hyper-parameter. bool takeMean; }; // class CosineEmbeddingLoss @@ -153,4 +137,4 @@ class CosineEmbeddingLoss // Include implementation. #include "cosine_embedding_loss_impl.hpp" -#endif \ No newline at end of file +#endif diff --git a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp index a463742b49..5e0bbccbe8 100644 --- a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp @@ -20,123 +20,104 @@ namespace ann /** Artificial Neural Network. */ { template CosineEmbeddingLoss::CosineEmbeddingLoss( - const double margin, const bool takeMean): - margin(margin), takeMean(takeMean) + const double margin, const bool similarity, const bool takeMean): + margin(margin), similarity(similarity), takeMean(takeMean) { // Nothing to do here. } template -template< - typename FirstTensor, - typename SecondTensor, - typename ThirdTensor -> +template double CosineEmbeddingLoss::Forward( - const FirstTensor&& x1, - const SecondTensor&& x2, - const ThirdTensor&& y) + const InputType& input, const TargetType& target) { - const size_t cols = x1.n_cols; - const size_t batchSize = x1.n_elem / cols; - if (x1.n_rows != x2.n_rows || x1.n_cols != x2.n_cols || - x1.n_elem != x2.n_elem) + const size_t cols = input.n_cols; + const size_t batchSize = input.n_elem / cols; + if (arma::size(input) != arma::size(target)) { - Log::Fatal << "Input Dimensions must be same." << std::endl; + Log::Fatal << "Input Tensors must have same dimensions." << std::endl; } - if (y.n_elem < batchSize) - { - Log::Fatal << "Number of rows mismatch." << std::endl; - } - - arma::colvec inputTemp1 = arma::vectorise(x1); - arma::colvec inputTemp2 = arma::vectorise(x2); + arma::colvec inputTemp1 = arma::vectorise(input); + arma::colvec inputTemp2 = arma::vectorise(target); double loss = 0.0; - for(size_t i = 0; i < inputTemp1.n_elem; i+=cols) + for (size_t i = 0; i < inputTemp1.n_elem; i += cols) { - if (y(i / cols) == 1) + double cosDist = kernel::CosineDistance::Evaluate(inputTemp1(arma::span(i, + i + cols - 1)), inputTemp2(arma::span(i, i + cols - 1))); + if (similarity) { - loss += 1 - CosineDistance(inputTemp1(arma::span(i, i + cols - 1)), - inputTemp2(arma::span(i, i + cols - 1))); - } - else if (y(i / cols) == -1) - { - double currentLoss = CosineDistance(inputTemp1(arma::span(i, i + cols - 1)), - inputTemp2(arma::span(i, i + cols - 1))) - margin; - loss += currentLoss > 0 ? currentLoss : 0; + loss += 1 - cosDist; } else { - Log::Fatal << "y should only contain 1 and -1." << std::endl; + double currentLoss = cosDist - margin; + loss += currentLoss > 0 ? currentLoss : 0; } } if (takeMean) { - loss = (double)loss / y.n_elem; + loss = (double)loss / batchSize; } + return loss; } template -template< - typename FirstTensor, - typename SecondTensor, - typename ThirdTensor, - typename OutputTensor -> +template void CosineEmbeddingLoss::Backward( - const FirstTensor&& x1, - const SecondTensor&& x2, - const ThirdTensor&& y, - const OutputTensor&& output) + const InputType& input, + const TargetType& target, + OutputType& output) { - const size_t cols = x1.n_cols; - const size_t batchSize = x1.n_elem / cols; - if (x1.n_rows != x2.n_rows || x1.n_cols != x2.n_cols || - x1.n_elem != x2.n_elem) + const size_t cols = input.n_cols; + const size_t batchSize = input.n_elem / cols; + if (arma::size(input) != arma::size(target)) { - Log::Fatal << "Input Dimensions must be same." << std::endl; + Log::Fatal << "Input Tensors must have same dimensions." << std::endl; } - if (y.n_elem < batchSize) - { - Log::Fatal << "Number of rows mismatch." << std::endl; - } - - arma::colvec inputTemp1 = arma::vectorise(x1); - arma::colvec inputTemp2 = arma::vectorise(x2); - arma::colvec outputTemp(inputTemp1.n_elem, 1); + arma::colvec inputTemp1 = arma::vectorise(input); + arma::colvec inputTemp2 = arma::vectorise(target); + output.set_size(arma::size(inputTemp1)); - for(size_t i = 0; i < inputTemp1.n_elem; i+=cols) + arma::colvec outputTemp(output.memptr(), inputTemp1.n_elem, + false, false); + for (size_t i = 0; i < inputTemp1.n_elem; i += cols) { - if (y(i / cols) != 1 && y(i / cols) != -1) + double cosDist = kernel::CosineDistance::Evaluate(inputTemp1(arma::span(i, + i + cols -1)), inputTemp2(arma::span(i, i + cols -1))); + + if (cosDist < margin && !similarity) { - Log::Fatal << "y should only contain 1 and -1." << std::endl; + outputTemp(arma::span(i, i + cols - 1)).zeros(); + } + else + { + int multiplier = similarity ? 1 : -1; + outputTemp(arma::span(i, i + cols -1)) = -1 * multiplier * + (arma::normalise(inputTemp2(arma::span(i, i + cols - 1))) - + cosDist * arma::normalise(inputTemp1(arma::span(i, i + cols - + 1)))) / std::sqrt(arma::accu(arma::pow(inputTemp1(arma::span(i, i + + cols - 1)), 2))); } - - outputTemp(arma::span(i, i + cols -1)) = arma::sign(y(i / cols)) * - (arma::normalise(inputTemp2(arma::span(i, i + cols - 1))) - - arma::normalise(inputTemp1(arma::span(i, i + cols - 1))) * - CosineDistance(inputTemp1(arma::span(i, i + cols - 1)), - inputTemp2(arma::span(i, i + cols - 1)))) / - std::sqrt(arma::accu(arma::pow(inputTemp1(arma::span(i, i + cols - 1)), - 2))); } } template template void CosineEmbeddingLoss::serialize( - Archive& /* ar */, + Archive& ar , const unsigned int /* version */) { - // Nothing to do here. + ar & BOOST_SERIALIZATION_NVP(margin); + ar & BOOST_SERIALIZATION_NVP(similarity); + ar & BOOST_SERIALIZATION_NVP(takeMean); } } // namespace ann } // namespace mlpack -#endif \ No newline at end of file +#endif diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index 8a5819afe9..a09cf77b2a 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -25,15 +25,9 @@ #include #include #include -<<<<<<< HEAD #include -<<<<<<< HEAD #include -======= -======= #include ->>>>>>> Add Cosine Embeddings ->>>>>>> Add Cosine Embeddings #include #include @@ -602,19 +596,22 @@ BOOST_AUTO_TEST_CASE(CosineEmbeddingLossTest) input2.ones(); y = arma::mat(1, 1); y.ones(); - loss = module.Forward(std::move(input1),std::move(input1), - std::move(y)); - BOOST_REQUIRE_CLOSE(loss, 0.0, 1e-4); + loss = module.Forward(input1, input1); + BOOST_REQUIRE_SMALL(loss, 1e-6); + + // Test the Backward function. + module.Backward(input1, input1, output); + BOOST_REQUIRE_SMALL(arma::accu(output), 1e-6); + // Check for dissimilarity. - y.fill(-1); - loss = module.Forward(std::move(input1),std::move(input1), - std::move(y)); + module.Similarity() = false; + loss = module.Forward(input1, input1); BOOST_REQUIRE_CLOSE(loss, 1.0, 1e-4); // Test the Backward function. - module.Backward(std::move(input1), std::move(input1), std::move(y), - std::move(output)); - + module.Backward(input1, input1, output); + BOOST_REQUIRE_SMALL(arma::accu(output), 1e-6); + input1 = arma::mat(3, 2); input2 = arma::mat(3, 2); input1.fill(1); @@ -623,15 +620,16 @@ BOOST_AUTO_TEST_CASE(CosineEmbeddingLossTest) input2(0) = 2; input2(1) = 2; input2(2) = 2; - y = arma::mat(3, 1); - y.fill(-1); - loss = module.Forward(std::move(input1),std::move(input2), - std::move(y)); + loss = module.Forward(input1, input2); // Caclulated using torch.nn.CosineEmbeddingLoss(). - BOOST_REQUIRE_CLOSE(loss, 2.8973665961010275, 1e-3); + BOOST_REQUIRE_CLOSE(loss, 2.897367, 1e-3); + + // Test the Backward function. + module.Backward(input1, input2, output); + BOOST_REQUIRE_CLOSE(arma::accu(output), 0.06324556, 1e-3); // Check for correctness for cube. - CosineEmbeddingLoss<> module2(0.5); + CosineEmbeddingLoss<> module2(0.5, true); arma::cube input3(3, 2, 2); arma::cube input4(3, 2, 2); @@ -646,20 +644,27 @@ BOOST_AUTO_TEST_CASE(CosineEmbeddingLossTest) input4(2) = 2; input4(9) = 2; input4(11) = 2; - y = arma::mat(6, 1); - y.fill(1); - y(5) = -1; - y(2) = -1; - loss = module2.Forward(std::move(input3),std::move(input4), - std::move(y)); + loss = module2.Forward(input3, input4); // Caclulated using torch.nn.CosineEmbeddingLoss(). - BOOST_REQUIRE_CLOSE(loss, 1.0513167019494862, 1e-3); + BOOST_REQUIRE_CLOSE(loss, 0.55395, 1e-3); + + // Test the Backward function. + module2.Backward(input3, input4, output); + BOOST_REQUIRE_CLOSE(arma::accu(output), -0.36649111, 1e-3); // Check Output for mean type of reduction. - CosineEmbeddingLoss<> module3(0.0, true); - loss = module3.Forward(std::move(input3),std::move(input4), - std::move(y)); - BOOST_REQUIRE_CLOSE(loss, 0.34188611699158106, 1e-3); + CosineEmbeddingLoss<> module3(0.0, true, true); + loss = module3.Forward(input3, input4); + BOOST_REQUIRE_CLOSE(loss, 0.092325, 1e-3); + + // Check correctness for cube. + module3.Similarity() = false; + loss = module3.Forward(input3, input4); + BOOST_REQUIRE_CLOSE(loss, 0.90767498236, 1e-3); + + // Test the Backward function. + module3.Backward(input3, input4, output); + BOOST_REQUIRE_CLOSE(arma::accu(output), 0.36649111, 1e-4); } BOOST_AUTO_TEST_SUITE_END(); From fe5d4bd1b01f55134a81e58e448c1dad20c34fc2 Mon Sep 17 00:00:00 2001 From: kartikdutt18 Date: Fri, 3 Apr 2020 13:12:01 +0530 Subject: [PATCH 256/265] Added to History.md, Changed to templated return, remobed unnecc brackets --- HISTORY.md | 2 + .../loss_functions/cosine_embedding_loss.hpp | 27 +++++++------- .../cosine_embedding_loss_impl.hpp | 37 +++++++++---------- src/mlpack/tests/loss_functions_test.cpp | 4 +- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index bcd3e5fac6..0241e86681 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -72,6 +72,8 @@ * Add Hinge Embedding Loss Function (#2229). + * Add Cosine Embedding Loss Function (#2209). + ### mlpack 3.2.2 ###### 2019-11-26 * Add `valid` and `same` padding option in `Convolution` and `Atrous diff --git a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp index f792c19675..044fbbdd0e 100644 --- a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss.hpp @@ -18,7 +18,7 @@ namespace mlpack { namespace ann /** Artificial Neural Network. */ { /** - * Cosine Embeddings Loss function is used for measuring whether two inputs are + * Cosine Embedding Loss function is used for measuring whether two inputs are * similar or dissimilar, using the cosine distance, and is typically used * for learning nonlinear embeddings or semi-supervised learning. * @@ -51,8 +51,8 @@ class CosineEmbeddingLoss * to 0 and 1 respectively. Default value = 0. */ CosineEmbeddingLoss(const double margin = 0.0, - const bool similarity = true, - const bool takeMean = false); + const bool similarity = true, + const bool takeMean = false); /** * Ordinary feed forward pass of a neural network. @@ -61,7 +61,8 @@ class CosineEmbeddingLoss * @param target The target vector. */ template - double Forward(const InputType& input, const TargetType& target); + typename InputType::elem_type Forward(const InputType& input, + const TargetType& target); /** * Ordinary feed backward pass of a neural network. @@ -76,34 +77,34 @@ class CosineEmbeddingLoss OutputType& output); //! Get the input parameter. - InputDataType &InputParameter() const { return inputParameter; } + InputDataType& InputParameter() const { return inputParameter; } //! Modify the input parameter. - InputDataType &InputParameter() { return inputParameter; } + InputDataType& InputParameter() { return inputParameter; } //! Get the output parameter. - OutputDataType &OutputParameter() const { return outputParameter; } + OutputDataType& OutputParameter() const { return outputParameter; } //! Modify the output parameter. - OutputDataType &OutputParameter() { return outputParameter; } + OutputDataType& OutputParameter() { return outputParameter; } //! Get the delta. - OutputDataType &Delta() const { return delta; } + OutputDataType& Delta() const { return delta; } //! Modify the delta. - OutputDataType &Delta() { return delta; } + OutputDataType& Delta() { return delta; } //! Get the value of takeMean. bool TakeMean() const { return takeMean; } //! Modify the value of takeMean. - bool &TakeMean() { return takeMean; } + bool& TakeMean() { return takeMean; } //! Get the value of margin. double Margin() const { return margin; } //! Modify the value of takeMean. - double &Margin() { return margin; } + double& Margin() { return margin; } //! Get the value of similarity hyperparameter. bool Similarity() const { return similarity; } //! Modify the value of takeMean. - bool &Similarity() { return similarity; } + bool& Similarity() { return similarity; } /** * Serialize the layer. diff --git a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp index 5e0bbccbe8..b708567b28 100644 --- a/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/cosine_embedding_loss_impl.hpp @@ -28,39 +28,38 @@ CosineEmbeddingLoss::CosineEmbeddingLoss( template template -double CosineEmbeddingLoss::Forward( - const InputType& input, const TargetType& target) +typename InputType::elem_type +CosineEmbeddingLoss::Forward( + const InputType& input, + const TargetType& target) { + typedef typename InputType::elem_type ElemType; + const size_t cols = input.n_cols; const size_t batchSize = input.n_elem / cols; if (arma::size(input) != arma::size(target)) - { Log::Fatal << "Input Tensors must have same dimensions." << std::endl; - } arma::colvec inputTemp1 = arma::vectorise(input); arma::colvec inputTemp2 = arma::vectorise(target); - double loss = 0.0; + ElemType loss = 0.0; for (size_t i = 0; i < inputTemp1.n_elem; i += cols) { - double cosDist = kernel::CosineDistance::Evaluate(inputTemp1(arma::span(i, - i + cols - 1)), inputTemp2(arma::span(i, i + cols - 1))); + const ElemType cosDist = kernel::CosineDistance::Evaluate( + inputTemp1(arma::span(i, i + cols - 1)), inputTemp2(arma::span(i, + i + cols - 1))); if (similarity) - { loss += 1 - cosDist; - } else { - double currentLoss = cosDist - margin; + const ElemType currentLoss = cosDist - margin; loss += currentLoss > 0 ? currentLoss : 0; } } if (takeMean) - { - loss = (double)loss / batchSize; - } + loss = (ElemType) loss / batchSize; return loss; } @@ -72,12 +71,12 @@ void CosineEmbeddingLoss::Backward( const TargetType& target, OutputType& output) { + typedef typename InputType::elem_type ElemType; + const size_t cols = input.n_cols; const size_t batchSize = input.n_elem / cols; if (arma::size(input) != arma::size(target)) - { Log::Fatal << "Input Tensors must have same dimensions." << std::endl; - } arma::colvec inputTemp1 = arma::vectorise(input); arma::colvec inputTemp2 = arma::vectorise(target); @@ -87,16 +86,14 @@ void CosineEmbeddingLoss::Backward( false, false); for (size_t i = 0; i < inputTemp1.n_elem; i += cols) { - double cosDist = kernel::CosineDistance::Evaluate(inputTemp1(arma::span(i, - i + cols -1)), inputTemp2(arma::span(i, i + cols -1))); + const ElemType cosDist = kernel::CosineDistance::Evaluate(inputTemp1( + arma::span(i, i + cols -1)), inputTemp2(arma::span(i, i + cols -1))); if (cosDist < margin && !similarity) - { outputTemp(arma::span(i, i + cols - 1)).zeros(); - } else { - int multiplier = similarity ? 1 : -1; + const int multiplier = similarity ? 1 : -1; outputTemp(arma::span(i, i + cols -1)) = -1 * multiplier * (arma::normalise(inputTemp2(arma::span(i, i + cols - 1))) - cosDist * arma::normalise(inputTemp1(arma::span(i, i + cols - diff --git a/src/mlpack/tests/loss_functions_test.cpp b/src/mlpack/tests/loss_functions_test.cpp index a09cf77b2a..9182f0da88 100644 --- a/src/mlpack/tests/loss_functions_test.cpp +++ b/src/mlpack/tests/loss_functions_test.cpp @@ -621,7 +621,7 @@ BOOST_AUTO_TEST_CASE(CosineEmbeddingLossTest) input2(1) = 2; input2(2) = 2; loss = module.Forward(input1, input2); - // Caclulated using torch.nn.CosineEmbeddingLoss(). + // Calculated using torch.nn.CosineEmbeddingLoss(). BOOST_REQUIRE_CLOSE(loss, 2.897367, 1e-3); // Test the Backward function. @@ -645,7 +645,7 @@ BOOST_AUTO_TEST_CASE(CosineEmbeddingLossTest) input4(9) = 2; input4(11) = 2; loss = module2.Forward(input3, input4); - // Caclulated using torch.nn.CosineEmbeddingLoss(). + // Calculated using torch.nn.CosineEmbeddingLoss(). BOOST_REQUIRE_CLOSE(loss, 0.55395, 1e-3); // Test the Backward function. From 8ca0543a0261017a621c0b1f21f9cdf3cc298f56 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Fri, 3 Apr 2020 11:11:20 +0300 Subject: [PATCH 257/265] Retrigger tests From 51c05e6ef58ae7b74d216630114025d86a458f04 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Fri, 3 Apr 2020 11:11:30 +0300 Subject: [PATCH 258/265] Rebuild From 9deace019341918e9a18318e36087e562e8acaf8 Mon Sep 17 00:00:00 2001 From: Mikhail Lozhnikov Date: Fri, 3 Apr 2020 13:05:35 +0300 Subject: [PATCH 259/265] - Fixed tiny style issues. - The Bag of Words algorithm calculates the number of occurrences. --- .../bag_of_words_encoding_policy.hpp | 16 ++++++++-------- .../dictionary_encoding_policy.hpp | 6 +++--- .../tf_idf_encoding_policy.hpp | 6 +++--- src/mlpack/tests/string_encoding_test.cpp | 14 +++++++------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index 3b3333c380..ff8097c1ac 100644 --- a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -24,10 +24,10 @@ namespace data { * Definition of the BagOfWordsEncodingPolicy class. * * BagOfWords is used as a helper class for StringEncoding. The encoder maps - * each dataset item to a vector of size N, where N is equal to the total number - * of tokens. If an item of the dataset has the i-th token, then the i-th - * coordinate of the corresponding vector is equal to 1, otherwise it's equal to - * zero. The order in which the tokens are labeled is defined by the dictionary + * each dataset item to a vector of size N, where N is equal to the total unique + * number of tokens. The i-th coordinate of the output vector is equal to + * the number of times when the i-th token occurs in the corresponding dataset + * item. The order in which the tokens are labeled is defined by the dictionary * used by the StringEncoding class. The encoder writes data either in the * column-major order or in the row-major order depending on the output data * type. @@ -44,7 +44,7 @@ class BagOfWordsEncodingPolicy * @param output Output matrix to store the encoded results (sp_mat or mat). * @param datasetSize The number of strings in the input dataset. * @param maxNumTokens The maximum number of tokens in the strings of the - input dataset (not used). + * input dataset (not used). * @param dictionarySize The size of the dictionary. */ template @@ -67,7 +67,7 @@ class BagOfWordsEncodingPolicy * @param output Output matrix to store the encoded results. * @param datasetSize The number of strings in the input dataset. * @param maxNumTokens The maximum number of tokens in the strings of the - input dataset (not used). + * input dataset (not used). * @param dictionarySize The size of the dictionary. */ template @@ -98,7 +98,7 @@ class BagOfWordsEncodingPolicy const size_t /* index */) { // The labels are assigned sequentially starting from one. - output(value - 1, line) = 1; + output(value - 1, line) += 1; } /** @@ -123,7 +123,7 @@ class BagOfWordsEncodingPolicy const size_t /* index */) { // The labels are assigned sequentially starting from one. - output[line][value - 1] = 1; + output[line][value - 1] += 1; } /** diff --git a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp index d0b49775e0..24a5fa21b9 100644 --- a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp @@ -41,7 +41,7 @@ class DictionaryEncodingPolicy * @param output Output matrix to store the encoded results (sp_mat or mat). * @param datasetSize The number of strings in the input dataset. * @param maxNumTokens The maximum number of tokens in the strings of the - input dataset. + * input dataset. * @param dictionarySize The size of the dictionary (not used). */ template @@ -55,7 +55,7 @@ class DictionaryEncodingPolicy /** * The function performs the dictionary encoding algorithm i.e. it writes - * the encoded token to the ouput. The encoder writes data in the + * the encoded token to the output. The encoder writes data in the * column-major order. * * @tparam MatType The output matrix type. @@ -76,7 +76,7 @@ class DictionaryEncodingPolicy /** * The function performs the dictionary encoding algorithm i.e. it writes - * the encoded token to the ouput. This is an overload function which saves + * the encoded token to the output. This is an overload function which saves * the result into the given vector to avoid padding. The encoder writes data * in the row-major order. * diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index 141bb8ea1f..c9aae8f5e3 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -46,7 +46,7 @@ class TfIdfEncodingPolicy * TERM_FREQUENCY Term frequency equals the number of times when the encoded * token occurs in the row divided by the row size. * SUBLINEAR_TF Term frequency equals \f$ 1 + log(rawCount), \f$ where - rawCount is equal to the number of times when the encoded + * rawCount is equal to the number of times when the encoded * token occurs in the row. */ enum class TfTypes @@ -86,7 +86,7 @@ class TfIdfEncodingPolicy * @param output Output matrix to store the encoded results (sp_mat or mat). * @param datasetSize The number of strings in the input dataset. * @param maxNumTokens The maximum number of tokens in the strings of the - input dataset (not used). + * input dataset (not used). * @param dictionarySize The size of the dictionary. */ template @@ -109,7 +109,7 @@ class TfIdfEncodingPolicy * @param output Output matrix to store the encoded results. * @param datasetSize The number of strings in the input dataset. * @param maxNumTokens The maximum number of tokens in the strings of the - input dataset (not used). + * input dataset (not used). * @param dictionarySize The size of the dictionary. */ template diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 23d1dc59dd..92befdfd77 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -596,7 +596,7 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) vectorizer = CountVectorizer(strip_accents=False, lowercase=False, preprocessor=None, tokenizer=tokenizer, stop_words=None, - vocabulary=dictionary, binary=True) + vocabulary=dictionary, binary=False) X = vectorizer.fit_transform(string_encoding_input) @@ -607,7 +607,7 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingTest) arma::mat expected = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + { 0, 1, 0, 0, 0, 2, 0, 0, 3, 3, 0, 0, 0, 3, 0, 0, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } @@ -647,7 +647,7 @@ BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingTest) vector> expected = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + { 0, 1, 0, 0, 0, 2, 0, 0, 3, 3, 0, 0, 0, 3, 0, 0, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } @@ -673,8 +673,8 @@ BOOST_AUTO_TEST_CASE(BagOfWordsEncodingIndividualCharactersTest) encoder.Encode(input, output, CharExtract()); arma::mat target = { - { 1, 1, 1, 0, 0 }, - { 0, 1, 1, 1, 1 }, + { 1, 2, 2, 0, 0 }, + { 0, 2, 2, 2, 1 }, { 1, 1, 0, 1, 0 } }; @@ -699,8 +699,8 @@ BOOST_AUTO_TEST_CASE(VectorBagOfWordsEncodingIndividualCharactersTest) encoder.Encode(input, output, CharExtract()); vector> expected = { - { 1, 1, 1, 0, 0 }, - { 0, 1, 1, 1, 1 }, + { 1, 2, 2, 0, 0 }, + { 0, 2, 2, 2, 1 }, { 1, 1, 0, 1, 0 } }; From 5ff26b029f88453c910f7784c868c866cb847a3c Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Fri, 3 Apr 2020 15:41:11 +0530 Subject: [PATCH 260/265] Windows build updated --- .ci/ci.yaml | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/.ci/ci.yaml b/.ci/ci.yaml index 739ba9906c..e0654b49e1 100644 --- a/.ci/ci.yaml +++ b/.ci/ci.yaml @@ -57,23 +57,6 @@ jobs: steps: - template: macos-steps.yaml -- job: WindowsVS14 - timeoutInMinutes: 360 - displayName: Windows VS14 - pool: - vmImage: vs2015-win2012r2 - strategy: - matrix: - Plain: - CMakeArgs: '-DDEBUG=ON -DPROFILE=OFF -DBUILD_PYTHON_BINDINGS=OFF' - CMakeGenerator: '-G "Visual Studio 14 2015 Win64"' - MSBuildVersion: '14.0' - ArchiveNoLibs: 'mlpack-windows-vs14-no-libs.zip' - ArchiveLibs: 'mlpack-windows-vs14.zip' - ArchiveTests: 'mlpack_test-vs14.xml' - steps: - - template: windows-steps.yaml - - job: WindowsVS15 timeoutInMinutes: 360 displayName: Windows VS15 From c57eeeb1798996021fed7e2f48269e5ff8271e78 Mon Sep 17 00:00:00 2001 From: AndreiMihalea Date: Fri, 3 Apr 2020 13:19:21 +0300 Subject: [PATCH 261/265] Retrigger tests From 69004bab0749069f55299205360f370cca3049cc Mon Sep 17 00:00:00 2001 From: Mikhail Lozhnikov Date: Fri, 3 Apr 2020 17:38:43 +0300 Subject: [PATCH 262/265] Force the string encoder to reset the necessary policy internal variables. --- src/mlpack/core/data/string_encoding_impl.hpp | 5 +++++ .../bag_of_words_encoding_policy.hpp | 8 ++++++++ .../dictionary_encoding_policy.hpp | 8 ++++++++ .../tf_idf_encoding_policy.hpp | 10 ++++++++++ src/mlpack/tests/string_encoding_test.cpp | 4 ++-- 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_impl.hpp b/src/mlpack/core/data/string_encoding_impl.hpp index 2dac65563f..eea72b8fc6 100644 --- a/src/mlpack/core/data/string_encoding_impl.hpp +++ b/src/mlpack/core/data/string_encoding_impl.hpp @@ -107,6 +107,8 @@ EncodeHelper(const std::vector& input, { size_t numColumns = 0; + policy.Reset(); + // The first pass adds the extracted tokens to the dictionary. for (size_t i = 0; i < input.size(); i++) { @@ -132,6 +134,7 @@ EncodeHelper(const std::vector& input, token = tokenizer(strView); numTokens++; } + numColumns = std::max(numColumns, numTokens); } policy.InitMatrix(output, input.size(), numColumns, dictionary.Size()); @@ -162,6 +165,8 @@ EncodeHelper(const std::vector& input, typename std::enable_if::onePassEncoding>::type*) { + policy.Reset(); + // The loop below extracts the tokens and writes the encoded values // at once. for (size_t i = 0; i < input.size(); i++) diff --git a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp index ff8097c1ac..d71a82ac6e 100644 --- a/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/bag_of_words_encoding_policy.hpp @@ -35,6 +35,14 @@ namespace data { class BagOfWordsEncodingPolicy { public: + /** + * Clear the necessary internal variables. + */ + static void Reset() + { + // Nothing to do. + } + /** * The function initializes the output matrix. The encoder writes data * in the column-major order. diff --git a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp index 24a5fa21b9..100c8394a1 100644 --- a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp @@ -32,6 +32,14 @@ namespace data { class DictionaryEncodingPolicy { public: + /** + * Clear the necessary internal variables. + */ + static void Reset() + { + // Nothing to do. + } + /** * The function initializes the output matrix. The encoder writes data * in the column-major order. diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index c9aae8f5e3..e93dfd573f 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -77,6 +77,16 @@ class TfIdfEncodingPolicy smoothIdf(smoothIdf) { } + /** + * Clear the necessary internal variables. + */ + void Reset() + { + tokensFrequences.clear(); + numContainingStrings.clear(); + linesSizes.clear(); + } + /** * The function initializes the output matrix. The encoder writes data * in the row-major order. diff --git a/src/mlpack/tests/string_encoding_test.cpp b/src/mlpack/tests/string_encoding_test.cpp index 92befdfd77..31a0f22713 100644 --- a/src/mlpack/tests/string_encoding_test.cpp +++ b/src/mlpack/tests/string_encoding_test.cpp @@ -1267,11 +1267,11 @@ BOOST_AUTO_TEST_CASE(SublinearTfIdfEncodingIndividualCharactersTest) TfIdfEncoding encoder(TfIdfEncodingPolicy::TfTypes::SUBLINEAR_TF, false); + encoder.Encode(input, output, CharExtract()); + /* The expected values were obtained by almost the same script as in RawCountSmoothIdfEncodingIndividualCharactersTest. The only difference is tf_type equals 'sublinear_tf' and smooth_idf equals False. */ - encoder.Encode(input, output, CharExtract()); - arma::mat target = { { 1.40546510810816, 1.69314718055995, 2.37965928516872, 0, 0 }, { 0, 1.69314718055995, 2.37965928516872, 2.37965928516872, From 6d4dee6383de65fa7827acf2dab1a02a5b6fee0f Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Fri, 3 Apr 2020 23:15:54 +0530 Subject: [PATCH 263/265] Reording include to pass CI build --- src/mlpack/methods/preprocess/image_converter_main.cpp | 2 +- src/mlpack/methods/preprocess/preprocess_scale_main.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/preprocess/image_converter_main.cpp b/src/mlpack/methods/preprocess/image_converter_main.cpp index 8cc73359fc..0d4beecf4b 100644 --- a/src/mlpack/methods/preprocess/image_converter_main.cpp +++ b/src/mlpack/methods/preprocess/image_converter_main.cpp @@ -10,8 +10,8 @@ * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ #include -#include #include +#include #include using namespace mlpack; diff --git a/src/mlpack/methods/preprocess/preprocess_scale_main.cpp b/src/mlpack/methods/preprocess/preprocess_scale_main.cpp index bcbdc46f08..dadd2554a7 100644 --- a/src/mlpack/methods/preprocess/preprocess_scale_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_scale_main.cpp @@ -10,9 +10,9 @@ * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ #include +#include #include #include -#include #include #include #include From f369ae57e92d89b7c8fd520f0284bd0f6d14476a Mon Sep 17 00:00:00 2001 From: jeffin143 Date: Fri, 3 Apr 2020 23:35:59 +0530 Subject: [PATCH 264/265] add ccov.hpp to fix julia binding failures --- src/mlpack/methods/preprocess/preprocess_scale_main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mlpack/methods/preprocess/preprocess_scale_main.cpp b/src/mlpack/methods/preprocess/preprocess_scale_main.cpp index dadd2554a7..cf68b8408c 100644 --- a/src/mlpack/methods/preprocess/preprocess_scale_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_scale_main.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include From 8c5fe9dc84c6e926c5dea7475446abdaf9e070df Mon Sep 17 00:00:00 2001 From: Mikhail Lozhnikov Date: Sat, 4 Apr 2020 15:28:14 +0300 Subject: [PATCH 265/265] Tiny documentation fixes for some string encoding algorithms. --- src/mlpack/core/data/string_encoding_impl.hpp | 1 + .../string_encoding_policies/dictionary_encoding_policy.hpp | 2 +- .../data/string_encoding_policies/tf_idf_encoding_policy.hpp | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mlpack/core/data/string_encoding_impl.hpp b/src/mlpack/core/data/string_encoding_impl.hpp index eea72b8fc6..0e50f6c1d8 100644 --- a/src/mlpack/core/data/string_encoding_impl.hpp +++ b/src/mlpack/core/data/string_encoding_impl.hpp @@ -137,6 +137,7 @@ EncodeHelper(const std::vector& input, numColumns = std::max(numColumns, numTokens); } + policy.InitMatrix(output, input.size(), numColumns, dictionary.Size()); // The second pass writes the encoded values to the output. diff --git a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp index 100c8394a1..d9a37cacac 100644 --- a/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/dictionary_encoding_policy.hpp @@ -84,7 +84,7 @@ class DictionaryEncodingPolicy /** * The function performs the dictionary encoding algorithm i.e. it writes - * the encoded token to the output. This is an overload function which saves + * the encoded token to the output. This is an overloaded function which saves * the result into the given vector to avoid padding. The encoder writes data * in the row-major order. * diff --git a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp index e93dfd573f..853493e285 100644 --- a/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp +++ b/src/mlpack/core/data/string_encoding_policies/tf_idf_encoding_policy.hpp @@ -44,7 +44,8 @@ class TfIdfEncodingPolicy * RAW_COUNT Term frequency equals the number of times when the encoded * token occurs in the row. * TERM_FREQUENCY Term frequency equals the number of times when the encoded - * token occurs in the row divided by the row size. + * token occurs in the row divided by the total number of + * tokens in the row. * SUBLINEAR_TF Term frequency equals \f$ 1 + log(rawCount), \f$ where * rawCount is equal to the number of times when the encoded * token occurs in the row.