C++#
创建日期: 2020 年 5 月 14 日 | 最后更新日期: 2025 年 7 月 15 日
注意
如果您正在寻找 PyTorch C++ API 文档,请直接 点击此处。
PyTorch 提供了多项用于 C++ 的功能,最好根据您的需求进行选择。总体而言,支持如下:
C++ 中的 Tensor 和 Autograd#
PyTorch Python API 中的大多数 Tensor 和 Autograd 操作在 C++ API 中也可用。这些包括:
torch::Tensor方法,如add/reshape/clone。可用方法的完整列表,请参阅:https://pytorch.ac.cn/cppdocs/api/classat_1_1_tensor.htmlC++ Tensor 索引 API,其外观和行为与 Python API 相同。有关其用法的详细信息,请参阅:https://pytorch.ac.cn/cppdocs/notes/tensor_indexing.html
Tensor Autograd API 和
torch::autograd包,这对于在 C++ 前端构建动态神经网络至关重要。更多详情,请参阅:https://pytorch.ac.cn/tutorials/advanced/cpp_autograd.html
在 C++ 中编写模型#
我们提供了完全在 C++ 中编写和训练神经网络模型的全部功能,包含熟悉的组件,如 torch::nn / torch::nn::functional / torch::optim,它们与 Python API 非常相似。
有关 PyTorch C++ 模型编写和训练 API 的概述,请参阅:https://pytorch.ac.cn/cppdocs/frontend.html
有关如何使用该 API 的详细教程,请参阅:https://pytorch.ac.cn/tutorials/advanced/cpp_frontend.html
有关
torch::nn/torch::nn::functional/torch::optim等组件的文档,请参阅:https://pytorch.ac.cn/cppdocs/api/library_root.html
C++ 打包#
有关如何安装 libtorch(包含上述所有 C++ API 的库)并与其链接的指南,请参阅:https://pytorch.ac.cn/cppdocs/installing.html。请注意,在 Linux 上,我们提供了两种类型的 libtorch 二进制文件:一种是用 GCC pre-cxx11 ABI 编译的,另一种是用 GCC cxx11 ABI 编译的,您应根据系统使用的 GCC ABI 进行选择。