ubuntu20.04 使用libtorch

一、官网下载文件

https://pytorch.org/get-started/locally/

选择下载Stable(1.10.1) C++ CPU(cxx 11 ABI)

解压.zip压缩包,将libtorch移动到存放第三方库的位置

二、使用

2.1 创建torch_ws文件夹,然后创建如下两文件

example-app.cpp

#include "torch/torch.h"
#include <iostream>

int main() {
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout << tensor << std::endl;
}

 CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(test_pytorch)

set(CMAKE_CXX_STANDARD 14) #设置为C++11,可能报错
set(Torch_DIR ~/third_party/libtorch/share/cmake/Torch) #确定.cmake文件的地址
find_package(Torch REQUIRED)  
message(STATUS "Torch library status:")
message(STATUS "    include path: ${TORCH_INCLUDE_DIRS}")
message(STATUS "    lib path : ${TORCH_LIBRARIES} ")

add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")

2.2 编译运行

mkdir build
cmake ..
make

 

posted @   chenjian688  阅读(1479)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示