From 2e93edd77e3efd0c8166968da1efe25f08557227 Mon Sep 17 00:00:00 2001 From: farouk Date: Sun, 25 Mar 2018 01:20:23 -0400 Subject: [PATCH 01/13] Added the stan library include directories into CMakeLists as well as the internal boost and cvodes that stan uses --- src/shogun/CMakeLists.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/shogun/CMakeLists.txt b/src/shogun/CMakeLists.txt index c8f79ac75d9..0ad431b1450 100644 --- a/src/shogun/CMakeLists.txt +++ b/src/shogun/CMakeLists.txt @@ -217,6 +217,21 @@ CHECK_FUNCTION_EXISTS(lgammal HAVE_LGAMMAL) ############################ external dependencies +#Stan math +include(external/StanMath) +SHOGUN_INCLUDE_DIRS(SCOPE PUBLIC SYSTEM + $ + $ +) +SHOGUN_INCLUDE_DIRS(SCOPE PUBLIC SYSTEM + $ + $ +) +SHOGUN_INCLUDE_DIRS(SCOPE PUBLIC SYSTEM + $ + $ +) + ####### LINALG FIND_PACKAGE(Eigen3 ${EIGEN_VERSION_MINIMUM}) IF(NOT EIGEN3_FOUND) From abd8a31fb5a7aace9b81522702befdf0d3beb391 Mon Sep 17 00:00:00 2001 From: farouk Date: Sun, 25 Mar 2018 01:21:18 -0400 Subject: [PATCH 02/13] Built a StanMath.cmake file to be used in the CMakelists.txt in the src --- cmake/external/StanMath.cmake | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 cmake/external/StanMath.cmake diff --git a/cmake/external/StanMath.cmake b/cmake/external/StanMath.cmake new file mode 100644 index 00000000000..75f06c9dc64 --- /dev/null +++ b/cmake/external/StanMath.cmake @@ -0,0 +1,18 @@ +include(ExternalProject) +ExternalProject_Add( + StanMath + PREFIX ${CMAKE_BINARY_DIR}/StanMath + DOWNLOAD_DIR ${THIRD_PARTY_DIR}/StanMath + GIT_REPOSITORY https://github.com/stan-dev/math + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + LOG_DOWNLOAD ON + ) + + +add_dependencies(libshogun StanMath) + +set(STAN_INCLUDE_DIR_STAN_MATH ${CMAKE_BINARY_DIR}/StanMath/src/StanMath) +set(STAN_INCLUDE_DIR_BOOST ${CMAKE_BINARY_DIR}/StanMath/src/StanMath/lib/boost_1.66.0) +set(STAN_INCLUDE_DIR_CVODES ${CMAKE_BINARY_DIR}/StanMath/src/StanMath/lib/cvodes_2.9.0/include) From 823a71a2e8e80174bd60e33a0e5c9d24c2ffeb40 Mon Sep 17 00:00:00 2001 From: farouk Date: Fri, 4 May 2018 07:35:11 -0400 Subject: [PATCH 03/13] updating data submodule to latest --- data | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data b/data index 5e34dcd6f20..5242d58e94b 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit 5e34dcd6f2037a59c682bf5628c12e2d06060019 +Subproject commit 5242d58e94b39725d21f451fb645d3b68245e72c From 46dac390c29a41dd43b0d4ba38bc9178330e1dd7 Mon Sep 17 00:00:00 2001 From: farouk Date: Thu, 29 Mar 2018 00:20:03 -0400 Subject: [PATCH 04/13] Added the latest stable version of Stan math instead of fetching from HEAD --- cmake/external/StanMath.cmake | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmake/external/StanMath.cmake b/cmake/external/StanMath.cmake index 75f06c9dc64..c2e6c94baf4 100644 --- a/cmake/external/StanMath.cmake +++ b/cmake/external/StanMath.cmake @@ -1,9 +1,12 @@ +set(STAN_STABLE_RELEASE 4b1a10bc877d941bbe0a12c198526807be27167a) + include(ExternalProject) ExternalProject_Add( StanMath PREFIX ${CMAKE_BINARY_DIR}/StanMath DOWNLOAD_DIR ${THIRD_PARTY_DIR}/StanMath GIT_REPOSITORY https://github.com/stan-dev/math + GIT_TAG ${STAN_STABLE_RELEASE} CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" From 9053ae6c534d8eadc72b68f7cd7f964e1bdb2415 Mon Sep 17 00:00:00 2001 From: farouk Date: Thu, 29 Mar 2018 00:20:48 -0400 Subject: [PATCH 05/13] Changed stan1,2,3 to be more expressive of what the directories have --- src/shogun/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/shogun/CMakeLists.txt b/src/shogun/CMakeLists.txt index 0ad431b1450..71ba25330a8 100644 --- a/src/shogun/CMakeLists.txt +++ b/src/shogun/CMakeLists.txt @@ -221,15 +221,15 @@ CHECK_FUNCTION_EXISTS(lgammal HAVE_LGAMMAL) include(external/StanMath) SHOGUN_INCLUDE_DIRS(SCOPE PUBLIC SYSTEM $ - $ + $ ) SHOGUN_INCLUDE_DIRS(SCOPE PUBLIC SYSTEM $ - $ + $ ) SHOGUN_INCLUDE_DIRS(SCOPE PUBLIC SYSTEM $ - $ + $ ) ####### LINALG From 86b8f593f7e0ca3cff1b89576f3b7d0000f76d1d Mon Sep 17 00:00:00 2001 From: farouk Date: Thu, 5 Apr 2018 17:30:44 -0400 Subject: [PATCH 06/13] Changed the cmake boost file from 66 to 64 since it was a typo --- cmake/external/StanMath.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/external/StanMath.cmake b/cmake/external/StanMath.cmake index c2e6c94baf4..ed33c843157 100644 --- a/cmake/external/StanMath.cmake +++ b/cmake/external/StanMath.cmake @@ -17,5 +17,5 @@ ExternalProject_Add( add_dependencies(libshogun StanMath) set(STAN_INCLUDE_DIR_STAN_MATH ${CMAKE_BINARY_DIR}/StanMath/src/StanMath) -set(STAN_INCLUDE_DIR_BOOST ${CMAKE_BINARY_DIR}/StanMath/src/StanMath/lib/boost_1.66.0) +set(STAN_INCLUDE_DIR_BOOST ${CMAKE_BINARY_DIR}/StanMath/src/StanMath/lib/boost_1.64.0) set(STAN_INCLUDE_DIR_CVODES ${CMAKE_BINARY_DIR}/StanMath/src/StanMath/lib/cvodes_2.9.0/include) From b6362302ebfc258376a1c572f02cdd5e081744ab Mon Sep 17 00:00:00 2001 From: root Date: Mon, 16 Apr 2018 07:31:15 +0000 Subject: [PATCH 07/13] Added installation requirements --- cmake/external/StanMath.cmake | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmake/external/StanMath.cmake b/cmake/external/StanMath.cmake index ed33c843157..59bfdbd3540 100644 --- a/cmake/external/StanMath.cmake +++ b/cmake/external/StanMath.cmake @@ -19,3 +19,8 @@ add_dependencies(libshogun StanMath) set(STAN_INCLUDE_DIR_STAN_MATH ${CMAKE_BINARY_DIR}/StanMath/src/StanMath) set(STAN_INCLUDE_DIR_BOOST ${CMAKE_BINARY_DIR}/StanMath/src/StanMath/lib/boost_1.64.0) set(STAN_INCLUDE_DIR_CVODES ${CMAKE_BINARY_DIR}/StanMath/src/StanMath/lib/cvodes_2.9.0/include) + + +INSTALL( DIRECTORY ${STAN_INCLUDE_DIR_STAN_MATH} DESTINATION include/shogun/lib/external/Stan ) +INSTALL( DIRECTORY ${STAN_INCLUDE_DIR_BOOST} DESTINATION include/shogun/lib/external/Stan_Boost ) +INSTALL( DIRECTORY ${STAN_INCLUDE_DIR_CVODES} DESTINATION include/shogun/lib/external/Stan_Cvodes ) From 651bec12b2150198207b691a1c24aa7605171f8c Mon Sep 17 00:00:00 2001 From: root Date: Mon, 16 Apr 2018 07:31:59 +0000 Subject: [PATCH 08/13] Fixed the Cmake file for stan --- src/shogun/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/shogun/CMakeLists.txt b/src/shogun/CMakeLists.txt index 71ba25330a8..67fe67a6b98 100644 --- a/src/shogun/CMakeLists.txt +++ b/src/shogun/CMakeLists.txt @@ -219,6 +219,7 @@ CHECK_FUNCTION_EXISTS(lgammal HAVE_LGAMMAL) #Stan math include(external/StanMath) + SHOGUN_INCLUDE_DIRS(SCOPE PUBLIC SYSTEM $ $ From ff3e699cc215ed49eb18df8861fd451905e47804 Mon Sep 17 00:00:00 2001 From: farouk Date: Fri, 4 May 2018 08:29:09 -0400 Subject: [PATCH 09/13] Changed CMakeLists so that Stan installs in third_party instead of lib --- src/shogun/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/shogun/CMakeLists.txt b/src/shogun/CMakeLists.txt index 67fe67a6b98..7b613cc27ac 100644 --- a/src/shogun/CMakeLists.txt +++ b/src/shogun/CMakeLists.txt @@ -222,15 +222,15 @@ include(external/StanMath) SHOGUN_INCLUDE_DIRS(SCOPE PUBLIC SYSTEM $ - $ + $ ) SHOGUN_INCLUDE_DIRS(SCOPE PUBLIC SYSTEM $ - $ + $ ) SHOGUN_INCLUDE_DIRS(SCOPE PUBLIC SYSTEM $ - $ + $ ) ####### LINALG From ef0a69bac3f9f9249c3ee26a51167deafcf020b9 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 4 May 2018 12:35:20 +0000 Subject: [PATCH 10/13] Added the Stan test and removed unnecciary comments --- .../StanMath/StanPerceptronTest_unittest.cc | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tests/unit/StanMath/StanPerceptronTest_unittest.cc diff --git a/tests/unit/StanMath/StanPerceptronTest_unittest.cc b/tests/unit/StanMath/StanPerceptronTest_unittest.cc new file mode 100644 index 00000000000..240362efd8a --- /dev/null +++ b/tests/unit/StanMath/StanPerceptronTest_unittest.cc @@ -0,0 +1,74 @@ +//tests/unit/stanmath/StanPerceptronTest_unittest.cc +#include +#include +#include +#include +#include +#include +using namespace std; +using namespace Eigen; +using namespace stan::math; + + +/* +This test will implement a perceptron using stan math +The perceptron will have one input, the column vector [1 1 1]^T +Then, it will have 2x3 Matrix of weights that it will learn +Then, the output is a 2x1 column vector +In this example, we want to learn the weights W such that the square +Error loss from the output of the perceptron to [1 1]^T is minimized. +Since we can find weights from [1 1 1]^T to [1 1]^T in a perceptron, +this error should be very close to zero after 100 epochs. +*/ +TEST(StanPerceptronTest, sample_perceptron) +{ + //Initialize the Input Vector + Matrix inp; + inp(0,0)=1; + inp(1,0)=1; + inp(2,0)=1; + + //Randomly Initialize the weights on the perceptron + std::random_device rd{}; + std::mt19937 gen{rd()}; + normal_distribution<> d{0,1}; + Matrix W1; + for(int i=0; i<2; ++i) + { + for(int j=0; j<3; ++j) + { + W1(i,j)=0.01 * d(gen); + } + } + + //Define the outputs of the neural network + Matrix outputs; + + double learning_rate = 0.1; + double last_error = 0; + for(int epoch=0; epoch<100; ++epoch) + { + var error=0; + outputs = W1*inp; + for(int i=0; i<2; ++i) + { + error += (outputs(i,0)-1)*(outputs(i,0)-1); + } + error.grad(); + + //Now use gradient descent to change the weights + for(int i=0; i<2; ++i) + { + for(int j=0; j<3; ++j) + { + W1(i,j)=W1(i,j) - learning_rate* W1(i,j).adj(); + } + } + + //Store the value of current error in last_error + last_error = value_of(error); + } + + //Error should be very close to 0.0 + EXPECT_NEAR(last_error, 0.0, 1e-6); +} From 9e44ffda281df3bda53ca8530d94e401fae2bf78 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 4 May 2018 18:03:32 +0000 Subject: [PATCH 11/13] removed INSTALL and replaced it with copy_if_different --- cmake/external/StanMath.cmake | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/cmake/external/StanMath.cmake b/cmake/external/StanMath.cmake index 59bfdbd3540..7685f29ee37 100644 --- a/cmake/external/StanMath.cmake +++ b/cmake/external/StanMath.cmake @@ -1,5 +1,9 @@ set(STAN_STABLE_RELEASE 4b1a10bc877d941bbe0a12c198526807be27167a) +set(STAN_INCLUDE_DIR_STAN_MATH ${CMAKE_BINARY_DIR}/StanMath/src/StanMath) +set(STAN_INCLUDE_DIR_BOOST ${CMAKE_BINARY_DIR}/StanMath/src/StanMath/lib/boost_1.64.0) +set(STAN_INCLUDE_DIR_CVODES ${CMAKE_BINARY_DIR}/StanMath/src/StanMath/lib/cvodes_2.9.0/include) + include(ExternalProject) ExternalProject_Add( StanMath @@ -9,18 +13,10 @@ ExternalProject_Add( GIT_TAG ${STAN_STABLE_RELEASE} CONFIGURE_COMMAND "" BUILD_COMMAND "" - INSTALL_COMMAND "" + INSTALL_COMMAND + ${CMAKE_COMMAND} -E copy_if_different ${STAN_INCLUDE_DIR_STAN_MATH} include/shogun/third_party/Stan + && ${CMAKE_COMMAND} -E copy_if_different ${STAN_INCLUDE_DIR_BOOST} include/shogun/third_party/Stan_Boost + && ${CMAKE_COMMAND} -E copy_if_different ${STAN_INCLUDE_DIR_CVODES} include/shogun/third_party/Stan_Cvodes LOG_DOWNLOAD ON ) - - add_dependencies(libshogun StanMath) - -set(STAN_INCLUDE_DIR_STAN_MATH ${CMAKE_BINARY_DIR}/StanMath/src/StanMath) -set(STAN_INCLUDE_DIR_BOOST ${CMAKE_BINARY_DIR}/StanMath/src/StanMath/lib/boost_1.64.0) -set(STAN_INCLUDE_DIR_CVODES ${CMAKE_BINARY_DIR}/StanMath/src/StanMath/lib/cvodes_2.9.0/include) - - -INSTALL( DIRECTORY ${STAN_INCLUDE_DIR_STAN_MATH} DESTINATION include/shogun/lib/external/Stan ) -INSTALL( DIRECTORY ${STAN_INCLUDE_DIR_BOOST} DESTINATION include/shogun/lib/external/Stan_Boost ) -INSTALL( DIRECTORY ${STAN_INCLUDE_DIR_CVODES} DESTINATION include/shogun/lib/external/Stan_Cvodes ) From d2dca46106d83347dfc47e94dce1528bcc67c995 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 4 May 2018 18:32:45 +0000 Subject: [PATCH 12/13] Rebased with data HEAD --- data | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data b/data index 5242d58e94b..5e34dcd6f20 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit 5242d58e94b39725d21f451fb645d3b68245e72c +Subproject commit 5e34dcd6f2037a59c682bf5628c12e2d06060019 From 910d32cd974c22126931dc843e4d79d8f0ae2bea Mon Sep 17 00:00:00 2001 From: root Date: Fri, 4 May 2018 23:46:58 +0000 Subject: [PATCH 13/13] Fixed clang-format style check --- .../StanMath/StanPerceptronTest_unittest.cc | 95 +++++++++---------- 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/tests/unit/StanMath/StanPerceptronTest_unittest.cc b/tests/unit/StanMath/StanPerceptronTest_unittest.cc index 240362efd8a..8a0315dab3b 100644 --- a/tests/unit/StanMath/StanPerceptronTest_unittest.cc +++ b/tests/unit/StanMath/StanPerceptronTest_unittest.cc @@ -1,15 +1,14 @@ -//tests/unit/stanmath/StanPerceptronTest_unittest.cc +// tests/unit/stanmath/StanPerceptronTest_unittest.cc +#include +#include #include -#include #include -#include -#include #include +#include using namespace std; using namespace Eigen; using namespace stan::math; - /* This test will implement a perceptron using stan math The perceptron will have one input, the column vector [1 1 1]^T @@ -22,53 +21,53 @@ this error should be very close to zero after 100 epochs. */ TEST(StanPerceptronTest, sample_perceptron) { - //Initialize the Input Vector - Matrix inp; - inp(0,0)=1; - inp(1,0)=1; - inp(2,0)=1; + // Initialize the Input Vector + Matrix inp; + inp(0, 0) = 1; + inp(1, 0) = 1; + inp(2, 0) = 1; - //Randomly Initialize the weights on the perceptron - std::random_device rd{}; - std::mt19937 gen{rd()}; - normal_distribution<> d{0,1}; - Matrix W1; - for(int i=0; i<2; ++i) - { - for(int j=0; j<3; ++j) - { - W1(i,j)=0.01 * d(gen); - } - } + // Randomly Initialize the weights on the perceptron + std::random_device rd{}; + std::mt19937 gen{rd()}; + normal_distribution<> d{0, 1}; + Matrix W1; + for (int i = 0; i < 2; ++i) + { + for (int j = 0; j < 3; ++j) + { + W1(i, j) = 0.01 * d(gen); + } + } - //Define the outputs of the neural network - Matrix outputs; + // Define the outputs of the neural network + Matrix outputs; - double learning_rate = 0.1; - double last_error = 0; - for(int epoch=0; epoch<100; ++epoch) - { - var error=0; - outputs = W1*inp; - for(int i=0; i<2; ++i) - { - error += (outputs(i,0)-1)*(outputs(i,0)-1); - } - error.grad(); + double learning_rate = 0.1; + double last_error = 0; + for (int epoch = 0; epoch < 100; ++epoch) + { + var error = 0; + outputs = W1 * inp; + for (int i = 0; i < 2; ++i) + { + error += (outputs(i, 0) - 1) * (outputs(i, 0) - 1); + } + error.grad(); - //Now use gradient descent to change the weights - for(int i=0; i<2; ++i) - { - for(int j=0; j<3; ++j) - { - W1(i,j)=W1(i,j) - learning_rate* W1(i,j).adj(); - } - } + // Now use gradient descent to change the weights + for (int i = 0; i < 2; ++i) + { + for (int j = 0; j < 3; ++j) + { + W1(i, j) = W1(i, j) - learning_rate * W1(i, j).adj(); + } + } - //Store the value of current error in last_error - last_error = value_of(error); - } + // Store the value of current error in last_error + last_error = value_of(error); + } - //Error should be very close to 0.0 - EXPECT_NEAR(last_error, 0.0, 1e-6); + // Error should be very close to 0.0 + EXPECT_NEAR(last_error, 0.0, 1e-6); }