pytorch

Catalogue

介绍

https://pytorch.org/ 下载 LibTorch 代码

Hello World

写一个 Hello World 程序

1
2
3
4
5
6
7
8
9

#include <torch/torch.h>

using namespace std;

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

编译

Makefile 如下:

其中 .../libtorch 中保存着 LibTorch 的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
TORCH = /Users/zhang/Codes/C++/pytorch-c++/libtorch
CC = clang++
CFLAGS = -Wall -std=c++0x
CFLAGS += -I$(TORCH)/include -L$(TORCH)/lib
CFLAGS += -I$(TORCH)/include/torch/csrc/api/include
CLINKS = -ltorch.1 -lcaffe2 -lc10


NAME = ex1
SRC = $(NAME).cpp
TAR = $(NAME).out

$(TAR) : $(SRC)
$(CC) $(CFLAGS) -o [email protected] $^ $(CLINKS)

.PHONY : run clean

run :
LD_LIBRARY_PATH=$(TORCH)/lib ./$(TAR)

clean :
rm -rf *.out

参考资料