Torch 学习
1. 下载与安装:
https://pytorch.org/get-started/locally/
安装cuda9.2 & cudnn https://developer.nvidia.com/rdp/cudnn-download
2. 第一个程序
编写CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
project(simnet)
set(Torch_DIR "/opensource/libtorch/share/cmake/Torch")
find_package(Torch REQUIRED)
message(STATUS "Pytorch status:")
message(STATUS " libraries: ${TORCH_LIBRARIES}")
add_executable(simnet run2.cpp)
add_compile_options(-fpermissive)
target_link_libraries(simnet ${TORCH_LIBRARIES})
set_property(TARGET simnet PROPERTY CXX_STANDARD 14)
编写测试程序
#include <torch/script.h> // One-stop header. #include <iostream> #include <memory> int main(int argc, const char* argv[]) { if (argc != 2) { std::cerr << "usage: example-app <path-to-exported-script-module>\n"; return -1; } torch::jit::script::Module module; try { // Deserialize the ScriptModule from a file using torch::jit::load(). module = torch::jit::load(argv[1]); } catch (const c10::Error& e) { std::cerr << "error loading the model\n"; return -1; } std::cout << "ok\n"; }
mkdir build
cmake ..
make