安装 PyTorch C++ API libtorch 及一个最小例子
安裝 PyTorch C++ API libtorch 及一個(gè)最小例子
翻譯自:https://pytorch.org/cppdocs/installing.html
我們提供依賴 PyTorch 所需的所有頭文件、庫(kù)和 CMake 配置文件的二進(jìn)制分發(fā)版。我們將此發(fā)行版稱為 LibTorch,您可以在我們的網(wǎng)站上下載包含最新 LibTorch 發(fā)行版的 ZIP 檔案。下面是編寫(xiě)一個(gè)依賴 LibTorch 并使用torch::TensorPyTorch C++ API 附帶的類(lèi)的最小應(yīng)用程序的小示例。
安裝
先安裝預(yù)編譯的 libtorch :
wget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip unzip libtorch-shared-with-deps-latest.zip該鏈接僅支持 CPU,若要支持 GPU,請(qǐng)到官網(wǎng)找到對(duì)應(yīng)版本
直接解壓,就可以了。
最小例子
接下來(lái),我們可以編寫(xiě)一個(gè)最小的 CMake 構(gòu)建配置來(lái)開(kāi)發(fā)一個(gè)依賴 LibTorch 的小型應(yīng)用程序。CMake 不是使用 LibTorch 的必須要求,但它是我們推薦的構(gòu)建系統(tǒng),并在將來(lái)會(huì)得到很好的支持。最基本的 CMakeLists.txt 文件可能如下所示:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR) project(example-app)find_package(Torch REQUIRED) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")add_executable(example-app example-app.cpp) target_link_libraries(example-app "${TORCH_LIBRARIES}") set_property(TARGET example-app PROPERTY CXX_STANDARD 14)# 以下代碼建議在Windows上使用 if (MSVC)file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")add_custom_command(TARGET example-appPOST_BUILDCOMMAND ${CMAKE_COMMAND} -E copy_if_different${TORCH_DLLS}$<TARGET_FILE_DIR:example-app>) endif (MSVC)我們示例的實(shí)現(xiàn)將簡(jiǎn)單地創(chuàng)建一個(gè)新的 torch::Tensor 并打印它:
#include <torch/torch.h> #include <iostream>int main() {torch::Tensor tensor = torch::rand({2, 3});std::cout << tensor << std::endl; }雖然您可以包含更多細(xì)粒度的頭文件以僅訪問(wèn) PyTorch C++ API 的一部分,但包括torch/torch.h是包含其大部分功能的最可靠的方式。
最后一步是構(gòu)建應(yīng)用程序。為此,假設(shè)我們的示例目錄如下所示:
example-app/CMakeLists.txtexample-app.cpp我們現(xiàn)在可以運(yùn)行以下命令從example-app/文件夾中構(gòu)建應(yīng)用程序 :
mkdir build cd build cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch .. cmake --build . --config Release其中 /absolute/path/to/libtorch 應(yīng)該是解壓后的 libTorch 發(fā)行版的絕對(duì) (!) 路徑。如果 PyTorch 是通過(guò) conda 或 pip 安裝的,則可以使用 torch.utils.cmake_prefix_path 變量查詢CMAKE_PREFIX_PATH(譯者注:這里是指如果本機(jī) pip 或 conda 安裝過(guò) PyTorch,那可以不用再裝 libtorch,因?yàn)樵?PyTorch 中是有的)。在這種情況下,CMake 配置步驟將如下所示:
cmake -DCMAKE_PREFIX_PATH=`python -c 'import torch;print(torch.utils.cmake_prefix_path)'`如果一切順利,整個(gè)過(guò)程及輸出將會(huì)是這樣:
root@4b5a67132e81:/example-app# mkdir build root@4b5a67132e81:/example-app# cd build root@4b5a67132e81:/example-app/build# cmake -DCMAKE_PREFIX_PATH=/path/to/libtorch .. -- The C compiler identification is GNU 5.4.0 -- The CXX compiler identification is GNU 5.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Looking for pthread.h -- Looking for pthread.h - found -- Looking for pthread_create -- Looking for pthread_create - not found -- Looking for pthread_create in pthreads -- Looking for pthread_create in pthreads - not found -- Looking for pthread_create in pthread -- Looking for pthread_create in pthread - found -- Found Threads: TRUE -- Configuring done -- Generating done -- Build files have been written to: /example-app/build root@4b5a67132e81:/example-app/build# cmake --build . --config Release Scanning dependencies of target example-app [ 50%] Building CXX object CMakeFiles/example-app.dir/example-app.cpp.o [100%] Linking CXX executable example-app [100%] Built target example-app執(zhí)行 example-app 在 build 文件夾中找到的結(jié)果二進(jìn)制文件現(xiàn)在應(yīng)該可以打印張量(具體輸出受隨機(jī)性影響):
root@4b5a67132e81:/example-app/build# ./example-app 0.2063 0.6593 0.0866 0.0796 0.5841 0.1569 [ Variable[CPUFloatType]{2,3} ]https://pytorch.org/cppdocs/installing.html
總結(jié)
以上是生活随笔為你收集整理的安装 PyTorch C++ API libtorch 及一个最小例子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python 条件概率_使用Pymc3的
- 下一篇: 万事达和visa的区别