PyTorch

PyTorch
原作者Adam Paszke, Sam Gross, Soumith Chintala, Gregory Chanan
開發者Meta AI英语Meta AI
首次发布2016年10月,​7年前​(2016-October
当前版本
  • 2.3.0 (2024年4月24日;穩定版本)[1]
編輯維基數據鏈接
源代码库github.com/pytorch/pytorch
编程语言Python, C++, CUDA
操作系统Linux, macOS, Windows
平台IA-32, x86-64, ARM64
类型机器学习深度学习
许可协议BSD许可证
网站pytorch.org
机器学习数据挖掘
范式
问题
  • 因素分析
  • CCA
  • ICA
  • LDA
  • NMF英语Non-negative matrix factorization
  • PCA
  • PGD英语Proper generalized decomposition
  • t-SNE英语t-distributed stochastic neighbor embedding
  • SDL
结构预测英语Structured prediction
  • RANSAC
  • k-NN
  • 局部异常因子英语Local outlier factor
  • 孤立森林英语Isolation forest
与人类学习
  • 主动学习英语Active learning (machine learning)
  • 众包
  • Human-in-the-loop英语Human-in-the-loop
模型诊断
  • 学习曲线英语Learning curve (machine learning)
数学基础
  • 内核机器英语Kernel machines
  • 偏差–方差困境英语Bias–variance tradeoff
  • 计算学习理论英语Computational learning theory
  • 经验风险最小化
  • 奥卡姆学习英语Occam learning
  • PAC学习英语Probably approximately correct learning
  • 统计学习
  • VC理论
大会与出版物
  • NeurIPS
  • ICML英语International Conference on Machine Learning
  • ICLR
  • ML英语Machine Learning (journal)
  • JMLR英语Journal of Machine Learning Research
相关条目
  • 人工智能术语英语Glossary of artificial intelligence
  • 机器学习研究数据集列表英语List of datasets for machine-learning research
  • 机器学习概要英语Outline of machine learning

PyTorch是一个开源Python机器学习,基于Torch英语Torch (machine_learning)[2][3][4],底层由C++实现,应用于人工智能领域,如计算机视觉自然语言处理[5]。它最初由Meta Platforms的人工智能研究团队开发,現在屬於Linux基金会的一部分[6][7][8]。它是在修改後的BSD許可證下發布的自由及开放源代码软件。 儘管Python接口更加完善並且是開發的主要重點,但 PyTorch 也有C++接口[9]

許多深度學習軟體都是基於 PyTorch 構建的,包括特斯拉自动驾驶[10]Uber的Pyro[11]Hugging Face的Transformers[12]、 PyTorch Lightning[13][14]、和Catalyst[15][16]

概述

PyTorch主要有两大特征:[17]

PyTorch包括torch.autograd、torch.nn、torch.optim等子模块[20]

PyTorch包含多种损失函数,包括 MSE(均方误差 = L2 范数)、交叉熵损失和负熵似然损失(对分类器有用)等。

PyTorch張量

PyTorch定義了一個名為張量(torch.Tensor) 的類別來儲存和操作同構多維矩形數字陣列。 PyTorch張量與NumPy陣列類似,但也可以在支援 CUDA英伟达 GPU 上運作。 PyTorch 也一直在開發對其他 GPU 平台的支持,例如 AMD 的 ROCm 和 Apple 的Metal Framework[21]

张量是 PyTorch 中的核心数据抽象,PyTorch 支援各種張量子類型[22]。通常地,一维张量称为向量(vector),二维张量称为矩阵(matrix)。

张量的数据类型包括:

  • torch.bool
  • torch.int8
  • torch.uint8
  • torch.int16
  • torch.int32
  • torch.int64
  • torch.half
  • torch.float
  • torch.double
  • torch.bfloat

PyTorch神經網絡

神经网络由对数据执行操作的层/模块组成。 torch.nn 命名空间提供了使用者需要的所有构建块来构建自己的神经网络。PyTorch 中的每个模块都对应nn.模块。 神经网络本身是由其他模块(层)组成的模块。这种嵌套结构允许使用者轻松构建并管理复杂的架构。神经网络中的许多层都是参数化的,即具有相关的权重以及在训练期间优化的偏差。自动子类化跟踪模型对象中定义的所有字段,并生成所有参数可使用模型或方法访问。[2]

import torch                     # for all things PyTorch
import torch.nn as nn            # for torch.nn.Module, the parent object for PyTorch models
import torch.nn.functional as F  # for the activation function

激活函数torch.nn.Module具有封装所有主要内容的对象激活功能,包括 ReLU 及其许多变体、Tanh、 Hardtanh、sigmoid 等。[3]

PyTorch模型常见图层类型

线性层

最基本的神经网络层类型是线性完全连接层。在这个层中,每个输入都会影响每个图层的输出到由图层权重指定的程度。如果 模型有 m 个输入和 n 个输出,权重将是一个 m x n 矩阵。

卷积层

卷积层旨在处理高度空间相关性。它们在计算机视觉中非常常用, 它们检测组成的特征的紧密分组更高级别的功能。它们也会在其他上下文中弹出。例如, 在 NLP 应用程序中,单词的直接上下文(即序列中附近的其他单词)可以影响语句。

循环层

递归神经网络(RNN)是用于顺序数据(从科学仪器到时间序列测量)的自然语言句子。

例子

下面的程序用简单的例子展示这个程序库的低层功能。

>>> 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. ^ 1.0 1.1 Release 2.3.0. 2024年4月24日 [2024年4月25日]. 
  2. ^ 2.0 2.1 Yegulalp, Serdar. Facebook brings GPU-powered machine learning to Python. InfoWorld. 19 January 2017 [11 December 2017]. (原始内容存档于2018-07-12). 
  3. ^ 3.0 3.1 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). 
  4. ^ 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) (英语). 
  5. ^ Natural Language Processing (NLP) with PyTorch — NLP with PyTorch documentation. dl4nlp.info. [2017-12-18]. (原始内容存档于2019-06-21) (英语). 
  6. ^ Patel, Mo. When two trends fuse: PyTorch and recommender systems. O'Reilly Media. 2017-12-07 [2017-12-18]. (原始内容存档于2019-03-30) (英语). 
  7. ^ 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. 
  8. ^ Arakelyan, Sophia. Tech giants are using open source frameworks to dominate the AI community. VentureBeat. 2017-11-29 [2017-12-18]. (原始内容存档于2019-03-30) (美国英语). 
  9. ^ The C++ Frontend. PyTorch Master Documentation. [2019-07-29]. 
  10. ^ Karpathy, Andrej. PyTorch at Tesla - Andrej Karpathy, Tesla. 
  11. ^ Uber AI Labs Open Sources Pyro, a Deep Probabilistic Programming Language. Uber Engineering Blog. 2017-11-03 [2017-12-18] (美国英语). 
  12. ^ PYTORCH-TRANSFORMERS: PyTorch implementations of popular NLP Transformers, PyTorch Hub, 2019-12-01 [2019-12-01] 
  13. ^ PYTORCH-Lightning: The lightweight PyTorch wrapper for ML researchers. Scale your models. Write less boilerplate, Lightning-Team, 2020-06-18 [2020-06-18] 
  14. ^ Ecosystem Tools. pytorch.org. [2020-06-18] (英语). 
  15. ^ GitHub - catalyst-team/catalyst: Accelerated DL & RL, Catalyst-Team, 2019-12-05 [2019-12-05] 
  16. ^ Ecosystem Tools. pytorch.org. [2020-04-04] (英语). 
  17. ^ PyTorch – About. pytorch.org. [2018-06-11]. (原始内容存档于2018-06-15). 
  18. ^ R.E. Wengert. A simple automatic derivative evaluation program. Comm. ACM. 1964, 7: 463–464. doi:10.1145/355586.364791. 
  19. ^ 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. ^ 20.0 20.1 神经网络与PyTorch实战 Application of Neural Network and PyTorch. 机械工业出版社. 2018. ISBN 9787111605775. 
  21. ^ Introducing Accelerated PyTorch Training on Mac. pytorch.org. [2022-06-04] (英语). 
  22. ^ An Introduction to PyTorch – A Simple yet Powerful Deep Learning Library. analyticsvidhya.com. 2018-02-22 [2018-06-11]. 

参见

外部链接

  • 官方网站
  • 从 GitHub 访问 PyTorch 教程
  • PyTorch基金会的Youtube账号
深度学习软件英语Comparison of deep learning software
开源软件
专有
  • 分类
  • 比较
可微分计算
概论
概念
应用
硬件
  • TPU
  • VPU
  • IPU英语Graphcore
  • 憶阻器
  • SpiNNaker英语SpiNNaker
软件库
实现
视觉·语音
自然语言
决策
人物
组织
架构
  • 主题 主题
    • 计算机编程
    • 技术
  • 分类 分类
    • 人工神经网络
    • 机器学习