|
mlpack快速、灵活的C++机器学习库的安装与使用
例子:https://github.com/mlpack/examples/
文档:https://mlpack.org/docs.html
依赖:Ensmallen 是一个用于非线性数值优化的高质量 C++ 库 https://github.com/mlpack/ensmallen
stb_image 是一个简单易用的图像解码库 https://github.com/nothings/stb
源码编译安装:
mkdir build && cd build
cmake .. 输出:
编译:make -j8
安装到系统: sudo make install
make mlpack_test
运行测试案例:bin/mlpack_test Running 320 test cases...
输出:*** No errors detected
- // neighbor_search_dm01.cpp
- // This simple program uses the mlpack::neighbor::NeighborSearch object
- // to find the nearest neighbor of each point in a dataset using the L1 metric,
- // and then print the index of the neighbor and the distance of it to stdout.
- #include <mlpack/core.hpp>
- #include <mlpack/methods/neighbor_search/neighbor_search.hpp>
- using namespace mlpack;
- using namespace mlpack::neighbor; // NeighborSearch and NearestNeighborSort
- using namespace mlpack::metric; // ManhattanDistance
- int main()
- {
- // Load the data from data.csv (hard-coded). Use CLI for simple command-line
- // parameter handling.
- arma::mat data("0.339406815,0.843176636,0.472701471; \
- 0.212587646,0.351174901,0.81056695; \
- 0.160147626,0.255047893,0.04072469; \
- 0.564535197,0.943435462,0.597070812");
- data = data.t();
- // Use templates to specify that we want a NeighborSearch object which uses
- // the Manhattan distance.
- NeighborSearch<NearestNeighborSort, ManhattanDistance> nn(data);
- // Create the object we will store the nearest neighbors in.
- arma::Mat<size_t> neighbors;
- arma::mat distances; // We need to store the distance too.
- // Compute the neighbors.
- nn.Search(1, neighbors, distances);
- // Write each neighbor and distance using Log.
- for (size_t i = 0; i < neighbors.n_elem; ++i)
- {
- std::cout << "Nearest neighbor of point " << i << " is point "
- << neighbors[i] << " and the distance is " << distances[i] << "." << std::endl;
- }
- return 0;
- }
复制代码
- cmake_minimum_required (VERSION 2.8)
- project (main)
- #SET(CMAKE_CXX_STANDARD 11)
- #SET(CMAKE_C_STANDARD 11)
- set(CMAKE_CXX_STANDARD 14)
- # aux_source_directory(./base_pan ALL_SRCS) # 添加当前目录下所有的源文件
- set(ALL_SRCS "neighbor_search_dm01.cpp")
- # aux_source_directory(. ALL_SRCS) # 添加当前目录下所有的源文件
- message(WARNING ${ALL_SRCS})
- #add_subdirectory(lib) # 添加lib子目录
- add_executable(main ${ALL_SRCS}) # 指定生成目标,注意这里要用${ALL_SRCS}!!!
- #target_link_libraries(main power) # 添加链接库,power是在lib子目录的CMakeLists中定义的
- option(USE_OPENMP "If available, use OpenMP for parallelization." ON)
- if (USE_OPENMP)
- find_package(OpenMP)
- endif ()
- if (OPENMP_FOUND)
- add_definitions(-DHAS_OPENMP)
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
- endif ()
- target_link_libraries(main -lmlpack -larmadillo)
- # target_link_libraries(main -lboost_system -lboost_thread -lfftw3f -lpthread)
- # mlpack was compiled with OpenMP support, but you are compiling without OpenMP support
复制代码
|
|