doc22_028.md 4.3 KB
Newer Older
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
1 2
# C++

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
3
> 原文:[`pytorch.org/docs/stable/cpp_index.html`](https://pytorch.org/docs/stable/cpp_index.html)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
4 5 6

注意

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
7
如果您正在寻找 PyTorch C++ API 文档,请直接前往[此处](https://pytorch.org/cppdocs/)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
8

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
9
PyTorch 提供了几个用于处理 C++的功能,最好根据您的需求选择其中之一。在高层次上,以下支持可用:
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
10 11 12

## TorchScript C++ API

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
13
[TorchScript](https://pytorch.org/docs/stable/jit.html)允许将在 Python 中定义的 PyTorch 模型序列化,然后在 C++中加载和运行,通过编译或跟踪其执行来捕获模型代码。您可以在[在 C++中加载 TorchScript 模型](https://pytorch.org/tutorials/advanced/cpp_export.html)教程中了解更多信息。这意味着您可以尽可能在 Python 中定义模型,但随后通过 TorchScript 导出它们,以在生产或嵌入式环境中进行无 Python 执行。TorchScript C++ API 用于与这些模型和 TorchScript 执行引擎进行交互,包括:
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
14

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
15
+   加载从 Python 保存的序列化 TorchScript 模型
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
16 17 18

+   如果需要,进行简单的模型修改(例如,提取子模块)

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
19
+   使用 C++ Tensor API 构建输入并进行预处理
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
20

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
21
## 使用 C++扩展扩展 PyTorch 和 TorchScript[](#extending-pytorch-and-torchscript-with-c-extensions "跳转到此标题的永久链接")
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
22

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
23
TorchScript 可以通过自定义运算符和自定义类增强用户提供的代码。一旦在 TorchScript 中注册了这些运算符和类,这些运算符和类可以在 Python 中运行的 TorchScript 代码中被调用,或者作为序列化的 TorchScript 模型的一部分在 C++中被调用。[使用自定义 C++运算符扩展 TorchScript](https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html)教程介绍了如何将 TorchScript 与 OpenCV 进行接口。除了使用自定义运算符包装函数调用外,C++类和结构体还可以通过类似于 pybind11 的接口绑定到 TorchScript 中,这在[使用自定义 C++类扩展 TorchScript](https://pytorch.org/tutorials/advanced/torch_script_custom_classes.html)教程中有解释。
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
24

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
25
## 在 C++中的 Tensor 和 Autograd
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
26

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
27
PyTorch Python API 中的大多数张量和自动求导操作也可在 C++ API 中使用。包括:
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
28

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
29
+   `torch::Tensor`方法,如`add` / `reshape` / `clone`。有关可用方法的完整列表,请参阅:[`pytorch.org/cppdocs/api/classat_1_1_tensor.html`](https://pytorch.org/cppdocs/api/classat_1_1_tensor.html)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
30

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
31
+   C++张量索引 API,看起来和行为与 Python API 相同。有关其用法的详细信息,请参阅:[`pytorch.org/cppdocs/notes/tensor_indexing.html`](https://pytorch.org/cppdocs/notes/tensor_indexing.html)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
32

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
33
+   在 C++前端构建动态神经网络至关重要的张量自动求导 API 和`torch::autograd`包。有关更多详细信息,请参阅:[`pytorch.org/tutorials/advanced/cpp_autograd.html`](https://pytorch.org/tutorials/advanced/cpp_autograd.html)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
34

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
35
## 在 C++中编写模型
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
36

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
37
“在 TorchScript 中编写,使用 C++进行推断”的工作流程要求在 TorchScript 中进行模型编写。但是,可能存在必须在 C++中编写模型的情况(例如,在不希望使用 Python 组件的工作流程中)。为了满足这种用例,我们提供了在 C++中完全编写和训练神经网络模型的完整功能,其中包括`torch::nn` / `torch::nn::functional` / `torch::optim`等熟悉的组件,这些组件与 Python API 非常相似。
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
38

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
39
+   有关 PyTorch C++模型编写和训练 API 的概述,请参阅:[`pytorch.org/cppdocs/frontend.html`](https://pytorch.org/cppdocs/frontend.html)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
40

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
41
+   有关如何使用 API 的详细教程,请参阅:[`pytorch.org/tutorials/advanced/cpp_frontend.html`](https://pytorch.org/tutorials/advanced/cpp_frontend.html)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
42

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
43
+   有关诸如`torch::nn` / `torch::nn::functional` / `torch::optim`等组件的文档,请访问:[`pytorch.org/cppdocs/api/library_root.html`](https://pytorch.org/cppdocs/api/library_root.html)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
44 45 46

## C++包装

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
47
有关如何安装和链接 libtorch(包含所有上述 C++ API 的库)的指导,请参阅:[`pytorch.org/cppdocs/installing.html`](https://pytorch.org/cppdocs/installing.html)。请注意,在 Linux 上提供了两种类型的 libtorch 二进制文件:一种是使用 GCC pre-cxx11 ABI 编译的,另一种是使用 GCC cxx11 ABI 编译的,您应该根据系统使用的 GCC ABI 进行选择。