├── Chapter01 ├── dlib_samples │ ├── CMakeLists.txt │ ├── linalg_dlib.cc │ └── linreg_dlib.cc ├── eigen_samples │ ├── CMakeLists.txt │ ├── linalg_eigen.cc │ └── linreg_eigen.cc ├── sharkml_samples │ ├── CMakeLists.txt │ ├── linalg_shark.cc │ └── linreg_shark.cc ├── shogun_samples │ ├── CMakeLists.txt │ └── linreg_shogun.cc └── xtensor_samples │ ├── CMakeLists.txt │ └── linalg_xtensor.cc ├── Chapter02 ├── csv │ ├── cpp │ │ ├── CMakeLists.txt │ │ └── csv.cc │ ├── dlib │ │ ├── CMakeLists.txt │ │ └── csv_dlib.cc │ ├── sharkml │ │ ├── CMakeLists.txt │ │ └── csv_shark.cc │ └── shogun │ │ ├── CMakeLists.txt │ │ └── csv_shogun.cc ├── data │ ├── iris.data │ └── reviews.json ├── hdf5 │ └── cpp │ │ ├── CMakeLists.txt │ │ └── hdf5.cc ├── img │ ├── dlib │ │ ├── CMakeLists.txt │ │ └── img_dlib.cc │ └── opencv │ │ ├── CMakeLists.txt │ │ └── ocv.cc └── json │ └── cpp │ ├── CMakeLists.txt │ ├── json.cc │ ├── paper.h │ ├── review.h │ ├── reviewsreader.cpp │ └── reviewsreader.h ├── Chapter03 ├── dlib │ ├── CMakeLists.txt │ └── grid-dlib.cc ├── sharkml │ ├── CMakeLists.txt │ ├── data.cpp │ ├── data.h │ ├── grid-shark.cc │ ├── monitor.h │ ├── plot.cpp │ ├── plot.h │ ├── polynomial-model.h │ ├── polynomial-regression.h │ └── regression-shark.cc └── shogun │ ├── CMakeLists.txt │ ├── data.cc │ ├── data.h │ └── grid-shogun.cc ├── Chapter04 ├── data │ ├── dataset0.csv │ ├── dataset1.csv │ ├── dataset2.csv │ ├── dataset3.csv │ ├── dataset4.csv │ └── dataset5.csv ├── dlib │ ├── CMakeLists.txt │ └── dlib-cluster.cc ├── sharkml │ ├── CMakeLists.txt │ └── sharkml-cluster.cc └── shogun │ ├── CMakeLists.txt │ └── shogun-cluster.cc ├── Chapter05 ├── data │ ├── multivar.csv │ ├── twoclusters.csv │ └── univar.csv ├── dlib │ ├── CMakeLists.txt │ ├── dlib-anomaly.cc │ └── isolation-forest.h ├── sharkml │ ├── CMakeLists.txt │ └── sharkml-anomaly.cc └── shogun │ ├── CMakeLists.txt │ └── shogun-anomaly.cc ├── Chapter06 ├── data │ ├── photo.png │ ├── swissroll.dat │ └── swissroll_labels.dat ├── dlib │ ├── CMakeLists.txt │ └── dlib-dr.cc ├── sharkml │ ├── CMakeLists.txt │ └── sharkml-dr.cc └── shogun │ ├── CMakeLists.txt │ └── shogun-dr.cc ├── Chapter07 ├── dlib │ ├── CMakeLists.txt │ └── dlib-classify.cc ├── sharkml │ ├── CMakeLists.txt │ └── sharkml-classify.cc └── shogun │ ├── CMakeLists.txt │ └── shogun-classify.cc ├── Chapter08 ├── eigen │ ├── CMakeLists.txt │ ├── data_loader.h │ └── eigen_recommender.cc └── mlpack │ ├── CMakeLists.txt │ └── mlpack-recommender.cc ├── Chapter09 ├── sharkml │ ├── CMakeLists.txt │ └── sharkml-ensemble.cc └── shogun │ ├── CMakeLists.txt │ ├── data │ ├── data.cc │ └── data.h │ └── shogun-ensemble.cc ├── Chapter10 ├── data │ ├── data.cc │ └── data.h ├── dlib │ ├── CMakeLists.txt │ └── mlp-dlib.cc ├── pytorch │ ├── CMakeLists.txt │ ├── lenet5.cpp │ ├── lenet5.h │ ├── main.cpp │ ├── mnistdataset.cpp │ └── mnistdataset.h ├── sharkml │ ├── CMakeLists.txt │ └── mlp-sharkml.cc └── shogun │ ├── CMakeLists.txt │ └── mlp-shogun.cc ├── Chapter11 └── pytorch │ ├── CMakeLists.txt │ ├── glovedict.cc │ ├── glovedict.h │ ├── imdbdataset.cc │ ├── imdbdataset.h │ ├── imdbreader.cc │ ├── imdbreader.h │ ├── main.cc │ ├── rnn.cc │ ├── rnn.h │ ├── vocabulary.cc │ └── vocabulary.h ├── Chapter12 ├── caffe2 │ ├── CMakeLists.txt │ └── main.cc ├── dlib │ ├── CMakeLists.txt │ └── dlib-save.cc ├── pytorch │ ├── CMakeLists.txt │ └── main.cc ├── sharkml │ ├── CMakeLists.txt │ └── shark-save.cc └── shogun │ ├── CMakeLists.txt │ └── shogun-save.cc ├── Chapter13 ├── android_classify │ ├── app │ │ ├── build.gradle │ │ ├── proguard-rules.pro │ │ └── src │ │ │ └── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── assets │ │ │ └── synset.txt │ │ │ ├── cpp │ │ │ ├── CMakeLists.txt │ │ │ └── native-lib.cpp │ │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── camera2 │ │ │ │ └── MainActivity.java │ │ │ └── res │ │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── drawable │ │ │ └── ic_launcher_background.xml │ │ │ ├── layout │ │ │ └── activity_main.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── local.properties │ └── settings.gradle ├── cloud_classify │ ├── client │ │ ├── index.html │ │ └── upload.js │ └── server │ │ ├── CMakeLists.txt │ │ ├── README.md │ │ ├── main.cc │ │ ├── network.cc │ │ ├── network.h │ │ ├── utils.cc │ │ └── utils.h └── python │ ├── export.sh │ └── model_export.py ├── LICENSE ├── README.md ├── build_scripts ├── build_ch1.sh ├── build_ch10.sh ├── build_ch11.sh ├── build_ch12.sh ├── build_ch13.sh ├── build_ch2.sh ├── build_ch3.sh ├── build_ch4.sh ├── build_ch5.sh ├── build_ch6.sh ├── build_ch7.sh ├── build_ch8.sh └── build_ch9.sh └── env_scripts ├── README.md ├── checkout_lib.sh ├── dockerfile ├── install_android.sh ├── install_env.sh └── install_lib.sh /Chapter01/dlib_samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-samples) 3 | 4 | set(DLIB_PATH "" CACHE PATH "path to dlib install dir") 5 | 6 | if (NOT DLIB_PATH) 7 | message(FATAL_ERROR "Missing DLib install path, please specify DLIB_PATH") 8 | else() 9 | message("DLib path is ${DLIB_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(requiredlibs "cblas") 15 | list(APPEND requiredlibs "lapack") 16 | 17 | find_package(CUDA) 18 | if (CUDA_FOUND) 19 | list(APPEND requiredlibs ${CUDA_LIBRARIES}) 20 | list(APPEND requiredlibs ${CUDA_CUBLAS_LIBRARIES}) 21 | list(APPEND requiredlibs ${CUDA_cusolver_LIBRARY}) 22 | list(APPEND requiredlibs ${CUDA_curand_LIBRARY}) 23 | 24 | get_filename_component(cudnn_hint_path "${CUDA_CUBLAS_LIBRARIES}" PATH) 25 | find_library(cudnn cudnn 26 | HINTS ${cudnn_hint_path} ENV CUDNN_LIBRARY_DIR ENV CUDNN_HOME 27 | PATHS /usr/local /usr/local/cuda ENV LD_LIBRARY_PATH 28 | PATH_SUFFIXES lib64 lib x64 29 | ) 30 | message("CUDNN found ${cudnn}") 31 | list(APPEND requiredlibs ${cudnn}) 32 | endif() 33 | 34 | 35 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 36 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 37 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 38 | 39 | include_directories(${DLIB_PATH}/include) 40 | link_directories(${DLIB_PATH}/lib) 41 | link_directories(${DLIB_PATH}/lib64) 42 | 43 | add_executable(linalg-dlib "linalg_dlib.cc") 44 | target_link_libraries(linalg-dlib optimized dlib debug dlibd) 45 | target_link_libraries(linalg-dlib ${requiredlibs}) 46 | 47 | add_executable(linreg-dlib "linreg_dlib.cc") 48 | target_link_libraries(linreg-dlib optimized dlib debug dlibd) 49 | target_link_libraries(linreg-dlib ${requiredlibs}) 50 | 51 | 52 | -------------------------------------------------------------------------------- /Chapter01/dlib_samples/linalg_dlib.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | // definitions 6 | { 7 | // compile time sized matrix 8 | dlib::matrix y; 9 | // dynamically sized matrix 10 | dlib::matrix m(3, 3); 11 | // later we can change size of this matrix 12 | m.set_size(6, 6); 13 | } 14 | // initializations 15 | { 16 | // comma operator 17 | dlib::matrix m(3, 3); 18 | m = 1., 2., 3., 4., 5., 6., 7., 8., 9.; 19 | std::cout << "Matix from comma operator\n" << m << std::endl; 20 | 21 | // wrap array 22 | double data[] = {1, 2, 3, 4, 5, 6}; 23 | auto m2 = dlib::mat(data, 2, 3); // create matrix with size 2x3 24 | std::cout << "Matix from array\n" << m2 << std::endl; 25 | 26 | // Matrix elements can be accessed with () operator 27 | m(1, 2) = 300; 28 | std::cout << "Matix element updated\n" << m << std::endl; 29 | 30 | // Also you can initialize matrix with some predefined values 31 | auto a = dlib::identity_matrix(3); 32 | std::cout << "Identity matix \n" << a << std::endl; 33 | 34 | auto b = dlib::ones_matrix(3, 4); 35 | std::cout << "Ones matix \n" << b << std::endl; 36 | 37 | auto c = dlib::randm(3, 4); // matrix with random values with size 3x3 38 | std::cout << "Random matix \n" << c << std::endl; 39 | } 40 | // arithmetic operations 41 | { 42 | dlib::matrix a(2, 2); 43 | a = 1, 1, 1, 1; 44 | dlib::matrix b(2, 2); 45 | b = 2, 2, 2, 2; 46 | 47 | auto c = a + b; 48 | std::cout << "c = a + b \n" << c << std::endl; 49 | 50 | auto e = a * b; // real matrix multiplication 51 | std::cout << "e = a dot b \n" << e << std::endl; 52 | 53 | a += 5; 54 | std::cout << "a += 5 \n" << a << std::endl; 55 | 56 | auto d = dlib::pointwise_multiply(a, b); // element wise multiplication 57 | std::cout << "d = a * b \n" << e << std::endl; 58 | 59 | auto t = dlib::trans(a); // transpose matrix 60 | std::cout << "transposed matrix a \n" << t << std::endl; 61 | } 62 | // partial access 63 | { 64 | dlib::matrix m; 65 | m = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16; 66 | auto sm = 67 | dlib::subm(m, dlib::range(1, 2), 68 | dlib::range(1, 2)); // original matrix can't be updated 69 | std::cout << "Sub matrix \n" << sm << std::endl; 70 | 71 | dlib::set_subm(m, dlib::range(1, 2), dlib::range(1, 2)) = 100; 72 | std::cout << "Updated sub matrix \n" << m << std::endl; 73 | } 74 | // there are no implicit broadcasting in dlib 75 | { 76 | // we can simulate broadcasting with partial access 77 | dlib::matrix v; 78 | v = 10, 10; 79 | dlib::matrix m; 80 | m = 1, 2, 3, 4, 5, 6; 81 | for (int i = 0; i < m.nc(); ++i) { 82 | dlib::set_colm(m, i) += v; 83 | } 84 | std::cout << "Matrix with updated columns \n" << m << std::endl; 85 | } 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /Chapter01/dlib_samples/linreg_dlib.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | float func(float x) { 8 | return 4.f + 0.3f * x; // line coeficients 9 | } 10 | 11 | using SampleType = dlib::matrix; 12 | using KernelType = dlib::linear_kernel; 13 | 14 | int main() { 15 | using namespace dlib; 16 | size_t n = 1000; 17 | std::vector> x(n); 18 | std::vector y(n); 19 | 20 | std::random_device rd; 21 | std::mt19937 re(rd()); 22 | std::uniform_real_distribution dist(-1.5, 1.5); 23 | 24 | // generate data 25 | for (size_t i = 0; i < n; ++i) { 26 | x[i].set_size(1, 1); 27 | x[i](0, 0) = i; 28 | 29 | y[i] = func(i) + dist(re); 30 | } 31 | 32 | // // normalize data 33 | vector_normalizer> normalizer_x; 34 | // let the normalizer learn the mean and standard deviation of the samples 35 | normalizer_x.train(x); 36 | // now normalize each sample 37 | for (size_t i = 0; i < x.size(); ++i) { 38 | x[i] = normalizer_x(x[i]); 39 | } 40 | 41 | krr_trainer trainer; 42 | trainer.set_kernel(KernelType()); 43 | decision_function df = trainer.train(x, y); 44 | 45 | // Generate new data 46 | std::cout << "Original data \n"; 47 | std::vector> new_x(5); 48 | for (size_t i = 0; i < 5; ++i) { 49 | new_x[i].set_size(1, 1); 50 | new_x[i](0, 0) = i; 51 | new_x[i] = normalizer_x(new_x[i]); 52 | std::cout << func(i) << std::endl; 53 | } 54 | 55 | std::cout << "Predictions \n"; 56 | for (auto& v : new_x) { 57 | auto prediction = df(v); 58 | std::cout << prediction << std::endl; 59 | } 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Chapter01/eigen_samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(eigen_samples) 3 | 4 | set(EIGEN_PATH "" CACHE PATH "Path to Eigen") 5 | 6 | if(NOT EIGEN_PATH) 7 | message(FATAL_ERROR "Missing Eigen install path, please specify EIGEN_PATH") 8 | else() 9 | message("Eigen path is ${EIGEN_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra ") 15 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 16 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 17 | 18 | include_directories(${EIGEN_PATH}) 19 | 20 | add_executable(linalg_eigen "linalg_eigen.cc") 21 | add_executable(linreg_eigen "linreg_eigen.cc") 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter01/eigen_samples/linalg_eigen.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef Eigen::Matrix MyMatrix33f; 5 | typedef Eigen::Matrix MyVector3f; 6 | typedef Eigen::Matrix MyMatrix; 7 | 8 | int main() { 9 | { 10 | // declaration 11 | MyMatrix33f a; 12 | MyVector3f v; 13 | MyMatrix m(10, 15); 14 | 15 | // initialization 16 | a = MyMatrix33f::Zero(); 17 | std::cout << "Zero matrix:\n" << a << std::endl; 18 | 19 | a = MyMatrix33f::Identity(); 20 | std::cout << "Identity matrix:\n" << a << std::endl; 21 | 22 | v = MyVector3f::Random(); 23 | std::cout << "Random vector:\n" << v << std::endl; 24 | 25 | a << 1, 2, 3, 4, 5, 6, 7, 8, 9; 26 | std::cout << "Comma initilized matrix:\n" << a << std::endl; 27 | 28 | a(0, 0) = 3; 29 | std::cout << "Matrix with changed element[0][0]:\n" << a << std::endl; 30 | 31 | int data[] = {1, 2, 3, 4}; 32 | Eigen::Map v_map(data, 4); 33 | std::cout << "Row vector mapped to array:\n" << v_map << std::endl; 34 | 35 | std::vector vdata = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 36 | Eigen::Map a_map(vdata.data()); 37 | std::cout << "Matrix mapped to array:\n" << a_map << std::endl; 38 | } 39 | // arithmetic 40 | { 41 | Eigen::Matrix2d a; 42 | a << 1, 2, 3, 4; 43 | Eigen::Matrix2d b; 44 | b << 1, 2, 3, 4; 45 | 46 | // element wise operations 47 | Eigen::Matrix2d result = a.array() * b.array(); 48 | std::cout << "element wise a * b :\n" << result << std::endl; 49 | 50 | result = a.array() / b.array(); 51 | std::cout << "element wise a / b :\n" << result << std::endl; 52 | 53 | a = b.array() * 4; 54 | std::cout << "element wise a = b * 4 :\n" << a << std::endl; 55 | 56 | // matrix operations 57 | result = a + b; 58 | std::cout << "matrices a + b :\n" << result << std::endl; 59 | 60 | a += b; 61 | std::cout << "matrices a += b :\n" << result << std::endl; 62 | 63 | result = a * b; 64 | std::cout << "matrices a * b :\n" << result << std::endl; 65 | } 66 | 67 | // patial access 68 | { 69 | Eigen::MatrixXf m = Eigen::MatrixXf::Random(4, 4); 70 | std::cout << "Random 4x4 matrix :\n" << m << std::endl; 71 | 72 | Eigen::Matrix2f b = 73 | m.block(1, 1, 2, 2); // coping the middle part of matrix 74 | std::cout << "Middle of 4x4 matrix :\n" << b << std::endl; 75 | 76 | m.block(1, 1, 2, 2) *= 0; // change values in original matrix 77 | std::cout << "Modified middle of 4x4 matrix :\n" << m << std::endl; 78 | 79 | m.row(1).array() += 3; 80 | std::cout << "Modified row of 4x4 matrix :\n" << m << std::endl; 81 | 82 | m.col(2).array() /= 4; 83 | std::cout << "Modified col of 4x4 matrix :\n" << m << std::endl; 84 | } 85 | 86 | // broadcasting 87 | { 88 | Eigen::MatrixXf mat = Eigen::MatrixXf::Random(2, 4); 89 | std::cout << "Random 2x4 matrix :\n" << mat << std::endl; 90 | 91 | Eigen::VectorXf v(2); // column vector 92 | v << 100, 100; 93 | mat.colwise() += v; 94 | std::cout << "Sum broadcasted over columns :\n" << mat << std::endl; 95 | } 96 | return 0; 97 | }; 98 | -------------------------------------------------------------------------------- /Chapter01/eigen_samples/linreg_eigen.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | std::pair GenerateData(size_t n) { 10 | std::vector x_data(n); 11 | std::iota(x_data.begin(), x_data.end(), 0); 12 | std::vector y_data(n); 13 | std::iota(y_data.begin(), y_data.end(), 0); 14 | 15 | // mutate data 16 | std::random_device rd; 17 | std::mt19937 re(rd()); 18 | std::uniform_real_distribution dist(-1.5f, 1.5f); 19 | 20 | for (auto& x : x_data) { 21 | x += dist(re); // add noise 22 | } 23 | 24 | for (auto& y : y_data) { 25 | y += dist(re); // add noise 26 | } 27 | 28 | // Make result 29 | Eigen::Map x(x_data.data(), static_cast(n), 1); 30 | Eigen::Map y(y_data.data(), static_cast(n), 1); 31 | 32 | return {x, y}; 33 | } 34 | 35 | int main() { 36 | size_t n = 1000; 37 | // generate training data 38 | Eigen::MatrixXf x1, y; 39 | std::tie(x1, y) = GenerateData(n); 40 | Eigen::MatrixXf x0 = Eigen::MatrixXf::Ones(n, 1); 41 | // setup line coeficients y = b(4) + k(0.3)*x 42 | y.array() *= 0.3f; 43 | y.array() += 4.f; 44 | Eigen::MatrixXf x(n, 2); 45 | x << x0, x1; 46 | 47 | // train estimator 48 | Eigen::LeastSquaresConjugateGradient gd; 49 | gd.setMaxIterations(100); 50 | gd.setTolerance(0.001f); 51 | gd.compute(x); 52 | Eigen::VectorXf b = gd.solve(y); 53 | std::cout << "Estimated parameters vector : " << b << std::endl; 54 | 55 | // normal equations 56 | Eigen::VectorXf b_norm = (x.transpose() * x).ldlt().solve(x.transpose() * y); 57 | std::cout << "Estimated with normal equation parameters vector : " << b_norm 58 | << std::endl; 59 | 60 | // predict 61 | Eigen::MatrixXf new_x(5, 2); 62 | new_x << 1, 1, 1, 2, 1, 3, 1, 4, 1, 5; 63 | auto new_y = new_x.array().rowwise() * b.transpose().array(); 64 | std::cout << "Predicted values : \n" << new_y << std::endl; 65 | 66 | auto new_y_norm = new_x.array().rowwise() * b_norm.transpose().array(); 67 | std::cout << "Predicted(norm) values : \n" << new_y_norm << std::endl; 68 | 69 | return 0; 70 | }; 71 | -------------------------------------------------------------------------------- /Chapter01/sharkml_samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(sharkml-samples) 3 | 4 | set(SHARK_PATH "" CACHE PATH "path to SharkML install dir") 5 | 6 | if(NOT SHARK_PATH) 7 | message(FATAL_ERROR "Missing SharkML install path, please specify SHARK_PATH") 8 | else() 9 | message("SharkML path is ${SHARK_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(Boost_USE_STATIC_LIBS ON) 15 | set(Boost_USE_STATIC_RUNTIME ON) 16 | find_package(Boost REQUIRED serialization) 17 | 18 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 19 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 20 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 21 | 22 | include_directories(${SHARK_PATH}/include) 23 | link_directories(${SHARK_PATH}/lib) 24 | link_directories(${SHARK_PATH}/lib64) 25 | 26 | add_executable(linalg-shark "linalg_shark.cc") 27 | target_link_libraries(linalg-shark shark cblas) 28 | 29 | add_executable(linreg-shark "linreg_shark.cc") 30 | target_link_libraries(linreg-shark shark cblas ${Boost_LIBRARIES} ) 31 | 32 | -------------------------------------------------------------------------------- /Chapter01/sharkml_samples/linalg_shark.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | template 7 | void vec_sum( 8 | remora::vector_expression const& v) { 9 | auto const& elem_result = eval_block(v); 10 | } 11 | int main() { 12 | // definitions 13 | { 14 | // dynamically sized array dense vector. 15 | remora::vector b(100, 16 | 1.0); // vector of size 100 and filled with 1.0 17 | 18 | // dynamically sized array dense matrix 19 | remora::matrix C(2, 2); // 2x2 matrix 20 | } 21 | // initializations 22 | { 23 | // fill with one value 24 | remora::matrix m_zero(2, 2, 0.0f); 25 | std::cout << "Zero matrix \n" << m_zero << std::endl; 26 | 27 | // initializer list 28 | remora::matrix m_ones{{1, 1}, {1, 1}}; 29 | std::cout << "Initializer list matrix \n" << m_ones << std::endl; 30 | 31 | // wrap c++ array 32 | std::vector data{1, 2, 3, 4}; 33 | auto m = remora::dense_matrix_adaptor(data.data(), 2, 2); 34 | std::cout << "Wrapped array matrix \n" << m << std::endl; 35 | auto v = remora::dense_vector_adaptor(data.data(), 4); 36 | std::cout << "Wrapped array vector \n" << v << std::endl; 37 | 38 | // Direct access to container elements 39 | m(0, 0) = 3.14f; 40 | std::cout << "Changed matrix element\n" << m << std::endl; 41 | v(0) = 3.14f; 42 | std::cout << "Changed vector element\n" << v << std::endl; 43 | } 44 | // Arithmetic operations examples 45 | { 46 | remora::matrix a{{1, 1}, {1, 1}}; 47 | remora::matrix b{{2, 2}, {2, 2}}; 48 | 49 | remora::matrix c = a + b; 50 | std::cout << "c = a + b\n" << c << std::endl; 51 | 52 | a -= b; 53 | std::cout << "a -= b\n" << a << std::endl; 54 | 55 | c = remora::prod(a, b); 56 | std::cout << "c = a dot b\n" << c << std::endl; 57 | 58 | c = a % b; // also dot product 59 | std::cout << "c = a % b\n" << c << std::endl; 60 | 61 | c = a * b; // element wise product 62 | std::cout << "c = a *b\n" << c << std::endl; 63 | 64 | c = a + 5; 65 | std::cout << "c = a + 5\n" << c << std::endl; 66 | } 67 | // partial access 68 | { 69 | remora::matrix m{ 70 | {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; 71 | auto r = remora::rows(m, 0, 2); 72 | std::cout << "Matrix rows 0,2\n" << r << std::endl; 73 | 74 | auto sr = remora::subrange(m, 1, 3, 1, 3); 75 | std::cout << "Matrix subrange\n" << sr << std::endl; 76 | sr *= 67; 77 | std::cout << "Matrix with updated subrange\n" << m << std::endl; 78 | } 79 | // broadcasting is not supported directly 80 | { 81 | // Reductions 82 | remora::matrix m{{1, 2, 3, 4}, {5, 6, 7, 8}}; 83 | remora::vector v{10, 10}; 84 | auto cols = remora::as_columns(m); 85 | std::cout << "Sum reduction for columns\n" 86 | << remora::sum(cols) << std::endl; 87 | 88 | // Update matrix rows 89 | for (size_t i = 0; i < m.size2(); ++i) { 90 | remora::column(m, i) += v; 91 | } 92 | std::cout << "Updated rows\n" << m << std::endl; 93 | } 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /Chapter01/sharkml_samples/linreg_shark.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace shark; 6 | 7 | std::pair, Data> GenerateData(size_t n) { 8 | std::vector x_data(n); 9 | std::vector y_data(n); 10 | 11 | std::random_device rd; 12 | std::mt19937 re(rd()); 13 | std::uniform_real_distribution dist(-1.5, 1.5); 14 | 15 | // generate data 16 | RealVector x_v(1); // it's a typdef to remora::vector 17 | RealVector y_v(1); 18 | for (size_t i = 0; i < n; ++i) { 19 | x_v(0) = i + dist(re); 20 | x_data[i] = x_v; 21 | 22 | y_v(0) = i + dist(re); // add noise 23 | y_v(0) = 4. + 0.3 * y_v(0); // line coeficients 24 | y_data[i] = y_v; 25 | } 26 | 27 | return {createDataFromRange(x_data), createDataFromRange(y_data)}; 28 | } 29 | 30 | int main() { 31 | Data x; 32 | Data y; 33 | std::tie(x, y) = GenerateData(1000); 34 | RegressionDataset data(x, y); 35 | LinearModel<> model; 36 | LinearRegression trainer; 37 | trainer.train(model, data); 38 | 39 | // We can get calculated parameters vector 40 | auto b = model.parameterVector(); 41 | std::cout << "Estimated parameters :\n" << b << std::endl; 42 | 43 | // Also we can calculate value of Squared Error 44 | SquaredLoss<> loss; 45 | Data prediction = model(x); 46 | auto se = loss(y, prediction); 47 | std::cout << "Squared Error :\n" << se << std::endl; 48 | 49 | // For new X data you can predict new Y 50 | std::vector new_x_data; 51 | new_x_data.push_back({1}); 52 | new_x_data.push_back({2}); 53 | new_x_data.push_back({3}); 54 | prediction = model(createDataFromRange(new_x_data)); 55 | std::cout << "Predictions \n" << prediction << std::endl; 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Chapter01/shogun_samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(shogun-samples) 3 | 4 | set(SHOGUN_PATH "" CACHE PATH "path to shogun install dir") 5 | 6 | if(NOT DEFINED SHOGUN_PATH) 7 | message(FATAL_ERROR "Missing Shogun install path, please specify SHOGUN_PATH") 8 | else() 9 | message("Shogun path is ${SHOGUN_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | find_package(HDF5 REQUIRED) 15 | 16 | set(CMAKE_CXX_FLAGS "-std=c++14 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 17 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 18 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 19 | 20 | include_directories(${SHOGUN_PATH}/include) 21 | link_directories(${SHOGUN_PATH}/lib) 22 | 23 | add_executable(linreg_shogun "linreg_shogun.cc") 24 | target_link_libraries(linreg_shogun shogun) 25 | target_link_libraries(linreg_shogun ${HDF5_LIBRARIES} json-c ${PROTOBUF_LIBRARIES} cblas lapack) 26 | 27 | -------------------------------------------------------------------------------- /Chapter01/shogun_samples/linreg_shogun.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace shogun; 12 | 13 | int main(int, char*[]) { 14 | shogun::init_shogun_with_defaults(); 15 | shogun::sg_io->set_loglevel(shogun::MSG_DEBUG); 16 | 17 | const int32_t n = 1000; 18 | SGMatrix x_values(1, n); 19 | SGVector y_values(n); 20 | 21 | std::random_device rd; 22 | std::mt19937 re(rd()); 23 | std::uniform_real_distribution dist(-1.5, 1.5); 24 | 25 | // generate data 26 | for (int32_t i = 0; i < n; ++i) { 27 | x_values.set_element(i + dist(re), 0, i); 28 | 29 | auto y_val = i + dist(re); // add noise 30 | y_val = 4. + 0.3 * y_val; // line coeficients 31 | y_values.set_element(y_val, i); 32 | } 33 | 34 | auto x = some>(x_values); 35 | auto y = some(y_values); 36 | 37 | float64_t tau_regularization = 0.0001; 38 | auto lr = some(tau_regularization, nullptr, nullptr); 39 | lr->set_labels(y); 40 | if (!lr->train(x)) { 41 | std::cerr << "training failed\n"; 42 | } 43 | 44 | // e can get calculated bias term 45 | auto bias = lr->get_bias(); 46 | std::cout << "Bias = " << bias << "\n"; 47 | 48 | // We can get calculated parameters vector 49 | auto weights = lr->get_w(); 50 | std::cout << "Weights = \n"; 51 | for (int32_t i = 0; i < weights.size(); ++i) { 52 | std::cout << weights[i] << "\n"; 53 | } 54 | 55 | // Also we can calculate value of Mean Squared Error: 56 | auto y_predict = lr->apply_regression(x); 57 | auto eval = some(); 58 | auto mse = eval->evaluate(y_predict, y); 59 | std::cout << "MSE = " << mse << std::endl; 60 | 61 | // For new X data you can predict new Y 62 | SGMatrix new_x_values(1, 5); 63 | new_x_values.set_element(1, 0, 0); 64 | new_x_values.set_element(2, 0, 1); 65 | new_x_values.set_element(3, 0, 2); 66 | new_x_values.set_element(4, 0, 3); 67 | new_x_values.set_element(5, 0, 4); 68 | auto new_x = some>(new_x_values); 69 | y_predict = lr->apply_regression(new_x); 70 | std::cout << "Predicted values\n" << y_predict->to_string() << std::endl; 71 | 72 | shogun::exit_shogun(); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Chapter01/xtensor_samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(xtensor_samples) 3 | 4 | set(XTENSOR_PATH "" CACHE PATH "Path to xtensor include dir") 5 | 6 | if(NOT XTENSOR_PATH) 7 | message(FATAL_ERROR "Missing xtensor install path, please specify XTENSOR_PATH") 8 | else() 9 | message("xtensor path is ${XTENSOR_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra ") 15 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 16 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 17 | 18 | include_directories(${XTENSOR_PATH}) 19 | 20 | add_executable(linalg_xtensor "linalg_xtensor.cc") 21 | target_link_libraries(linalg_xtensor cblas) 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter01/xtensor_samples/linalg_xtensor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | int main() { 13 | { 14 | // declaration of dynamically sized array 15 | { 16 | std::vector shape = {3, 2, 4}; 17 | xt::xarray a(shape); 18 | } 19 | // declaration of dynamically sized tensor with fixed dimmentions number 20 | { 21 | std::array shape = {3, 2, 4}; 22 | xt::xtensor a(shape); 23 | } 24 | 25 | // declaration of tensor with shape fixed at compile time. 26 | { xt::xtensor_fixed> a; } 27 | 28 | // Initialization of xtensor arrays can be done with C++ initializer lists: 29 | { 30 | xt::xarray arr1{{1.0, 2.0, 3.0}, 31 | {2.0, 5.0, 7.0}, 32 | {2.0, 5.0, 7.0}}; // initialize a 3x3 array 33 | std::cout << "Tensor from initializer list :\n" << arr1 << std::endl; 34 | } 35 | // Special types of initializers 36 | { 37 | std::vector shape = {2, 2}; 38 | std::cout << "Ones matrix :\n" << xt::ones(shape) << std::endl; 39 | std::cout << "Zero matrix :\n" << xt::zeros(shape) << std::endl; 40 | std::cout << "Matrix with ones on the diagonal:\n" 41 | << xt::eye(shape) << std::endl; 42 | } 43 | // Mapping c++ array to tensors 44 | { 45 | std::vector data{1, 2, 3, 4}; 46 | std::vector shape{2, 2}; 47 | auto data_x = xt::adapt(data, shape); 48 | std::cout << "Matrix from vector :\n" << data_x << std::endl; 49 | } 50 | // Element access 51 | { 52 | std::vector shape = {3, 2, 4}; 53 | xt::xarray a = xt::ones(shape); 54 | a(2, 1, 3) = 3.14f; 55 | std::cout << "Updated element :\n" << a << std::endl; 56 | } 57 | // Arithmetic operations examples 58 | { 59 | xt::xarray a = xt::random::rand({2, 2}); 60 | xt::xarray b = xt::random::rand({2, 2}); 61 | 62 | std::cout << "A: \n" << a << std::endl; 63 | std::cout << "B: \n" << b << std::endl; 64 | 65 | xt::xarray c = a + b; 66 | std::cout << "c = a + b \n" << c << std::endl; 67 | a -= b; 68 | std::cout << "a -= b \n" << a << std::endl; 69 | c = xt::linalg::dot(a, b); 70 | std::cout << "a dot b \n" << c << std::endl; 71 | c = a + 5; 72 | std::cout << "c = a + 5 \n" << c << std::endl; 73 | c = a * b; 74 | std::cout << "c = a * b \n" << c << std::endl; 75 | } 76 | // Partial access to xtensor containers 77 | { 78 | xt::xarray a{ 79 | {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; 80 | auto b = xt::view(a, xt::range(1, 3), xt::range(1, 3)); 81 | std::cout << "Partial view on a \n" << b << std::endl; 82 | } 83 | // Broadcasting 84 | { 85 | auto a = xt::xarray({{1, 2}, {3, 4}}); 86 | auto b = xt::xarray({10, 20}); 87 | b.reshape({2, 1}); 88 | std::cout << "A: \n" << a << std::endl; 89 | std::cout << "B: \n" << b << std::endl; 90 | auto c = a + b; 91 | std::cout << "Columns broadcasting: \n" << c << std::endl; 92 | } 93 | } 94 | return 0; 95 | }; 96 | -------------------------------------------------------------------------------- /Chapter02/csv/cpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(csv_sample) 3 | 4 | set(CSV_LIB_PATH "" CACHE PATH "Path to csv library include dir") 5 | set(EIGEN_LIB_PATH "" CACHE PATH "Path to Eigen library include dir") 6 | 7 | if (NOT CSV_LIB_PATH) 8 | message(FATAL_ERROR "Missigng CSV lib install path, please specify CSV_LIB_PATH") 9 | else() 10 | message("CSV lib path is ${CSV_LIB_PATH}") 11 | endif() 12 | 13 | if (NOT EIGEN_LIB_PATH) 14 | message(FATAL_ERROR "Missing Eigen install path, please specify EIGEN_LIB_PATH") 15 | else() 16 | message("Eigen path is ${EIGEN_LIB_PATH}") 17 | endif() 18 | 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra ") 23 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 24 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 25 | 26 | set(requiredlibs "stdc++fs") 27 | list(APPEND requiredlibs "stdc++") 28 | 29 | include_directories(${CSV_LIB_PATH}) 30 | include_directories(${EIGEN_LIB_PATH}) 31 | 32 | add_executable(csv_sample "csv.cc") 33 | target_link_libraries(csv_sample ${requiredlibs}) 34 | 35 | -------------------------------------------------------------------------------- /Chapter02/csv/cpp/csv.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace fs = std::experimental::filesystem; 9 | 10 | template 11 | bool read_row_help(std::index_sequence, T& row, R& r) { 12 | return r.read_row(std::get(row)...); 13 | } 14 | 15 | template 16 | void fill_values(std::index_sequence, 17 | T& row, 18 | std::vector& data) { 19 | data.insert(data.end(), {std::get(row)...}); 20 | } 21 | 22 | int main(int argc, char** argv) { 23 | if (argc > 1) { 24 | auto file_path = fs::path(argv[1]); 25 | if (fs::exists(file_path)) { 26 | const uint32_t columns_num = 5; 27 | io::CSVReader csv_reader(file_path); 28 | 29 | std::vector categorical_column; 30 | std::vector values; 31 | using RowType = std::tuple; 32 | RowType row; 33 | 34 | uint32_t rows_num = 0; 35 | try { 36 | bool done = false; 37 | while (!done) { 38 | done = !read_row_help( 39 | std::make_index_sequence::value>{}, row, 40 | csv_reader); 41 | if (!done) { 42 | categorical_column.push_back(std::get<4>(row)); 43 | fill_values(std::make_index_sequence{}, row, 44 | values); 45 | ++rows_num; 46 | } 47 | } 48 | } catch (const io::error::no_digit& err) { 49 | // ignore bad formated samples 50 | std::cerr << err.what() << std::endl; 51 | } 52 | 53 | auto x_data = Eigen::Map>( 55 | values.data(), rows_num, columns_num - 1); 56 | 57 | std::cout << x_data << std::endl; 58 | 59 | // Feature-scaling(Normalization): 60 | // Standardization - zero mean + 1 std 61 | Eigen::Array std_dev = 62 | ((x_data.rowwise() - x_data.colwise().mean()) 63 | .array() 64 | .square() 65 | .colwise() 66 | .sum() / 67 | (x_data.rows() - 1)) 68 | .sqrt(); 69 | 70 | Eigen::Matrix x_data_std = 71 | (x_data.rowwise() - x_data.colwise().mean()).array().rowwise() / 72 | std_dev; 73 | 74 | std::cout << x_data_std << std::endl; 75 | 76 | // Min-Max normalization 77 | Eigen::Matrix x_data_min_max = 78 | (x_data.rowwise() - x_data.colwise().minCoeff()).array().rowwise() / 79 | (x_data.colwise().maxCoeff() - x_data.colwise().minCoeff()).array(); 80 | 81 | std::cout << x_data_min_max << std::endl; 82 | 83 | // Average normalization 84 | Eigen::Matrix x_data_avg = 85 | (x_data.rowwise() - x_data.colwise().mean()).array().rowwise() / 86 | (x_data.colwise().maxCoeff() - x_data.colwise().minCoeff()).array(); 87 | 88 | std::cout << x_data_avg << std::endl; 89 | 90 | } else { 91 | std::cout << "File path is incorrect " << file_path << "\n"; 92 | } 93 | } else { 94 | std::cout << "Please provide a path to a dataset file\n"; 95 | } 96 | 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /Chapter02/csv/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-csv-sample) 3 | 4 | set(DLIB_PATH "" CACHE PATH "path to dlib install dir") 5 | 6 | if (NOT DLIB_PATH) 7 | message(FATAL_ERROR "Missigng Dlib install path, please specify DLIB_PATH") 8 | else() 9 | message("Dlib path is ${DLIB_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(requiredlibs "cblas") 15 | list(APPEND requiredlibs "lapack") 16 | list(APPEND requiredlibs "stdc++") 17 | list(APPEND requiredlibs "stdc++fs") 18 | 19 | find_package(CUDA) 20 | if (CUDA_FOUND) 21 | list(APPEND requiredlibs ${CUDA_LIBRARIES}) 22 | list(APPEND requiredlibs ${CUDA_CUBLAS_LIBRARIES}) 23 | list(APPEND requiredlibs ${CUDA_cusolver_LIBRARY}) 24 | list(APPEND requiredlibs ${CUDA_curand_LIBRARY}) 25 | 26 | get_filename_component(cudnn_hint_path "${CUDA_CUBLAS_LIBRARIES}" PATH) 27 | find_library(cudnn cudnn 28 | HINTS ${cudnn_hint_path} ENV CUDNN_LIBRARY_DIR ENV CUDNN_HOME 29 | PATHS /usr/local /usr/local/cuda ENV LD_LIBRARY_PATH 30 | PATH_SUFFIXES lib64 lib x64 31 | ) 32 | message("CUDNN found ${cudnn}") 33 | list(APPEND requiredlibs ${cudnn}) 34 | endif() 35 | 36 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 37 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 38 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 39 | 40 | include_directories(${DLIB_PATH}/include) 41 | link_directories(${DLIB_PATH}/lib) 42 | link_directories(${DLIB_PATH}/lib64) 43 | 44 | add_executable(csv-dlib "csv_dlib.cc") 45 | target_link_libraries(csv-dlib optimized dlib debug dlibd) 46 | target_link_libraries(csv-dlib ${requiredlibs}) 47 | 48 | -------------------------------------------------------------------------------- /Chapter02/csv/dlib/csv_dlib.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | namespace fs = std::experimental::filesystem; 10 | 11 | int main(int argc, char** argv) { 12 | using namespace dlib; 13 | if (argc > 1) { 14 | if (fs::exists(argv[1])) { 15 | std::stringstream str_stream; 16 | { 17 | // replace categorial values with numeric ones 18 | std::ifstream data_stream(argv[1]); 19 | std::string data_string((std::istreambuf_iterator(data_stream)), 20 | std::istreambuf_iterator()); 21 | 22 | // replace string labels, because SharkML parssr can't handle strings 23 | data_string = 24 | std::regex_replace(data_string, std::regex("Iris-setosa"), "1"); 25 | data_string = 26 | std::regex_replace(data_string, std::regex("Iris-versicolor"), "2"); 27 | data_string = 28 | std::regex_replace(data_string, std::regex("Iris-virginica"), "3"); 29 | 30 | str_stream << data_string; 31 | } 32 | 33 | matrix data; 34 | str_stream >> data; 35 | 36 | std::cout << data << std::endl; 37 | 38 | matrix x_data = subm(data, 0, 0, data.nr(), data.nc() - 1); 39 | 40 | std::vector> samples; 41 | for (int r = 0; r < x_data.nr(); ++r) { 42 | samples.push_back(rowm(x_data, r)); 43 | } 44 | 45 | // Normalization 46 | // Standardization 47 | matrix m(mean(mat(samples))); 48 | matrix sd(reciprocal(stddev(mat(samples)))); 49 | for (size_t i = 0; i < samples.size(); ++i) 50 | samples[i] = pointwise_multiply(samples[i] - m, sd); 51 | std::cout << mat(samples) << std::endl; 52 | 53 | // Another approach 54 | // vector_normalizer> normalizer; 55 | // samples normalizer.train(samples); 56 | // samples = normalizer(samples); 57 | } else { 58 | std::cerr << "Invalid file path " << argv[1] << std::endl; 59 | } 60 | } else { 61 | std::cerr << "Please provide a path to a dataset\n"; 62 | } 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Chapter02/csv/sharkml/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(sharkml-csv-sample) 3 | 4 | set(SHARK_PATH "" CACHE PATH "path to SharkML install dir") 5 | 6 | if(NOT SHARK_PATH) 7 | message(FATAL_ERROR "Missing SharkML install path, please specify SHARK_PATH") 8 | else() 9 | message("SharkML path is ${SHARK_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | find_package(Boost REQUIRED serialization) 15 | 16 | set(CMAKE_CXX_FLAGS "-msse3 -fopenmp -Wall -Wextra") 17 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 18 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 19 | 20 | include_directories(${SHARK_PATH}/include) 21 | link_directories(${SHARK_PATH}/lib) 22 | link_directories(${SHARK_PATH}/lib64) 23 | 24 | set(requiredlibs "stdc++fs") 25 | list(APPEND requiredlibs "stdc++") 26 | list(APPEND requiredlibs shark cblas) 27 | 28 | add_executable(csv_sample csv_shark.cc) 29 | target_link_libraries(csv_sample ${Boost_LIBRARIES} ${requiredlibs}) 30 | 31 | -------------------------------------------------------------------------------- /Chapter02/csv/sharkml/csv_shark.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | namespace fs = std::experimental::filesystem; 10 | 11 | using namespace shark; 12 | 13 | int main(int argc, char** argv) { 14 | try { 15 | if (argc > 1) { 16 | if (fs::exists(argv[1])) { 17 | // we need to preprocess dataset because SharkML fails to load csv with 18 | // string values 19 | std::ifstream data_stream(argv[1]); 20 | std::string data_string((std::istreambuf_iterator(data_stream)), 21 | std::istreambuf_iterator()); 22 | 23 | // replace string labels, because SharkML parssr can't handle strings 24 | data_string = 25 | std::regex_replace(data_string, std::regex("Iris-setosa"), "1"); 26 | data_string = 27 | std::regex_replace(data_string, std::regex("Iris-versicolor"), "2"); 28 | data_string = 29 | std::regex_replace(data_string, std::regex("Iris-virginica"), "3"); 30 | 31 | ClassificationDataset dataset; 32 | csvStringToData(dataset, data_string, LAST_COLUMN); 33 | dataset.shuffle(); 34 | std::size_t classes = numberOfClasses(dataset); 35 | std::cout << "Number of classes " << classes << std::endl; 36 | std::vector sizes = classSizes(dataset); 37 | std::cout << "Class size: " << std::endl; 38 | for (auto cs : sizes) { 39 | std::cout << cs << std::endl; 40 | } 41 | std::size_t dim = inputDimension(dataset); 42 | std::cout << "Input dimension " << dim << std::endl; 43 | 44 | // normalization 45 | Normalizer normalizer; 46 | NormalizeComponentsUnitVariance normalizingTrainer( 47 | /*removeMean*/ true); 48 | normalizingTrainer.train(normalizer, dataset.inputs()); 49 | dataset = transformInputs(dataset, normalizer); 50 | std::cout << dataset << std::endl; 51 | 52 | } else { 53 | std::cerr << "Invalid file path " << argv[1] << "\n"; 54 | } 55 | } else { 56 | std::cerr << "Please specify path to the dataset\n"; 57 | } 58 | } catch (const std::exception& err) { 59 | std::cerr << err.what() << std::endl; 60 | } 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Chapter02/csv/shogun/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(shogun-csv-sample) 3 | 4 | set(SHOGUN_PATH "" CACHE PATH "path to shogun install dir") 5 | 6 | if(NOT DEFINED SHOGUN_PATH) 7 | message(FATAL_ERROR "Missing Shogun install path, please specify SHOGUN_PATH") 8 | else() 9 | message("Shogun path is ${SHOGUN_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(CMAKE_CXX_FLAGS "-std=c++14 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 15 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 16 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 17 | 18 | 19 | set(requiredlibs "stdc++fs") 20 | list(APPEND requiredlibs "stdc++") 21 | list(APPEND requiredlibs json-c ${PROTOBUF_LIBRARIES} cblas lapack) 22 | 23 | include_directories(${SHOGUN_PATH}/include) 24 | link_directories(${SHOGUN_PATH}/lib) 25 | 26 | add_executable(csv_shogun "csv_shogun.cc") 27 | target_link_libraries(csv_shogun shogun ${requiredlibs}) 28 | 29 | -------------------------------------------------------------------------------- /Chapter02/csv/shogun/csv_shogun.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | namespace fs = std::experimental::filesystem; 17 | 18 | using namespace shogun; 19 | using DataType = float64_t; 20 | using Matrix = shogun::SGMatrix; 21 | 22 | int main(int argc, char** argv) { 23 | shogun::init_shogun_with_defaults(); 24 | if (argc > 1 && fs::exists(argv[1])) { 25 | // we need to convert label to numbers to read whole file with shogun 26 | // functions 27 | { 28 | std::ifstream data_stream(argv[1]); 29 | std::string data_string((std::istreambuf_iterator(data_stream)), 30 | std::istreambuf_iterator()); 31 | 32 | // replace string labels, because SharkML parssr can't handle strings 33 | data_string = 34 | std::regex_replace(data_string, std::regex("Iris-setosa"), "1"); 35 | data_string = 36 | std::regex_replace(data_string, std::regex("Iris-versicolor"), "2"); 37 | data_string = 38 | std::regex_replace(data_string, std::regex("Iris-virginica"), "3"); 39 | std::ofstream out_stream("iris_fix.csv"); 40 | out_stream << data_string; 41 | } 42 | 43 | auto csv_file = shogun::some("iris_fix.csv"); 44 | Matrix data; 45 | data.load(csv_file); 46 | 47 | // Exclude classification info from data 48 | // Shogun csv loader loads matrixes in column major order 49 | Matrix::transpose_matrix(data.matrix, data.num_rows, data.num_cols); 50 | Matrix inputs = data.submatrix(0, data.num_cols - 1); // make a view 51 | inputs = inputs.clone(); // copy exact data 52 | Matrix outputs = data.submatrix(4, 5); // make a view 53 | outputs = outputs.clone(); // copy exact data 54 | // Transpose back because shogun algorithms expect that samples are in 55 | // colums 56 | Matrix::transpose_matrix(inputs.matrix, inputs.num_rows, inputs.num_cols); 57 | 58 | // create a dataset 59 | auto features = shogun::some>(inputs); 60 | 61 | std::cout << "samples num = " << features->get_num_vectors() << "\n" 62 | << "features num = " << features->get_num_features() << std::endl; 63 | 64 | auto features_matrix = features->get_feature_matrix(); 65 | // Show first 5 samples 66 | for (int i = 0; i < 5; ++i) { 67 | std::cout << "Sample idx " << i << " "; 68 | features_matrix.get_column(i).display_vector(); 69 | } 70 | 71 | auto labels = 72 | shogun::wrap(new shogun::CMulticlassLabels(outputs.get_column(0))); 73 | 74 | std::cout << "labels num = " << labels->get_num_labels() << std::endl; 75 | 76 | std::cout << "Label idx 0 = " << labels->get_label(0) << std::endl; 77 | std::cout << "Label idx 50 = " << labels->get_label(50) << std::endl; 78 | std::cout << "Label idx 100 = " << labels->get_label(100) << std::endl; 79 | 80 | // feature scaling 81 | auto scaler = shogun::wrap(new shogun::CRescaleFeatures()); 82 | scaler->fit(features); 83 | scaler->transform(features); 84 | // Show rescaled samples 85 | for (int i = 0; i < 5; ++i) { 86 | std::cout << "Sample idx " << i << " "; 87 | features_matrix.get_column(i).display_vector(); 88 | } 89 | } 90 | 91 | shogun::exit_shogun(); 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /Chapter02/hdf5/cpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(hdf5_sample) 3 | 4 | find_package(HDF5 REQUIRED) 5 | 6 | set(HDF5_LIB_PATH "" CACHE PATH "Path to hdf5 library include dir") 7 | set(JSON_LIB_PATH "" CACHE PATH "Path to json library include dir") 8 | 9 | if (NOT HIGHFIVE_LIB_PATH) 10 | message(FATAL_ERROR "Missigng HIGHFIVE lib install path, please specify HIGHFIVE_LIB_PATH") 11 | else() 12 | message("HIGHFIVE lib path is ${HIGHFIVE_LIB_PATH}") 13 | endif() 14 | 15 | if (NOT JSON_LIB_PATH) 16 | message(FATAL_ERROR "Missing Json lib install path, please specify JSON_LIB_PATH") 17 | else() 18 | message("Json lib path is ${JSON_LIB_PATH}") 19 | endif() 20 | 21 | set(CMAKE_VERBOSE_MAKEFILE ON) 22 | 23 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra ") 24 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 25 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 26 | 27 | set(requiredlibs "stdc++fs") 28 | list(APPEND requiredlibs "stdc++") 29 | list(APPEND requiredlibs ${HDF5_LIBRARIES}) 30 | 31 | include_directories(${HDF5_INCLUDE_DIR}) 32 | include_directories(${HIGHFIVE_LIB_PATH}) 33 | include_directories(${JSON_LIB_PATH}) 34 | 35 | set(SOURCES hdf5.cc 36 | ../../json/cpp/paper.h 37 | ../../json/cpp/review.h 38 | ../../json/cpp/reviewsreader.h 39 | ../../json/cpp/reviewsreader.cpp) 40 | 41 | add_executable(hdf5_sample ${SOURCES}) 42 | target_link_libraries(hdf5_sample ${requiredlibs}) 43 | 44 | -------------------------------------------------------------------------------- /Chapter02/hdf5/cpp/hdf5.cc: -------------------------------------------------------------------------------- 1 | #include "../../json/cpp/reviewsreader.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | const std::string file_name("reviews.h5"); 8 | 9 | // convert datset from json to hdf5 format and read from it 10 | 11 | int main(int argc, char** argv) { 12 | try { 13 | if (argc > 1) { 14 | auto papers = ReadPapersReviews(argv[1]); 15 | 16 | // write dataset 17 | { 18 | HighFive::File file(file_name, HighFive::File::ReadWrite | 19 | HighFive::File::Create | 20 | HighFive::File::Truncate); 21 | 22 | auto papers_group = file.createGroup("papers"); 23 | for (const auto& paper : papers) { 24 | auto paper_group = 25 | papers_group.createGroup("paper_" + std::to_string(paper.id)); 26 | std::vector id = {paper.id}; 27 | auto id_attr = paper_group.createAttribute( 28 | "id", HighFive::DataSpace::From(id)); 29 | 30 | id_attr.write(id); 31 | auto dec_attr = paper_group.createAttribute( 32 | "preliminary_decision", 33 | HighFive::DataSpace::From(paper.preliminary_decision)); 34 | dec_attr.write(paper.preliminary_decision); 35 | auto reviews_group = paper_group.createGroup("reviews"); 36 | 37 | std::vector dims = {3}; 38 | std::vector values(3); 39 | for (const auto& r : paper.reviews) { 40 | auto dataset = reviews_group.createDataSet( 41 | std::to_string(r.id), HighFive::DataSpace(dims)); 42 | values[0] = std::stoi(r.confidence); 43 | values[1] = std::stoi(r.evaluation); 44 | values[2] = std::stoi(r.orientation); 45 | dataset.write(values); 46 | } 47 | } 48 | } 49 | // read dataset 50 | { 51 | HighFive::File file(file_name, HighFive::File::ReadOnly); 52 | auto papers_group = file.getGroup("papers"); 53 | auto papers_names = papers_group.listObjectNames(); 54 | for (const auto& pname : papers_names) { 55 | auto paper_group = papers_group.getGroup(pname); 56 | std::vector id; 57 | paper_group.getAttribute("id").read(id); 58 | std::cout << id[0]; 59 | 60 | std::string decision; 61 | paper_group.getAttribute("preliminary_decision").read(decision); 62 | std::cout << " " << decision << std::endl; 63 | 64 | auto reviews_group = paper_group.getGroup("reviews"); 65 | auto reviews_names = reviews_group.listObjectNames(); 66 | std::vector values(2); 67 | for (const auto& rname : reviews_names) { 68 | std::cout << "\t review: " << rname << std::endl; 69 | auto dataset = reviews_group.getDataSet(rname); 70 | auto selection = dataset.select( 71 | {1}, {2}); // or use just dataset.read method to get whole data 72 | selection.read(values); 73 | std::cout << "\t\t evaluation: " << values[0] << std::endl; 74 | std::cout << "\t\t orientation: " << values[1] << std::endl; 75 | } 76 | } 77 | } 78 | 79 | } else { 80 | std::cerr << "Please provide path to the dataset \n"; 81 | } 82 | } catch (const std::exception& err) { 83 | std::cerr << err.what() << std::endl; 84 | } 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /Chapter02/img/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-img-sample) 3 | 4 | set(DLIB_PATH "" CACHE PATH "path to dlib install dir") 5 | 6 | if (NOT DLIB_PATH) 7 | message(FATAL_ERROR "Missigng Dlib install path, please specify DLIB_PATH") 8 | else() 9 | message("Dlib path is ${DLIB_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(requiredlibs "cblas") 15 | list(APPEND requiredlibs "lapack") 16 | list(APPEND requiredlibs "stdc++") 17 | list(APPEND requiredlibs "stdc++fs") 18 | 19 | find_package(CUDA) 20 | if (CUDA_FOUND) 21 | list(APPEND requiredlibs ${CUDA_LIBRARIES}) 22 | list(APPEND requiredlibs ${CUDA_CUBLAS_LIBRARIES}) 23 | list(APPEND requiredlibs ${CUDA_cusolver_LIBRARY}) 24 | list(APPEND requiredlibs ${CUDA_curand_LIBRARY}) 25 | 26 | get_filename_component(cudnn_hint_path "${CUDA_CUBLAS_LIBRARIES}" PATH) 27 | find_library(cudnn cudnn 28 | HINTS ${cudnn_hint_path} ENV CUDNN_LIBRARY_DIR ENV CUDNN_HOME 29 | PATHS /usr/local /usr/local/cuda ENV LD_LIBRARY_PATH 30 | PATH_SUFFIXES lib64 lib x64 31 | ) 32 | message("CUDNN found ${cudnn}") 33 | list(APPEND requiredlibs ${cudnn}) 34 | endif() 35 | 36 | list(APPEND requiredlibs X11) 37 | list(APPEND requiredlibs png jpeg) 38 | 39 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 40 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 41 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 42 | 43 | include_directories(${DLIB_PATH}/include) 44 | link_directories(${DLIB_PATH}/lib) 45 | link_directories(${DLIB_PATH}/lib64) 46 | 47 | add_executable(img-dlib "img_dlib.cc") 48 | target_link_libraries(img-dlib optimized dlib debug dlibd) 49 | target_link_libraries(img-dlib ${requiredlibs}) 50 | 51 | -------------------------------------------------------------------------------- /Chapter02/img/opencv/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0 FATAL_ERROR) 2 | project(opencv-example) 3 | 4 | find_package(OpenCV REQUIRED) 5 | 6 | set(CMAKE_VERBOSE_MAKEFILE ON) 7 | set(CMAKE_CXX_STANDARD 17) 8 | 9 | add_compile_options( -fopenmp 10 | -pthread 11 | -msse3 12 | -Wall 13 | -Wextra 14 | -Wno-unused-parameter) 15 | 16 | set(REQUIRED_LIBS "stdc++fs") 17 | list(APPEND REQUIRED_LIBS ${OpenCV_LIBS}) 18 | 19 | add_executable(ocv_sample ocv.cc) 20 | target_link_libraries(ocv_sample ${REQUIRED_LIBS}) 21 | 22 | -------------------------------------------------------------------------------- /Chapter02/json/cpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(json_sample) 3 | 4 | set(JSON_LIB_PATH "" CACHE PATH "Path to json library include dir") 5 | set(EIGEN_LIB_PATH "" CACHE PATH "Path to Eigen library include dir") 6 | 7 | if (NOT JSON_LIB_PATH) 8 | message(FATAL_ERROR "Missigng Json lib install path, please specify JSON_LIB_PATH") 9 | else() 10 | message("Json lib path is ${JSON_LIB_PATH}") 11 | endif() 12 | 13 | if (NOT EIGEN_LIB_PATH) 14 | message(FATAL_ERROR "Missing Eigen install path, please specify EIGEN_LIB_PATH") 15 | else() 16 | message("Eigen path is ${EIGEN_LIB_PATH}") 17 | endif() 18 | 19 | set(CMAKE_VERBOSE_MAKEFILE ON) 20 | 21 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra ") 22 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 23 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 24 | 25 | set(requiredlibs "stdc++fs") 26 | list(APPEND requiredlibs "stdc++") 27 | 28 | include_directories(${JSON_LIB_PATH}) 29 | include_directories(${EIGEN_LIB_PATH}) 30 | 31 | set(SOURCES json.cc 32 | review.h 33 | paper.h 34 | reviewsreader.h 35 | reviewsreader.cpp) 36 | 37 | add_executable(json_sample ${SOURCES}) 38 | target_link_libraries(json_sample ${requiredlibs}) 39 | 40 | -------------------------------------------------------------------------------- /Chapter02/json/cpp/json.cc: -------------------------------------------------------------------------------- 1 | #include "reviewsreader.h" 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | namespace fs = std::experimental::filesystem; 10 | 11 | int main(int argc, char** argv) { 12 | if (argc > 1) { 13 | auto file_path = fs::path(argv[1]); 14 | if (fs::exists(file_path)) { 15 | auto papers = ReadPapersReviews(file_path); 16 | // create matrices 17 | Eigen::MatrixXi x_data(papers.size(), 3); 18 | Eigen::MatrixXi y_data(papers.size(), 1); 19 | Eigen::Index i = 0; 20 | for (const auto& p : papers) { 21 | if (p.preliminary_decision == "accept") 22 | y_data(i, 0) = 1; 23 | else 24 | y_data(i, 0) = 0; 25 | 26 | if (!p.reviews.empty()) { 27 | int64_t confidence_avg = 0; 28 | int64_t evaluation_avg = 0; 29 | int64_t orientation_avg = 0; 30 | for (const auto& r : p.reviews) { 31 | confidence_avg += std::stoi(r.confidence); 32 | evaluation_avg += std::stoi(r.evaluation); 33 | orientation_avg += std::stoi(r.orientation); 34 | } 35 | int64_t reviews_num = static_cast(p.reviews.size()); 36 | x_data(i, 0) = static_cast(confidence_avg / reviews_num); 37 | x_data(i, 1) = static_cast(evaluation_avg / reviews_num); 38 | x_data(i, 2) = static_cast(orientation_avg / reviews_num); 39 | } 40 | ++i; 41 | } 42 | std::cout << x_data << std::endl; 43 | std::cout << y_data << std::endl; 44 | 45 | } else { 46 | std::cout << "File path is incorrect " << file_path << "\n"; 47 | } 48 | } else { 49 | std::cout << "Please provide a path to a dataset file\n"; 50 | } 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Chapter02/json/cpp/paper.h: -------------------------------------------------------------------------------- 1 | #ifndef PAPER_H 2 | #define PAPER_H 3 | 4 | #include "review.h" 5 | 6 | #include 7 | 8 | struct Paper { 9 | uint32_t id{0}; 10 | std::string preliminary_decision; 11 | std::vector reviews; 12 | }; 13 | 14 | using Papers = std::vector; 15 | 16 | #endif // PAPER_H 17 | -------------------------------------------------------------------------------- /Chapter02/json/cpp/review.h: -------------------------------------------------------------------------------- 1 | #ifndef REVIEW_H 2 | #define REVIEW_H 3 | 4 | #include 5 | 6 | struct Review { 7 | std::string confidence; 8 | std::string evaluation; 9 | uint32_t id{0}; 10 | std::string language; 11 | std::string orientation; 12 | std::string remarks; 13 | std::string text; 14 | std::string timespan; 15 | }; 16 | 17 | #endif // REVIEW_H 18 | -------------------------------------------------------------------------------- /Chapter02/json/cpp/reviewsreader.h: -------------------------------------------------------------------------------- 1 | #ifndef REVIEWSREADER_H 2 | #define REVIEWSREADER_H 3 | 4 | #include "paper.h" 5 | 6 | Papers ReadPapersReviews(const std::string& filename); 7 | 8 | #endif // REVIEWSREADER_H 9 | -------------------------------------------------------------------------------- /Chapter03/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-sample) 3 | 4 | set(DLIB_PATH "" CACHE PATH "path to dlib install dir") 5 | 6 | if (NOT DLIB_PATH) 7 | message(FATAL_ERROR "Missigng Dlib install path, please specify DLIB_PATH") 8 | else() 9 | message("Dlib path is ${DLIB_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(requiredlibs "cblas") 23 | list(APPEND requiredlibs "lapack") 24 | list(APPEND requiredlibs "stdc++") 25 | list(APPEND requiredlibs "stdc++fs") 26 | 27 | find_package(CUDA) 28 | if (CUDA_FOUND) 29 | list(APPEND requiredlibs ${CUDA_LIBRARIES}) 30 | list(APPEND requiredlibs ${CUDA_CUBLAS_LIBRARIES}) 31 | list(APPEND requiredlibs ${CUDA_cusolver_LIBRARY}) 32 | list(APPEND requiredlibs ${CUDA_curand_LIBRARY}) 33 | 34 | get_filename_component(cudnn_hint_path "${CUDA_CUBLAS_LIBRARIES}" PATH) 35 | find_library(cudnn cudnn 36 | HINTS ${cudnn_hint_path} ENV CUDNN_LIBRARY_DIR ENV CUDNN_HOME 37 | PATHS /usr/local /usr/local/cuda ENV LD_LIBRARY_PATH 38 | PATH_SUFFIXES lib64 lib x64 39 | ) 40 | message("CUDNN found ${cudnn}") 41 | list(APPEND requiredlibs ${cudnn}) 42 | endif() 43 | 44 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 45 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 46 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 47 | 48 | include_directories(${DLIB_PATH}/include) 49 | include_directories(${PLOTCPP_PATH}) 50 | 51 | link_directories(${DLIB_PATH}/lib) 52 | link_directories(${DLIB_PATH}/lib64) 53 | 54 | add_executable(grid-dlib "grid-dlib.cc") 55 | target_link_libraries(grid-dlib optimized dlib debug dlibd) 56 | target_link_libraries(grid-dlib ${requiredlibs}) 57 | 58 | -------------------------------------------------------------------------------- /Chapter03/sharkml/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(sharkml-samples) 3 | 4 | set(SHARK_PATH "" CACHE PATH "path to SharkML install dir") 5 | 6 | if(NOT SHARK_PATH) 7 | message(FATAL_ERROR "Missing SharkML install path, please specify SHARK_PATH") 8 | else() 9 | message("SharkML path is ${SHARK_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | 21 | set(CMAKE_VERBOSE_MAKEFILE ON) 22 | 23 | set(Boost_USE_STATIC_LIBS ON) 24 | set(Boost_USE_STATIC_RUNTIME ON) 25 | find_package(Boost REQUIRED serialization) 26 | 27 | set(CMAKE_CXX_FLAGS "-msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 28 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 29 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 30 | 31 | include_directories(${SHARK_PATH}/include) 32 | include_directories(${PLOTCPP_PATH}) 33 | link_directories(${SHARK_PATH}/lib) 34 | link_directories(${SHARK_PATH}/lib64) 35 | 36 | set(SOURCES polynomial-model.h 37 | polynomial-regression.h 38 | monitor.h 39 | data.h 40 | data.cpp 41 | plot.h 42 | plot.cpp) 43 | 44 | add_executable(regression-shark regression-shark.cc ${SOURCES}) 45 | target_link_libraries(regression-shark shark cblas ${Boost_LIBRARIES} ) 46 | 47 | add_executable(grid-shark grid-shark.cc ${SOURCES}) 48 | target_link_libraries(grid-shark shark cblas ${Boost_LIBRARIES} ) 49 | -------------------------------------------------------------------------------- /Chapter03/sharkml/data.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_H 2 | #define DATA_H 3 | 4 | #include 5 | 6 | shark::Data LinSpace(double s, double e, size_t n); 7 | 8 | std::pair, shark::Data> 9 | GenerateData(size_t num_samples, bool no_noise, size_t seed); 10 | 11 | std::pair, shark::Data> 12 | GetXYData(); 13 | 14 | std::pair, shark::Data> 15 | GetXYValidationData(); 16 | 17 | template 18 | auto DataMin(const D& data) { 19 | auto elems = data.elements(); 20 | auto mm = std::minmax_element( 21 | elems.begin(), elems.end(), 22 | [](const auto& a, const auto& b) { return a(0) < b(0); }); 23 | return std::make_pair((*mm.first)(0), (*mm.second)(0)); 24 | } 25 | 26 | #endif // DATA_H 27 | -------------------------------------------------------------------------------- /Chapter03/sharkml/grid-shark.cc: -------------------------------------------------------------------------------- 1 | #include "data.h" 2 | #include "plot.h" 3 | #include "polynomial-model.h" 4 | #include "polynomial-regression.h" 5 | 6 | #define SHARK_CV_VERBOSE 1 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace shark; 15 | 16 | int main() { 17 | Data x; 18 | Data y; 19 | std::tie(x, y) = 20 | GenerateData(/*num_samples*/ 500, /*no_noise*/ false, /*seed*/ 7568); 21 | RegressionDataset train_data(x, y); 22 | train_data.shuffle(); 23 | 24 | auto x_minmax = DataMin(train_data.inputs()); 25 | 26 | // normalization 27 | bool remove_mean = true; 28 | shark::Normalizer x_normalizer; 29 | shark::NormalizeComponentsUnitVariance normalizing_trainer( 30 | remove_mean); 31 | normalizing_trainer.train(x_normalizer, train_data.inputs()); 32 | train_data = transformInputs(train_data, x_normalizer); 33 | 34 | // split dataset 35 | const unsigned int num_folds = 5; 36 | CVFolds folds = 37 | createCVSameSize(train_data, num_folds); 38 | 39 | // Grid search the model 40 | AbsoluteLoss<> loss; 41 | // SquaredLoss<> loss; 42 | double regularization_factor = 0.0; 43 | double polynomial_degree = 8; 44 | int num_epochs = 300; 45 | PolynomialModel<> model; 46 | PolynomialRegression trainer(regularization_factor, polynomial_degree, 47 | num_epochs); 48 | 49 | CrossValidationError, RealVector> cv_error( 50 | folds, &trainer, &model, &trainer, &loss); 51 | 52 | GridSearch grid; 53 | std::vector min(2); 54 | std::vector max(2); 55 | std::vector sections(2); 56 | // regularization factor 57 | min[0] = 0.0; 58 | max[0] = 0.00001; 59 | sections[0] = 6; 60 | // polynomial degree 61 | min[1] = 4; 62 | max[1] = 10.0; 63 | sections[1] = 6; 64 | grid.configure(min, max, sections); 65 | grid.step(cv_error); 66 | 67 | // train final model 68 | std::cout << grid.solution() << std::endl; 69 | trainer.setParameterVector(grid.solution().point); 70 | trainer.train(model, train_data); 71 | 72 | // plot the data 73 | auto new_x = LinSpace(x_minmax.first, x_minmax.second, 50); 74 | new_x = x_normalizer(new_x); 75 | Data prediction = model(new_x); 76 | 77 | PlotData(train_data.inputs(), train_data.labels(), 78 | folds.validation(0).inputs(), folds.validation(0).labels(), new_x, 79 | prediction); 80 | 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /Chapter03/sharkml/monitor.h: -------------------------------------------------------------------------------- 1 | #ifndef MONITOR_H 2 | #define MONITOR_H 3 | 4 | #include 5 | 6 | template 7 | class Monitor { 8 | public: 9 | void clear() { 10 | train_steps.clear(); 11 | validation_steps.clear(); 12 | } 13 | void addTrainStep(double value) { train_steps.push_back(value); } 14 | void addValidationStep(Model& model) { 15 | auto prediction = model(validation_data.inputs()); 16 | validation_steps.push_back(loss(validation_data.labels(), prediction)); 17 | } 18 | 19 | std::vector train_steps; 20 | std::vector validation_steps; 21 | Dataset validation_data; 22 | Loss loss; 23 | }; 24 | 25 | #endif // MONITOR_H 26 | -------------------------------------------------------------------------------- /Chapter03/sharkml/plot.cpp: -------------------------------------------------------------------------------- 1 | #include "plot.h" 2 | 3 | #include 4 | 5 | #include 6 | 7 | using namespace shark; 8 | 9 | namespace { 10 | template 11 | auto DataToVector(const T& data) { 12 | std::vector vec; 13 | vec.reserve(data.shape()[0]); 14 | std::for_each(data.elements().begin(), data.elements().end(), 15 | [&](auto elem) { vec.emplace_back(elem[0]); }); 16 | return vec; 17 | } 18 | } // namespace 19 | 20 | void PlotData(const Data& x, 21 | const Data& y, 22 | const Data& x_val, 23 | const Data& y_val, 24 | const Data& x_pred, 25 | const Data& y_pred) { 26 | plotcpp::Plot plt; 27 | plt.SetTerminal("png"); 28 | plt.SetOutput("plot.png"); 29 | plt.SetTitle("Polynomial regression"); 30 | plt.SetXLabel("x"); 31 | plt.SetYLabel("y"); 32 | plt.SetAutoscale(); 33 | plt.GnuplotCommand("set grid"); 34 | 35 | auto x_coords = DataToVector(x); 36 | auto y_coords = DataToVector(y); 37 | 38 | auto x_val_coords = DataToVector(x_val); 39 | auto y_val_coords = DataToVector(y_val); 40 | 41 | auto x_pred_coords = DataToVector(x_pred); 42 | auto y_pred_coords = DataToVector(y_pred); 43 | 44 | plt.Draw2D( 45 | plotcpp::Points(x_coords.begin(), x_coords.end(), y_coords.begin(), 46 | "orig", "lc rgb 'black' pt 7"), 47 | plotcpp::Points(x_val_coords.begin(), x_val_coords.end(), 48 | y_val_coords.begin(), "val", "lc rgb 'green' pt 7"), 49 | plotcpp::Lines(x_pred_coords.begin(), x_pred_coords.end(), 50 | y_pred_coords.begin(), "pred", "lc rgb 'red' lw 2")); 51 | plt.Flush(); 52 | } 53 | 54 | void PlotTrain(const std::vector& train, 55 | const std::vector& validation) { 56 | plotcpp::Plot plt; 57 | plt.SetTerminal("png"); 58 | plt.SetOutput("train_plot.png"); 59 | plt.SetTitle("Losses"); 60 | plt.SetXLabel("num iteration"); 61 | plt.SetYLabel("loss"); 62 | plt.SetAutoscale(); 63 | plt.GnuplotCommand("set grid"); 64 | 65 | std::vector x_coords(train.size()); 66 | std::iota(x_coords.begin(), x_coords.end(), 0); 67 | 68 | plt.Draw2D( 69 | plotcpp::Lines(x_coords.begin(), x_coords.end(), train.begin(), "train", 70 | "lc rgb 'black' lw 2"), 71 | plotcpp::Lines(x_coords.begin(), x_coords.end(), validation.begin(), 72 | "validation", "lc rgb 'red' lw 2")); 73 | plt.Flush(); 74 | } 75 | -------------------------------------------------------------------------------- /Chapter03/sharkml/plot.h: -------------------------------------------------------------------------------- 1 | #ifndef PLOT_H 2 | #define PLOT_H 3 | 4 | #include 5 | 6 | #include 7 | 8 | void PlotData(const shark::Data& x, 9 | const shark::Data& y, 10 | const shark::Data& x_val, 11 | const shark::Data& y_val, 12 | const shark::Data& x_pred, 13 | const shark::Data& y_pred); 14 | 15 | void PlotTrain(const std::vector& train, 16 | const std::vector& validation); 17 | 18 | #endif // PLOT_H 19 | -------------------------------------------------------------------------------- /Chapter03/sharkml/polynomial-regression.h: -------------------------------------------------------------------------------- 1 | #ifndef POLYNOMIALREGRESSION_H 2 | #define POLYNOMIALREGRESSION_H 3 | 4 | #include "monitor.h" 5 | #include "polynomial-model.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | class PolynomialRegression : public shark::AbstractTrainer>, 16 | public shark::IParameterizable<> { 17 | public: 18 | using MonitorType = 19 | Monitor, 20 | shark::LabeledData, 21 | shark::AbsoluteLoss<>>; 22 | 23 | PolynomialRegression(double regularization = 0.0, 24 | double polynomial_degree = 1, 25 | int num_epochs = 100) 26 | : m_regularization{regularization}, 27 | m_polynomial_degree{polynomial_degree}, 28 | m_num_epochs{num_epochs} {} 29 | 30 | std::string name() const override { return "PolynomialRegression"; } 31 | 32 | double regularization() const { return m_regularization; } 33 | void setRegularization(double regularization) { 34 | RANGE_CHECK(regularization >= 0.0); 35 | m_regularization = regularization; 36 | } 37 | 38 | double polynomialDegree() const { return m_polynomial_degree; } 39 | void setPolynomialDegree(double polynomial_degree) { 40 | RANGE_CHECK(polynomial_degree >= 1.0); 41 | m_polynomial_degree = polynomial_degree; 42 | } 43 | 44 | shark::RealVector parameterVector() const override { 45 | shark::RealVector param(2); 46 | param(0) = m_regularization; 47 | param(0) = m_polynomial_degree; 48 | return param; 49 | } 50 | void setParameterVector(const shark::RealVector& param) override { 51 | SIZE_CHECK(param.size() == 2); 52 | m_regularization = param(0); 53 | m_polynomial_degree = param(1); 54 | } 55 | size_t numberOfParameters() const override { return 2; } 56 | 57 | void setMonitor(MonitorType* monitor) { m_monitor = monitor; } 58 | 59 | void train(PolynomialModel<>& model, 60 | shark::LabeledData const& 61 | dataset) override { 62 | std::size_t inputDim = inputDimension(dataset); 63 | std::size_t outputDim = labelDimension(dataset); 64 | 65 | model.setStructure(static_cast(m_polynomial_degree), inputDim, 66 | outputDim); 67 | 68 | shark::SquaredLoss<> loss; 69 | shark::SquaredLoss<> abs_loss; 70 | shark::ErrorFunction<> errorFunction(dataset, &model, &loss); 71 | // shark::TwoNormRegularizer<> regularizer; 72 | shark::OneNormRegularizer<> regularizer; 73 | errorFunction.setRegularizer(m_regularization, ®ularizer); 74 | errorFunction.init(); 75 | 76 | shark::CG<> optimizer; 77 | optimizer.init(errorFunction); 78 | if (m_monitor) 79 | m_monitor->clear(); 80 | for (int i = 0; i != m_num_epochs; ++i) { 81 | optimizer.step(errorFunction); 82 | if (m_monitor) { 83 | auto pred = model(dataset.inputs()); 84 | m_monitor->addTrainStep(abs_loss(dataset.labels(), pred)); 85 | m_monitor->addValidationStep(model); 86 | } 87 | } 88 | // copy solution parameters into model 89 | model.setParameterVector(optimizer.solution().point); 90 | } 91 | 92 | protected: 93 | double m_regularization; 94 | double m_polynomial_degree; 95 | int m_num_epochs; 96 | MonitorType* m_monitor{nullptr}; 97 | }; 98 | 99 | #endif // POLYNOMIALREGRESSION_H 100 | -------------------------------------------------------------------------------- /Chapter03/sharkml/regression-shark.cc: -------------------------------------------------------------------------------- 1 | #include "data.h" 2 | #include "plot.h" 3 | #include "polynomial-model.h" 4 | #include "polynomial-regression.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace shark; 12 | 13 | int main() { 14 | Data x; 15 | Data y; 16 | std::tie(x, y) = GetXYData(); 17 | RegressionDataset train_data(x, y); 18 | train_data.shuffle(); 19 | 20 | Data val_x; 21 | Data val_y; 22 | std::tie(val_x, val_y) = GetXYValidationData(); 23 | RegressionDataset validation_data(val_x, val_y); 24 | 25 | auto x_minmax = DataMin(train_data.inputs()); 26 | 27 | // normalization 28 | bool remove_mean = true; 29 | shark::Normalizer x_normalizer; 30 | shark::NormalizeComponentsUnitVariance normalizing_trainer( 31 | remove_mean); 32 | normalizing_trainer.train(x_normalizer, train_data.inputs()); 33 | train_data = transformInputs(train_data, x_normalizer); 34 | validation_data = transformInputs(validation_data, x_normalizer); 35 | 36 | shark::Normalizer y_normalizer; 37 | normalizing_trainer.train(y_normalizer, train_data.labels()); 38 | train_data = transformLabels(train_data, x_normalizer); 39 | validation_data = transformLabels(validation_data, x_normalizer); 40 | 41 | // Train the model 42 | 43 | // try to change this factor to be greater than zero for polynomial dergees 44 | // higher 10 to see how regularization beat high variance 45 | double regularization_factor = 0.0; 46 | 47 | // use degree of 1 - 4 to see the high bias effect 48 | // use degree of 10 - 15 to see the high variance effect 49 | double polynomial_degree = 8; 50 | 51 | int num_epochs = 300; 52 | 53 | PolynomialModel<> model; 54 | PolynomialRegression trainer(regularization_factor, polynomial_degree, 55 | num_epochs); 56 | PolynomialRegression::MonitorType monitor; 57 | monitor.validation_data = validation_data; 58 | trainer.setMonitor(&monitor); 59 | trainer.train(model, train_data); 60 | 61 | // Show metrics 62 | SquaredLoss<> mse_loss; 63 | Data prediction = model(train_data.inputs()); 64 | auto mse = mse_loss(train_data.labels(), prediction); 65 | auto rmse = std::sqrt(mse); 66 | AbsoluteLoss<> abs_loss; 67 | auto mae = abs_loss(train_data.labels(), prediction); 68 | // R^2 69 | auto var = shark::variance(train_data.labels()); 70 | auto r_squared = 1 - mse / var(0); 71 | // Adjusted R^2 72 | auto n = train_data.labels().numberOfElements(); 73 | auto k = std::pow(train_data.inputs().shape()[0] + 1, n); 74 | auto adj_r_squared = 1 - ((1 - r_squared) * (n - 1) / (n - k - 1)); 75 | 76 | std::cout << "Mean Squared Error :\n" << mse << std::endl; 77 | std::cout << "Root Mean Squared Error :\n" << rmse << std::endl; 78 | std::cout << "Root Absolute Error :\n" << mae << std::endl; 79 | std::cout << "R Squared :\n" << r_squared << std::endl; 80 | std::cout << "Adjusted R Squared :\n" << adj_r_squared << std::endl; 81 | 82 | // plot the data 83 | auto new_x = LinSpace(x_minmax.first, x_minmax.second, 50); 84 | new_x = x_normalizer(new_x); 85 | prediction = model(new_x); 86 | 87 | PlotData(train_data.inputs(), train_data.labels(), validation_data.inputs(), 88 | validation_data.labels(), new_x, prediction); 89 | PlotTrain(monitor.train_steps, monitor.validation_steps); 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /Chapter03/shogun/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(shogun-samples) 3 | 4 | set(SHOGUN_PATH "" CACHE PATH "path to shogun install dir") 5 | 6 | if(NOT DEFINED SHOGUN_PATH) 7 | message(FATAL_ERROR "Missing Shogun install path, please specify SHOGUN_PATH") 8 | else() 9 | message("Shogun path is ${SHOGUN_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(CMAKE_CXX_FLAGS "-std=c++14 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 23 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 24 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 25 | 26 | include_directories(${SHOGUN_PATH}/include) 27 | include_directories(${PLOTCPP_PATH}) 28 | link_directories(${SHOGUN_PATH}/lib) 29 | 30 | set(SOURCES data.h 31 | data.cc 32 | grid-shogun.cc) 33 | 34 | add_executable(grid-shogun ${SOURCES}) 35 | target_link_libraries(grid-shogun shogun json-c ${PROTOBUF_LIBRARIES} cblas lapack) 36 | 37 | -------------------------------------------------------------------------------- /Chapter03/shogun/data.cc: -------------------------------------------------------------------------------- 1 | #include "data.h" 2 | 3 | #include 4 | 5 | using namespace shogun; 6 | 7 | SGMatrix LinSpace(double s, double e, size_t n) { 8 | SGMatrix x_values(1, n); 9 | // generate data 10 | double step = (e - s) / n; 11 | 12 | double v = s; 13 | for (size_t i = 0; i < n; ++i) { 14 | x_values.set_element(v, 0, i); 15 | v += step; 16 | } 17 | x_values.set_element(e, 0, n - 1); 18 | 19 | return x_values; 20 | } 21 | 22 | std::pair, SGVector> 23 | GenerateData(size_t num_samples, size_t seed, bool no_noise) { 24 | SGMatrix x_values(1, num_samples); 25 | SGVector y_values(num_samples); 26 | 27 | std::mt19937 re(seed); 28 | std::normal_distribution dist; 29 | 30 | for (size_t i = 0; i < num_samples; ++i) { 31 | auto x_val = dist(re); 32 | x_values.set_element(x_val, 0, i); 33 | 34 | auto y_val = std::cos(M_PI * x_val) + (no_noise ? 0 : (dist(re) * 0.3)); 35 | y_values.set_element(y_val, i); 36 | } 37 | return {x_values, y_values}; 38 | } 39 | -------------------------------------------------------------------------------- /Chapter03/shogun/data.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_H 2 | #define DATA_H 3 | 4 | #include 5 | #include 6 | 7 | shogun::SGMatrix LinSpace(double s, double e, size_t n); 8 | 9 | std::pair, shogun::SGVector> 10 | GenerateData(size_t num_samples, size_t seed, bool no_noise); 11 | 12 | #endif // DATA_H 13 | -------------------------------------------------------------------------------- /Chapter04/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-cluster) 3 | 4 | set(DLIB_PATH "" CACHE PATH "path to dlib install dir") 5 | 6 | if (NOT DLIB_PATH) 7 | message(FATAL_ERROR "Missigng Dlib install path, please specify DLIB_PATH") 8 | else() 9 | message("Dlib path is ${DLIB_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(requiredlibs "cblas") 23 | list(APPEND requiredlibs "lapack") 24 | list(APPEND requiredlibs "stdc++") 25 | list(APPEND requiredlibs "stdc++fs") 26 | 27 | find_package(CUDA) 28 | if (CUDA_FOUND) 29 | list(APPEND requiredlibs ${CUDA_LIBRARIES}) 30 | list(APPEND requiredlibs ${CUDA_CUBLAS_LIBRARIES}) 31 | list(APPEND requiredlibs ${CUDA_cusolver_LIBRARY}) 32 | list(APPEND requiredlibs ${CUDA_curand_LIBRARY}) 33 | 34 | get_filename_component(cudnn_hint_path "${CUDA_CUBLAS_LIBRARIES}" PATH) 35 | find_library(cudnn cudnn 36 | HINTS ${cudnn_hint_path} ENV CUDNN_LIBRARY_DIR ENV CUDNN_HOME 37 | PATHS /usr/local /usr/local/cuda ENV LD_LIBRARY_PATH 38 | PATH_SUFFIXES lib64 lib x64 39 | ) 40 | message("CUDNN found ${cudnn}") 41 | list(APPEND requiredlibs ${cudnn}) 42 | endif() 43 | 44 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 45 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 46 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 47 | 48 | include_directories(${DLIB_PATH}/include) 49 | include_directories(${PLOTCPP_PATH}) 50 | 51 | link_directories(${DLIB_PATH}/lib) 52 | link_directories(${DLIB_PATH}/lib64) 53 | 54 | add_executable(dlib-cluster "dlib-cluster.cc") 55 | target_link_libraries(dlib-cluster optimized dlib debug dlibd) 56 | target_link_libraries(dlib-cluster ${requiredlibs}) 57 | 58 | -------------------------------------------------------------------------------- /Chapter04/sharkml/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(sharkml-cluster) 3 | 4 | set(SHARK_PATH "" CACHE PATH "path to SharkML install dir") 5 | 6 | if(NOT SHARK_PATH) 7 | message(FATAL_ERROR "Missing SharkML install path, please specify SHARK_PATH") 8 | else() 9 | message("SharkML path is ${SHARK_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(Boost_USE_STATIC_LIBS ON) 23 | set(Boost_USE_STATIC_RUNTIME ON) 24 | find_package(Boost REQUIRED serialization) 25 | 26 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 27 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 28 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 29 | 30 | include_directories(${SHARK_PATH}/include) 31 | include_directories(${PLOTCPP_PATH}) 32 | link_directories(${SHARK_PATH}/lib) 33 | link_directories(${SHARK_PATH}/lib64) 34 | 35 | set(SOURCES sharkml-cluster.cc) 36 | 37 | add_executable(sharkml-cluster ${SOURCES}) 38 | target_link_libraries(sharkml-cluster shark cblas ${Boost_LIBRARIES} stdc++fs ) 39 | 40 | -------------------------------------------------------------------------------- /Chapter04/shogun/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(shogun-clustering) 3 | 4 | set(SHOGUN_PATH "" CACHE PATH "path to shogun install dir") 5 | 6 | if(NOT DEFINED SHOGUN_PATH) 7 | message(FATAL_ERROR "Missing Shogun install path, please specify SHOGUN_PATH") 8 | else() 9 | message("Shogun path is ${SHOGUN_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | 21 | set(CMAKE_VERBOSE_MAKEFILE ON) 22 | 23 | set(CMAKE_CXX_FLAGS "-std=c++14 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 24 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 25 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 26 | 27 | 28 | set(requiredlibs "stdc++fs") 29 | list(APPEND requiredlibs "stdc++") 30 | list(APPEND requiredlibs json-c ${PROTOBUF_LIBRARIES} cblas lapack) 31 | 32 | include_directories(${PLOTCPP_PATH}) 33 | include_directories(${SHOGUN_PATH}/include) 34 | link_directories(${SHOGUN_PATH}/lib) 35 | 36 | add_executable(shogun-cluster "shogun-cluster.cc") 37 | target_link_libraries(shogun-cluster shogun ${requiredlibs}) 38 | 39 | -------------------------------------------------------------------------------- /Chapter05/data/multivar.csv: -------------------------------------------------------------------------------- 1 | -2.366071741736619050e+00,8.650395628888157828e-01 2 | -2.155754253582252922e+00,2.132222203711148456e+00 3 | -1.945436765427887016e+00,2.015619142427458765e+00 4 | -1.735119277273520666e+00,1.117873018462888446e+00 5 | -1.524801789119154538e+00,2.172147469944559095e+00 6 | -1.314484300964788410e+00,1.649987573729479573e+00 7 | -1.104166812810422282e+00,1.121810591207495378e+00 8 | -8.938493246560559324e-01,5.186163482186424700e-02 9 | -6.835318365016899156e-01,1.426654324999584167e-01 10 | -4.732143483473238432e-01,7.018454311790772726e-01 11 | -4.732143483473238432e-01,1.022930364234008183e+00 12 | -4.405788760475083388e-01,2.354185565669291713e-01 13 | -4.079434037476928898e-01,1.226449814389925841e+00 14 | -3.753079314478774409e-01,3.868910032502616558e-01 15 | -3.426724591480619919e-01,-2.544523328121584549e-01 16 | -3.100369868482465430e-01,3.657503327330084986e-01 17 | -2.774015145484310385e-01,8.435027789268647602e-01 18 | -2.447660422486156173e-01,-1.046225942801146225e+00 19 | -2.121305699488001406e-01,1.108090305823731025e+00 20 | -1.794950976489846917e-01,-8.619055461521095696e-01 21 | -1.468596253491695758e-01,9.392878543658147450e-01 22 | -1.142241530493541130e-01,-4.656391608976957475e-01 23 | -8.158868074953865013e-02,-1.408916465526846573e-01 24 | -4.895320844972318730e-02,-6.511466492714065302e-01 25 | -1.631773614990772794e-02,-2.215014870058659435e-01 26 | 1.631773614990772794e-02,-3.007114269538465612e-01 27 | 4.895320844972318730e-02,5.165426704147627612e-01 28 | 8.158868074953865013e-02,-1.825263761954630179e-02 29 | 1.142241530493541130e-01,1.923576893053128289e-01 30 | 1.468596253491695758e-01,-2.260784287348586441e-01 31 | 1.794950976489850247e-01,-1.496801548082859687e-02 32 | 2.121305699488004737e-01,-1.215400566124676812e-01 33 | 2.447660422486156173e-01,-2.063076696355736206e-01 34 | 2.774015145484310385e-01,2.397131895238458765e-01 35 | 3.100369868482465430e-01,-1.984560675759159398e-01 36 | 3.426724591480619919e-01,-5.755975250449898439e-01 37 | 3.753079314478774409e-01,-1.223317702674122653e-01 38 | 4.079434037476928898e-01,-8.655580590524503126e-01 39 | 4.405788760475083388e-01,-7.779034381553371347e-01 40 | 4.732143483473238432e-01,1.659989657811084818e-01 41 | 4.732143483473238432e-01,-7.717732003953987618e-01 42 | 6.835318365016899156e-01,9.560850339548961074e-02 43 | 8.938493246560559324e-01,-3.996008610269475292e-01 44 | 1.104166812810422060e+00,-1.280067860233096377e+00 45 | 1.314484300964788188e+00,-1.248099544067293287e+00 46 | 1.524801789119154760e+00,-2.532438696316576809e+00 47 | 1.735119277273520888e+00,-1.624757223026498609e+00 48 | 1.945436765427887016e+00,-1.426747826902983407e+00 49 | 2.155754253582252922e+00,-1.709137301946266119e+00 50 | 2.366071741736619050e+00,-1.247523715042455184e+00 51 | -1.500000000000000000e+00,-1.500000000000000000e+00 52 | 1.500000000000000000e+00,1.500000000000000000e+00 53 | -------------------------------------------------------------------------------- /Chapter05/data/univar.csv: -------------------------------------------------------------------------------- 1 | 1.300000000000000000e+01,9.000000000000000000e+00 2 | 8.000000000000000000e+00,8.000000000000000000e+00 3 | 1.400000000000000000e+01,9.000000000000000000e+00 4 | 8.000000000000000000e+00,6.000000000000000000e+00 5 | 6.000000000000000000e+00,6.000000000000000000e+00 6 | 9.000000000000000000e+00,1.000000000000000000e+01 7 | 7.000000000000000000e+00,1.200000000000000000e+01 8 | 9.000000000000000000e+00,1.200000000000000000e+01 9 | 5.000000000000000000e+00,9.000000000000000000e+00 10 | 1.100000000000000000e+01,6.000000000000000000e+00 11 | 1.400000000000000000e+01,1.100000000000000000e+01 12 | 1.200000000000000000e+01,1.300000000000000000e+01 13 | 1.100000000000000000e+01,9.000000000000000000e+00 14 | 7.000000000000000000e+00,1.200000000000000000e+01 15 | 8.000000000000000000e+00,1.400000000000000000e+01 16 | 8.000000000000000000e+00,7.000000000000000000e+00 17 | 9.000000000000000000e+00,5.000000000000000000e+00 18 | 8.000000000000000000e+00,7.000000000000000000e+00 19 | 6.000000000000000000e+00,6.000000000000000000e+00 20 | 5.000000000000000000e+00,1.000000000000000000e+01 21 | 1.400000000000000000e+01,6.000000000000000000e+00 22 | 1.400000000000000000e+01,1.000000000000000000e+01 23 | 1.100000000000000000e+01,1.200000000000000000e+01 24 | 6.000000000000000000e+00,7.000000000000000000e+00 25 | 5.000000000000000000e+00,9.000000000000000000e+00 26 | 5.000000000000000000e+00,6.000000000000000000e+00 27 | 1.100000000000000000e+01,1.300000000000000000e+01 28 | 1.000000000000000000e+01,1.300000000000000000e+01 29 | 5.000000000000000000e+00,8.000000000000000000e+00 30 | 1.400000000000000000e+01,6.000000000000000000e+00 31 | 9.000000000000000000e+00,8.000000000000000000e+00 32 | 1.300000000000000000e+01,6.000000000000000000e+00 33 | 6.000000000000000000e+00,1.000000000000000000e+01 34 | 1.100000000000000000e+01,1.200000000000000000e+01 35 | 8.000000000000000000e+00,1.400000000000000000e+01 36 | 1.200000000000000000e+01,1.000000000000000000e+01 37 | 1.100000000000000000e+01,8.000000000000000000e+00 38 | 1.300000000000000000e+01,1.400000000000000000e+01 39 | 1.200000000000000000e+01,9.000000000000000000e+00 40 | 9.000000000000000000e+00,1.300000000000000000e+01 41 | 9.000000000000000000e+00,1.000000000000000000e+01 42 | 6.000000000000000000e+00,7.000000000000000000e+00 43 | 5.000000000000000000e+00,1.000000000000000000e+01 44 | 1.300000000000000000e+01,8.000000000000000000e+00 45 | 6.000000000000000000e+00,5.000000000000000000e+00 46 | 1.300000000000000000e+01,5.000000000000000000e+00 47 | 6.000000000000000000e+00,1.200000000000000000e+01 48 | 6.000000000000000000e+00,7.000000000000000000e+00 49 | 9.000000000000000000e+00,9.000000000000000000e+00 50 | 7.000000000000000000e+00,1.100000000000000000e+01 51 | 7.000000000000000000e+00,2.500000000000000000e+01 52 | 1.400000000000000000e+01,1.600000000000000000e+01 53 | 1.300000000000000000e+01,2.200000000000000000e+01 54 | 1.500000000000000000e+01,2.200000000000000000e+01 55 | -------------------------------------------------------------------------------- /Chapter05/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-anomaly) 3 | 4 | set(DLIB_PATH "" CACHE PATH "path to dlib install dir") 5 | 6 | if (NOT DLIB_PATH) 7 | message(FATAL_ERROR "Missigng Dlib install path, please specify DLIB_PATH") 8 | else() 9 | message("Dlib path is ${DLIB_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(requiredlibs "cblas") 23 | list(APPEND requiredlibs "lapack") 24 | list(APPEND requiredlibs "stdc++") 25 | list(APPEND requiredlibs "stdc++fs") 26 | 27 | find_package(CUDA) 28 | if (CUDA_FOUND) 29 | list(APPEND requiredlibs ${CUDA_LIBRARIES}) 30 | list(APPEND requiredlibs ${CUDA_CUBLAS_LIBRARIES}) 31 | list(APPEND requiredlibs ${CUDA_cusolver_LIBRARY}) 32 | list(APPEND requiredlibs ${CUDA_curand_LIBRARY}) 33 | 34 | get_filename_component(cudnn_hint_path "${CUDA_CUBLAS_LIBRARIES}" PATH) 35 | find_library(cudnn cudnn 36 | HINTS ${cudnn_hint_path} ENV CUDNN_LIBRARY_DIR ENV CUDNN_HOME 37 | PATHS /usr/local /usr/local/cuda ENV LD_LIBRARY_PATH 38 | PATH_SUFFIXES lib64 lib x64 39 | ) 40 | message("CUDNN found ${cudnn}") 41 | list(APPEND requiredlibs ${cudnn}) 42 | endif() 43 | 44 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 45 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 46 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 47 | 48 | include_directories(${DLIB_PATH}/include) 49 | include_directories(${PLOTCPP_PATH}) 50 | 51 | link_directories(${DLIB_PATH}/lib) 52 | link_directories(${DLIB_PATH}/lib64) 53 | 54 | set(SOURCES dlib-anomaly.cc 55 | isolation-forest.h) 56 | 57 | add_executable(dlib-anomaly ${SOURCES}) 58 | #target_link_libraries(dlib-anomaly optimized dlib debug dlibd) 59 | target_link_libraries(dlib-anomaly dlib) 60 | target_link_libraries(dlib-anomaly ${requiredlibs}) 61 | 62 | -------------------------------------------------------------------------------- /Chapter05/sharkml/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(sharkml-anomaly) 3 | 4 | set(SHARK_PATH "" CACHE PATH "path to SharkML install dir") 5 | 6 | if(NOT SHARK_PATH) 7 | message(FATAL_ERROR "Missing SharkML install path, please specify SHARK_PATH") 8 | else() 9 | message("SharkML path is ${SHARK_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(Boost_USE_STATIC_LIBS ON) 23 | set(Boost_USE_STATIC_RUNTIME ON) 24 | find_package(Boost REQUIRED serialization) 25 | 26 | set(CMAKE_CXX_FLAGS "-msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 27 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 28 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 29 | 30 | include_directories(${SHARK_PATH}/include) 31 | include_directories(${PLOTCPP_PATH}) 32 | link_directories(${SHARK_PATH}/lib) 33 | link_directories(${SHARK_PATH}/lib64) 34 | 35 | set(SOURCES sharkml-anomaly.cc) 36 | 37 | add_executable(sharkml-anomaly ${SOURCES}) 38 | target_link_libraries(sharkml-anomaly shark cblas ${Boost_LIBRARIES} stdc++fs ) 39 | 40 | -------------------------------------------------------------------------------- /Chapter05/sharkml/sharkml-anomaly.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SHARK_CV_VERBOSE 1 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | namespace fs = std::experimental::filesystem; 14 | 15 | using namespace shark; 16 | 17 | const std::vector colors{"black", "red", "blue", "green", "cyan"}; 18 | 19 | using DataType = double; 20 | using Coords = std::vector; 21 | using PointCoords = std::pair; 22 | using Clusters = std::unordered_map; 23 | 24 | void PlotClusters(const Clusters& clusters, 25 | const std::string& name, 26 | const std::string& file_name) { 27 | plotcpp::Plot plt(true); 28 | // plt.SetTerminal("qt"); 29 | plt.SetTerminal("png"); 30 | plt.SetOutput(file_name); 31 | plt.SetTitle(name); 32 | plt.SetXLabel("x"); 33 | plt.SetYLabel("y"); 34 | // plt.SetAutoscale(); 35 | plt.GnuplotCommand("set size square"); 36 | plt.GnuplotCommand("set grid"); 37 | 38 | auto draw_state = plt.StartDraw2D(); 39 | for (auto& cluster : clusters) { 40 | std::stringstream params; 41 | params << "lc rgb '" << colors[cluster.first] << "' pt 7"; 42 | plt.AddDrawing(draw_state, 43 | plotcpp::Points( 44 | cluster.second.first.begin(), cluster.second.first.end(), 45 | cluster.second.second.begin(), 46 | std::to_string(cluster.first) + " cls", params.str())); 47 | } 48 | 49 | plt.EndDraw2D(draw_state); 50 | plt.Flush(); 51 | } 52 | 53 | int main(int argc, char** argv) { 54 | if (argc > 1) { 55 | auto base_dir = fs::path(argv[1]); 56 | std::string data_name_multi{"multivar.csv"}; 57 | auto dataset_name = base_dir / data_name_multi; 58 | if (fs::exists(dataset_name)) { 59 | UnlabeledData data; 60 | importCSV(data, dataset_name); 61 | 62 | data.splitBatch(0, 50); 63 | auto test_data = data.splice(1); 64 | 65 | std::size_t num_samples = data.numberOfElements(); 66 | std::size_t num_features = dataDimension(data.inputs()); 67 | 68 | std::cout << "Num samples: " << num_samples 69 | << " num features: " << num_features << std::endl; 70 | 71 | double gamma = 0.5; // kernel bandwidth parameter 72 | GaussianRbfKernel<> kernel(gamma); 73 | KernelExpansion ke(&kernel); 74 | 75 | double nu = 0.5; // parameter of the method for controlling the 76 | // smoothness of the solution 77 | 78 | OneClassSvmTrainer trainer(&kernel, nu); 79 | trainer.stoppingCondition().minAccuracy = 1e-6; 80 | trainer.train(ke, data); 81 | 82 | double dist_threshold = -0.2; 83 | Clusters plot_clusters; 84 | RealVector output; 85 | auto detect = [&](const UnlabeledData& data) { 86 | for (size_t i = 0; i < data.numberOfElements(); ++i) { 87 | ke.eval(data.element(i), output); 88 | auto x = data.element(i)[0]; 89 | auto y = data.element(i)[1]; 90 | if (output[0] > dist_threshold) { 91 | plot_clusters[0].first.push_back(x); 92 | plot_clusters[0].second.push_back(y); 93 | } else { 94 | plot_clusters[1].first.push_back(x); 95 | plot_clusters[1].second.push_back(y); 96 | } 97 | } 98 | }; 99 | detect(data); 100 | detect(test_data); 101 | 102 | PlotClusters(plot_clusters, "One Class Svm", "shark-ocsvm.png"); 103 | 104 | } else { 105 | std::cerr << "Dataset file " << dataset_name << " missed\n"; 106 | } 107 | } else { 108 | std::cerr << "Please provider path to the datasets folder\n"; 109 | } 110 | 111 | return 0; 112 | } 113 | -------------------------------------------------------------------------------- /Chapter05/shogun/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(shogun-anomaly) 3 | 4 | set(SHOGUN_PATH "" CACHE PATH "path to shogun install dir") 5 | 6 | if(NOT DEFINED SHOGUN_PATH) 7 | message(FATAL_ERROR "Missing Shogun install path, please specify SHOGUN_PATH") 8 | else() 9 | message("Shogun path is ${SHOGUN_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(CMAKE_CXX_FLAGS "-std=c++14 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 23 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 24 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 25 | 26 | 27 | set(requiredlibs "stdc++fs") 28 | list(APPEND requiredlibs "stdc++") 29 | list(APPEND requiredlibs json-c ${PROTOBUF_LIBRARIES} cblas lapack) 30 | 31 | include_directories(${PLOTCPP_PATH}) 32 | include_directories(${SHOGUN_PATH}/include) 33 | link_directories(${SHOGUN_PATH}/lib) 34 | 35 | add_executable(shogun-anomaly "shogun-anomaly.cc") 36 | target_link_libraries(shogun-anomaly shogun ${requiredlibs}) 37 | 38 | -------------------------------------------------------------------------------- /Chapter06/data/photo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Machine-Learning-with-CPP/9db0df34f41abeed1cca6792ddb79d88518ac017/Chapter06/data/photo.png -------------------------------------------------------------------------------- /Chapter06/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-dr) 3 | 4 | set(DLIB_PATH "" CACHE PATH "path to dlib install dir") 5 | 6 | if (NOT DLIB_PATH) 7 | message(FATAL_ERROR "Missigng Dlib install path, please specify DLIB_PATH") 8 | else() 9 | message("Dlib path is ${DLIB_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(requiredlibs "cblas") 23 | list(APPEND requiredlibs "lapack") 24 | list(APPEND requiredlibs "stdc++") 25 | list(APPEND requiredlibs "stdc++fs") 26 | 27 | find_package(CUDA) 28 | if (CUDA_FOUND) 29 | list(APPEND requiredlibs ${CUDA_LIBRARIES}) 30 | list(APPEND requiredlibs ${CUDA_CUBLAS_LIBRARIES}) 31 | list(APPEND requiredlibs ${CUDA_cusolver_LIBRARY}) 32 | list(APPEND requiredlibs ${CUDA_curand_LIBRARY}) 33 | 34 | get_filename_component(cudnn_hint_path "${CUDA_CUBLAS_LIBRARIES}" PATH) 35 | find_library(cudnn cudnn 36 | HINTS ${cudnn_hint_path} ENV CUDNN_LIBRARY_DIR ENV CUDNN_HOME 37 | PATHS /usr/local /usr/local/cuda ENV LD_LIBRARY_PATH 38 | PATH_SUFFIXES lib64 lib x64 39 | ) 40 | message("CUDNN found ${cudnn}") 41 | list(APPEND requiredlibs ${cudnn}) 42 | endif() 43 | 44 | list(APPEND requiredlibs png jpeg) 45 | 46 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 47 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 48 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 49 | 50 | include_directories(${DLIB_PATH}/include) 51 | include_directories(${PLOTCPP_PATH}) 52 | 53 | link_directories(${DLIB_PATH}/lib) 54 | link_directories(${DLIB_PATH}/lib64) 55 | 56 | set(SOURCES dlib-dr.cc) 57 | 58 | add_executable(dlib-dr ${SOURCES}) 59 | target_link_libraries(dlib-dr optimized dlib debug dlibd) 60 | # target_link_libraries(dlib-dr dlib) 61 | target_link_libraries(dlib-dr ${requiredlibs}) 62 | 63 | -------------------------------------------------------------------------------- /Chapter06/sharkml/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(sharkml-dr) 3 | 4 | set(SHARK_PATH "" CACHE PATH "path to SharkML install dir") 5 | 6 | if(NOT SHARK_PATH) 7 | message(FATAL_ERROR "Missing SharkML install path, please specify SHARK_PATH") 8 | else() 9 | message("SharkML path is ${SHARK_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(Boost_USE_STATIC_LIBS ON) 23 | set(Boost_USE_STATIC_RUNTIME ON) 24 | find_package(Boost REQUIRED serialization) 25 | 26 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 27 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 28 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 29 | 30 | include_directories(${SHARK_PATH}/include) 31 | include_directories(${PLOTCPP_PATH}) 32 | link_directories(${SHARK_PATH}/lib) 33 | link_directories(${SHARK_PATH}/lib64) 34 | 35 | set(SOURCES sharkml-dr.cc) 36 | 37 | add_executable(sharkml-dr ${SOURCES}) 38 | target_link_libraries(sharkml-dr shark cblas ${Boost_LIBRARIES} stdc++fs ) 39 | 40 | -------------------------------------------------------------------------------- /Chapter06/shogun/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(shogun-dr) 3 | 4 | set(SHOGUN_PATH "" CACHE PATH "path to shogun install dir") 5 | 6 | if(NOT DEFINED SHOGUN_PATH) 7 | message(FATAL_ERROR "Missing Shogun install path, please specify SHOGUN_PATH") 8 | else() 9 | message("Shogun path is ${SHOGUN_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(CMAKE_CXX_FLAGS "-std=c++14 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 23 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 24 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 25 | 26 | 27 | set(requiredlibs "stdc++fs") 28 | list(APPEND requiredlibs "stdc++") 29 | list(APPEND requiredlibs json-c ${PROTOBUF_LIBRARIES} cblas lapack) 30 | 31 | include_directories(${PLOTCPP_PATH}) 32 | include_directories(${SHOGUN_PATH}/include) 33 | link_directories(${SHOGUN_PATH}/lib) 34 | 35 | add_executable(shogun-dr "shogun-dr.cc") 36 | target_link_libraries(shogun-dr shogun) 37 | target_link_libraries(shogun-dr ${requiredlibs}) 38 | 39 | -------------------------------------------------------------------------------- /Chapter07/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-classify) 3 | 4 | set(DLIB_PATH "" CACHE PATH "path to dlib install dir") 5 | 6 | if (NOT DLIB_PATH) 7 | message(FATAL_ERROR "Missigng Dlib install path, please specify DLIB_PATH") 8 | else() 9 | message("Dlib path is ${DLIB_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(requiredlibs "cblas") 23 | list(APPEND requiredlibs "lapack") 24 | list(APPEND requiredlibs "stdc++") 25 | list(APPEND requiredlibs "stdc++fs") 26 | 27 | find_package(CUDA) 28 | if (CUDA_FOUND) 29 | list(APPEND requiredlibs ${CUDA_LIBRARIES}) 30 | list(APPEND requiredlibs ${CUDA_CUBLAS_LIBRARIES}) 31 | list(APPEND requiredlibs ${CUDA_cusolver_LIBRARY}) 32 | list(APPEND requiredlibs ${CUDA_curand_LIBRARY}) 33 | 34 | get_filename_component(cudnn_hint_path "${CUDA_CUBLAS_LIBRARIES}" PATH) 35 | find_library(cudnn cudnn 36 | HINTS ${cudnn_hint_path} ENV CUDNN_LIBRARY_DIR ENV CUDNN_HOME 37 | PATHS /usr/local /usr/local/cuda ENV LD_LIBRARY_PATH 38 | PATH_SUFFIXES lib64 lib x64 39 | ) 40 | message("CUDNN found ${cudnn}") 41 | list(APPEND requiredlibs ${cudnn}) 42 | endif() 43 | 44 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 45 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 46 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 47 | 48 | include_directories(${DLIB_PATH}/include) 49 | include_directories(${PLOTCPP_PATH}) 50 | 51 | link_directories(${DLIB_PATH}/lib) 52 | link_directories(${DLIB_PATH}/lib64) 53 | 54 | add_executable(dlib-classify "dlib-classify.cc") 55 | target_link_libraries(dlib-classify optimized dlib debug dlibd) 56 | target_link_libraries(dlib-classify ${requiredlibs}) 57 | 58 | -------------------------------------------------------------------------------- /Chapter07/sharkml/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(sharkml-classify) 3 | 4 | set(SHARK_PATH "" CACHE PATH "path to SharkML install dir") 5 | 6 | if(NOT SHARK_PATH) 7 | message(FATAL_ERROR "Missing SharkML install path, please specify SHARK_PATH") 8 | else() 9 | message("SharkML path is ${SHARK_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(Boost_USE_STATIC_LIBS ON) 23 | set(Boost_USE_STATIC_RUNTIME ON) 24 | find_package(Boost REQUIRED serialization) 25 | 26 | set(CMAKE_CXX_FLAGS "-msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 27 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 28 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 29 | 30 | include_directories(${SHARK_PATH}/include) 31 | include_directories(${PLOTCPP_PATH}) 32 | link_directories(${SHARK_PATH}/lib) 33 | link_directories(${SHARK_PATH}/lib64) 34 | 35 | set(SOURCES sharkml-classify.cc) 36 | 37 | add_executable(sharkml-classify ${SOURCES}) 38 | target_link_libraries(sharkml-classify shark cblas ${Boost_LIBRARIES} stdc++fs ) 39 | 40 | -------------------------------------------------------------------------------- /Chapter07/shogun/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(shogun-classify) 3 | 4 | set(SHOGUN_PATH "" CACHE PATH "path to shogun install dir") 5 | 6 | if(NOT DEFINED SHOGUN_PATH) 7 | message(FATAL_ERROR "Missing Shogun install path, please specify SHOGUN_PATH") 8 | else() 9 | message("Shogun path is ${SHOGUN_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(CMAKE_CXX_FLAGS "-std=c++14 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 23 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 24 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 25 | 26 | 27 | set(requiredlibs "stdc++fs") 28 | list(APPEND requiredlibs "stdc++") 29 | list(APPEND requiredlibs json-c ${PROTOBUF_LIBRARIES} cblas lapack) 30 | 31 | include_directories(${PLOTCPP_PATH}) 32 | include_directories(${SHOGUN_PATH}/include) 33 | link_directories(${SHOGUN_PATH}/lib) 34 | 35 | add_executable(shogun-classify "shogun-classify.cc") 36 | target_link_libraries(shogun-classify shogun ${requiredlibs}) 37 | 38 | -------------------------------------------------------------------------------- /Chapter08/eigen/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(eigen_recommender) 3 | 4 | set(EIGEN_PATH "" CACHE PATH "Path to Eigen") 5 | 6 | if(NOT EIGEN_PATH) 7 | message(FATAL_ERROR "Missing Eigen install path, please specify EIGEN_PATH") 8 | else() 9 | message("Eigen path is ${EIGEN_PATH}") 10 | endif() 11 | 12 | 13 | set(CMAKE_VERBOSE_MAKEFILE ON) 14 | 15 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra ") 16 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 17 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 18 | 19 | set(requiredlibs "stdc++fs") 20 | list(APPEND requiredlibs "stdc++") 21 | 22 | include_directories(${EIGEN_PATH}) 23 | 24 | set(SOURCES 25 | eigen_recommender.cc 26 | data_loader.h) 27 | 28 | add_executable(eigen_recommender ${SOURCES}) 29 | target_link_libraries(eigen_recommender ${requiredlibs}) 30 | 31 | 32 | -------------------------------------------------------------------------------- /Chapter08/eigen/data_loader.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_LOADER_H 2 | #define DATA_LOADER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using Movies = std::map; 11 | 12 | using UserRate = std::pair; 13 | using Ratings = std::unordered_map>; 14 | 15 | Movies LoadMovies(const std::string& path) { 16 | Movies movies; 17 | int32_t row = 0; 18 | std::ifstream indata(path); 19 | if (indata) { 20 | std::string line; 21 | 22 | while (std::getline(indata, line)) { 23 | if (row != 0) { 24 | std::stringstream line_stream(line); 25 | std::string cell; 26 | std::getline(line_stream, cell, ','); 27 | auto id = std::stoi(cell); 28 | std::getline(line_stream, cell, ','); 29 | movies.insert({id, cell}); 30 | } 31 | ++row; 32 | } 33 | } 34 | return movies; 35 | } 36 | 37 | Ratings LoadRatings(const std::string& path) { 38 | Ratings ratings; 39 | std::ifstream indata(path); 40 | if (indata) { 41 | std::string line; 42 | int32_t row = 0; 43 | while (std::getline(indata, line)) { 44 | if (row != 0) { 45 | std::stringstream line_stream(line); 46 | std::string cell; 47 | std::getline(line_stream, cell, ','); 48 | auto user_id = std::stoi(cell); 49 | std::getline(line_stream, cell, ','); 50 | auto movie_id = std::stoi(cell); 51 | std::getline(line_stream, cell, ','); 52 | auto rating = std::stof(cell); 53 | ratings[user_id].push_back({movie_id, rating}); 54 | } 55 | ++row; 56 | } 57 | } 58 | return ratings; 59 | } 60 | 61 | #endif // DATA_LOADER_H 62 | -------------------------------------------------------------------------------- /Chapter08/mlpack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(mlpack-recommender) 3 | 4 | set(MLPACK_PATH "" CACHE PATH "path to mlpack install dir") 5 | 6 | if (NOT MLPACK_PATH) 7 | message(FATAL_ERROR "Missigng mlpack install path, please specify MLPACK_PATH") 8 | else() 9 | message("mlpack path is ${MLPACK_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(Boost_USE_STATIC_LIBS ON) 15 | set(Boost_USE_STATIC_RUNTIME ON) 16 | find_package(Boost REQUIRED serialization) 17 | 18 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 19 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 20 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 21 | 22 | include_directories(${MLPACK_PATH}/include) 23 | link_directories(${MLPACK_PATH}/lib) 24 | link_directories(${MLPACK_PATH}/lib64) 25 | 26 | set(SOURCES mlpack-recommender.cc) 27 | 28 | add_executable(mlpack-recommender ${SOURCES}) 29 | target_link_libraries(mlpack-recommender mlpack armadillo cblas lapack ${Boost_LIBRARIES} stdc++fs ) 30 | 31 | -------------------------------------------------------------------------------- /Chapter08/mlpack/mlpack-recommender.cc: -------------------------------------------------------------------------------- 1 | #include "../eigen/data_loader.h" 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace fs = std::experimental::filesystem; 12 | using DataType = double; 13 | 14 | int main(int argc, char** argv) { 15 | if (argc > 1) { 16 | mlpack::Log::Info.ignoreInput = false; 17 | mlpack::Log::Warn.ignoreInput = false; 18 | // mlpack::Log::Debug.ignoreInput = false; 19 | mlpack::Log::Fatal.ignoreInput = false; 20 | 21 | auto root_path = fs::path(argv[1]); 22 | 23 | std::cout << "Data loading .." << std::endl; 24 | 25 | auto movies_file = root_path / "movies.csv"; 26 | auto movies = LoadMovies(movies_file); 27 | 28 | auto ratings_file = root_path / "ratings.csv"; 29 | auto ratings = LoadRatings(ratings_file); 30 | 31 | std::cout << "Data loaded" << std::endl; 32 | 33 | // The data which the CF constructor takes should be an Armadillo matrix 34 | // (arma::mat ) with three rows. The first row corresponds to users; the 35 | // second row corresponds to items; the third column corresponds to the 36 | // rating. This is a coordinate list format. Or a sparse matrix representing 37 | // (user, item) table 38 | 39 | arma::SpMat ratings_matrix(ratings.size(), movies.size()); 40 | std::vector movie_titles; 41 | { 42 | // merge movies and users 43 | std::cout << "Data merging..." << std::endl; 44 | // fill matrix 45 | movie_titles.resize(movies.size()); 46 | 47 | size_t user_idx = 0; 48 | for (auto& r : ratings) { 49 | for (auto& m : r.second) { 50 | auto mi = movies.find(m.first); 51 | auto movie_idx = std::distance(movies.begin(), mi); 52 | movie_titles[static_cast(movie_idx)] = mi->second; 53 | ratings_matrix(user_idx, movie_idx) = static_cast(m.second); 54 | } 55 | ++user_idx; 56 | } 57 | std::cout << "Data merged" << std::endl; 58 | } 59 | 60 | // factorization rank 61 | size_t n_factors = 100; 62 | 63 | // mlpack to avoid calculating the full rating matrix, do 64 | // nearest neighbor search only on the H matrix, using the observation that 65 | // if the rating matrix X = W*H, then d(X.col(i), X.col(j)) = d(W H.col(i), 66 | // W H.col(j)). This can be seen as nearest neighbor search on the H 67 | // matrix with the Mahalanobis distance. 68 | size_t neighborhood = 50; 69 | 70 | // Non negative matrix factorization with Alternating Least Squares 71 | // approach 72 | mlpack::cf::NMFPolicy decomposition_policy; 73 | 74 | // mlpack::cf::BatchSVDPolicy decomposition_policy; 75 | 76 | // stoping criterions 77 | size_t max_iterations = 20; 78 | double min_residue = 1e-3; 79 | 80 | std::cout << "Training..." << std::endl; 81 | mlpack::cf::CFType cf(ratings_matrix, decomposition_policy, neighborhood, 82 | n_factors, max_iterations, min_residue); 83 | 84 | std::cout << "Training done" << std::endl; 85 | 86 | std::cout << "Predicting..." << std::endl; 87 | arma::Mat recommendations; 88 | // Get 5 recommendations for specified users. 89 | arma::Col users; 90 | users << 1 << 2 << 3; 91 | 92 | cf.GetRecommendations(5, recommendations, users); 93 | std::cout << "Predicting done" << std::endl; 94 | 95 | for (size_t u = 0; u < recommendations.n_cols; ++u) { 96 | std::cout << "User " << users(u) << " recomendations are: "; 97 | for (size_t i = 0; i < recommendations.n_rows; ++i) { 98 | std::cout << movie_titles[recommendations(i, u)] << ";"; 99 | } 100 | std::cout << std::endl; 101 | } 102 | } else { 103 | std::cerr << "Please provider path to the dataset folder\n"; 104 | } 105 | 106 | return 0; 107 | } 108 | -------------------------------------------------------------------------------- /Chapter09/sharkml/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(sharkml-ensemble) 3 | 4 | set(SHARK_PATH "" CACHE PATH "path to SharkML install dir") 5 | 6 | if(NOT SHARK_PATH) 7 | message(FATAL_ERROR "Missing SharkML install path, please specify SHARK_PATH") 8 | else() 9 | message("SharkML path is ${SHARK_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(Boost_USE_STATIC_LIBS ON) 23 | set(Boost_USE_STATIC_RUNTIME ON) 24 | find_package(Boost REQUIRED serialization) 25 | 26 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 27 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 28 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 29 | 30 | include_directories(${SHARK_PATH}/include) 31 | include_directories(${PLOTCPP_PATH}) 32 | link_directories(${SHARK_PATH}/lib) 33 | link_directories(${SHARK_PATH}/lib64) 34 | 35 | set(SOURCES sharkml-ensemble.cc) 36 | 37 | add_executable(sharkml-ensemble ${SOURCES}) 38 | target_link_libraries(sharkml-ensemble shark cblas ${Boost_LIBRARIES} stdc++fs ) 39 | 40 | -------------------------------------------------------------------------------- /Chapter09/shogun/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(shogun-ensemble) 3 | 4 | set(SHOGUN_PATH "" CACHE PATH "path to shogun install dir") 5 | 6 | if(NOT DEFINED SHOGUN_PATH) 7 | message(FATAL_ERROR "Missing Shogun install path, please specify SHOGUN_PATH") 8 | else() 9 | message("Shogun path is ${SHOGUN_PATH}") 10 | endif() 11 | 12 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 13 | 14 | if (NOT PLOTCPP_PATH) 15 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 16 | else() 17 | message("plotcpp path is ${PLOTCPP_PATH}") 18 | endif() 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | set(CMAKE_CXX_FLAGS "-std=c++14 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 23 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 24 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 25 | 26 | 27 | set(requiredlibs "stdc++fs") 28 | list(APPEND requiredlibs "stdc++") 29 | list(APPEND requiredlibs json-c cblas lapack) 30 | 31 | include_directories(${PLOTCPP_PATH}) 32 | include_directories(${SHOGUN_PATH}/include) 33 | link_directories(${SHOGUN_PATH}/lib) 34 | 35 | set(SOURCES 36 | shogun-ensemble.cc 37 | data/data.h 38 | data/data.cc 39 | ) 40 | 41 | add_executable(shogun-ensemble ${SOURCES}) 42 | target_link_libraries(shogun-ensemble shogun ${requiredlibs}) 43 | 44 | -------------------------------------------------------------------------------- /Chapter09/shogun/data/data.cc: -------------------------------------------------------------------------------- 1 | #include "data.h" 2 | 3 | #include 4 | 5 | Values LinSpace(double s, double e, size_t n) { 6 | Values x_values(n); 7 | double step = (e - s) / n; 8 | 9 | double v = s; 10 | for (size_t i = 0; i < n; ++i) { 11 | x_values[i] = v; 12 | v += step; 13 | } 14 | x_values[n - 1] = e; 15 | 16 | return x_values; 17 | } 18 | 19 | std::pair GenerateData(double s, 20 | double e, 21 | size_t n, 22 | size_t seed, 23 | bool noise) { 24 | std::vector x; 25 | std::vector y; 26 | if (noise) { 27 | std::mt19937 re(seed); 28 | std::uniform_real_distribution dist(s, e); 29 | std::normal_distribution noise_dist; 30 | 31 | x.resize(n); 32 | y.resize(n); 33 | for (size_t i = 0; i < n; ++i) { 34 | auto x_val = dist(re); 35 | auto y_val = std::cos(M_PI * x_val) + (noise_dist(re) * 0.3); 36 | x[i] = x_val; 37 | y[i] = y_val; 38 | } 39 | } else { 40 | x = LinSpace(s, e, n); 41 | y.reserve(x.size()); 42 | for (auto x_val : x) { 43 | auto y_val = std::cos(M_PI * x_val); 44 | y.push_back(y_val); 45 | } 46 | } 47 | 48 | return {x, y}; 49 | } 50 | -------------------------------------------------------------------------------- /Chapter09/shogun/data/data.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_H 2 | #define DATA_H 3 | 4 | #include 5 | 6 | using DataType = double; 7 | using Values = std::vector; 8 | 9 | Values LinSpace(double s, double e, size_t n); 10 | 11 | std::pair GenerateData(double s, 12 | double e, 13 | size_t n, 14 | size_t seed, 15 | bool noise); 16 | 17 | #endif // DATA_H 18 | -------------------------------------------------------------------------------- /Chapter10/data/data.cc: -------------------------------------------------------------------------------- 1 | #include "data.h" 2 | 3 | #include 4 | 5 | Values LinSpace(double s, double e, size_t n) { 6 | Values x_values(n); 7 | double step = (e - s) / n; 8 | 9 | double v = s; 10 | for (size_t i = 0; i < n; ++i) { 11 | x_values[i] = v; 12 | v += step; 13 | } 14 | x_values[n - 1] = e; 15 | 16 | return x_values; 17 | } 18 | 19 | std::pair GenerateData(double s, 20 | double e, 21 | size_t n, 22 | size_t seed, 23 | bool noise) { 24 | std::vector x; 25 | std::vector y; 26 | if (noise) { 27 | std::mt19937 re(seed); 28 | std::uniform_real_distribution dist(s, e); 29 | std::normal_distribution noise_dist; 30 | 31 | x.resize(n); 32 | y.resize(n); 33 | for (size_t i = 0; i < n; ++i) { 34 | auto x_val = dist(re); 35 | auto y_val = std::cos(M_PI * x_val) + (noise_dist(re) * 0.3); 36 | x[i] = x_val; 37 | y[i] = y_val; 38 | } 39 | } else { 40 | x = LinSpace(s, e, n); 41 | y.reserve(x.size()); 42 | for (auto x_val : x) { 43 | auto y_val = std::cos(M_PI * x_val); 44 | y.push_back(y_val); 45 | } 46 | } 47 | 48 | return {x, y}; 49 | } 50 | -------------------------------------------------------------------------------- /Chapter10/data/data.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_H 2 | #define DATA_H 3 | 4 | #include 5 | 6 | using DataType = double; 7 | using Values = std::vector; 8 | 9 | Values LinSpace(double s, double e, size_t n); 10 | 11 | std::pair GenerateData(double s, 12 | double e, 13 | size_t n, 14 | size_t seed, 15 | bool noise); 16 | 17 | #endif // DATA_H 18 | -------------------------------------------------------------------------------- /Chapter10/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(mlp-dlib) 3 | 4 | set(DLIB_PATH "" CACHE PATH "path to dlib install dir") 5 | 6 | if (NOT DLIB_PATH) 7 | message(FATAL_ERROR "Missigng Dlib install path, please specify DLIB_PATH") 8 | else() 9 | message("Dlib path is ${DLIB_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(requiredlibs "cblas") 15 | list(APPEND requiredlibs "lapack") 16 | 17 | find_package(CUDA) 18 | if (CUDA_FOUND) 19 | list(APPEND requiredlibs ${CUDA_LIBRARIES}) 20 | list(APPEND requiredlibs ${CUDA_CUBLAS_LIBRARIES}) 21 | list(APPEND requiredlibs ${CUDA_cusolver_LIBRARY}) 22 | list(APPEND requiredlibs ${CUDA_curand_LIBRARY}) 23 | 24 | get_filename_component(cudnn_hint_path "${CUDA_CUBLAS_LIBRARIES}" PATH) 25 | find_library(cudnn cudnn 26 | HINTS ${cudnn_hint_path} ENV CUDNN_LIBRARY_DIR ENV CUDNN_HOME 27 | PATHS /usr/local /usr/local/cuda ENV LD_LIBRARY_PATH 28 | PATH_SUFFIXES lib64 lib x64 29 | ) 30 | message("CUDNN found ${cudnn}") 31 | list(APPEND requiredlibs ${cudnn}) 32 | endif() 33 | 34 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 35 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 36 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 37 | 38 | include_directories(${DLIB_PATH}/include) 39 | link_directories(${DLIB_PATH}/lib) 40 | link_directories(${DLIB_PATH}/lib64) 41 | 42 | set(SOURCES 43 | mlp-dlib.cc 44 | ../data/data.h 45 | ../data/data.cc 46 | ) 47 | 48 | add_executable(mlp-dlib ${SOURCES}) 49 | target_link_libraries(mlp-dlib optimized dlib debug dlibd) 50 | target_link_libraries(mlp-dlib ${requiredlibs}) 51 | 52 | 53 | -------------------------------------------------------------------------------- /Chapter10/dlib/mlp-dlib.cc: -------------------------------------------------------------------------------- 1 | #include "../data/data.h" 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | int main() { 10 | using namespace dlib; 11 | 12 | size_t n = 10000; 13 | size_t seed = 45345; 14 | auto data = GenerateData(-1.5, 1.5, n, seed, false); 15 | 16 | std::vector> x(n); 17 | std::vector> y_data(n); 18 | 19 | for (size_t i = 0; i < n; ++i) { 20 | x[i].set_size(1, 1); 21 | x[i](0, 0) = data.first[i]; 22 | 23 | y_data[i].set_size(1, 1); 24 | y_data[i](0, 0) = data.second[i]; 25 | } 26 | 27 | // normalize data 28 | vector_normalizer> normalizer_x; 29 | vector_normalizer> normalizer_y; 30 | 31 | // let the normalizer learn the mean and standard deviation of the samples 32 | normalizer_x.train(x); 33 | normalizer_y.train(y_data); 34 | 35 | std::vector y(n); 36 | 37 | // now normalize each sample 38 | for (size_t i = 0; i < x.size(); ++i) { 39 | x[i] = normalizer_x(x[i]); 40 | y_data[i] = normalizer_y(y_data[i]); 41 | y[i] = static_cast(y_data[i](0, 0)); 42 | } 43 | 44 | using NetworkType = loss_mean_squared< 45 | fc<1, htan>>>>>>>>>; 46 | NetworkType network; 47 | float weight_decay = 0.0001f; 48 | float momentum = 0.5f; 49 | sgd solver(weight_decay, momentum); 50 | dnn_trainer trainer(network, solver); 51 | trainer.set_learning_rate(0.01); 52 | trainer.set_learning_rate_shrink_factor(1); // disable learning rate changes 53 | trainer.set_mini_batch_size(64); 54 | trainer.set_max_num_epochs(500); 55 | trainer.be_verbose(); 56 | trainer.train(x, y); 57 | network.clean(); 58 | 59 | // auto predictions = network(new_x); 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Chapter10/pytorch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0 FATAL_ERROR) 2 | project(lenet-pytorch) 3 | 4 | find_package(Torch REQUIRED) 5 | find_package(OpenCV REQUIRED) 6 | 7 | set(CMAKE_VERBOSE_MAKEFILE ON) 8 | set(CMAKE_CXX_STANDARD 17) 9 | 10 | include_directories(${TORCH_INCLUDE_DIRS}) 11 | 12 | set(CMAKE_CXX_FLAGS "-msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter -pthread -fopenmp") 13 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 14 | set(CMAKE_CXX_FLAGS_DEBUG "-ggdb -O0") 15 | 16 | set(SOURCE_FILES main.cpp 17 | mnistdataset.h 18 | mnistdataset.cpp 19 | lenet5.h 20 | lenet5.cpp) 21 | 22 | set(REQUIRED_LIBS "stdc++fs") 23 | list(APPEND REQUIRED_LIBS ${TORCH_LIBRARIES}) 24 | list(APPEND REQUIRED_LIBS ${OpenCV_LIBS}) 25 | 26 | 27 | add_executable("${CMAKE_PROJECT_NAME}" ${SOURCE_FILES}) 28 | target_link_libraries("${CMAKE_PROJECT_NAME}" ${REQUIRED_LIBS}) 29 | 30 | -------------------------------------------------------------------------------- /Chapter10/pytorch/lenet5.cpp: -------------------------------------------------------------------------------- 1 | #include "lenet5.h" 2 | 3 | static std::vector k_size = {2, 2}; 4 | static std::vector p_size = {0, 0}; 5 | static c10::optional divisor_override; 6 | 7 | LeNet5Impl::LeNet5Impl() { 8 | conv_ = torch::nn::Sequential( 9 | torch::nn::Conv2d(torch::nn::Conv2dOptions(1, 6, 5)), 10 | torch::nn::Functional(torch::tanh), 11 | torch::nn::Functional(torch::avg_pool2d, 12 | /*kernel_size*/ torch::IntArrayRef(k_size), 13 | /*stride*/ torch::IntArrayRef(k_size), 14 | /*padding*/ torch::IntArrayRef(p_size), 15 | /*ceil_mode*/ false, 16 | /*count_include_pad*/ false, 17 | divisor_override), 18 | torch::nn::Conv2d(torch::nn::Conv2dOptions(6, 16, 5)), 19 | torch::nn::Functional(torch::tanh), 20 | torch::nn::Functional(torch::avg_pool2d, 21 | /*kernel_size*/ torch::IntArrayRef(k_size), 22 | /*stride*/ torch::IntArrayRef(k_size), 23 | /*padding*/ torch::IntArrayRef(p_size), 24 | /*ceil_mode*/ false, 25 | /*count_include_pad*/ false, 26 | divisor_override), 27 | torch::nn::Conv2d(torch::nn::Conv2dOptions(16, 120, 5)), 28 | torch::nn::Functional(torch::tanh)); 29 | register_module("conv", conv_); 30 | 31 | full_ = torch::nn::Sequential( 32 | torch::nn::Linear(torch::nn::LinearOptions(120, 84)), 33 | torch::nn::Functional(torch::tanh), 34 | torch::nn::Linear(torch::nn::LinearOptions(84, 10))); 35 | register_module("full", full_); 36 | } 37 | 38 | torch::Tensor LeNet5Impl::forward(at::Tensor x) { 39 | auto output = conv_->forward(x); 40 | output = output.view({x.size(0), -1}); 41 | output = full_->forward(output); 42 | // not all of the functions can be used with torch::nn::Functional class 43 | // because of same names 44 | output = torch::log_softmax(output, -1); 45 | return output; 46 | } 47 | -------------------------------------------------------------------------------- /Chapter10/pytorch/lenet5.h: -------------------------------------------------------------------------------- 1 | #ifndef LENET5_H 2 | #define LENET5_H 3 | 4 | #include 5 | 6 | class LeNet5Impl : public torch::nn::Module { 7 | public: 8 | LeNet5Impl(); 9 | 10 | torch::Tensor forward(torch::Tensor x); 11 | 12 | private: 13 | torch::nn::Sequential conv_; 14 | torch::nn::Sequential full_; 15 | }; 16 | 17 | TORCH_MODULE(LeNet5); 18 | 19 | #endif // LENET5_H 20 | -------------------------------------------------------------------------------- /Chapter10/pytorch/mnistdataset.cpp: -------------------------------------------------------------------------------- 1 | #include "mnistdataset.h" 2 | #include 3 | #include 4 | 5 | namespace { 6 | template 7 | bool read_header(T* out, std::istream& stream) { 8 | auto size = static_cast(sizeof(T)); 9 | T value; 10 | if (!stream.read(reinterpret_cast(&value), size)) { 11 | return false; 12 | } else { 13 | // flip endianness 14 | *out = (value << 24) | ((value << 8) & 0x00FF0000) | 15 | ((value >> 8) & 0X0000FF00) | (value >> 24); 16 | return true; 17 | } 18 | } 19 | 20 | torch::Tensor CvImageToTensor(const cv::Mat& image, torch::DeviceType device) { 21 | assert(image.channels() == 1); 22 | 23 | std::vector dims{static_cast(1), 24 | static_cast(image.rows), 25 | static_cast(image.cols)}; 26 | 27 | torch::Tensor tensor_image = 28 | torch::from_blob( 29 | image.data, torch::IntArrayRef(dims), 30 | torch::TensorOptions().dtype(torch::kFloat).requires_grad(false)) 31 | .clone(); // clone is required to copy data from temporary object 32 | return tensor_image.to(device); 33 | } 34 | 35 | } // namespace 36 | 37 | void MNISTDataset::ReadLabels(const std::string& labels_file_name) { 38 | std::ifstream labels_file(labels_file_name, 39 | std::ios::binary | std::ios::binary); 40 | labels_file.exceptions(std::ifstream::failbit | std::ifstream::badbit); 41 | if (labels_file) { 42 | uint32_t magic_num = 0; 43 | uint32_t num_items = 0; 44 | if (read_header(&magic_num, labels_file) && 45 | read_header(&num_items, labels_file)) { 46 | labels_.resize(static_cast(num_items)); 47 | labels_file.read(reinterpret_cast(labels_.data()), num_items); 48 | } 49 | } 50 | } 51 | 52 | void MNISTDataset::ReadImages(const std::string& images_file_name) { 53 | std::ifstream labels_file(images_file_name, 54 | std::ios::binary | std::ios::binary); 55 | labels_file.exceptions(std::ifstream::failbit | std::ifstream::badbit); 56 | if (labels_file) { 57 | uint32_t magic_num = 0; 58 | uint32_t num_items = 0; 59 | rows_ = 0; 60 | columns_ = 0; 61 | if (read_header(&magic_num, labels_file) && 62 | read_header(&num_items, labels_file) && 63 | read_header(&rows_, labels_file) && 64 | read_header(&columns_, labels_file)) { 65 | assert(num_items == labels_.size()); 66 | images_.resize(num_items); 67 | cv::Mat img(static_cast(rows_), static_cast(columns_), CV_8UC1); 68 | 69 | for (uint32_t i = 0; i < num_items; ++i) { 70 | labels_file.read(reinterpret_cast(img.data), 71 | static_cast(img.size().area())); 72 | img.convertTo(images_[i], CV_32F); 73 | images_[i] /= 255; // normalize 74 | cv::resize(images_[i], images_[i], 75 | cv::Size(32, 32)); // Resize to 32x32 size 76 | } 77 | } 78 | } 79 | } 80 | 81 | MNISTDataset::MNISTDataset(const std::string& images_file_name, 82 | const std::string& labels_file_name, 83 | torch::DeviceType device) 84 | : device_(device) { 85 | ReadLabels(labels_file_name); 86 | ReadImages(images_file_name); 87 | } 88 | 89 | void MNISTDataset::ShowItem(size_t index) const { 90 | cv::imshow(std::to_string(labels_[index]), images_[index]); 91 | cv::waitKey(0); 92 | cv::destroyAllWindows(); 93 | } 94 | 95 | torch::data::Example<> MNISTDataset::get(size_t index) { 96 | return {CvImageToTensor(images_[index], device_), 97 | torch::tensor(static_cast(labels_[index]), 98 | torch::TensorOptions() 99 | .dtype(torch::kLong) 100 | .device(device_))}; 101 | } 102 | 103 | torch::optional MNISTDataset::size() const { 104 | return labels_.size(); 105 | } 106 | -------------------------------------------------------------------------------- /Chapter10/pytorch/mnistdataset.h: -------------------------------------------------------------------------------- 1 | #ifndef MNISTDATASET_H 2 | #define MNISTDATASET_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class MNISTDataset : public torch::data::Dataset { 10 | public: 11 | MNISTDataset(const std::string& images_file_name, 12 | const std::string& labels_file_name, 13 | torch::DeviceType device); 14 | 15 | // test only method 16 | void ShowItem(size_t index) const; 17 | 18 | // torch::data::Dataset implementation 19 | torch::data::Example<> get(size_t index) override; 20 | torch::optional size() const override; 21 | 22 | private: 23 | void ReadLabels(const std::string& labels_file_name); 24 | void ReadImages(const std::string& images_file_name); 25 | 26 | uint32_t rows_ = 0; 27 | uint32_t columns_ = 0; 28 | std::vector labels_; 29 | std::vector images_; 30 | torch::DeviceType device_; 31 | }; 32 | 33 | #endif // MNISTDATASET_H 34 | -------------------------------------------------------------------------------- /Chapter10/sharkml/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(mlp-sharkml) 3 | 4 | set(SHARK_PATH "" CACHE PATH "path to SharkML install dir") 5 | 6 | if(NOT SHARK_PATH) 7 | message(FATAL_ERROR "Missing SharkML install path, please specify SHARK_PATH") 8 | else() 9 | message("SharkML path is ${SHARK_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(Boost_USE_STATIC_LIBS ON) 15 | set(Boost_USE_STATIC_RUNTIME ON) 16 | find_package(Boost REQUIRED serialization) 17 | 18 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 19 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 20 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 21 | 22 | include_directories(${SHARK_PATH}/include) 23 | link_directories(${SHARK_PATH}/lib) 24 | link_directories(${SHARK_PATH}/lib64) 25 | 26 | set(SOURCES 27 | mlp-sharkml.cc 28 | ../data/data.h 29 | ../data/data.cc 30 | ) 31 | 32 | add_executable(mlp-sharkml ${SOURCES}) 33 | target_link_libraries(mlp-sharkml shark cblas ${Boost_LIBRARIES}) 34 | 35 | -------------------------------------------------------------------------------- /Chapter10/sharkml/mlp-sharkml.cc: -------------------------------------------------------------------------------- 1 | #include "../data/data.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace shark; 11 | 12 | int main() { 13 | size_t n = 10000; 14 | size_t seed = 45345; 15 | auto data = GenerateData(-1.5, 1.5, n, seed, false); 16 | std::vector x_data(n); 17 | std::vector y_data(n); 18 | for (size_t i = 0; i < n; ++i) { 19 | x_data[i] = RealVector(1, data.first[i]); 20 | y_data[i] = RealVector(1, data.second[i]); 21 | } 22 | 23 | Data x = createDataFromRange(x_data); 24 | Data y = createDataFromRange(y_data); 25 | RegressionDataset train_data(x, y); 26 | 27 | using DenseLayer = LinearModel; 28 | 29 | DenseLayer layer1(1, 32, true); 30 | DenseLayer layer2(32, 16, true); 31 | DenseLayer layer3(16, 8, true); 32 | 33 | LinearModel output(8, 1, true); 34 | auto network = layer1 >> layer2 >> layer3 >> output; 35 | 36 | SquaredLoss<> loss; 37 | ErrorFunction<> error(train_data, &network, &loss, true); 38 | TwoNormRegularizer<> regularizer(error.numberOfVariables()); 39 | double weight_decay = 0.0001; 40 | error.setRegularizer(weight_decay, ®ularizer); 41 | error.init(); 42 | 43 | initRandomNormal(network, 0.001); 44 | 45 | SteepestDescent<> optimizer; 46 | optimizer.setMomentum(0.5); 47 | optimizer.setLearningRate(0.01); 48 | optimizer.init(error); 49 | 50 | size_t epochs = 1000; 51 | size_t iterations = train_data.numberOfBatches(); 52 | for (size_t epoch = 0; epoch != epochs; ++epoch) { 53 | double avg_loss = 0.0; 54 | for (size_t i = 0; i != iterations; ++i) { 55 | optimizer.step(error); 56 | if (i % 100 == 0) { 57 | avg_loss += optimizer.solution().value; 58 | } 59 | } 60 | avg_loss /= iterations; 61 | std::cout << "Epoch " << epoch << " | Avg. Loss " << avg_loss << std::endl; 62 | } 63 | network.setParameterVector(optimizer.solution().point); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Chapter10/shogun/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(mlp-shogun) 3 | 4 | set(SHOGUN_PATH "" CACHE PATH "path to shogun install dir") 5 | 6 | if(NOT DEFINED SHOGUN_PATH) 7 | message(FATAL_ERROR "Missing Shogun install path, please specify SHOGUN_PATH") 8 | else() 9 | message("Shogun path is ${SHOGUN_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(CMAKE_CXX_FLAGS "-std=c++14 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 15 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 16 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 17 | 18 | include_directories(${SHOGUN_PATH}/include) 19 | link_directories(${SHOGUN_PATH}/lib) 20 | 21 | set(SOURCES 22 | mlp-shogun.cc 23 | ../data/data.h 24 | ../data/data.cc 25 | ) 26 | 27 | add_executable(mlp-shogun ${SOURCES}) 28 | target_link_libraries(mlp-shogun shogun) 29 | target_link_libraries(mlp-shogun json-c ${PROTOBUF_LIBRARIES} cblas lapack) 30 | 31 | -------------------------------------------------------------------------------- /Chapter10/shogun/mlp-shogun.cc: -------------------------------------------------------------------------------- 1 | #include "../data/data.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace shogun; 15 | 16 | int main(int, char*[]) { 17 | shogun::init_shogun_with_defaults(); 18 | shogun::sg_io->set_loglevel(shogun::MSG_DEBUG); 19 | 20 | size_t n = 10000; 21 | size_t seed = 45345; 22 | auto data = GenerateData(-1.5, 1.5, n, seed, false); 23 | 24 | SGMatrix x_values(1, static_cast(n)); 25 | SGVector y_values(static_cast(n)); 26 | 27 | for (size_t i = 0; i < n; ++i) { 28 | x_values.set_element(data.first[i], 0, static_cast(i)); 29 | y_values.set_element(data.second[i], static_cast(i)); 30 | } 31 | 32 | auto x = some>(x_values); 33 | auto y = some(y_values); 34 | 35 | auto dimensions = x->get_num_features(); 36 | auto layers = some(); 37 | layers = wrap(layers->input(dimensions)); 38 | layers = wrap(layers->rectified_linear(32)); 39 | layers = wrap(layers->rectified_linear(16)); 40 | layers = wrap(layers->rectified_linear(8)); 41 | layers = wrap(layers->linear(1)); 42 | auto all_layers = layers->done(); 43 | 44 | auto network = some(all_layers); 45 | network->quick_connect(); 46 | network->initialize_neural_network(); 47 | 48 | network->set_optimization_method(NNOM_GRADIENT_DESCENT); 49 | network->set_gd_mini_batch_size(64); 50 | network->set_l2_coefficient(0.0001); // regularization 51 | network->set_max_num_epochs(500); 52 | network->set_epsilon(0.0); // convergence criteria 53 | network->set_gd_learning_rate(0.01); 54 | network->set_gd_momentum(0.5); 55 | 56 | network->set_labels(y); 57 | // loss function is not configurable 58 | network->train(x); 59 | 60 | auto labels_predict = network->apply_regression(x); 61 | auto err = some(); 62 | auto mse = err->evaluate(labels_predict, y); 63 | 64 | std::cout << "Total Loss " << mse << std::endl; 65 | 66 | shogun::exit_shogun(); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Chapter11/pytorch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0 FATAL_ERROR) 2 | project(rnn-pytorch) 3 | 4 | find_package(Torch REQUIRED) 5 | 6 | set(CMAKE_VERBOSE_MAKEFILE ON) 7 | set(CMAKE_CXX_STANDARD 17) 8 | 9 | include_directories(${TORCH_INCLUDE_DIRS}) 10 | include_directories(${Boost_INCLUDE_DIRS}) 11 | 12 | set(CMAKE_CXX_FLAGS "-msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter -pthread -fopenmp") 13 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 14 | set(CMAKE_CXX_FLAGS_DEBUG "-ggdb -O0") 15 | 16 | set(SOURCE_FILES main.cc 17 | imdbdataset.h 18 | imdbdataset.cc 19 | rnn.h 20 | rnn.cc 21 | glovedict.h 22 | glovedict.cc 23 | imdbreader.cc 24 | imdbreader.h 25 | vocabulary.h 26 | vocabulary.cc 27 | ) 28 | 29 | set(REQUIRED_LIBS "stdc++fs") 30 | list(APPEND REQUIRED_LIBS ${TORCH_LIBRARIES}) 31 | 32 | 33 | add_executable("${CMAKE_PROJECT_NAME}" ${SOURCE_FILES}) 34 | target_link_libraries("${CMAKE_PROJECT_NAME}" ${REQUIRED_LIBS}) 35 | 36 | -------------------------------------------------------------------------------- /Chapter11/pytorch/glovedict.cc: -------------------------------------------------------------------------------- 1 | #include "glovedict.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | GloveDict::GloveDict(const std::string& file_name, int64_t vec_size) { 8 | std::ifstream file; 9 | file.exceptions(std::ifstream::badbit); 10 | file.open(file_name); 11 | if (file) { 12 | auto sizes = {static_cast(vec_size)}; 13 | std::string line; 14 | std::vector vec(static_cast(vec_size)); 15 | unknown_ = torch::zeros(sizes, torch::dtype(torch::kFloat)); 16 | std::string key; 17 | std::string token; 18 | while (std::getline(file, line)) { 19 | if (!line.empty()) { 20 | std::stringstream line_stream(line); 21 | size_t num = 0; 22 | while (std::getline(line_stream, token, ' ')) { 23 | if (num == 0) { 24 | key = token; 25 | } else { 26 | vec[num - 1] = std::stof(token); 27 | } 28 | ++num; 29 | } 30 | assert(num == (static_cast(vec_size) + 1)); 31 | torch::Tensor tvec = torch::from_blob( 32 | vec.data(), sizes, torch::TensorOptions().dtype(torch::kFloat)); 33 | dict_[key] = tvec.clone(); 34 | } 35 | } 36 | } 37 | } 38 | 39 | torch::Tensor GloveDict::GetUnknown() const { 40 | return unknown_; 41 | } 42 | 43 | torch::Tensor GloveDict::Get(const std::string& key) const { 44 | auto i = dict_.find(key); 45 | if (i != dict_.end()) { 46 | return i->second; 47 | } else { 48 | return torch::empty({0}); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Chapter11/pytorch/glovedict.h: -------------------------------------------------------------------------------- 1 | #ifndef GLOVEDICT_H 2 | #define GLOVEDICT_H 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | class GloveDict { 10 | public: 11 | GloveDict(const std::string& file_name, int64_t vec_size); 12 | torch::Tensor Get(const std::string& key) const; 13 | torch::Tensor GetUnknown() const; 14 | 15 | private: 16 | torch::Tensor unknown_; 17 | std::unordered_map dict_; 18 | }; 19 | 20 | #endif // GLOVEDICT_H 21 | -------------------------------------------------------------------------------- /Chapter11/pytorch/imdbdataset.cc: -------------------------------------------------------------------------------- 1 | #include "imdbdataset.h" 2 | #include 3 | #include 4 | 5 | ImdbDataset::ImdbDataset(ImdbReader* reader, 6 | Vocabulary* vocabulary, 7 | torch::DeviceType device) 8 | : device_(device), reader_(reader), vocabulary_(vocabulary) {} 9 | 10 | ImdbExample ImdbDataset::get(size_t index) { 11 | torch::Tensor target; 12 | const ImdbReader::Review* review{nullptr}; 13 | if (index < reader_->GetPosSize()) { 14 | review = &reader_->GetPos(index); 15 | target = torch::tensor( 16 | 1.f, torch::dtype(torch::kFloat).device(device_).requires_grad(false)); 17 | } else { 18 | review = &reader_->GetNeg(index - reader_->GetPosSize()); 19 | target = torch::tensor( 20 | 0.f, torch::dtype(torch::kFloat).device(device_).requires_grad(false)); 21 | } 22 | // encode text 23 | std::vector indices(reader_->GetMaxSize()); 24 | size_t i = 0; 25 | 26 | for (auto& w : (*review)) { 27 | indices[i] = vocabulary_->GetIndex(w); 28 | ++i; 29 | } 30 | // pad text to same size 31 | for (; i < indices.size(); ++i) { 32 | indices[i] = vocabulary_->GetPaddingIndex(); 33 | } 34 | 35 | auto data = torch::from_blob(indices.data(), 36 | {static_cast(reader_->GetMaxSize())}, 37 | torch::dtype(torch::kLong).requires_grad(false)); 38 | auto data_len = 39 | torch::tensor(static_cast(review->size()), 40 | torch::dtype(torch::kLong).requires_grad(false)); 41 | return {{data.clone().to(device_), data_len.clone()}, target.squeeze()}; 42 | } 43 | 44 | torch::optional ImdbDataset::size() const { 45 | return reader_->GetPosSize() + reader_->GetNegSize(); 46 | } 47 | -------------------------------------------------------------------------------- /Chapter11/pytorch/imdbdataset.h: -------------------------------------------------------------------------------- 1 | #ifndef MNISTDATASET_H 2 | #define MNISTDATASET_H 3 | 4 | #include "imdbreader.h" 5 | #include "vocabulary.h" 6 | 7 | #include 8 | 9 | #include 10 | 11 | using ImdbData = std::pair; 12 | using ImdbExample = torch::data::Example; 13 | 14 | class ImdbDataset : public torch::data::Dataset { 15 | public: 16 | ImdbDataset(ImdbReader* reader, 17 | Vocabulary* vocabulary, 18 | torch::DeviceType device); 19 | 20 | // torch::data::Dataset implementation 21 | ImdbExample get(size_t index) override; 22 | torch::optional size() const override; 23 | 24 | private: 25 | torch::DeviceType device_{torch::DeviceType::CPU}; 26 | ImdbReader* reader_{nullptr}; 27 | Vocabulary* vocabulary_{nullptr}; 28 | }; 29 | 30 | #endif // MNISTDATASET_H 31 | -------------------------------------------------------------------------------- /Chapter11/pytorch/imdbreader.cc: -------------------------------------------------------------------------------- 1 | #include "imdbreader.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace fs = std::experimental::filesystem; 9 | 10 | ImdbReader::ImdbReader(const std::string& root_path) { 11 | auto root = fs::path(root_path); 12 | auto neg_path = root / "neg"; 13 | auto pos_path = root / "pos"; 14 | if (fs::exists(neg_path) && fs::exists(pos_path)) { 15 | auto neg = std::async(std::launch::async, 16 | [&]() { ReadDirectory(neg_path, neg_samples_); }); 17 | auto pos = std::async(std::launch::async, 18 | [&]() { ReadDirectory(pos_path, pos_samples_); }); 19 | neg.get(); 20 | pos.get(); 21 | } else { 22 | throw std::invalid_argument("ImdbReader incorrect path"); 23 | } 24 | } 25 | 26 | const ImdbReader::Review& ImdbReader::GetPos(size_t index) const { 27 | return pos_samples_.at(index); 28 | } 29 | 30 | const ImdbReader::Review& ImdbReader::GetNeg(size_t index) const { 31 | return neg_samples_.at(index); 32 | } 33 | 34 | size_t ImdbReader::GetPosSize() const { 35 | return pos_samples_.size(); 36 | } 37 | 38 | size_t ImdbReader::GetNegSize() const { 39 | return neg_samples_.size(); 40 | } 41 | 42 | size_t ImdbReader::GetMaxSize() const { 43 | return max_size_; 44 | } 45 | 46 | void ImdbReader::ReadDirectory(const std::string& path, Reviews& reviews) { 47 | std::regex re("[^a-zA-Z0-9]"); 48 | std::sregex_token_iterator end; 49 | 50 | for (auto& entry : fs::directory_iterator(path)) { 51 | if (fs::is_regular_file(entry)) { 52 | std::ifstream file(entry.path()); 53 | if (file) { 54 | std::string text; 55 | { 56 | std::stringstream buffer; 57 | buffer << file.rdbuf(); 58 | text = buffer.str(); 59 | } 60 | 61 | std::sregex_token_iterator token(text.begin(), text.end(), re, -1); 62 | 63 | Review words; 64 | for (; token != end; ++token) { 65 | if (token->length() > 1) { // don't use one letter words 66 | words.push_back(*token); 67 | } 68 | } 69 | max_size_ = std::max(max_size_, words.size()); 70 | reviews.push_back(std::move(words)); 71 | } 72 | } 73 | } 74 | } 75 | 76 | void GetWordsFrequencies(const ImdbReader& reader, 77 | WordsFrequencies& frequencies) { 78 | for (size_t i = 0; i < reader.GetPosSize(); ++i) { 79 | const ImdbReader::Review& review = reader.GetPos(i); 80 | for (auto& word : review) { 81 | frequencies[word] += 1; 82 | } 83 | } 84 | 85 | for (size_t i = 0; i < reader.GetNegSize(); ++i) { 86 | const ImdbReader::Review& review = reader.GetNeg(i); 87 | for (auto& word : review) { 88 | frequencies[word] += 1; 89 | } 90 | } 91 | } 92 | 93 | void SelectTopFrequencies(WordsFrequencies& vocab, int64_t new_size) { 94 | using FreqItem = std::pair; 95 | std::vector freq_items; 96 | freq_items.reserve(vocab.size()); 97 | auto i = vocab.begin(); 98 | auto e = vocab.end(); 99 | for (; i != e; ++i) { 100 | freq_items.push_back({i->second, i}); 101 | } 102 | 103 | std::sort( 104 | freq_items.begin(), freq_items.end(), 105 | [](const FreqItem& a, const FreqItem& b) { return a.first < b.first; }); 106 | 107 | std::reverse(freq_items.begin(), freq_items.end()); 108 | 109 | freq_items.resize(static_cast(new_size)); 110 | 111 | WordsFrequencies new_vocab; 112 | 113 | for (auto& item : freq_items) { 114 | new_vocab.insert({item.second->first, item.first}); 115 | } 116 | 117 | vocab = new_vocab; 118 | } 119 | -------------------------------------------------------------------------------- /Chapter11/pytorch/imdbreader.h: -------------------------------------------------------------------------------- 1 | #ifndef IMDBREADER_H 2 | #define IMDBREADER_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class ImdbReader { 9 | public: 10 | ImdbReader(const std::string& root_path); 11 | 12 | size_t GetPosSize() const; 13 | size_t GetNegSize() const; 14 | size_t GetMaxSize() const; 15 | 16 | using Review = std::vector; 17 | const Review& GetPos(size_t index) const; 18 | const Review& GetNeg(size_t index) const; 19 | 20 | private: 21 | using Reviews = std::vector; 22 | 23 | void ReadDirectory(const std::string& path, Reviews& reviews); 24 | 25 | private: 26 | Reviews pos_samples_; 27 | Reviews neg_samples_; 28 | size_t max_size_{0}; 29 | }; 30 | 31 | using WordsFrequencies = std::unordered_map; 32 | void GetWordsFrequencies(const ImdbReader& reader, 33 | WordsFrequencies& frequencies); 34 | 35 | void SelectTopFrequencies(WordsFrequencies& vocab, int64_t new_size); 36 | #endif // IMDBREADER_H 37 | -------------------------------------------------------------------------------- /Chapter11/pytorch/rnn.h: -------------------------------------------------------------------------------- 1 | #ifndef LENET5_H 2 | #define LENET5_H 3 | 4 | #include 5 | 6 | class PackedLSTMImpl : public torch::nn::Module { 7 | public: 8 | explicit PackedLSTMImpl(const torch::nn::LSTMOptions& options); 9 | 10 | std::vector flat_weights() const; 11 | 12 | torch::nn::RNNOutput forward(const torch::Tensor& input, 13 | const torch::Tensor& lengths, 14 | torch::Tensor state = {}); 15 | 16 | const torch::nn::LSTMOptions& options() const; 17 | 18 | private: 19 | torch::nn::LSTM rnn_ = nullptr; 20 | }; 21 | 22 | TORCH_MODULE(PackedLSTM); 23 | 24 | class SentimentRNNImpl : public torch::nn::Module { 25 | public: 26 | SentimentRNNImpl(int64_t vocab_size, 27 | int64_t embedding_dim, 28 | int64_t hidden_dim, 29 | int64_t output_dim, 30 | int64_t n_layers, 31 | bool bidirectional, 32 | double dropout, 33 | int64_t pad_idx); 34 | 35 | void SetPretrainedEmbeddings(const torch::Tensor& weights); 36 | 37 | torch::Tensor forward(const torch::Tensor& text, const at::Tensor& length); 38 | 39 | private: 40 | int64_t pad_idx_{-1}; 41 | torch::autograd::Variable embeddings_weights_; 42 | PackedLSTM rnn_ = nullptr; 43 | torch::nn::Linear fc_ = nullptr; 44 | torch::nn::Dropout dropout_ = nullptr; 45 | }; 46 | 47 | TORCH_MODULE(SentimentRNN); 48 | 49 | #endif // LENET5_H 50 | -------------------------------------------------------------------------------- /Chapter11/pytorch/vocabulary.cc: -------------------------------------------------------------------------------- 1 | #include "vocabulary.h" 2 | 3 | Vocabulary::Vocabulary(const WordsFrequencies& words_frequencies, 4 | const GloveDict& glove_dict) { 5 | words_to_index_map_.reserve(words_frequencies.size()); 6 | embeddings_.reserve(words_frequencies.size()); 7 | 8 | unk_index_ = 0; 9 | pad_index_ = unk_index_ + 1; 10 | 11 | embeddings_.push_back(glove_dict.GetUnknown()); // unknown 12 | embeddings_.push_back(glove_dict.GetUnknown()); // padding 13 | 14 | size_t index = pad_index_ + 1; 15 | for (auto& wf : words_frequencies) { 16 | auto embedding = glove_dict.Get(wf.first); 17 | if (embedding.size(0) != 0) { 18 | embeddings_.push_back(embedding); 19 | words_to_index_map_.insert({wf.first, index}); 20 | ++index; 21 | } else { 22 | words_to_index_map_.insert({wf.first, unk_index_}); 23 | } 24 | } 25 | } 26 | 27 | int64_t Vocabulary::GetIndex(const std::string& word) const { 28 | auto i = words_to_index_map_.find(word); 29 | if (i != words_to_index_map_.end()) { 30 | return static_cast(i->second); 31 | } else { 32 | return static_cast(unk_index_); 33 | } 34 | } 35 | 36 | int64_t Vocabulary::GetPaddingIndex() const { 37 | return static_cast(pad_index_); 38 | } 39 | 40 | at::Tensor Vocabulary::GetEmbeddings() const { 41 | at::Tensor weights = torch::stack(embeddings_); 42 | return weights; 43 | } 44 | 45 | int64_t Vocabulary::GetEmbeddingsCount() const { 46 | return static_cast(embeddings_.size()); 47 | } 48 | -------------------------------------------------------------------------------- /Chapter11/pytorch/vocabulary.h: -------------------------------------------------------------------------------- 1 | #ifndef VOCABULARY_H 2 | #define VOCABULARY_H 3 | 4 | #include "glovedict.h" 5 | #include "imdbreader.h" 6 | 7 | #include 8 | #include 9 | 10 | class Vocabulary { 11 | public: 12 | Vocabulary(const WordsFrequencies& words_frequencies, 13 | const GloveDict& glove_dict); 14 | 15 | int64_t GetIndex(const std::string& word) const; 16 | int64_t GetPaddingIndex() const; 17 | torch::Tensor GetEmbeddings() const; 18 | int64_t GetEmbeddingsCount() const; 19 | 20 | private: 21 | std::unordered_map words_to_index_map_; 22 | std::vector embeddings_; 23 | size_t unk_index_; 24 | size_t pad_index_; 25 | }; 26 | 27 | #endif // VOCABULARY_H 28 | -------------------------------------------------------------------------------- /Chapter12/caffe2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0 FATAL_ERROR) 2 | project(caffe2-load) 3 | set(CMAKE_VERBOSE_MAKEFILE ON) 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | find_package(Protobuf REQUIRED) 7 | find_package(Torch REQUIRED) 8 | find_package(ONNX REQUIRED) 9 | find_package(OpenCV REQUIRED) 10 | 11 | 12 | include_directories(${PROTOBUF_INCLUDE_DIRS}) 13 | include_directories(${ONNX_INCLUDE_DIRS}) 14 | include_directories(${OpenCV_INCLUDE_DIRS}) 15 | include_directories(${TORCH_INCLUDE_DIRS}) 16 | 17 | set(CMAKE_CXX_FLAGS "-msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter -pthread -fopenmp -fPIC") 18 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 19 | set(CMAKE_CXX_FLAGS_DEBUG "-ggdb -O0") 20 | 21 | add_definitions(-DONNX_NAMESPACE=onnx_torch) 22 | add_definitions(-DONNX_ML=1) 23 | 24 | set(SOURCE_FILES main.cc) 25 | 26 | set(REQUIRED_LIBS "") 27 | list(APPEND REQUIRED_LIBS ${TORCH_LIBRARIES}) 28 | list(APPEND REQUIRED_LIBS ${PROTOBUF_LIBRARY}) 29 | list(APPEND REQUIRED_LIBS ${OpenCV_LIBRARIES}) 30 | 31 | add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES}) 32 | target_link_libraries(${CMAKE_PROJECT_NAME} ${REQUIRED_LIBS}) 33 | 34 | -------------------------------------------------------------------------------- /Chapter12/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-save) 3 | 4 | set(DLIB_PATH "" CACHE PATH "path to dlib install dir") 5 | 6 | if (NOT DLIB_PATH) 7 | message(FATAL_ERROR "Missigng Dlib install path, please specify DLIB_PATH") 8 | else() 9 | message("Dlib path is ${DLIB_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(requiredlibs "cblas") 15 | list(APPEND requiredlibs "lapack") 16 | 17 | find_package(CUDA) 18 | if (CUDA_FOUND) 19 | list(APPEND requiredlibs ${CUDA_LIBRARIES}) 20 | list(APPEND requiredlibs ${CUDA_CUBLAS_LIBRARIES}) 21 | list(APPEND requiredlibs ${CUDA_cusolver_LIBRARY}) 22 | list(APPEND requiredlibs ${CUDA_curand_LIBRARY}) 23 | 24 | get_filename_component(cudnn_hint_path "${CUDA_CUBLAS_LIBRARIES}" PATH) 25 | find_library(cudnn cudnn 26 | HINTS ${cudnn_hint_path} ENV CUDNN_LIBRARY_DIR ENV CUDNN_HOME 27 | PATHS /usr/local /usr/local/cuda ENV LD_LIBRARY_PATH 28 | PATH_SUFFIXES lib64 lib x64 29 | ) 30 | message("CUDNN found ${cudnn}") 31 | list(APPEND requiredlibs ${cudnn}) 32 | endif() 33 | 34 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 35 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 36 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 37 | 38 | include_directories(${DLIB_PATH}/include) 39 | link_directories(${DLIB_PATH}/lib) 40 | link_directories(${DLIB_PATH}/lib64) 41 | 42 | add_executable(${CMAKE_PROJECT_NAME} "dlib-save.cc") 43 | target_link_libraries(${CMAKE_PROJECT_NAME} optimized dlib debug dlibd) 44 | target_link_libraries(${CMAKE_PROJECT_NAME} ${requiredlibs}) 45 | 46 | 47 | -------------------------------------------------------------------------------- /Chapter12/dlib/dlib-save.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | using namespace dlib; 8 | 9 | using NetworkType = loss_mean_squared>>>; 10 | using SampleType = matrix; 11 | using KernelType = linear_kernel; 12 | 13 | float func(float x) { 14 | return 4.f + 0.3f * x; // line coeficients 15 | } 16 | 17 | void TrainAndSaveKRR(const std::vector>& x, 18 | const std::vector& y) { 19 | krr_trainer trainer; 20 | trainer.set_kernel(KernelType()); 21 | decision_function df = trainer.train(x, y); 22 | serialize("dlib-krr.dat") << df; 23 | } 24 | 25 | void LoadAndPredictKRR(const std::vector>& x) { 26 | decision_function df; 27 | 28 | deserialize("dlib-krr.dat") >> df; 29 | 30 | // Predict 31 | 32 | std::cout << "KRR predictions \n"; 33 | for (auto& v : x) { 34 | auto p = df(v); 35 | std::cout << static_cast(p) << std::endl; 36 | } 37 | } 38 | 39 | void TrainAndSaveNetwork(const std::vector>& x, 40 | const std::vector& y) { 41 | NetworkType network; 42 | sgd solver; 43 | dnn_trainer trainer(network, solver); 44 | trainer.set_learning_rate(0.0001); 45 | trainer.set_mini_batch_size(50); 46 | trainer.set_max_num_epochs(300); 47 | trainer.be_verbose(); 48 | trainer.train(x, y); 49 | network.clean(); 50 | 51 | serialize("dlib-net.dat") << network; 52 | net_to_xml(network, "net.xml"); 53 | } 54 | 55 | void LoadAndPredictNetwork(const std::vector>& x) { 56 | NetworkType network; 57 | 58 | deserialize("dlib-net.dat") >> network; 59 | 60 | // Predict 61 | auto predictions = network(x); 62 | 63 | std::cout << "Net predictions \n"; 64 | for (auto p : predictions) { 65 | std::cout << static_cast(p) << std::endl; 66 | } 67 | } 68 | 69 | int main() { 70 | size_t n = 1000; 71 | std::vector> x(n); 72 | std::vector y(n); 73 | 74 | std::random_device rd; 75 | std::mt19937 re(rd()); 76 | std::uniform_real_distribution dist(-1.5, 1.5); 77 | 78 | // generate data 79 | for (size_t i = 0; i < n; ++i) { 80 | x[i].set_size(1, 1); 81 | x[i](0, 0) = i; 82 | 83 | y[i] = func(i) + dist(re); 84 | } 85 | 86 | // normalize data 87 | vector_normalizer> normalizer_x; 88 | // let the normalizer learn the mean and standard deviation of the samples 89 | normalizer_x.train(x); 90 | // now normalize each sample 91 | for (size_t i = 0; i < x.size(); ++i) { 92 | x[i] = normalizer_x(x[i]); 93 | } 94 | 95 | TrainAndSaveNetwork(x, y); 96 | TrainAndSaveKRR(x, y); 97 | 98 | // Generate new data 99 | std::cout << "Target values \n"; 100 | std::vector> new_x(5); 101 | for (size_t i = 0; i < 5; ++i) { 102 | new_x[i].set_size(1, 1); 103 | new_x[i](0, 0) = i; 104 | new_x[i] = normalizer_x(new_x[i]); 105 | std::cout << func(i) << std::endl; 106 | } 107 | 108 | // Predict 109 | LoadAndPredictNetwork(new_x); 110 | LoadAndPredictKRR(new_x); 111 | 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /Chapter12/pytorch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0 FATAL_ERROR) 2 | project(pytorch-save) 3 | 4 | find_package(Torch REQUIRED) 5 | 6 | set(CMAKE_VERBOSE_MAKEFILE ON) 7 | set(CMAKE_CXX_STANDARD 17) 8 | 9 | include_directories(${TORCH_INCLUDE_DIRS}) 10 | 11 | set(CMAKE_CXX_FLAGS "-msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter -pthread -fopenmp") 12 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 13 | set(CMAKE_CXX_FLAGS_DEBUG "-ggdb -O0") 14 | 15 | set(SOURCE_FILES main.cc) 16 | 17 | set(REQUIRED_LIBS "stdc++fs") 18 | list(APPEND REQUIRED_LIBS cblas) 19 | list(APPEND REQUIRED_LIBS ${TORCH_LIBRARIES}) 20 | 21 | 22 | add_executable("${CMAKE_PROJECT_NAME}" ${SOURCE_FILES}) 23 | target_link_libraries("${CMAKE_PROJECT_NAME}" ${REQUIRED_LIBS}) 24 | 25 | -------------------------------------------------------------------------------- /Chapter12/sharkml/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(shark-save) 3 | 4 | set(SHARK_PATH "" CACHE PATH "path to SharkML install dir") 5 | 6 | if(NOT SHARK_PATH) 7 | message(FATAL_ERROR "Missing SharkML install path, please specify SHARK_PATH") 8 | else() 9 | message("SharkML path is ${SHARK_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | set(Boost_USE_STATIC_LIBS ON) 15 | set(Boost_USE_STATIC_RUNTIME ON) 16 | find_package(Boost REQUIRED serialization) 17 | 18 | set(CMAKE_CXX_FLAGS "-std=c++17 -msse3 -fopenmp -Wall -Wextra") 19 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 20 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 21 | 22 | include_directories(${SHARK_PATH}/include) 23 | link_directories(${SHARK_PATH}/lib) 24 | link_directories(${SHARK_PATH}/lib64) 25 | 26 | add_executable(${CMAKE_PROJECT_NAME} "shark-save.cc") 27 | target_link_libraries(${CMAKE_PROJECT_NAME} shark cblas ${Boost_LIBRARIES}) 28 | 29 | -------------------------------------------------------------------------------- /Chapter12/sharkml/shark-save.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace shark; 6 | 7 | double func(double x) { 8 | return 4. + 0.3 * x; // line coeficients 9 | } 10 | 11 | std::pair, Data> GenerateData(size_t n) { 12 | std::vector x_data(n); 13 | std::vector y_data(n); 14 | 15 | std::random_device rd; 16 | std::mt19937 re(rd()); 17 | std::uniform_real_distribution dist(-1.5, 1.5); 18 | 19 | // generate data 20 | RealVector x_v(1); // it's a typdef to remora::vector 21 | RealVector y_v(1); 22 | for (size_t i = 0; i < n; ++i) { 23 | x_v(0) = i; 24 | x_data[i] = x_v; 25 | 26 | y_v(0) = func(i) + dist(re); // add noise 27 | y_data[i] = y_v; 28 | } 29 | 30 | return {createDataFromRange(x_data), createDataFromRange(y_data)}; 31 | } 32 | 33 | int main() { 34 | { 35 | Data x; 36 | Data y; 37 | std::tie(x, y) = GenerateData(1000); 38 | RegressionDataset data(x, y); 39 | LinearModel<> model; 40 | LinearRegression trainer; 41 | trainer.train(model, data); 42 | 43 | std::ofstream ofs("shark-linear.dat"); 44 | // boost::archive::polymorphic_text_oarchive oa(ofs); 45 | boost::archive::polymorphic_binary_oarchive oa(ofs); 46 | model.write(oa); 47 | } 48 | 49 | std::ifstream ifs("shark-linear.dat"); 50 | // boost::archive::polymorphic_text_iarchive ia(ifs); 51 | boost::archive::polymorphic_binary_iarchive ia(ifs); 52 | LinearModel<> model; 53 | model.read(ia); 54 | 55 | std::cout << "Target values: \n"; 56 | std::vector new_x_data; 57 | for (size_t i = 0; i < 5; ++i) { 58 | new_x_data.push_back({static_cast(i)}); 59 | std::cout << func(i) << std::endl; 60 | } 61 | auto prediction = model(createDataFromRange(new_x_data)); 62 | std::cout << "Predictions: \n" << prediction << std::endl; 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Chapter12/shogun/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(shogun-save) 3 | 4 | set(SHOGUN_PATH "" CACHE PATH "path to shogun install dir") 5 | 6 | if(NOT DEFINED SHOGUN_PATH) 7 | message(FATAL_ERROR "Missing Shogun install path, please specify SHOGUN_PATH") 8 | else() 9 | message("Shogun path is ${SHOGUN_PATH}") 10 | endif() 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | find_package(HDF5 REQUIRED) 15 | 16 | set(CMAKE_CXX_FLAGS "-std=c++14 -msse3 -fopenmp -Wall -Wextra -Wno-unused-parameter") 17 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 18 | set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 19 | 20 | include_directories(${HDF5_INCLUDE_DIR}) 21 | include_directories(${SHOGUN_PATH}/include) 22 | include_directories(/usr/include/json-c/) 23 | link_directories(${SHOGUN_PATH}/lib) 24 | 25 | add_executable(${CMAKE_PROJECT_NAME} "shogun-save.cc") 26 | target_link_libraries(${CMAKE_PROJECT_NAME} shogun) 27 | target_link_libraries(${CMAKE_PROJECT_NAME} json-c ${HDF5_LIBRARIES} cblas lapack) 28 | 29 | -------------------------------------------------------------------------------- /Chapter13/android_classify/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | Properties properties = new Properties() 4 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 5 | def pytorch_sdk_path = properties.getProperty("pytorch.dir") 6 | 7 | android { 8 | compileSdkVersion 29 9 | buildToolsVersion "29.0.2" 10 | defaultConfig { 11 | applicationId "com.example.camera2" 12 | minSdkVersion 24 13 | targetSdkVersion 29 14 | versionCode 1 15 | versionName "1.0" 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | externalNativeBuild { 18 | cmake { 19 | arguments "-DPATH_TO_PYTORCH=${pytorch_sdk_path}" 20 | cppFlags "-Wall -Wextra -std=c++14" 21 | } 22 | } 23 | ndk { 24 | // abiFilters 'x86' 25 | abiFilters 'armeabi-v7a' 26 | } 27 | } 28 | buildTypes { 29 | release { 30 | minifyEnabled false 31 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 32 | } 33 | } 34 | externalNativeBuild { 35 | cmake { 36 | path "src/main/cpp/CMakeLists.txt" 37 | version "3.10.2" 38 | } 39 | } 40 | } 41 | 42 | dependencies { 43 | implementation fileTree(dir: 'libs', include: ['*.jar']) 44 | implementation 'androidx.appcompat:appcompat:1.0.2' 45 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 46 | testImplementation 'junit:junit:4.12' 47 | androidTestImplementation 'androidx.test:runner:1.2.0' 48 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 49 | } 50 | -------------------------------------------------------------------------------- /Chapter13/android_classify/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /Chapter13/android_classify/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Chapter13/android_classify/app/src/main/cpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # For more information about using CMake with Android Studio, read the 2 | # documentation: https://d.android.com/studio/projects/add-native-code.html 3 | 4 | # Sets the minimum version of CMake required to build the native library. 5 | 6 | cmake_minimum_required(VERSION 3.4.1) 7 | 8 | set(CMAKE_VERBOSE_MAKEFILE ON) 9 | 10 | set(PATH_TO_PYTORCH CACHE STRING "") 11 | 12 | set(THREADS_PREFER_PTHREAD_FLAG ON) 13 | find_package(Threads REQUIRED) 14 | 15 | include_directories(${PATH_TO_PYTORCH}/include) 16 | 17 | add_definitions(-DONNX_NAMESPACE=onnx_torch) 18 | 19 | # Creates and names a library, sets it as either STATIC 20 | # or SHARED, and provides the relative paths to its source code. 21 | # You can define multiple libraries, and CMake builds them for you. 22 | # Gradle automatically packages shared libraries with your APK. 23 | 24 | add_library( # Sets the name of the library. 25 | native-lib 26 | 27 | # Sets the library as a shared library. 28 | SHARED 29 | 30 | # Provides a relative path to your source file(s). 31 | native-lib.cpp) 32 | 33 | # Searches for a specified prebuilt library and stores the path as a 34 | # variable. Because CMake includes system libraries in the search path by 35 | # default, you only need to specify the name of the public NDK library 36 | # you want to add. CMake verifies that the library exists before 37 | # completing its build. 38 | 39 | find_library( # Sets the name of the path variable. 40 | log-lib 41 | 42 | # Specifies the name of the NDK library that 43 | # you want CMake to locate. 44 | log) 45 | 46 | # Specifies libraries CMake should link to your target library. You 47 | # can link multiple libraries, such as libraries you define in this 48 | # build script, prebuilt third-party libraries, or system libraries. 49 | 50 | set(pytorch-lib ${PATH_TO_PYTORCH}/lib/libtorch.so) 51 | set(c10-lib ${PATH_TO_PYTORCH}/lib/libc10.so) 52 | 53 | #set(pytorch-lib ${PATH_TO_PYTORCH}/lib/libtorch.a) 54 | #set(c10-lib ${PATH_TO_PYTORCH}/lib/libc10.a) 55 | #set(cpuinfo-lib ${PATH_TO_PYTORCH}/lib/libcpuinfo.a) 56 | #set(clog-lib ${PATH_TO_PYTORCH}/lib/libclog.a) 57 | #set(caffe2-protos-lib ${PATH_TO_PYTORCH}/lib/libcaffe2_protos.a) 58 | #set(protobuf-lib ${PATH_TO_PYTORCH}/lib/libprotobuf.a) 59 | #set(nnpack-lib ${PATH_TO_PYTORCH}/lib/libnnpack.a) 60 | #set(qnnpack-lib ${PATH_TO_PYTORCH}/lib/libqnnpack.a) 61 | #set(thread-lib ${PATH_TO_PYTORCH}/lib/libpthreadpool.a) 62 | 63 | target_link_libraries( # Specifies the target library. 64 | native-lib 65 | 66 | # -Wl,--whole-archive -Wl,--no-as-needed 67 | 68 | ${log-lib} 69 | ${pytorch-lib} 70 | ${c10-lib} 71 | # ${cpuinfo-lib} 72 | # ${clog-lib} 73 | # ${caffe2-protos-lib} 74 | # ${protobuf-lib} 75 | # ${nnpack-lib} 76 | # ${qnnpack-lib} 77 | # ${thread-lib} 78 | android 79 | ) -------------------------------------------------------------------------------- /Chapter13/android_classify/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /Chapter13/android_classify/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 15 |