PyTorch
外观

![]() | |
原作者 | Adam Paszke, Sam Gross, Soumith Chintala, Gregory Chanan |
---|---|
開發者 | Meta AI |
首次发布 | 2016年10月 |
当前版本 | 2.7.0[1]![]() |
源代码库 | github |
编程语言 | Python, C++, CUDA |
操作系统 | Linux, macOS, Windows |
类型 | 机器学习和深度学习库 |
许可协议 | BSD许可证 |
网站 | pytorch |
PyTorch是一个开源的Python机器学习库,基于Torch库[2][3][4],底层由C++实现,应用于人工智能领域,如计算机视觉和自然语言处理[5]。它最初由Meta Platforms的人工智能研究团队开发,現在屬於Linux基金会的一部分[6][7][8]。它是在修改後的BSD許可證下發布的自由及开放源代码软件。 儘管Python接口更加完善並且是開發的主要重點,但 PyTorch 也有C++接口[9]。
許多深度學習軟體都是基於 PyTorch 構建的,包括特斯拉自动驾驶[10]、 Uber的Pyro[11] Hugging Face's Transformers,[12]、 PyTorch Lightning[13][14]、 和 Catalyst[15][16]。
概述
PyTorch主要有两大特征:[17]
PyTorch包括torch.autograd、torch.nn、torch.optim等子模块[20]。
例子
下面的程序用简单的例子展示这个程序库的低层功能。
>>> import torch
>>> dtype = torch.float
>>> device = torch.device("cpu") # 本次在CPU上执行所有的计算
>>> # device = torch.device("cuda:0") # 本次在GPU上执行所有的计算
>>>
>>> # 建立一个张量并用随机数填充这个张量
>>> a = torch.randn(2, 3, device=device, dtype=dtype)
>>> print(a) # 输出张量a
tensor([[-0.1460, -0.3490, 0.3705],
[-1.1141, 0.7661, 1.0823]])
>>>
>>> # 建立一个张量并用随机数填充这个张量
>>> b = torch.randn(2, 3, device=device, dtype=dtype)
>>> print(b) # 输出张量B
tensor([[ 0.6901, -0.9663, 0.3634],
[-0.6538, -0.3728, -1.1323]])
>>>
>>> print(a*b) # 输出两个张量的乘积
tensor([[-0.1007, 0.3372, 0.1346],
[ 0.7284, -0.2856, -1.2256]])
>>> print(a.sum()) # 输出在张量a中所有元素的总和
tensor(0.6097)
>>>
>>> print(a[1,2]) # 输出第2行第3列(0起始)的元素
tensor(1.0823)
>>>
>>> print(a.max()) # 输出在张量a中的极大值
tensor(1.0823)
下列代码块展示了nn
模块提供的高层功能的例子。例子中定义了具有线性层的神经网络。
import torch
from torch import nn # 从PyTorch中导入nn子模块
class NeuralNetwork(nn.Module): # 神经网络被定义为类
def __init__(self): # 在__init__方法中定义诸层和变量
super(NeuralNetwork, self).__init__() # 必须出现在所有网络中
self.flatten = nn.Flatten() # 定义一个压平层
self.linear_relu_stack = nn.Sequential( # 定义诸层的一个堆栈
nn.Linear(28*28, 512), # 线性层有一个输入和输出形状
nn.ReLU(), # ReLU是nn提供的诸多激活函数之一
nn.Linear(512, 512),
nn.ReLU(),
nn.Linear(512, 10),
)
def forward(self, x): # 这个函数定义前向传递。
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits
参考文献
- ^ 1.0 1.1 Release 2.7.0. 2025年4月23日 [2025年4月27日].
- ^ Yegulalp, Serdar. Facebook brings GPU-powered machine learning to Python. InfoWorld. 19 January 2017 [11 December 2017]. (原始内容存档于2018-07-12).
- ^ Lorica, Ben. Why AI and machine learning researchers are beginning to embrace PyTorch. O'Reilly Media. 3 August 2017 [11 December 2017]. (原始内容存档于2019-05-17).
- ^ Ketkar, Nikhil. Deep Learning with Python. Apress, Berkeley, CA. 2017: 195–208 [2018-10-02]. ISBN 9781484227657. doi:10.1007/978-1-4842-2766-4_12. (原始内容存档于2018-07-12) (英语).
- ^ Natural Language Processing (NLP) with PyTorch — NLP with PyTorch documentation. dl4nlp.info. [2017-12-18]. (原始内容存档于2019-06-21) (英语).
- ^ Patel, Mo. When two trends fuse: PyTorch and recommender systems. O'Reilly Media. 2017-12-07 [2017-12-18]. (原始内容存档于2019-03-30) (英语).
- ^ Mannes, John. Facebook and Microsoft collaborate to simplify conversions from PyTorch to Caffe2. TechCrunch. [2017-12-18]. (原始内容存档于2020-07-06) (英语).
FAIR is accustomed to working with PyTorch — a deep learning framework optimized for achieving state of the art results in research, regardless of resource constraints. Unfortunately in the real world, most of us are limited by the computational capabilities of our smartphones and computers.
- ^ Arakelyan, Sophia. Tech giants are using open source frameworks to dominate the AI community. VentureBeat. 2017-11-29 [2017-12-18]. (原始内容存档于2019-03-30) (美国英语).
- ^ The C++ Frontend. PyTorch Master Documentation. [2019-07-29].
- ^ Karpathy, Andrej. PyTorch at Tesla - Andrej Karpathy, Tesla.
- ^ Uber AI Labs Open Sources Pyro, a Deep Probabilistic Programming Language. Uber Engineering Blog. 2017-11-03 [2017-12-18] (美国英语).
- ^ PYTORCH-TRANSFORMERS: PyTorch implementations of popular NLP Transformers, PyTorch Hub, 2019-12-01 [2019-12-01]
- ^ PYTORCH-Lightning: The lightweight PyTorch wrapper for ML researchers. Scale your models. Write less boilerplate, Lightning-Team, 2020-06-18 [2020-06-18]
- ^ Ecosystem Tools. pytorch.org. [2020-06-18] (英语).
- ^ GitHub - catalyst-team/catalyst: Accelerated DL & RL, Catalyst-Team, 2019-12-05 [2019-12-05]
- ^ Ecosystem Tools. pytorch.org. [2020-04-04] (英语).
- ^ PyTorch – About. pytorch.org. [2018-06-11]. (原始内容存档于2018-06-15).
- ^ R.E. Wengert. A simple automatic derivative evaluation program. Comm. ACM. 1964, 7: 463–464. doi:10.1145/355586.364791.
- ^ Bartholomew-Biggs, Michael; Brown, Steven; Christianson, Bruce; Dixon, Laurence. Automatic differentiation of algorithms (PDF). Journal of Computational and Applied Mathematics. 2000, 124 (1-2): 171–190. Bibcode:2000JCoAM.124..171B. doi:10.1016/S0377-0427(00)00422-2.
- ^ 20.0 20.1 神经网络与PyTorch实战 Application of Neural Network and PyTorch. 机械工业出版社. 2018. ISBN 9787111605775.