pytorch / pytorch

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

PyTorch is a Python package that provides two high-level features: Tensor computation (like NumPy) with strong GPU acceleration and deep neural networks built on a tape-based autograd system. You can reuse your favorite Python packages such as NumPy, SciPy, and Cython to extend PyTorch when needed. Our trunk health (Continuous Integration signals) can be found at hud.pytorch.org.

PyTorch 是一个 Python 软件包,提供两个高级功能:具有强大 GPU 加速功能的张量计算(类似于 NumPy),以及基于磁带式自动求导系统的深度神经网络。在需要时,你可以重复使用 NumPy、SciPy 和 Cython 等你喜爱的 Python 软件包来扩展 PyTorch。我们的主干健康状况(持续集成信号)可以在 hud.pytorch.org 上找到。

More About PyTorch: A GPU-Ready Tensor Library, Dynamic Neural Networks: Tape-Based Autograd, Python First, Imperative Experiences, Fast and Lean, Extensions Without Pain. Installation: Binaries, NVIDIA Jetson Platforms, From Source.

关于 PyTorch 的更多信息:GPU 就绪的张量库、动态神经网络:基于磁带的自动求导、Python 优先、命令式体验、快速且精简、无痛扩展。安装:二进制文件、NVIDIA Jetson 平台、从源码安装。

At a granular level, PyTorch is a library that consists of the following components: torch (a Tensor library like NumPy, with strong GPU support), torch.autograd (a tape-based automatic differentiation library), torch.nn (a neural networks library), torch.multiprocessing (Python multiprocessing with memory sharing), and torch.utils (DataLoader and other utility functions).

从细粒度层面来看,PyTorch 是一个由以下组件组成的库:torch(类似于 NumPy 的张量库,具有强大的 GPU 支持)、torch.autograd(基于磁带的自动微分库)、torch.nn(神经网络库)、torch.multiprocessing(具有内存共享功能的 Python 多进程处理)以及 torch.utils(DataLoader 和其他实用函数)。

Usually, PyTorch is used either as a replacement for NumPy to use the power of GPUs, or as a deep learning research platform that provides maximum flexibility and speed.

通常,PyTorch 要么被用作 NumPy 的替代品以利用 GPU 的强大功能,要么被用作提供最大灵活性和速度的深度学习研究平台。

If you use NumPy, then you have used Tensors (a.k.a. ndarray). PyTorch provides Tensors that can live either on the CPU or the GPU and accelerates the computation by a huge amount. We provide a wide variety of tensor routines to accelerate and fit your scientific computation needs such as slicing, indexing, mathematical operations, linear algebra, and reductions.

如果你使用过 NumPy,那么你就使用过张量(即 ndarray)。PyTorch 提供的张量既可以存在于 CPU 上,也可以存在于 GPU 上,并能极大地加速计算。我们提供了多种张量例程来加速并满足你的科学计算需求,例如切片、索引、数学运算、线性代数和归约。

PyTorch has a unique way of building neural networks: using and replaying a tape recorder. Most frameworks such as TensorFlow, Theano, Caffe, and CNTK have a static view of the world. One has to build a neural network and reuse the same structure again and again. Changing the way the network behaves means that one has to start from scratch.

PyTorch 构建神经网络的方式很独特:使用并重放磁带记录器。大多数框架(如 TensorFlow、Theano、Caffe 和 CNTK)对世界的看法是静态的。用户必须构建一个神经网络并反复重用相同的结构。改变网络的行为方式意味着必须从头开始。

With PyTorch, we use a technique called reverse-mode auto-differentiation, which allows you to change the way your network behaves arbitrarily with zero lag or overhead. Our inspiration comes from several research papers on this topic, as well as current and past work such as torch-autograd, autograd, Chainer, etc.

在 PyTorch 中,我们使用一种称为反向模式自动微分的技术,它允许你以零延迟或零开销随意改变网络的行为方式。我们的灵感来自于关于该主题的几篇研究论文,以及 torch-autograd、autograd、Chainer 等当前和过去的工作。

PyTorch is not a Python binding into a monolithic C++ framework. It is built to be deeply integrated into Python. You can use it naturally like you would use NumPy / SciPy / scikit-learn etc. You can write your new neural network layers in Python itself, using your favorite libraries and use packages such as Cython and Numba.

PyTorch 并非一个针对庞大 C++ 框架的 Python 绑定。它被设计为与 Python 深度集成。你可以像使用 NumPy、SciPy 或 scikit-learn 等库一样自然地使用它。你可以直接用 Python 编写新的神经网络层,使用你喜爱的库,并利用 Cython 和 Numba 等软件包。

PyTorch is designed to be intuitive, linear in thought, and easy to use. When you execute a line of code, it gets executed. There isn’t an asynchronous view of the world. When you drop into a debugger or receive error messages and stack traces, understanding them is straightforward.

PyTorch 的设计初衷是直观、思维线性且易于使用。当你执行一行代码时,它就会立即执行。这里没有异步的世界观。当你进入调试器或收到错误消息和堆栈跟踪时,理解它们非常简单。

PyTorch has minimal framework overhead. We integrate acceleration libraries such as Intel MKL and NVIDIA (cuDNN, NCCL) to maximize speed. At the core, its CPU and GPU Tensor and neural network backends are mature and have been tested for years. Hence, PyTorch is quite fast — whether you run small or large neural networks.

PyTorch 的框架开销极小。我们集成了 Intel MKL 和 NVIDIA(cuDNN, NCCL)等加速库以实现速度最大化。其核心的 CPU 和 GPU 张量及神经网络后端非常成熟,并经过了多年的测试。因此,无论你运行小型还是大型神经网络,PyTorch 都非常快。

Writing new neural network modules, or interfacing with PyTorch’s Tensor API, was designed to be straightforward and with minimal abstractions. You can write new neural network layers in Python using the torch API or your favorite NumPy-based libraries such as SciPy. If you want to write your layers in C/C++, we provide a convenient extension API that is efficient and with minimal boilerplate.

编写新的神经网络模块或与 PyTorch 的张量 API 进行交互的设计非常直观,且抽象程度极低。你可以使用 torch API 或你喜爱的基于 NumPy 的库(如 SciPy)在 Python 中编写新的神经网络层。如果你想用 C/C++ 编写层,我们提供了一个方便的扩展 API,它既高效又几乎不需要样板代码。