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的人工智能研究团队开发[6][7][8],并且被用于Uber最初发起而现属Linux基金会项目的概率编程软件Pyro[9]。
概述
PyTorch主要有两大特征:[10]
PyTorch包括torch.autograd、torch.nn、torch.optim等子模块[13]。
例子
下面的程序用简单的例子展示这个程序库的低层功能。
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([[-1.1884, 0.8498, -1.7129],
# [-0.8816, 0.1944, 0.5847]])
# 建立一个张量并用随机数填充这个张量
b = torch.randn(2, 3, device=device, dtype=dtype)
print(b) # Output of tensor B
# 输出: tensor([[ 0.7178, -0.8453, -1.3403],
# [ 1.3262, 1.1512, -1.7070]])
print(a*b) # 输出两个张量的乘积
# 输出: tensor([[-0.8530, -0.7183, 2.58],
# [-1.1692, 0.2238, -0.9981]])
print(a.sum()) # 输出在张量a中所有元素的总和
# 输出: tensor(-2.1540)
print(a[1,2]) # 输出第2行第3列(0起始)的元素
# 输出: tensor(0.5847)
print(a.max()) # 输出在张量a中的极大值
# 输出: tensor(-1.7129)
下列代码块展示了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) (美国英语).
- ^ Pyro- Deep universal probabilistic programming with Python and PyTorch.
- ^ 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.
- ^ 13.0 13.1 神经网络与PyTorch实战 Application of Neural Network and PyTorch. 机械工业出版社. 2018. ISBN 9787111605775.