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

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 和 AppleMetal 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]. 

參見[編輯]

外部連結[編輯]